2022-03-10 20:56:14 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::TaskFlags;
|
2022-03-10 20:56:14 -05:00
|
|
|
use crate::colors;
|
2022-06-08 17:30:16 -04:00
|
|
|
use crate::fs_util;
|
2022-03-10 20:56:14 -05:00
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use deno_core::anyhow::bail;
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::HashMap;
|
2022-06-08 17:30:16 -04:00
|
|
|
use std::path::PathBuf;
|
2022-03-10 20:56:14 -05:00
|
|
|
|
|
|
|
fn print_available_tasks(tasks_config: BTreeMap<String, String>) {
|
|
|
|
eprintln!("{}", colors::green("Available tasks:"));
|
|
|
|
|
|
|
|
for name in tasks_config.keys() {
|
|
|
|
eprintln!("- {}", colors::cyan(name));
|
|
|
|
eprintln!(" {}", tasks_config[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn execute_script(
|
|
|
|
flags: Flags,
|
|
|
|
task_flags: TaskFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
2022-03-15 21:24:07 -04:00
|
|
|
log::warn!(
|
|
|
|
"{} deno task is unstable and may drastically change in the future",
|
|
|
|
crate::colors::yellow("Warning"),
|
|
|
|
);
|
2022-06-28 16:45:55 -04:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2022-06-29 11:51:11 -04:00
|
|
|
let tasks_config = ps.options.resolve_tasks_config()?;
|
|
|
|
let config_file_url = ps.options.maybe_config_file_specifier().unwrap();
|
2022-03-10 20:56:14 -05:00
|
|
|
let config_file_path = if config_file_url.scheme() == "file" {
|
|
|
|
config_file_url.to_file_path().unwrap()
|
|
|
|
} else {
|
|
|
|
bail!("Only local configuration files are supported")
|
|
|
|
};
|
|
|
|
|
|
|
|
if task_flags.task.is_empty() {
|
|
|
|
print_available_tasks(tasks_config);
|
|
|
|
return Ok(1);
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:30:16 -04:00
|
|
|
let cwd = match task_flags.cwd {
|
|
|
|
Some(path) => fs_util::canonicalize_path(&PathBuf::from(path))?,
|
|
|
|
None => config_file_path.parent().unwrap().to_owned(),
|
|
|
|
};
|
2022-03-10 20:56:14 -05:00
|
|
|
let task_name = task_flags.task;
|
|
|
|
let maybe_script = tasks_config.get(&task_name);
|
|
|
|
|
|
|
|
if let Some(script) = maybe_script {
|
2022-06-28 16:45:55 -04:00
|
|
|
let additional_args = ps
|
2022-06-29 11:51:11 -04:00
|
|
|
.options
|
2022-06-28 16:45:55 -04:00
|
|
|
.argv()
|
2022-03-10 20:56:14 -05:00
|
|
|
.iter()
|
|
|
|
// surround all the additional arguments in double quotes
|
|
|
|
// and santize any command substition
|
|
|
|
.map(|a| format!("\"{}\"", a.replace('"', "\\\"").replace('$', "\\$")))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(" ");
|
|
|
|
let script = format!("{} {}", script, additional_args);
|
2022-03-15 21:24:07 -04:00
|
|
|
let script = script.trim();
|
2022-03-11 20:35:18 -05:00
|
|
|
log::info!(
|
|
|
|
"{} {} {}",
|
|
|
|
colors::green("Task"),
|
|
|
|
colors::cyan(&task_name),
|
2022-03-15 21:24:07 -04:00
|
|
|
script,
|
2022-03-11 20:35:18 -05:00
|
|
|
);
|
2022-03-15 21:24:07 -04:00
|
|
|
let seq_list = deno_task_shell::parser::parse(script)
|
2022-03-10 20:56:14 -05:00
|
|
|
.with_context(|| format!("Error parsing script '{}'.", task_name))?;
|
|
|
|
let env_vars = std::env::vars().collect::<HashMap<String, String>>();
|
2022-06-08 17:30:16 -04:00
|
|
|
let exit_code = deno_task_shell::execute(seq_list, env_vars, &cwd).await;
|
2022-03-10 20:56:14 -05:00
|
|
|
Ok(exit_code)
|
|
|
|
} else {
|
|
|
|
eprintln!("Task not found: {}", task_name);
|
|
|
|
print_available_tasks(tasks_config);
|
|
|
|
Ok(1)
|
|
|
|
}
|
|
|
|
}
|