diff --git a/mkwin/.gitignore b/mkwin/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/mkwin/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/mkwin/Cargo.toml b/mkwin/Cargo.toml new file mode 100644 index 0000000..7124133 --- /dev/null +++ b/mkwin/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "mkwin" +version = "0.0.1" +edition = "2021" + +[dependencies] +pico-args = { version = "0.5.0", features = [ "eq-separator" ] } + +[profile.release] +opt-level = 's' +codegen-units = 1 +debug = false +lto = true +panic = "abort" +strip = "symbols" + diff --git a/mkwin/src/error.rs b/mkwin/src/error.rs new file mode 100644 index 0000000..36aca2a --- /dev/null +++ b/mkwin/src/error.rs @@ -0,0 +1,11 @@ +//! error handling code + +use std::process::exit; + +/// no argument for target; code 1 +pub fn missing_target() { + println!("mkwin: missing target"); + exit(1); +} + + diff --git a/mkwin/src/flag.rs b/mkwin/src/flag.rs new file mode 100644 index 0000000..9165d3b --- /dev/null +++ b/mkwin/src/flag.rs @@ -0,0 +1,6 @@ +//! constants for flag arguments + +pub const HELP: [&str;2] = ["-h", "--help"]; +pub const PATH_CONVERT: [&str;2] = [ "--pc", "--path-convert" ]; +pub const QUIET: [&str;2] = ["-q", "--quiet"]; + diff --git a/mkwin/src/main.rs b/mkwin/src/main.rs new file mode 100644 index 0000000..79e9661 --- /dev/null +++ b/mkwin/src/main.rs @@ -0,0 +1,85 @@ + +use pico_args::Arguments; + +mod error; +mod flag; + +pub fn main() { + let mut args = Arguments::from_env(); + + // build reference string + let mut arg_string = String::new(); + for arg in args.clone().finish() { + arg_string += &format!(" {}", arg.to_str().unwrap()); + } + + // consume breaking flags + if args.contains(flag::HELP) { + help_text(); + return; + } + + // consume path convert flag + let path_convert: bool; + let path_convert_flags: Option; + match args.value_from_str(flag::PATH_CONVERT) { + Ok(flags) => { + path_convert = true; + path_convert_flags = if flags != "x" { Some(flags) } else { None }; + }, + Err(pico_args::Error::OptionWithoutAValue(_)) => { + path_convert = true; + path_convert_flags = None; + }, + _ => { + path_convert = false; + path_convert_flags = None; + } + } + + // consume simple flags + let quiet = args.contains(flag::QUIET); + + // get target executable + let i_target = args.subcommand().unwrap(); + if i_target.is_none() { + error::missing_target(); + } + let target = i_target.unwrap(); + + // construct output + print!("#!/usr/bin/bash\n# generated with: mkwin{arg_string}\n\n'{target}' "); + + if path_convert { + let flags = if let Some(i) = path_convert_flags { format!("-{i}") } else { String::new() }; + print!("`path-convert {flags} $@`"); + } else { + print!("$@"); + } + + if quiet { + print!(" > /dev/null 2>&1 &"); + } +} + +pub fn help_text() { +println!("mkwin v{} +Valerie Wolfe +Quickly make bash scripts to run windows programs in WSL. + +usage: mkwin [flags] + +args: + The target program the resulting script will run. + +flags: + -h, --help Shows this help text. + --pc=, The resulting script will use the 'path-convert' + --path-convert= to convert arguments from UNIX to DOS, with the + provided set of flags ('x' for no flags). + + -q, --quiet The resulting script will run the target program + in the background and with its outputs redirected + to '/dev/null'.", +env!("CARGO_PKG_VERSION"));} +