Skip to content

Commit

Permalink
Add source_file() and source_directory() functions (#2088)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored May 25, 2024
1 parent d7059f8 commit 7fb0476
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 135 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,18 @@ script:
./{{justfile_directory()}}/scripts/some_script
```

#### Source and Source Directory

- `source()`<sup>master</sup> - Retrieves the path of the current source file.

- `source_directory()`<sup>master</sup> - Retrieves the path of the parent directory of the
current source file.

`source()` and `source_directory()` behave the same as `justfile()` and
`justfile_directory()` in the root `justfile`, but will return the path and
directory, respectively, of the current `import` or `mod` source file when
called from within an import or submodule.

#### Just Executable

- `just_executable()` - Absolute path to the `just` executable.
Expand Down
65 changes: 24 additions & 41 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,13 @@ impl<'src, 'run> Evaluator<'src, 'run> {
Expression::Call { thunk } => {
use Thunk::*;

match thunk {
Nullary { name, function, .. } => function(self).map_err(|message| Error::FunctionCall {
function: *name,
message,
}),
Unary {
name,
function,
arg,
..
} => {
let result = match thunk {
Nullary { function, .. } => function(function::Context::new(self, thunk.name())),
Unary { function, arg, .. } => {
let arg = self.evaluate_expression(arg)?;
function(self, &arg).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(function::Context::new(self, thunk.name()), &arg)
}
UnaryOpt {
name,
function,
args: (a, b),
..
Expand All @@ -97,13 +85,9 @@ impl<'src, 'run> Evaluator<'src, 'run> {
None => None,
};

function(self, &a, b.as_deref()).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(function::Context::new(self, thunk.name()), &a, b.as_deref())
}
UnaryPlus {
name,
function,
args: (a, rest),
..
Expand All @@ -113,26 +97,22 @@ impl<'src, 'run> Evaluator<'src, 'run> {
for arg in rest {
rest_evaluated.push(self.evaluate_expression(arg)?);
}
function(self, &a, &rest_evaluated).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(
function::Context::new(self, thunk.name()),
&a,
&rest_evaluated,
)
}
Binary {
name,
function,
args: [a, b],
..
} => {
let a = self.evaluate_expression(a)?;
let b = self.evaluate_expression(b)?;
function(self, &a, &b).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(function::Context::new(self, thunk.name()), &a, &b)
}
BinaryPlus {
name,
function,
args: ([a, b], rest),
..
Expand All @@ -143,26 +123,29 @@ impl<'src, 'run> Evaluator<'src, 'run> {
for arg in rest {
rest_evaluated.push(self.evaluate_expression(arg)?);
}
function(self, &a, &b, &rest_evaluated).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(
function::Context::new(self, thunk.name()),
&a,
&b,
&rest_evaluated,
)
}
Ternary {
name,
function,
args: [a, b, c],
..
} => {
let a = self.evaluate_expression(a)?;
let b = self.evaluate_expression(b)?;
let c = self.evaluate_expression(c)?;
function(self, &a, &b, &c).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(function::Context::new(self, thunk.name()), &a, &b, &c)
}
}
};

result.map_err(|message| Error::FunctionCall {
function: thunk.name(),
message,
})
}
Expression::StringLiteral { string_literal } => Ok(string_literal.cooked.clone()),
Expression::Backtick { contents, token } => {
Expand Down
Loading

0 comments on commit 7fb0476

Please sign in to comment.