Skip to content

Commit

Permalink
feat(task): add --eval flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Nov 19, 2024
1 parent 46b6037 commit 3bcafcd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
57 changes: 54 additions & 3 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ pub struct TaskFlags {
pub cwd: Option<String>,
pub task: Option<String>,
pub is_run: bool,
pub eval: bool,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand Down Expand Up @@ -1385,7 +1386,7 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
"repl" => repl_parse(&mut flags, &mut m)?,
"run" => run_parse(&mut flags, &mut m, app, false)?,
"serve" => serve_parse(&mut flags, &mut m, app)?,
"task" => task_parse(&mut flags, &mut m),
"task" => task_parse(&mut flags, &mut m, app)?,
"test" => test_parse(&mut flags, &mut m)?,
"types" => types_parse(&mut flags, &mut m),
"uninstall" => uninstall_parse(&mut flags, &mut m),
Expand Down Expand Up @@ -2930,7 +2931,10 @@ fn task_subcommand() -> Command {
<p(245)>deno task build</>
List all available tasks:
<p(245)>deno task</>"
<p(245)>deno task</>
Evaluate a task from string
<p(245)>deno task --eval \"echo $(pwd)\"</>"
),
UnstableArgsConfig::ResolutionAndRuntime,
)
Expand All @@ -2946,6 +2950,13 @@ List all available tasks:
.help("Specify the directory to run the task in")
.value_hint(ValueHint::DirPath),
)
.arg(
Arg::new("eval")
.long("eval")
.help(
"Evaluate the passed value as if, it was a task in a configuration file",
).action(ArgAction::SetTrue)
)
.arg(node_modules_dir_arg())
})
}
Expand Down Expand Up @@ -5056,7 +5067,11 @@ fn serve_parse(
Ok(())
}

fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) {
fn task_parse(
flags: &mut Flags,
matches: &mut ArgMatches,
mut app: Command,
) -> clap::error::Result<()> {
flags.config_flag = matches
.remove_one::<String>("config")
.map(ConfigFlag::Path)
Expand All @@ -5069,6 +5084,7 @@ fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) {
cwd: matches.remove_one::<String>("cwd"),
task: None,
is_run: false,
eval: matches.get_flag("eval"),
};

if let Some((task, mut matches)) = matches.remove_subcommand() {
Expand All @@ -5081,9 +5097,15 @@ fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) {
.flatten()
.filter_map(|arg| arg.into_string().ok()),
);
} else if task_flags.eval {
return Err(app.find_subcommand_mut("task").unwrap().error(
clap::error::ErrorKind::MissingRequiredArgument,
"[TASK] must be specified when using --eval",
));
}

flags.subcommand = DenoSubcommand::Task(task_flags);
Ok(())
}

fn parallel_arg_parse(matches: &mut ArgMatches) -> Option<NonZeroUsize> {
Expand Down Expand Up @@ -10258,6 +10280,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["hello", "world"],
..Flags::default()
Expand All @@ -10272,6 +10295,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
..Flags::default()
}
Expand All @@ -10285,10 +10309,28 @@ mod tests {
cwd: Some("foo".to_string()),
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
..Flags::default()
}
);

let r = flags_from_vec(svec!["deno", "task", "--eval", "echo 1"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
cwd: None,
task: Some("echo 1".to_string()),
is_run: false,
eval: true,
}),
..Flags::default()
}
);

let r = flags_from_vec(svec!["deno", "task", "--eval"]);
assert!(r.is_err());
}

#[test]
Expand All @@ -10310,6 +10352,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["--", "hello", "world"],
config_flag: ConfigFlag::Path("deno.json".to_owned()),
Expand All @@ -10327,6 +10370,7 @@ mod tests {
cwd: Some("foo".to_string()),
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["--", "hello", "world"],
..Flags::default()
Expand All @@ -10345,6 +10389,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["--"],
..Flags::default()
Expand All @@ -10362,6 +10407,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["-1", "--test"],
..Flags::default()
Expand All @@ -10379,6 +10425,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
argv: svec!["--test"],
..Flags::default()
Expand All @@ -10397,6 +10444,7 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
is_run: false,
eval: false,
}),
log_level: Some(log::Level::Error),
..Flags::default()
Expand All @@ -10414,6 +10462,7 @@ mod tests {
cwd: None,
task: None,
is_run: false,
eval: false,
}),
..Flags::default()
}
Expand All @@ -10430,6 +10479,7 @@ mod tests {
cwd: None,
task: None,
is_run: false,
eval: false,
}),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
..Flags::default()
Expand All @@ -10447,6 +10497,7 @@ mod tests {
cwd: None,
task: None,
is_run: false,
eval: false,
}),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
..Flags::default()
Expand Down
1 change: 1 addition & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
cwd: None,
task: Some(run_flags.script.clone()),
is_run: true,
eval: false,
};
new_flags.subcommand = DenoSubcommand::Task(task_flags.clone());
let result = tools::task::execute_script(Arc::new(new_flags), task_flags.clone()).await;
Expand Down
15 changes: 14 additions & 1 deletion cli/tools/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn execute_script(
let factory = CliFactory::from_flags(flags);
let cli_options = factory.cli_options()?;
let start_dir = &cli_options.start_dir;
if !start_dir.has_deno_or_pkg_json() {
if !start_dir.has_deno_or_pkg_json() && !task_flags.eval {
bail!("deno task couldn't find deno.json(c). See https://docs.deno.com/go/config")
}
let force_use_pkg_json =
Expand Down Expand Up @@ -90,6 +90,19 @@ pub async fn execute_script(
concurrency: no_of_concurrent_tasks.into(),
};

if task_flags.eval {
return task_runner
.run_deno_task(
&Url::from_directory_path(cli_options.initial_cwd()).unwrap(),
&"".to_string(),
&TaskDefinition {
command: task_flags.task.as_ref().unwrap().to_string(),
dependencies: vec![],
description: None,
},
)
.await;
}
task_runner.run_task(task_name).await
}

Expand Down
22 changes: 22 additions & 0 deletions tests/specs/task/eval/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"tempDir": true,
"steps": [
{
"args": "task --eval",
"output": "no_arg.out",
"exitCode": 1
},
{
"args": ["task", "--eval", "echo $(pwd)"],
"output": "echo_pwd.out"
},
{
"args": [
"task",
"--eval",
"echo 12345 | (deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)' && deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)')"
],
"output": "piped.out"
}
]
}
2 changes: 2 additions & 0 deletions tests/specs/task/eval/echo_pwd.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Task echo $(pwd)
[WILDCARD]
4 changes: 4 additions & 0 deletions tests/specs/task/eval/no_arg.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: [TASK] must be specified when using --eval

Usage: deno task [OPTIONS] [TASK]

3 changes: 3 additions & 0 deletions tests/specs/task/eval/piped.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Task echo 12345 | (deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)' && deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)')
Uint8Array(1) [ 49 ]
Uint8Array(1) [ 50 ]

0 comments on commit 3bcafcd

Please sign in to comment.