From b66cce0f58cb484e4625fffdd22b3a87ac2a0129 Mon Sep 17 00:00:00 2001 From: Neal Harrington Date: Tue, 9 Jan 2024 03:40:08 -0500 Subject: [PATCH] Add `--no-deps` to skip running recipe dependencies (#1819) --- completions/just.bash | 2 +- completions/just.elvish | 1 + completions/just.fish | 1 + completions/just.powershell | 1 + completions/just.zsh | 1 + src/config.rs | 23 +++++++++++++++++ src/justfile.rs | 30 ++++++++++++----------- tests/lib.rs | 1 + tests/no_dependencies.rs | 49 +++++++++++++++++++++++++++++++++++++ 9 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 tests/no_dependencies.rs diff --git a/completions/just.bash b/completions/just.bash index 4647ceb..15adcf9 100644 --- a/completions/just.bash +++ b/completions/just.bash @@ -20,7 +20,7 @@ _just() { case "${cmd}" in just) - opts=" -n -q -u -v -e -l -h -V -f -d -c -s --check --yes --dry-run --highlight --no-dotenv --no-highlight --quiet --shell-command --clear-shell-args --unsorted --unstable --verbose --changelog --choose --dump --edit --evaluate --fmt --init --list --summary --variables --help --version --chooser --color --command-color --dump-format --list-heading --list-prefix --justfile --set --shell --shell-arg --working-directory --command --completions --show --dotenv-filename --dotenv-path ... " + opts=" -n -q -u -v -e -l -h -V -f -d -c -s --check --yes --dry-run --highlight --no-deps --no-dotenv --no-highlight --quiet --shell-command --clear-shell-args --unsorted --unstable --verbose --changelog --choose --dump --edit --evaluate --fmt --init --list --summary --variables --help --version --chooser --color --command-color --dump-format --list-heading --list-prefix --justfile --set --shell --shell-arg --working-directory --command --completions --show --dotenv-filename --dotenv-path ... " if [[ ${cur} == -* ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/just.elvish b/completions/just.elvish index 57ad502..49b19a2 100644 --- a/completions/just.elvish +++ b/completions/just.elvish @@ -39,6 +39,7 @@ edit:completion:arg-completer[just] = [@words]{ cand -n 'Print what just would do without doing it' cand --dry-run 'Print what just would do without doing it' cand --highlight 'Highlight echoed recipe lines in bold' + cand --no-deps 'Don''t run recipe dependencies' cand --no-dotenv 'Don''t load `.env` file' cand --no-highlight 'Don''t highlight echoed recipe lines in bold' cand -q 'Suppress all output' diff --git a/completions/just.fish b/completions/just.fish index aa28dbd..1ec8057 100644 --- a/completions/just.fish +++ b/completions/just.fish @@ -55,6 +55,7 @@ complete -c just -n "__fish_use_subcommand" -l check -d 'Run `--fmt` in \'check\ complete -c just -n "__fish_use_subcommand" -l yes -d 'Automatically confirm all recipes.' complete -c just -n "__fish_use_subcommand" -s n -l dry-run -d 'Print what just would do without doing it' complete -c just -n "__fish_use_subcommand" -l highlight -d 'Highlight echoed recipe lines in bold' +complete -c just -n "__fish_use_subcommand" -l no-deps -d 'Don\'t run recipe dependencies' complete -c just -n "__fish_use_subcommand" -l no-dotenv -d 'Don\'t load `.env` file' complete -c just -n "__fish_use_subcommand" -l no-highlight -d 'Don\'t highlight echoed recipe lines in bold' complete -c just -n "__fish_use_subcommand" -s q -l quiet -d 'Suppress all output' diff --git a/completions/just.powershell b/completions/just.powershell index 92439dc..cf3b6da 100644 --- a/completions/just.powershell +++ b/completions/just.powershell @@ -44,6 +44,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock { [CompletionResult]::new('-n', 'n', [CompletionResultType]::ParameterName, 'Print what just would do without doing it') [CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'Print what just would do without doing it') [CompletionResult]::new('--highlight', 'highlight', [CompletionResultType]::ParameterName, 'Highlight echoed recipe lines in bold') + [CompletionResult]::new('--no-deps', 'no-deps', [CompletionResultType]::ParameterName, 'Don''t run recipe dependencies') [CompletionResult]::new('--no-dotenv', 'no-dotenv', [CompletionResultType]::ParameterName, 'Don''t load `.env` file') [CompletionResult]::new('--no-highlight', 'no-highlight', [CompletionResultType]::ParameterName, 'Don''t highlight echoed recipe lines in bold') [CompletionResult]::new('-q', 'q', [CompletionResultType]::ParameterName, 'Suppress all output') diff --git a/completions/just.zsh b/completions/just.zsh index 46f3556..a8d1d7f 100644 --- a/completions/just.zsh +++ b/completions/just.zsh @@ -40,6 +40,7 @@ _just() { '(-q --quiet)-n[Print what just would do without doing it]' \ '(-q --quiet)--dry-run[Print what just would do without doing it]' \ '--highlight[Highlight echoed recipe lines in bold]' \ +'--no-deps[Don'\''t run recipe dependencies]' \ '--no-dotenv[Don'\''t load `.env` file]' \ '--no-highlight[Don'\''t highlight echoed recipe lines in bold]' \ '(-n --dry-run)-q[Suppress all output]' \ diff --git a/src/config.rs b/src/config.rs index d568585..0a07146 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,6 +31,7 @@ pub(crate) struct Config { pub(crate) list_heading: String, pub(crate) list_prefix: String, pub(crate) load_dotenv: bool, + pub(crate) no_dependencies: bool, pub(crate) search_config: SearchConfig, pub(crate) shell: Option, pub(crate) shell_args: Option>, @@ -102,6 +103,7 @@ mod arg { pub(crate) const JUSTFILE: &str = "JUSTFILE"; pub(crate) const LIST_HEADING: &str = "LIST-HEADING"; pub(crate) const LIST_PREFIX: &str = "LIST-PREFIX"; + pub(crate) const NO_DEPS: &str = "NO-DEPS"; pub(crate) const NO_DOTENV: &str = "NO-DOTENV"; pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT"; pub(crate) const QUIET: &str = "QUIET"; @@ -213,6 +215,12 @@ impl Config { .value_name("TEXT") .takes_value(true), ) + .arg ( + Arg::with_name(arg::NO_DEPS) + .long("no-deps") + .alias("no-dependencies") + .help("Don't run recipe dependencies") + ) .arg( Arg::with_name(arg::NO_DOTENV) .long("no-dotenv") @@ -650,6 +658,7 @@ impl Config { .unwrap_or(" ") .to_owned(), load_dotenv: !matches.is_present(arg::NO_DOTENV), + no_dependencies: matches.is_present(arg::NO_DEPS), search_config, shell: matches.value_of(arg::SHELL).map(str::to_owned), shell_args, @@ -695,6 +704,7 @@ mod tests { $(dry_run: $dry_run:expr,)? $(dump_format: $dump_format:expr,)? $(highlight: $highlight:expr,)? + $(no_dependencies: $no_dependencies:expr,)? $(search_config: $search_config:expr,)? $(shell: $shell:expr,)? $(shell_args: $shell_args:expr,)? @@ -714,6 +724,7 @@ mod tests { $(dry_run: $dry_run,)? $(dump_format: $dump_format,)? $(highlight: $highlight,)? + $(no_dependencies: $no_dependencies,)? $(search_config: $search_config,)? $(shell: $shell,)? $(shell_args: $shell_args,)? @@ -889,6 +900,18 @@ mod tests { highlight: false, } + test! { + name: no_deps, + args: ["--no-deps"], + no_dependencies: true, + } + + test! { + name: no_dependencies, + args: ["--no-dependencies"], + no_dependencies: true, + } + test! { name: unsorted_default, args: [], diff --git a/src/justfile.rs b/src/justfile.rs index 0b862d2..84e1c2b 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -281,17 +281,17 @@ impl<'src> Justfile<'src> { }; Self::run_recipe( - &context, - invocation.recipe, &invocation .arguments .iter() .copied() .map(str::to_string) .collect::>(), + &context, &dotenv, - search, &mut ran, + invocation.recipe, + search, )?; } @@ -402,12 +402,12 @@ impl<'src> Justfile<'src> { } fn run_recipe( - context: &RecipeContext<'src, '_>, - recipe: &Recipe<'src>, arguments: &[String], + context: &RecipeContext<'src, '_>, dotenv: &BTreeMap, - search: &Search, ran: &mut Ran<'src>, + recipe: &Recipe<'src>, + search: &Search, ) -> RunResult<'src> { if ran.has_run(&recipe.namepath, arguments) { return Ok(()); @@ -434,18 +434,20 @@ impl<'src> Justfile<'src> { let mut evaluator = Evaluator::recipe_evaluator(context.config, dotenv, &scope, context.settings, search); - for Dependency { recipe, arguments } in recipe.dependencies.iter().take(recipe.priors) { - let arguments = arguments - .iter() - .map(|argument| evaluator.evaluate_expression(argument)) - .collect::>>()?; + if !context.config.no_dependencies { + for Dependency { recipe, arguments } in recipe.dependencies.iter().take(recipe.priors) { + let arguments = arguments + .iter() + .map(|argument| evaluator.evaluate_expression(argument)) + .collect::>>()?; - Self::run_recipe(context, recipe, &arguments, dotenv, search, ran)?; + Self::run_recipe(&arguments, context, dotenv, ran, recipe, search)?; + } } recipe.run(context, dotenv, scope.child(), search, &positional)?; - { + if !context.config.no_dependencies { let mut ran = Ran::default(); for Dependency { recipe, arguments } in recipe.dependencies.iter().skip(recipe.priors) { @@ -455,7 +457,7 @@ impl<'src> Justfile<'src> { evaluated.push(evaluator.evaluate_expression(argument)?); } - Self::run_recipe(context, recipe, &evaluated, dotenv, search, &mut ran)?; + Self::run_recipe(&evaluated, context, dotenv, &mut ran, recipe, search)?; } } diff --git a/tests/lib.rs b/tests/lib.rs index 15799fb..24d3ffa 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -67,6 +67,7 @@ mod modules; mod multibyte_char; mod newline_escape; mod no_cd; +mod no_dependencies; mod no_exit_message; mod os_attributes; mod parser; diff --git a/tests/no_dependencies.rs b/tests/no_dependencies.rs new file mode 100644 index 0000000..68a3ac1 --- /dev/null +++ b/tests/no_dependencies.rs @@ -0,0 +1,49 @@ +use super::*; + +#[test] +fn skip_normal_dependency() { + Test::new() + .justfile( + " + a: + @echo 'a' + b: a + @echo 'b' + ", + ) + .args(["--no-deps", "b"]) + .stdout("b\n") + .run(); +} + +#[test] +fn skip_prior_dependency() { + Test::new() + .justfile( + " + a: + @echo 'a' + b: && a + @echo 'b' + ", + ) + .args(["--no-deps", "b"]) + .stdout("b\n") + .run(); +} + +#[test] +fn skip_dependency_multi() { + Test::new() + .justfile( + " + a: + @echo 'a' + b: && a + @echo 'b' + ", + ) + .args(["--no-deps", "b", "a"]) + .stdout("b\na\n") + .run(); +}