Skip to content

Commit

Permalink
refactor: argc run (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored May 25, 2024
1 parent aa0f67f commit fb14645
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ application = [
"num_cpus",
"threadpool",
"base64",
"path-absolutize"
]
native-runtime = ["which"]
eval = []
Expand Down Expand Up @@ -57,6 +58,7 @@ threadpool = { version = "1.8", optional = true }
base64 = { version = "0.22", optional = true }
natord = { version = "1.0", optional = true }
roff = { version = "0.2", optional = true }
path-absolutize = { version = "3.1.1", optional = true }

[dev-dependencies]
insta = "1.30"
Expand Down
36 changes: 19 additions & 17 deletions src/bin/argc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use argc::{
CompKind, NativeRuntime, Runtime, Shell, COMPGEN_KIND_SYMBOL,
};
use base64::{engine::general_purpose, Engine as _};
use path_absolutize::Absolutize;
use std::{
collections::HashMap,
env,
Expand Down Expand Up @@ -90,29 +91,30 @@ fn run() -> Result<i32> {
let shell = runtime.shell_path()?;
let script_path = normalize_script_path(&args[2]);
let (script_dir, script_path) = {
let script_path = fs::canonicalize(&script_path)
.with_context(|| format!("Failed to run '{script_path}'"))?;
let script_dir = script_path.parent().unwrap();
let script_path = {
let path = script_path.display().to_string();
if cfg!(windows) && path.starts_with(r"\\?\") {
path[4..].to_string()
} else {
path
}
};
(script_dir.to_path_buf(), script_path)
let absolute_script_path = Path::new(&script_path)
.absolutize()
.with_context(|| format!("Invalid script path '{script_path}'"))?;
let script_dir = absolute_script_path.parent().ok_or_else(|| {
anyhow!("Unable to retrieve the script dir from '{script_path}'")
})?;
(
script_dir.to_path_buf(),
absolute_script_path.display().to_string(),
)
};
let mut envs = HashMap::new();
if is_runner_script(&script_path) {
let cwd = if is_runner_script(&script_path) {
if let Some(cwd) = runtime.current_dir() {
if env::var("ARGC_PWD").is_err() {
envs.insert("ARGC_PWD".to_string(), escape_shell_words(&cwd));
}
}
}
Some(script_dir.as_path())
} else {
None
};
let args = [vec![&script_path], args.iter().skip(3).collect()].concat();
return run_command(&script_path, &shell, &args, envs, Some(&script_dir));
return run_command(&script_path, &shell, &args, envs, cwd);
}
"--argc-create" => {
if let Some((_, script_file)) = get_script_path(false) {
Expand Down Expand Up @@ -458,10 +460,10 @@ eval "$(argc --argc-eval "$0" "$@")"
}

fn get_script_path(recursive: bool) -> Option<(PathBuf, PathBuf)> {
let candidates = runner_script_names();
let names = runner_script_names();
let mut dir = env::current_dir().ok()?;
loop {
for name in candidates.iter() {
for name in names.iter() {
let path = dir.join(name);
if path.exists() {
return Some((dir, path));
Expand Down

0 comments on commit fb14645

Please sign in to comment.