Skip to content

Commit a5b2f86

Browse files
authored
Add module_path() function (#3270)
1 parent 57b34a7 commit a5b2f86

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,11 +1999,13 @@ called from within an import or submodule.
19991999

20002000
#### Module and Module Directory
20012001

2002-
- `module_file()` - Retrieves the path of the current module file.
2002+
- `module_file()` - Returns the path of the current module file.
20032003

2004-
- `module_directory()` - Retrieves the path of the parent directory of the
2004+
- `module_directory()` - Returns the path of the parent directory of the
20052005
current module file.
20062006

2007+
- `module_path()` - Returns the `::`-separated path to the current module.
2008+
20072009
`module_file()` and `module_directory()` behave the same as `justfile()` and
20082010
`justfile_directory()` in the root `justfile`, but will return the path and
20092011
directory, respectively, of the current `mod` source file when called from

src/function.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
7676
"lowercase" => Unary(lowercase),
7777
"module_directory" => Nullary(module_directory),
7878
"module_file" => Nullary(module_file),
79+
"module_path" => Nullary(module_path),
7980
"num_cpus" => Nullary(num_cpus),
8081
"os" => Nullary(os),
8182
"os_family" => Nullary(os_family),
@@ -463,6 +464,10 @@ fn module_file(context: Context) -> FunctionResult {
463464
})
464465
}
465466

467+
fn module_path(context: Context) -> FunctionResult {
468+
Ok(context.execution_context.module.modulepath.to_string())
469+
}
470+
466471
fn num_cpus(_context: Context) -> FunctionResult {
467472
let num = num_cpus::get();
468473
Ok(num.to_string())

tests/functions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,3 +1508,23 @@ fn shell_with_powershell() {
15081508
.stdout("bar\r\n")
15091509
.success();
15101510
}
1511+
1512+
#[test]
1513+
fn module_path() {
1514+
Test::new()
1515+
.justfile("foo := module_path()")
1516+
.args(["--evaluate", "foo"])
1517+
.stdout("")
1518+
.success();
1519+
}
1520+
1521+
#[test]
1522+
fn module_path_in_submodule() {
1523+
Test::new()
1524+
.write("foo.just", "mod bar")
1525+
.write("bar.just", "baz := module_path()")
1526+
.justfile("mod foo")
1527+
.args(["--evaluate", "foo::bar::baz"])
1528+
.stdout("foo::bar")
1529+
.success();
1530+
}

0 commit comments

Comments
 (0)