Skip to content

Instantly share code, notes, and snippets.

@ubnt-intrepid
Created April 6, 2017 18:44
Show Gist options
  • Select an option

  • Save ubnt-intrepid/97fe80fc233b40649f4446c4d299013a to your computer and use it in GitHub Desktop.

Select an option

Save ubnt-intrepid/97fe80fc233b40649f4446c4d299013a to your computer and use it in GitHub Desktop.
c:\windows\system32\bash.exe 経由で任意のコマンドを手軽に実行できるやつ(要は wslbridge の簡易版)

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.rs

TODO:

  • 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(&quoted(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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment