2026-08-09 16:37:41 -07:00
|
|
|
use std::path::{Path, PathBuf};
|
2026-07-18 05:19:31 -07:00
|
|
|
|
|
|
|
|
use brush_core::builtins;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
|
|
|
|
/// Evaluate the provided script in the current shell environment.
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
|
pub(crate) struct DotCommand {
|
|
|
|
|
/// Path to the script to evaluate.
|
|
|
|
|
script_path: String,
|
|
|
|
|
|
|
|
|
|
/// Any arguments to be passed as positional parameters to the script.
|
|
|
|
|
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
|
|
|
|
|
script_args: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl builtins::Command for DotCommand {
|
|
|
|
|
type Error = brush_core::Error;
|
|
|
|
|
|
|
|
|
|
async fn execute<SE: brush_core::ShellExtensions>(
|
|
|
|
|
&self,
|
|
|
|
|
context: brush_core::ExecutionContext<'_, SE>,
|
|
|
|
|
) -> Result<brush_core::ExecutionResult, Self::Error> {
|
|
|
|
|
// TODO(dot): Handle trap inheritance.
|
2026-08-11 20:11:26 -07:00
|
|
|
// hydrashell: POSIX requires `.` to search PATH when its operand has no slash.
|
2026-08-09 16:37:41 -07:00
|
|
|
// Git's `git-subtree` relies on this with `. git-sh-setup`; treating the
|
|
|
|
|
// operand as cwd-relative leaves `require_work_tree` undefined.
|
|
|
|
|
let script_path = Path::new(&self.script_path);
|
|
|
|
|
let resolved_path = if context.shell.options().source_builtin_searches_path
|
|
|
|
|
&& !self.script_path.chars().any(std::path::is_separator)
|
|
|
|
|
{
|
|
|
|
|
context
|
|
|
|
|
.shell
|
|
|
|
|
.env_str("PATH")
|
|
|
|
|
.and_then(|path| {
|
|
|
|
|
std::env::split_paths(path.as_ref())
|
|
|
|
|
.map(|dir| dir.join(script_path))
|
|
|
|
|
.find(|candidate| candidate.is_file())
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| PathBuf::from(script_path))
|
|
|
|
|
} else {
|
|
|
|
|
PathBuf::from(script_path)
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-18 05:19:31 -07:00
|
|
|
context
|
|
|
|
|
.shell
|
2026-08-09 16:37:41 -07:00
|
|
|
.source_script(&resolved_path, self.script_args.iter(), &context.params)
|
2026-07-18 05:19:31 -07:00
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|