Allow filtering variables to evaluate (#795)

If variable names are passed to `--evaluate`, only print those
variables.
This commit is contained in:
Casey Rodarmor 2021-04-05 21:50:50 -07:00 committed by GitHub
parent fec979c2c6
commit d03aedd5c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 28 deletions

View File

@ -43,7 +43,7 @@ edit:completion:arg-completer[just] = [@words]{
cand --dump 'Print entire justfile' cand --dump 'Print entire justfile'
cand -e 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' cand -e 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
cand --edit 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' cand --edit 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
cand --evaluate 'Print evaluated variables' cand --evaluate 'Evaluate and print all variables. If positional arguments are present, only print the variables whose names are given as arguments.'
cand --init 'Initialize new justfile in project root' cand --init 'Initialize new justfile in project root'
cand -l 'List available recipes and their arguments' cand -l 'List available recipes and their arguments'
cand --list 'List available recipes and their arguments' cand --list 'List available recipes and their arguments'

View File

@ -31,7 +31,7 @@ complete -c just -n "__fish_use_subcommand" -s v -l verbose -d 'Use verbose outp
complete -c just -n "__fish_use_subcommand" -l choose -d 'Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`' complete -c just -n "__fish_use_subcommand" -l choose -d 'Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
complete -c just -n "__fish_use_subcommand" -l dump -d 'Print entire justfile' complete -c just -n "__fish_use_subcommand" -l dump -d 'Print entire justfile'
complete -c just -n "__fish_use_subcommand" -s e -l edit -d 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' complete -c just -n "__fish_use_subcommand" -s e -l edit -d 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
complete -c just -n "__fish_use_subcommand" -l evaluate -d 'Print evaluated variables' complete -c just -n "__fish_use_subcommand" -l evaluate -d 'Evaluate and print all variables. If positional arguments are present, only print the variables whose names are given as arguments.'
complete -c just -n "__fish_use_subcommand" -l init -d 'Initialize new justfile in project root' complete -c just -n "__fish_use_subcommand" -l init -d 'Initialize new justfile in project root'
complete -c just -n "__fish_use_subcommand" -s l -l list -d 'List available recipes and their arguments' complete -c just -n "__fish_use_subcommand" -s l -l list -d 'List available recipes and their arguments'
complete -c just -n "__fish_use_subcommand" -l summary -d 'List names of available recipes' complete -c just -n "__fish_use_subcommand" -l summary -d 'List names of available recipes'

View File

@ -48,7 +48,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {
[CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print entire justfile') [CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print entire justfile')
[CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`') [CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
[CompletionResult]::new('--edit', 'edit', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`') [CompletionResult]::new('--edit', 'edit', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
[CompletionResult]::new('--evaluate', 'evaluate', [CompletionResultType]::ParameterName, 'Print evaluated variables') [CompletionResult]::new('--evaluate', 'evaluate', [CompletionResultType]::ParameterName, 'Evaluate and print all variables. If positional arguments are present, only print the variables whose names are given as arguments.')
[CompletionResult]::new('--init', 'init', [CompletionResultType]::ParameterName, 'Initialize new justfile in project root') [CompletionResult]::new('--init', 'init', [CompletionResultType]::ParameterName, 'Initialize new justfile in project root')
[CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'List available recipes and their arguments') [CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'List available recipes and their arguments')
[CompletionResult]::new('--list', 'list', [CompletionResultType]::ParameterName, 'List available recipes and their arguments') [CompletionResult]::new('--list', 'list', [CompletionResultType]::ParameterName, 'List available recipes and their arguments')

View File

@ -44,7 +44,7 @@ _just() {
'--dump[Print entire justfile]' \ '--dump[Print entire justfile]' \
'-e[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \ '-e[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
'--edit[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \ '--edit[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
'--evaluate[Print evaluated variables]' \ '--evaluate[Evaluate and print all variables. If positional arguments are present, only print the variables whose names are given as arguments.]' \
'--init[Initialize new justfile in project root]' \ '--init[Initialize new justfile in project root]' \
'-l[List available recipes and their arguments]' \ '-l[List available recipes and their arguments]' \
'--list[List available recipes and their arguments]' \ '--list[List available recipes and their arguments]' \

View File

@ -246,11 +246,10 @@ impl Config {
.long("edit") .long("edit")
.help("Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`"), .help("Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`"),
) )
.arg( .arg(Arg::with_name(cmd::EVALUATE).long("evaluate").help(
Arg::with_name(cmd::EVALUATE) "Evaluate and print all variables. If positional arguments are present, only print the \
.long("evaluate") variables whose names are given as arguments.",
.help("Print evaluated variables"), ))
)
.arg( .arg(
Arg::with_name(cmd::INIT) Arg::with_name(cmd::INIT)
.long("init") .long("init")
@ -421,13 +420,10 @@ impl Config {
name: name.to_owned(), name: name.to_owned(),
} }
} else if matches.is_present(cmd::EVALUATE) { } else if matches.is_present(cmd::EVALUATE) {
if !positional.arguments.is_empty() { Subcommand::Evaluate {
return Err(ConfigError::SubcommandArguments { variables: positional.arguments,
subcommand: format!("--{}", cmd::EVALUATE.to_lowercase()), overrides,
arguments: positional.arguments,
});
} }
Subcommand::Evaluate { overrides }
} else if matches.is_present(cmd::VARIABLES) { } else if matches.is_present(cmd::VARIABLES) {
Subcommand::Variables Subcommand::Variables
} else { } else {
@ -516,7 +512,7 @@ impl Config {
Choose { overrides, chooser } => Choose { overrides, chooser } =>
self.choose(justfile, &search, overrides, chooser.as_deref())?, self.choose(justfile, &search, overrides, chooser.as_deref())?,
Dump => Self::dump(justfile), Dump => Self::dump(justfile),
Evaluate { overrides } => self.run(justfile, &search, overrides, &[])?, Evaluate { overrides, .. } => self.run(justfile, &search, overrides, &[])?,
List => self.list(justfile), List => self.list(justfile),
Run { Run {
arguments, arguments,
@ -877,7 +873,9 @@ FLAGS:
--dump Print entire justfile --dump Print entire justfile
-e, --edit Edit justfile with editor given by $VISUAL or $EDITOR, falling back \ -e, --edit Edit justfile with editor given by $VISUAL or $EDITOR, falling back \
to `vim` to `vim`
--evaluate Print evaluated variables --evaluate Evaluate and print all variables. If positional arguments are \
present, only print the
variables whose names are given as arguments.
--highlight Highlight echoed recipe lines in bold --highlight Highlight echoed recipe lines in bold
--init Initialize new justfile in project root --init Initialize new justfile in project root
-l, --list List available recipes and their arguments -l, --list List available recipes and their arguments
@ -1332,6 +1330,7 @@ ARGS:
args: ["--evaluate"], args: ["--evaluate"],
subcommand: Subcommand::Evaluate { subcommand: Subcommand::Evaluate {
overrides: map!{}, overrides: map!{},
variables: vec![],
}, },
} }
@ -1340,6 +1339,16 @@ ARGS:
args: ["--evaluate", "x=y"], args: ["--evaluate", "x=y"],
subcommand: Subcommand::Evaluate { subcommand: Subcommand::Evaluate {
overrides: map!{"x": "y"}, overrides: map!{"x": "y"},
variables: vec![],
},
}
test! {
name: subcommand_evaluate_overrides_with_argument,
args: ["--evaluate", "x=y", "foo"],
subcommand: Subcommand::Evaluate {
overrides: map!{"x": "y"},
variables: vec!["foo".to_owned()],
}, },
} }
@ -1583,16 +1592,6 @@ ARGS:
}, },
} }
error! {
name: evaluate_arguments,
args: ["--evaluate", "bar"],
error: ConfigError::SubcommandArguments { subcommand, arguments },
check: {
assert_eq!(subcommand, "--evaluate");
assert_eq!(arguments, &["bar"]);
},
}
error! { error! {
name: dump_arguments, name: dump_arguments,
args: ["--dump", "bar"], args: ["--dump", "bar"],

View File

@ -107,14 +107,26 @@ impl<'src> Justfile<'src> {
)? )?
}; };
if let Subcommand::Evaluate { .. } = config.subcommand { if let Subcommand::Evaluate { variables, .. } = &config.subcommand {
let mut width = 0; let mut width = 0;
for name in scope.names() { for name in scope.names() {
if !variables.is_empty() && !variables.iter().any(|variable| variable == name) {
continue;
}
width = cmp::max(name.len(), width); width = cmp::max(name.len(), width);
} }
for binding in scope.bindings() { for binding in scope.bindings() {
if !variables.is_empty()
&& !variables
.iter()
.any(|variable| variable == binding.name.lexeme())
{
continue;
}
println!( println!(
"{0:1$} := \"{2}\"", "{0:1$} := \"{2}\"",
binding.name.lexeme(), binding.name.lexeme(),
@ -122,6 +134,7 @@ impl<'src> Justfile<'src> {
binding.value binding.value
); );
} }
return Ok(()); return Ok(());
} }

View File

@ -13,6 +13,7 @@ pub(crate) enum Subcommand {
Edit, Edit,
Evaluate { Evaluate {
overrides: BTreeMap<String, String>, overrides: BTreeMap<String, String>,
variables: Vec<String>,
}, },
Init, Init,
List, List,

View File

@ -27,3 +27,17 @@ test! {
a := "foo" a := "foo"
"#, "#,
} }
test! {
name: evaluate_arguments,
justfile: "
a := 'x'
b := 'y'
c := 'z'
",
args: ("--evaluate", "a", "c"),
stdout: r#"
a := "x"
c := "z"
"#,
}