Usage:
> $command = "vim"
> rustc -C opt-level=3 -C debuginfo=0 -o "C:\path\to\$($command).exe" wsl.rs
> C:\path\to\vim.exe .\wsl.rsTODO:
- Windows 側のパスを受け取った場合の対処
| // rustc -C opt-level=3 -C debuginfo=0 -o {command-name}.exe wsl.rs | |
| use std::env; | |
| use std::path::Path; | |
| use std::process::{Command, Stdio}; | |
| const BASH_PATH: &'static str = r"C:\Windows\System32\bash.exe"; | |
| fn main() { | |
| let mut command = env::args().next().and_then(|s| { | |
| Path::new(&s).file_stem().map(|s| s.to_string_lossy().into_owned()) | |
| }).unwrap(); | |
| let args = env::args().skip(1).fold(String::new(), |mut acc, s| { | |
| if !acc.is_empty() { | |
| acc.push_str(" "); | |
| } | |
| acc.push_str("ed(s)); | |
| acc | |
| }); | |
| let bash_arg = if !args.is_empty() { | |
| command.push_str(" "); | |
| command.push_str(&args); | |
| command | |
| } else { | |
| command | |
| }; | |
| println!("[debug] {:?}", bash_arg); | |
| Command::new(BASH_PATH).arg("-lc").arg(bash_arg) | |
| .stdin(Stdio::inherit()) | |
| .stdout(Stdio::inherit()) | |
| .stderr(Stdio::inherit()) | |
| .status().unwrap(); | |
| } | |
| fn quoted(s: String) -> String { | |
| let s = s.replace(r"\", "/"); | |
| if s.contains("\"") { | |
| format!("\"{}\"", s.replace("\"", "\\\"")) | |
| } else { | |
| s | |
| } | |
| } |