diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-03-11 02:56:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-10 20:56:14 -0500 |
commit | 47f22777beb7eb98a07fa56fbbd40ab5c1fa231e (patch) | |
tree | aa61065f07df3c1ad5a28326801720593a8cdd19 /cli/flags.rs | |
parent | 808f797633ba82c0e9198481ddd742284a03cb9c (diff) |
feat: "deno task" subcommand (#13725)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/flags.rs')
-rw-r--r-- | cli/flags.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index 614a975b1..dfa617462 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -140,6 +140,11 @@ pub struct RunFlags { } #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct TaskFlags { + pub task: String, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct TestFlags { pub ignore: Vec<PathBuf>, pub doc: bool, @@ -187,6 +192,7 @@ pub enum DenoSubcommand { Lint(LintFlags), Repl(ReplFlags), Run(RunFlags), + Task(TaskFlags), Test(TestFlags), Types, Upgrade(UpgradeFlags), @@ -500,6 +506,7 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> { Some(("compile", m)) => compile_parse(&mut flags, m), Some(("lsp", m)) => lsp_parse(&mut flags, m), Some(("vendor", m)) => vendor_parse(&mut flags, m), + Some(("task", m)) => task_parse(&mut flags, m), _ => handle_repl_flags(&mut flags, ReplFlags { eval: None }), } @@ -568,6 +575,7 @@ If the flag is set, restrict these messages to errors.", .subcommand(lint_subcommand()) .subcommand(repl_subcommand()) .subcommand(run_subcommand()) + .subcommand(task_subcommand()) .subcommand(test_subcommand()) .subcommand(types_subcommand()) .subcommand(upgrade_subcommand()) @@ -1256,6 +1264,25 @@ Deno allows specifying the filename '-' to read the file from stdin. ) } +fn task_subcommand<'a>() -> App<'a> { + App::new("task") + .setting(AppSettings::TrailingVarArg) + .arg(config_arg()) + .arg(Arg::new("task").help("Task to be executed")) + .arg( + Arg::new("task_args") + .multiple_values(true) + .multiple_occurrences(true) + .help("Additional arguments passed to the task"), + ) + .about("Run a task defined in the configuration file") + .long_about( + "Run a task defined in the configuration file + + deno task build", + ) +} + fn test_subcommand<'a>() -> App<'a> { runtime_args(App::new("test"), true, true) .setting(AppSettings::TrailingVarArg) @@ -2197,6 +2224,26 @@ fn run_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.subcommand = DenoSubcommand::Run(RunFlags { script }); } +fn task_parse(flags: &mut Flags, matches: &clap::ArgMatches) { + config_arg_parse(flags, matches); + + let mut task_name = "".to_string(); + if let Some(task) = matches.value_of("task") { + task_name = task.to_string(); + + let task_args: Vec<String> = matches + .values_of("task_args") + .unwrap_or_default() + .map(String::from) + .collect(); + for v in task_args { + flags.argv.push(v); + } + } + + flags.subcommand = DenoSubcommand::Task(TaskFlags { task: task_name }); +} + fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); // NOTE: `deno test` always uses `--no-prompt`, tests shouldn't ever do @@ -5063,4 +5110,60 @@ mod tests { } ); } + + #[test] + fn task_subcommand() { + let r = + flags_from_vec(svec!["deno", "task", "build", "--", "hello", "world",]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Task(TaskFlags { + task: "build".to_string(), + }), + argv: svec!["hello", "world"], + ..Flags::default() + } + ); + + let r = flags_from_vec(svec!["deno", "task", "build"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Task(TaskFlags { + task: "build".to_string(), + }), + ..Flags::default() + } + ); + } + + #[test] + fn task_subcommand_empty() { + let r = flags_from_vec(svec!["deno", "task",]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Task(TaskFlags { + task: "".to_string(), + }), + ..Flags::default() + } + ); + } + + #[test] + fn task_subcommand_config() { + let r = flags_from_vec(svec!["deno", "task", "--config", "deno.jsonc"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Task(TaskFlags { + task: "".to_string(), + }), + config_path: Some("deno.jsonc".to_string()), + ..Flags::default() + } + ); + } } |