Add just_pid
function (#1833)
This commit is contained in:
parent
8fc91b298c
commit
53cea2f823
17
README.md
17
README.md
@ -1323,6 +1323,23 @@ $ just
|
|||||||
The executable is at: /bin/just
|
The executable is at: /bin/just
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Just Process ID
|
||||||
|
|
||||||
|
- `just_pid()` - Process ID of the `just` executable.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```just
|
||||||
|
pid:
|
||||||
|
@echo The process ID is: {{ just_pid() }}
|
||||||
|
```
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ just
|
||||||
|
The process ID is: 420
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#### String Manipulation
|
#### String Manipulation
|
||||||
|
|
||||||
- `quote(s)` - Replace all single quotes with `'\''` and prepend and append
|
- `quote(s)` - Replace all single quotes with `'\''` and prepend and append
|
||||||
|
@ -41,6 +41,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
|
|||||||
"invocation_directory_native" => Nullary(invocation_directory_native),
|
"invocation_directory_native" => Nullary(invocation_directory_native),
|
||||||
"join" => BinaryPlus(join),
|
"join" => BinaryPlus(join),
|
||||||
"just_executable" => Nullary(just_executable),
|
"just_executable" => Nullary(just_executable),
|
||||||
|
"just_pid" => Nullary(just_pid),
|
||||||
"justfile" => Nullary(justfile),
|
"justfile" => Nullary(justfile),
|
||||||
"justfile_directory" => Nullary(justfile_directory),
|
"justfile_directory" => Nullary(justfile_directory),
|
||||||
"kebabcase" => Unary(kebabcase),
|
"kebabcase" => Unary(kebabcase),
|
||||||
@ -251,6 +252,10 @@ fn just_executable(_context: &FunctionContext) -> Result<String, String> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn just_pid(_context: &FunctionContext) -> Result<String, String> {
|
||||||
|
Ok(std::process::id().to_string())
|
||||||
|
}
|
||||||
|
|
||||||
fn justfile(context: &FunctionContext) -> Result<String, String> {
|
fn justfile(context: &FunctionContext) -> Result<String, String> {
|
||||||
context
|
context
|
||||||
.search
|
.search
|
||||||
|
@ -651,3 +651,14 @@ fn sha256_file() {
|
|||||||
.stdout("177b3d79aaafb53a7a4d7aaba99a82f27c73370e8cb0295571aade1e4fea1cd2")
|
.stdout("177b3d79aaafb53a7a4d7aaba99a82f27c73370e8cb0295571aade1e4fea1cd2")
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn just_pid() {
|
||||||
|
let Output { stdout, pid, .. } = Test::new()
|
||||||
|
.args(["--evaluate", "x"])
|
||||||
|
.justfile("x := just_pid()")
|
||||||
|
.stdout_regex(r"\d+")
|
||||||
|
.run();
|
||||||
|
|
||||||
|
assert_eq!(stdout.parse::<u32>().unwrap(), pid);
|
||||||
|
}
|
||||||
|
@ -85,7 +85,9 @@ fn test_invocation_directory() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn invocation_directory_native() {
|
fn invocation_directory_native() {
|
||||||
let Output { stdout, tempdir } = Test::new()
|
let Output {
|
||||||
|
stdout, tempdir, ..
|
||||||
|
} = Test::new()
|
||||||
.justfile("x := invocation_directory_native()")
|
.justfile("x := invocation_directory_native()")
|
||||||
.args(["--evaluate", "x"])
|
.args(["--evaluate", "x"])
|
||||||
.stdout_regex(".*")
|
.stdout_regex(".*")
|
||||||
|
@ -35,6 +35,7 @@ macro_rules! test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Output {
|
pub(crate) struct Output {
|
||||||
|
pub(crate) pid: u32,
|
||||||
pub(crate) stdout: String,
|
pub(crate) stdout: String,
|
||||||
pub(crate) tempdir: TempDir,
|
pub(crate) tempdir: TempDir,
|
||||||
}
|
}
|
||||||
@ -210,6 +211,8 @@ impl Test {
|
|||||||
.spawn()
|
.spawn()
|
||||||
.expect("just invocation failed");
|
.expect("just invocation failed");
|
||||||
|
|
||||||
|
let pid = child.id();
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut stdin_handle = child.stdin.take().expect("failed to unwrap stdin handle");
|
let mut stdin_handle = child.stdin.take().expect("failed to unwrap stdin handle");
|
||||||
|
|
||||||
@ -257,8 +260,9 @@ impl Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Output {
|
Output {
|
||||||
tempdir: self.tempdir,
|
pid,
|
||||||
stdout: output_stdout.into(),
|
stdout: output_stdout.into(),
|
||||||
|
tempdir: self.tempdir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user