From f1020b4e6a504412130b3e2ae29c86b8653c18a8 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Jul 2024 15:47:18 -0700 Subject: [PATCH] Allow abbreviating functions ending in `_directory` to `_dir` (#2235) --- README.md | 5 +++++ src/function.rs | 10 +++++++++- tests/functions.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c9f03a5..a992d62 100644 --- a/README.md +++ b/README.md @@ -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"`, diff --git a/src/function.rs b/src/function.rs index f741b5e..50bee73 100644 --- a/src/function.rs +++ b/src/function.rs @@ -32,7 +32,15 @@ impl<'src: 'run, 'run> Context<'src, 'run> { } pub(crate) fn get(name: &str) -> Option { - 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), diff --git a/tests/functions.rs b/tests/functions.rs index 8fc81ac..4ebd87e 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -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(); +}