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

Add just_executable() function #775

Merged
merged 5 commits into from
Mar 28, 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
16 changes: 16 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,22 @@ script:
./{{justfile_directory()}}/scripts/some_script
```

==== Just Executable

- `just_executable()` - Absolute path to the just executable.

For example:

```make
executable:
@echo The executable is at: {{just_executable()}}
```

```
$ just
The executable is at: /bin/just
```

==== Dotenv Integration

`just` will load environment variables from a file named `.env`. This file can be located in the same directory as your justfile or in a parent directory. These variables are environment variables, not `just` variables, and so must be accessed using `$VARIABLE_NAME` in recipes and backticks.
Expand Down
13 changes: 13 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static! {
("invocation_directory", Nullary(invocation_directory)),
("env_var", Unary(env_var)),
("env_var_or_default", Binary(env_var_or_default)),
("just_executable", Nullary(just_executable)),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -123,3 +124,15 @@ fn env_var_or_default(
Ok(value) => Ok(value),
}
}

fn just_executable(_context: &FunctionContext) -> Result<String, String> {
let exe_path =
std::env::current_exe().map_err(|e| format!("Error getting current executable: {}", e))?;

exe_path.to_str().map(str::to_owned).ok_or_else(|| {
format!(
"Executable path is not valid unicode: {}",
exe_path.to_string_lossy()
)
})
}
12 changes: 12 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,18 @@ test! {
status: EXIT_FAILURE,
}

test! {
name: test_just_executable_function,
justfile: "
a:
@printf 'Executable path is: %s\\n' '{{ just_executable() }}'
",
args: ("a"),
stdout: format!("Executable path is: {}\n", executable_path("just").to_str().unwrap()).as_str(),
stderr: "",
status: EXIT_SUCCESS,
}

test! {
name: infallable_command,
justfile: r#"
Expand Down