Skip to content

Commit

Permalink
Allow abbreviating functions ending in _directory to _dir (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jul 7, 2024
1 parent 5e9f46e commit f1020b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,11 @@ Done!
`just` provides a few built-in functions that might be useful when writing
recipes.

All functions ending in `_directory` can be abbreviated to `_dir`. So
`home_directory()` can also be written as `home_dir()`. In addition,
`invocation_directory_native()` can be abbreviated to
`invocation_dir_native()`.

#### System Information

- `arch()` — Instruction set architecture. Possible values are: `"aarch64"`,
Expand Down
10 changes: 9 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ impl<'src: 'run, 'run> Context<'src, 'run> {
}

pub(crate) fn get(name: &str) -> Option<Function> {
let function = match name {
let name = if let Some(prefix) = name.strip_suffix("_dir") {
format!("{prefix}_directory")
} else if let Some(prefix) = name.strip_suffix("_dir_native") {
format!("{prefix}_directory_native")
} else {
name.into()
};

let function = match name.as_str() {
"absolute_path" => Unary(absolute_path),
"append" => Binary(append),
"arch" => Nullary(arch),
Expand Down
30 changes: 30 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,33 @@ fn unary_argument_count_mismamatch_error_message() {
.status(EXIT_FAILURE)
.run();
}

#[test]
fn dir_abbreviations_are_accepted() {
Test::new()
.justfile(
"
abbreviated := justfile_dir()
unabbreviated := justfile_directory()
@foo:
# {{ assert(abbreviated == unabbreviated, 'fail') }}
",
)
.run();
}

#[test]
fn invocation_dir_native_abbreviation_is_accepted() {
Test::new()
.justfile(
"
abbreviated := invocation_directory_native()
unabbreviated := invocation_dir_native()
@foo:
# {{ assert(abbreviated == unabbreviated, 'fail') }}
",
)
.run();
}

0 comments on commit f1020b4

Please sign in to comment.