Skip to content

Commit eff4729

Browse files
committed
QL: Merge extractor binaries into one
There is now one binary, codeql-ql-extractor, which takes a positional argument specifying whether to extract, generate or autobuild.
1 parent e4b4d8a commit eff4729

File tree

6 files changed

+83
-67
lines changed

6 files changed

+83
-67
lines changed

ql/Cargo.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ql/extractor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "ql-extractor"
2+
name = "codeql-ql-extractor"
33
version = "0.1.0"
44
authors = ["GitHub"]
55
edition = "2018"

ql/extractor/src/bin/autobuilder.rs renamed to ql/extractor/src/autobuilder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use clap::Args;
12
use std::env;
23
use std::path::PathBuf;
34
use std::process::Command;
45

5-
fn main() -> std::io::Result<()> {
6+
#[derive(Args)]
7+
// The autobuilder takes no command-line options, but this may change in the future.
8+
pub struct Options {}
9+
10+
pub fn run(_: Options) -> std::io::Result<()> {
611
let dist = env::var("CODEQL_DIST").expect("CODEQL_DIST not set");
712
let db = env::var("CODEQL_EXTRACTOR_QL_WIP_DATABASE")
813
.expect("CODEQL_EXTRACTOR_QL_WIP_DATABASE not set");

ql/extractor/src/bin/extractor.rs renamed to ql/extractor/src/extractor.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
use clap::Args;
12
use rayon::prelude::*;
23
use std::fs;
34
use std::io::BufRead;
45
use std::path::{Path, PathBuf};
56

67
use codeql_extractor::{diagnostics, extractor, node_types, trap};
78

8-
fn main() -> std::io::Result<()> {
9+
#[derive(Args)]
10+
pub struct Options {
11+
/// Sets a custom source achive folder
12+
#[arg(long)]
13+
source_archive_dir: PathBuf,
14+
15+
/// Sets a custom trap folder
16+
#[arg(long)]
17+
output_dir: PathBuf,
18+
19+
/// A text file containing the paths of the files to extract
20+
#[arg(long)]
21+
file_list: PathBuf,
22+
}
23+
24+
pub fn run(options: Options) -> std::io::Result<()> {
925
tracing_subscriber::fmt()
1026
.with_target(false)
1127
.without_time()
@@ -58,30 +74,9 @@ fn main() -> std::io::Result<()> {
5874
.build_global()
5975
.unwrap();
6076

61-
let matches = clap::Command::new("QL extractor")
62-
.version("1.0")
63-
.author("GitHub")
64-
.about("CodeQL QL extractor")
65-
.args(&[
66-
clap::arg!(--"source-archive-dir" <DIR> "Sets a custom source archive folder"),
67-
clap::arg!(--"output-dir" <DIR> "Sets a custom trap folder"),
68-
clap::arg!(--"file-list" <FILE_LIST> "A text file containing the paths of the files to extract"),
69-
])
70-
.get_matches();
71-
let src_archive_dir = matches
72-
.get_one::<String>("source-archive-dir")
73-
.expect("missing --source-archive-dir");
74-
let src_archive_dir = PathBuf::from(src_archive_dir);
75-
76-
let trap_dir = matches
77-
.get_one::<String>("output-dir")
78-
.expect("missing --output-dir");
79-
let trap_dir = PathBuf::from(trap_dir);
80-
81-
let file_list = matches
82-
.get_one::<String>("file-list")
83-
.expect("missing --file-list");
84-
let file_list = fs::File::open(file_list)?;
77+
let trap_dir = options.output_dir;
78+
let file_list = fs::File::open(options.file_list)?;
79+
let source_archive_dir = options.source_archive_dir;
8580

8681
let language = tree_sitter_ql::language();
8782
let dbscheme = tree_sitter_ql_dbscheme::language();
@@ -114,7 +109,7 @@ fn main() -> std::io::Result<()> {
114109
return Ok(());
115110
}
116111
let path = PathBuf::from(line).canonicalize()?;
117-
let src_archive_file = path_for(&src_archive_dir, &path, "");
112+
let src_archive_file = path_for(&source_archive_dir, &path, "");
118113
let source = std::fs::read(&path)?;
119114
let code_ranges = vec![];
120115
let mut trap_writer = trap::Writer::new();
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1+
use clap::Args;
12
use std::path::PathBuf;
23

34
use codeql_extractor::generator::{generate, language::Language};
45

5-
fn main() -> std::io::Result<()> {
6+
#[derive(Args)]
7+
pub struct Options {
8+
/// Path of the generated dbscheme file
9+
#[arg(long)]
10+
dbscheme: PathBuf,
11+
12+
/// Path of the generated QLL file
13+
#[arg(long)]
14+
library: PathBuf,
15+
}
16+
17+
pub fn run(options: Options) -> std::io::Result<()> {
618
tracing_subscriber::fmt()
719
.with_target(false)
820
.without_time()
921
.with_level(true)
1022
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
1123
.init();
1224

13-
let matches = clap::Command::new("QL dbscheme generator")
14-
.version("1.0")
15-
.author("GitHub")
16-
.about("CodeQL QL dbscheme generator")
17-
.args(&[
18-
clap::arg!(--dbscheme <FILE> "Path of the generated dbscheme file"),
19-
clap::arg!(--library <FILE> "Path of the generated QLL file"),
20-
])
21-
.get_matches();
22-
let dbscheme_path = matches
23-
.get_one::<String>("dbscheme")
24-
.expect("missing --dbscheme");
25-
let dbscheme_path = PathBuf::from(dbscheme_path);
26-
27-
let ql_library_path = matches
28-
.get_one::<String>("library")
29-
.expect("missing --library");
30-
let ql_library_path = PathBuf::from(ql_library_path);
31-
3225
let languages = vec![
3326
Language {
3427
name: "QL".to_owned(),
@@ -52,5 +45,5 @@ fn main() -> std::io::Result<()> {
5245
},
5346
];
5447

55-
generate(languages, dbscheme_path, ql_library_path)
48+
generate(languages, options.dbscheme, options.library)
5649
}

ql/extractor/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use clap::Parser;
2+
3+
mod autobuilder;
4+
mod extractor;
5+
mod generator;
6+
7+
#[derive(Parser)]
8+
#[command(author, version, about, long_about = None)]
9+
enum Cli {
10+
Extract(extractor::Options),
11+
Generate(generator::Options),
12+
Autobuild(autobuilder::Options),
13+
}
14+
15+
fn main() -> std::io::Result<()> {
16+
let cli = Cli::parse();
17+
18+
match cli {
19+
Cli::Extract(options) => extractor::run(options),
20+
Cli::Generate(options) => generator::run(options),
21+
Cli::Autobuild(options) => autobuilder::run(options),
22+
}
23+
}

0 commit comments

Comments
 (0)