Merge branch 'mkwin'

This commit is contained in:
Valerie Wolfe 2024-06-11 09:12:49 -04:00
commit 6ff3b992bc
6 changed files with 125 additions and 0 deletions

View file

@ -3,6 +3,11 @@
These are tools I use to use Windows primarily through WSL.
## `mkwin`
A Rust utility to quickly make a bash script to run a windows executable with
certain parameters.
## `path-convert`
A Linux utility to canonicalize and convert paths from Unix to DOS, which makes

2
mkwin/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

16
mkwin/Cargo.toml Normal file
View file

@ -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"

11
mkwin/src/error.rs Normal file
View file

@ -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);
}

6
mkwin/src/flag.rs Normal file
View file

@ -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"];

85
mkwin/src/main.rs Normal file
View file

@ -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<String>;
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 <sleeplessval@gmail.com>
Quickly make bash scripts to run windows programs in WSL.
usage: mkwin [flags] <target>
args:
<target> The target program the resulting script will run.
flags:
-h, --help Shows this help text.
--pc=<flags>, The resulting script will use the 'path-convert'
--path-convert=<flags> 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"));}