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

Allow filtering varaibles to evaluate #795

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion completions/just.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ edit:completion:arg-completer[just] = [@words]{
cand --dump 'Print entire justfile'
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 --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 -l 'List available recipes and their arguments'
cand --list 'List available recipes and their arguments'
Expand Down
2 changes: 1 addition & 1 deletion completions/just.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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 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" -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" -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'
Expand Down
2 changes: 1 addition & 1 deletion completions/just.powershell
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {
[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('--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('-l', 'l', [CompletionResultType]::ParameterName, 'List available recipes and their arguments')
[CompletionResult]::new('--list', 'list', [CompletionResultType]::ParameterName, 'List available recipes and their arguments')
Expand Down
2 changes: 1 addition & 1 deletion completions/just.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ _just() {
'--dump[Print entire justfile]' \
'-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`]' \
'--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]' \
'-l[List available recipes and their arguments]' \
'--list[List available recipes and their arguments]' \
Expand Down
45 changes: 22 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,10 @@ impl Config {
.long("edit")
.help("Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`"),
)
.arg(
Arg::with_name(cmd::EVALUATE)
.long("evaluate")
.help("Print evaluated variables"),
)
.arg(Arg::with_name(cmd::EVALUATE).long("evaluate").help(
"Evaluate and print all variables. If positional arguments are present, only print the \
variables whose names are given as arguments.",
))
.arg(
Arg::with_name(cmd::INIT)
.long("init")
Expand Down Expand Up @@ -421,13 +420,10 @@ impl Config {
name: name.to_owned(),
}
} else if matches.is_present(cmd::EVALUATE) {
if !positional.arguments.is_empty() {
return Err(ConfigError::SubcommandArguments {
subcommand: format!("--{}", cmd::EVALUATE.to_lowercase()),
arguments: positional.arguments,
});
Subcommand::Evaluate {
variables: positional.arguments,
overrides,
}
Subcommand::Evaluate { overrides }
} else if matches.is_present(cmd::VARIABLES) {
Subcommand::Variables
} else {
Expand Down Expand Up @@ -516,7 +512,7 @@ impl Config {
Choose { overrides, chooser } =>
self.choose(justfile, &search, overrides, chooser.as_deref())?,
Dump => Self::dump(justfile),
Evaluate { overrides } => self.run(justfile, &search, overrides, &[])?,
Evaluate { overrides, .. } => self.run(justfile, &search, overrides, &[])?,
List => self.list(justfile),
Run {
arguments,
Expand Down Expand Up @@ -877,7 +873,9 @@ FLAGS:
--dump Print entire justfile
-e, --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.
--highlight Highlight echoed recipe lines in bold
--init Initialize new justfile in project root
-l, --list List available recipes and their arguments
Expand Down Expand Up @@ -1332,6 +1330,7 @@ ARGS:
args: ["--evaluate"],
subcommand: Subcommand::Evaluate {
overrides: map!{},
variables: vec![],
},
}

Expand All @@ -1340,6 +1339,16 @@ ARGS:
args: ["--evaluate", "x=y"],
subcommand: Subcommand::Evaluate {
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()],
},
}

Expand Down Expand Up @@ -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! {
name: dump_arguments,
args: ["--dump", "bar"],
Expand Down
15 changes: 14 additions & 1 deletion src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,34 @@ impl<'src> Justfile<'src> {
)?
};

if let Subcommand::Evaluate { .. } = config.subcommand {
if let Subcommand::Evaluate { variables, .. } = &config.subcommand {
let mut width = 0;

for name in scope.names() {
if !variables.is_empty() && !variables.iter().any(|variable| variable == name) {
continue;
}

width = cmp::max(name.len(), width);
}

for binding in scope.bindings() {
if !variables.is_empty()
&& !variables
.iter()
.any(|variable| variable == binding.name.lexeme())
{
continue;
}

println!(
"{0:1$} := \"{2}\"",
binding.name.lexeme(),
width,
binding.value
);
}

return Ok(());
}

Expand Down
1 change: 1 addition & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) enum Subcommand {
Edit,
Evaluate {
overrides: BTreeMap<String, String>,
variables: Vec<String>,
},
Init,
List,
Expand Down
14 changes: 14 additions & 0 deletions tests/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ test! {
a := "foo"
"#,
}

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