use super::*;
impl App {
pub(in crate::app) fn install_codeql_pack_dependencies(&mut self, pack: usize) {
let Some(p) = self.codeql.queries.get(pack) else {
return;
};
if p.name == crate::codeql_query::NO_PACK {
self.status =
String::from("These queries are in no pack, so they have no dependencies");
return;
}
let (name, args) = (
p.name.clone(),
crate::codeql_query::pack_install_args(&p.dir),
);
self.start_codeql_pack_job(
args,
format!("Installing the dependencies of {name}\u{2026}"),
format!("Installed the dependencies of {name}"),
format!("Could not install the dependencies of {name}"),
);
}
pub(in crate::app) fn prompt_download_codeql_packs(&mut self) {
use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
self.open_input_prompt(InputPrompt::new(
InputPurpose::CodeqlDownloadPacks,
String::from("Download Packs"),
"packs to download, e.g. codeql/java-queries codeql/python-all@1.0.0",
));
}
pub(in crate::app) fn submit_download_codeql_packs(&mut self, value: &str) {
let packs = crate::codeql_query::parse_pack_list(value);
if packs.is_empty() {
self.status = String::from("Name at least one pack to download");
return;
}
let what = packs.join(", ");
self.start_codeql_pack_job(
crate::codeql_query::pack_download_args(&packs),
format!("Downloading {what}\u{2026}"),
format!("Downloaded {what}"),
format!("Could not download {what}"),
);
}
pub(in crate::app) fn start_codeql_pack_job(
&mut self,
args: Vec<String>,
running: String,
done: String,
failed: String,
) {
if self.codeql_jobs.pack_job.is_some() {
self.status = String::from("A CodeQL pack install or download is already running");
return;
}
let program = self.codeql_jobs.program.clone();
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(Self::codeql_command(&program, &args));
});
self.status = running;
self.codeql_jobs.pack_job = Some((rx, done, failed));
}
pub fn drain_codeql_pack_job(&mut self) -> bool {
let Some((rx, _, _)) = self.codeql_jobs.pack_job.as_ref() else {
return false;
};
let outcome = match rx.try_recv() {
Ok(outcome) => outcome,
Err(std::sync::mpsc::TryRecvError::Empty) => return false,
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
Err(String::from("the pack command stopped unexpectedly"))
}
};
let Some((_, done, failed)) = self.codeql_jobs.pack_job.take() else {
return false;
};
self.status = match outcome {
Ok(()) => done,
Err(why) => format!("{failed}: {why}"),
};
true
}
}
impl App {
pub(in crate::app) fn run_codeql_query_suite(&mut self) {
if !self
.editor
.path
.as_deref()
.is_some_and(crate::codeql_query::is_suite)
{
self.status = String::from("Open a .qls query suite to run it");
return;
}
self.run_codeql_query();
}
pub(in crate::app) fn prompt_run_codeql_published_pack(&mut self) {
use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
self.open_input_prompt(InputPrompt::new(
InputPurpose::CodeqlRunPublishedPack,
String::from("Run Queries in Published Pack"),
"e.g. codeql/python-queries, scope/name@1.0.0, scope/name:path/in/pack",
));
}
pub(in crate::app) fn run_codeql_published_pack(&mut self, reference: &str) {
let reference = reference.trim();
if crate::codeql_query::published_pack(reference).is_none() {
self.status = format!("{reference:?} is not a pack: write scope/name");
return;
}
self.run_codeql_file_on(PathBuf::from(reference), "", CodeqlRunMode::Pack, None);
}
pub(in crate::app) fn check_codeql_suite(program: &Path, suite: &Path) -> Result<(), String> {
let out = crate::proc::output_retrying_busy(
std::process::Command::new(program)
.args(crate::codeql_query::resolve_suite_args(suite)),
)
.map_err(|e| format!("could not run codeql: {e}"))?;
if !out.status.success() {
return Err(crate::codeql_query::failure_reason(
&String::from_utf8_lossy(&out.stderr),
));
}
let tables = crate::codeql_query::table_queries_in_suite(
&String::from_utf8_lossy(&out.stdout),
|p| std::fs::read_to_string(p).ok(),
)?;
if tables.is_empty() {
return Ok(());
}
Err(format!(
"the suite selects queries that are not alert queries ({}); run those on their own",
tables.join(", ")
))
}
pub(in crate::app) fn run_codeql_pack(&mut self, pack: usize) {
if self.codeql_jobs.run.is_some() {
self.status = String::from("A CodeQL query is already running");
return;
}
let Some(queries) = self.codeql.queries.get(pack).map(|p| p.queries.clone()) else {
return;
};
self.run_codeql_batch(queries, "That pack has no queries");
}
}