|
| 1 | +use std::env; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +pub struct Autobuilder { |
| 6 | + include_extensions: Vec<String>, |
| 7 | + include_globs: Vec<String>, |
| 8 | + exclude_globs: Vec<String>, |
| 9 | + language: String, |
| 10 | + database: PathBuf, |
| 11 | + size_limit: Option<String>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Autobuilder { |
| 15 | + pub fn new(language: &str, database: PathBuf) -> Self { |
| 16 | + Self { |
| 17 | + language: language.to_string(), |
| 18 | + database: database, |
| 19 | + include_extensions: vec![], |
| 20 | + include_globs: vec![], |
| 21 | + exclude_globs: vec![], |
| 22 | + size_limit: None, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn include_extensions(&mut self, exts: &[&str]) -> &mut Self { |
| 27 | + self.include_extensions = exts.into_iter().map(|s| String::from(*s)).collect(); |
| 28 | + self |
| 29 | + } |
| 30 | + |
| 31 | + pub fn include_globs(&mut self, globs: &[&str]) -> &mut Self { |
| 32 | + self.include_globs = globs.into_iter().map(|s| String::from(*s)).collect(); |
| 33 | + self |
| 34 | + } |
| 35 | + |
| 36 | + pub fn exclude_globs(&mut self, globs: &[&str]) -> &mut Self { |
| 37 | + self.exclude_globs = globs.into_iter().map(|s| String::from(*s)).collect(); |
| 38 | + self |
| 39 | + } |
| 40 | + |
| 41 | + pub fn size_limit(&mut self, limit: &str) -> &mut Self { |
| 42 | + self.size_limit = Some(limit.to_string()); |
| 43 | + self |
| 44 | + } |
| 45 | + |
| 46 | + pub fn run(&self) -> std::io::Result<()> { |
| 47 | + let dist = env::var("CODEQL_DIST").expect("CODEQL_DIST not set"); |
| 48 | + let codeql = if env::consts::OS == "windows" { |
| 49 | + "codeql.exe" |
| 50 | + } else { |
| 51 | + "codeql" |
| 52 | + }; |
| 53 | + let codeql: PathBuf = [&dist, codeql].iter().collect(); |
| 54 | + let mut cmd = Command::new(codeql); |
| 55 | + cmd.arg("database").arg("index-files"); |
| 56 | + |
| 57 | + for ext in &self.include_extensions { |
| 58 | + cmd.arg(format!("--include-extension={}", ext)); |
| 59 | + } |
| 60 | + |
| 61 | + for glob in &self.include_globs { |
| 62 | + cmd.arg(format!("--include={}", glob)); |
| 63 | + } |
| 64 | + |
| 65 | + for glob in &self.exclude_globs { |
| 66 | + cmd.arg(format!("--exclude={}", glob)); |
| 67 | + } |
| 68 | + |
| 69 | + if let Some(limit) = &self.size_limit { |
| 70 | + cmd.arg(format!("--size-limit={}", limit)); |
| 71 | + } |
| 72 | + |
| 73 | + cmd.arg(format!("--language={}", &self.language)); |
| 74 | + cmd.arg("--working-dir=."); |
| 75 | + cmd.arg(&self.database); |
| 76 | + |
| 77 | + for line in env::var("LGTM_INDEX_FILTERS") |
| 78 | + .unwrap_or_default() |
| 79 | + .split('\n') |
| 80 | + { |
| 81 | + if let Some(stripped) = line.strip_prefix("include:") { |
| 82 | + cmd.arg("--also-match=".to_owned() + stripped); |
| 83 | + } else if let Some(stripped) = line.strip_prefix("exclude:") { |
| 84 | + cmd.arg("--exclude=".to_owned() + stripped); |
| 85 | + } |
| 86 | + } |
| 87 | + let exit = &cmd.spawn()?.wait()?; |
| 88 | + std::process::exit(exit.code().unwrap_or(1)) |
| 89 | + } |
| 90 | +} |
0 commit comments