Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set usage arguments and flags as environment variables for toml tasks #4159

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ impl Run {
for (script, args) in
task.render_run_scripts_with_args(self.cd.clone(), &task.args, &env)?
{
let get_args = || task.run.iter().chain(task.args.iter()).cloned().collect();
self.parse_usage_spec_and_init_env(task, &mut env, get_args)?;
self.exec_script(&script, &args, task, &env, &prefix)?;
}
}
Expand Down Expand Up @@ -578,14 +580,8 @@ impl Run {
let mut env = env.clone();
let command = file.to_string_lossy().to_string();
let args = task.args.iter().cloned().collect_vec();
let (spec, _) = task.parse_usage_spec(self.cd.clone(), &env)?;
if !spec.cmd.args.is_empty() || !spec.cmd.flags.is_empty() {
let args = once(command.clone()).chain(args.clone()).collect_vec();
let po = usage::parse(&spec, &args).map_err(|err| eyre!(err))?;
for (k, v) in po.as_env() {
env.insert(k, v);
}
}
let get_args = || once(command.clone()).chain(args.clone()).collect_vec();
self.parse_usage_spec_and_init_env(task, &mut env, get_args)?;

if !self.quiet(Some(task)) {
let cmd = format!("{} {}", display_path(file), args.join(" "))
Expand All @@ -599,6 +595,24 @@ impl Run {
self.exec(file, &args, task, &env, prefix)
}

fn parse_usage_spec_and_init_env(
&self,
task: &Task,
env: &mut EnvMap,
get_args: impl Fn() -> Vec<String>,
) -> Result<()> {
let (spec, _) = task.parse_usage_spec(self.cd.clone(), env)?;
if !spec.cmd.args.is_empty() || !spec.cmd.flags.is_empty() {
let args: Vec<String> = get_args();
let po = usage::parse(&spec, &args).map_err(|err| eyre!(err))?;
for (k, v) in po.as_env() {
env.insert(k, v);
}
}

Ok(())
}

fn exec(
&self,
file: &Path,
Expand Down
Loading