Skip to content

Commit

Permalink
prepare for v2 schema
Browse files Browse the repository at this point in the history
Summary:
Preparation step for new schema.
In order to migrate smoothly we gonna use `--v2` flag

Reviewed By: alanz

Differential Revision: D55063029

fbshipit-source-id: b566b581ae129cc13537a7fde24e05d66d8169d4
  • Loading branch information
perehonchuk authored and facebook-github-bot committed Apr 5, 2024
1 parent febffe9 commit 8fd0e13
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
2 changes: 2 additions & 0 deletions crates/elp/src/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ pub struct Glean {
/// Path to a directory where to dump result
#[bpaf(argument("TO"))]
pub to: Option<PathBuf>,
/// Produce glean db with macros, types, xrefs. Incompatible with previous
pub v2: bool,
}

#[derive(Clone, Debug)]
Expand Down
50 changes: 29 additions & 21 deletions crates/elp/src/bin/glean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@ impl IndexedFacts {
self.xref_facts.push(xref)
}
}

fn to_v1_facts(mut self) -> Vec<Fact> {
vec![
Fact::File {
facts: mem::take(&mut self.file_facts),
},
Fact::FileLine {
facts: mem::take(&mut self.file_line_facts),
},
Fact::FunctionDeclaration {
facts: mem::take(&mut self.declaration_facts),
},
Fact::XRef {
facts: mem::take(&mut self.xref_facts),
},
]
}

fn to_v2_facts(self) -> Vec<Fact> {
vec![]
}
}

pub struct GleanIndexer {
Expand All @@ -263,28 +284,15 @@ pub struct GleanIndexer {
pub fn index(args: &Glean, cli: &mut dyn Cli) -> Result<()> {
let indexer = GleanIndexer::new(args, cli)?;
let facts = indexer.index()?;
let facts = if args.v2 {
facts.to_v2_facts()
} else {
facts.to_v1_facts()
};
write_results(facts, cli, &args.to)
}

fn write_results(
mut indexed_facts: IndexedFacts,
cli: &mut dyn Cli,
to: &Option<PathBuf>,
) -> Result<()> {
let facts = vec![
Fact::File {
facts: mem::take(&mut indexed_facts.file_facts),
},
Fact::FileLine {
facts: mem::take(&mut indexed_facts.file_line_facts),
},
Fact::FunctionDeclaration {
facts: mem::take(&mut indexed_facts.declaration_facts),
},
Fact::XRef {
facts: mem::take(&mut indexed_facts.xref_facts),
},
];
fn write_results(facts: Vec<Fact>, cli: &mut dyn Cli, to: &Option<PathBuf>) -> Result<()> {
let content = serde_json::to_string(&facts)?;
match to {
Some(to) => std::fs::OpenOptions::new()
Expand Down Expand Up @@ -663,7 +671,7 @@ mod tests {
use super::*;

#[test]
fn serialization_test() {
fn serialization_test_v1() {
let mut cli = Fake::default();
let file_id = FileId(10071);
let location = Location::new(0, 10);
Expand Down Expand Up @@ -697,7 +705,7 @@ mod tests {
xref_facts,
};

write_results(facts, &mut cli, &None).expect("success");
write_results(facts.to_v1_facts(), &mut cli, &None).expect("success");

let (out, err) = cli.to_strings();
let expected = expect_file!["../resources/test/glean/serialization_test.out"];
Expand Down
3 changes: 2 additions & 1 deletion crates/elp/src/resources/test/glean_help.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Usage: [--project PROJECT] [--module MODULE] [--to TO]
Usage: [--project PROJECT] [--module MODULE] [--to TO] [--v2]

Available options:
--project <PROJECT> Path to directory with project, or to a JSON file (defaults to `.`)
--module <MODULE>
--to <TO> Path to a directory where to dump result
--v2 Produce glean db with macros, types, xrefs. Incompatible with previous
-h, --help Prints help information

0 comments on commit 8fd0e13

Please sign in to comment.