From eb675928a7b3ac6fdfc56b38645133f0ad43c81d Mon Sep 17 00:00:00 2001 From: Valerie Wolfe Date: Mon, 12 Aug 2024 15:06:09 -0400 Subject: [PATCH] initial implementation for 'or' flag --- man/mkwin.1 | 3 +++ mkwin/Cargo.toml | 2 +- mkwin/src/flag.rs | 1 + mkwin/src/main.rs | 13 ++++++++++--- scripts/pwsh.sh | 17 +++++------------ 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/man/mkwin.1 b/man/mkwin.1 index 63fe31d..ac9e887 100644 --- a/man/mkwin.1 +++ b/man/mkwin.1 @@ -10,6 +10,7 @@ .Op Fl -find Ar root .Op Fl -pc Ar flags .Ar target +.Op Fl -or Ar alt .Op Ar --\ args .Sh DESCRIPTION The @@ -29,6 +30,8 @@ The resulting script will locate the target binary in the given directory using .Xr find 1 . .It Fl h , Fl -help Print a short help message. +.It Fl -or Ar fallback +The resulting script will fall back to the provided argument if the target is not executable. .It Fl -pc Ar flags The resulting script will pass the arguments converted with .Xr path-convert 1 . path-convert will be invoked with the provided flags forwarded (use 'x' to forward no flags). diff --git a/mkwin/Cargo.toml b/mkwin/Cargo.toml index 99aeff0..a47af55 100644 --- a/mkwin/Cargo.toml +++ b/mkwin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mkwin" -version = "0.1.1" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/mkwin/src/flag.rs b/mkwin/src/flag.rs index c89b612..46ca17c 100644 --- a/mkwin/src/flag.rs +++ b/mkwin/src/flag.rs @@ -4,6 +4,7 @@ pub const HELP: [&str;2] = ["-h", "--help"]; pub const EMPTY: [&str;2] = ["-e", "--empty"]; pub const ENV_SHARE: [&str;2] = ["-E", "--env-share"]; pub const FIND_BIN: &str = "--find"; +pub const OR: &str = "--or"; pub const PATH_CONVERT: &str = "--pc"; pub const QUIET: [&str;2] = ["-q", "--quiet"]; diff --git a/mkwin/src/main.rs b/mkwin/src/main.rs index d8a5dad..2c9eba8 100644 --- a/mkwin/src/main.rs +++ b/mkwin/src/main.rs @@ -66,8 +66,8 @@ pub fn main() { // get target executable let target: String; if let Ok(Some(arg)) = args.subcommand() { - if let Some(root) = find_bin { // handle find binary flag - target = format!("target=`find {root} -name '{arg}'`\n\"$target\""); + if let Some(root) = &find_bin { // handle find binary flag + target = format!("`find {root} -name '{arg}'`"); } else { // handle normal path let path = Path::new(&arg); if let Ok(path) = path.canonicalize() { @@ -76,13 +76,20 @@ pub fn main() { } else { error::canonicalize_fail(arg); } } } else { error::missing_target(); } + + let exec = + if let Ok(alt) = args.value_from_str::<&str, String>(flag::OR) { + format!("target={target}\nif [ ! -x \"$target\" ]; then\n\ttarget='{alt}'\nfi\n\n\"$target\"") + } else if find_bin.is_some() { + format!("\"{target}\"") + } else { target }; // construct output println!("#!/usr/bin/bash\n# generated with: mkwin{arg_string}\n"); if env_share { println!("env-share 2> /dev/null\n"); } - print!("{target}"); + print!("{exec}"); // handle forwarded arguments if !forwarded.is_empty() { diff --git a/scripts/pwsh.sh b/scripts/pwsh.sh index 953b237..d426c31 100755 --- a/scripts/pwsh.sh +++ b/scripts/pwsh.sh @@ -1,18 +1,11 @@ #!/usr/bin/bash +# generated with: mkwin /mnt/c/Program Files/PowerShell/7/pwsh.exe -E --or /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe env-share 2> /dev/null -# try to use system-wide version first. -PWSH='/mnt/c/Program Files/PowerShell/7/pwsh.exe' -if [ ! -x "$PWSH" ]; then - unset PWSH +target='/mnt/c/Program Files/PowerShell/7/pwsh.exe' +if [ ! -x "$target" ]; then + target='/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe' fi -# fall back to System32 -if [ -z ${PWSH+x} ]; then - PWSH='/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe' -fi - -# run target -"$PWSH" $@ - +"$target" $@