rust-analyzer/xtask/src/not_bash.rs

210 lines
5.7 KiB
Rust
Raw Normal View History

2020-02-14 14:59:19 +00:00
//! A bad shell -- small cross platform module for writing glue code
2020-02-27 10:07:26 +00:00
2020-02-14 14:59:19 +00:00
use std::{
cell::RefCell,
env,
ffi::OsString,
io::Write,
2020-02-14 18:03:45 +00:00
path::{Path, PathBuf},
2020-02-14 14:59:19 +00:00
process::{Command, Stdio},
};
use anyhow::{bail, Context, Result};
pub mod fs2 {
use std::{fs, path::Path};
use anyhow::{Context, Result};
pub fn read_dir<P: AsRef<Path>>(path: P) -> Result<fs::ReadDir> {
let path = path.as_ref();
fs::read_dir(path).with_context(|| format!("Failed to read {}", path.display()))
}
pub fn read_to_string<P: AsRef<Path>>(path: P) -> Result<String> {
let path = path.as_ref();
fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))
}
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
let path = path.as_ref();
fs::write(path, contents).with_context(|| format!("Failed to write {}", path.display()))
}
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
let from = from.as_ref();
let to = to.as_ref();
fs::copy(from, to)
.with_context(|| format!("Failed to copy {} to {}", from.display(), to.display()))
}
2020-02-14 18:03:45 +00:00
pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
fs::remove_file(path).with_context(|| format!("Failed to remove file {}", path.display()))
}
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
fs::remove_dir_all(path).with_context(|| format!("Failed to remove dir {}", path.display()))
}
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
fs::create_dir_all(path).with_context(|| format!("Failed to create dir {}", path.display()))
}
}
2020-07-14 15:14:00 +00:00
#[macro_export]
macro_rules! run {
2020-02-14 14:59:19 +00:00
($($expr:expr),*) => {
run!($($expr),*; echo = true)
};
($($expr:expr),* ; echo = $echo:expr) => {
$crate::not_bash::run_process(format!($($expr),*), $echo, None)
};
($($expr:expr),* ; <$stdin:expr) => {
$crate::not_bash::run_process(format!($($expr),*), false, Some($stdin))
2020-02-14 14:59:19 +00:00
};
}
2020-07-14 15:14:00 +00:00
pub use crate::run;
2020-02-14 14:59:19 +00:00
pub struct Pushd {
_p: (),
}
pub fn pushd(path: impl Into<PathBuf>) -> Pushd {
Env::with(|env| env.pushd(path.into()));
Pushd { _p: () }
}
impl Drop for Pushd {
fn drop(&mut self) {
Env::with(|env| env.popd())
}
}
pub struct Pushenv {
_p: (),
}
pub fn pushenv(var: &str, value: &str) -> Pushenv {
Env::with(|env| env.pushenv(var.into(), value.into()));
Pushenv { _p: () }
}
impl Drop for Pushenv {
fn drop(&mut self) {
Env::with(|env| env.popenv())
}
}
2020-02-14 18:03:45 +00:00
pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
2020-02-17 14:33:31 +00:00
if !path.exists() {
return Ok(());
}
2020-02-14 18:03:45 +00:00
if path.is_file() {
fs2::remove_file(path)
} else {
fs2::remove_dir_all(path)
}
}
2020-02-14 14:59:19 +00:00
#[doc(hidden)]
pub fn run_process(cmd: String, echo: bool, stdin: Option<&[u8]>) -> Result<String> {
run_process_inner(&cmd, echo, stdin).with_context(|| format!("process `{}` failed", cmd))
2020-02-14 14:59:19 +00:00
}
2020-04-08 09:47:40 +00:00
pub fn date_iso() -> Result<String> {
run!("date --iso --utc")
}
fn run_process_inner(cmd: &str, echo: bool, stdin: Option<&[u8]>) -> Result<String> {
2020-02-14 14:59:19 +00:00
let mut args = shelx(cmd);
let binary = args.remove(0);
2020-03-05 10:15:55 +00:00
let current_dir = Env::with(|it| it.cwd().to_path_buf());
2020-02-14 14:59:19 +00:00
if echo {
println!("> {}", cmd)
}
let mut command = Command::new(binary);
command.args(args).current_dir(current_dir).stderr(Stdio::inherit());
let output = match stdin {
None => command.stdin(Stdio::null()).output(),
Some(stdin) => {
command.stdin(Stdio::piped()).stdout(Stdio::piped());
let mut process = command.spawn()?;
process.stdin.take().unwrap().write_all(stdin)?;
process.wait_with_output()
}
}?;
2020-02-14 14:59:19 +00:00
let stdout = String::from_utf8(output.stdout)?;
if echo {
print!("{}", stdout)
}
if !output.status.success() {
2020-02-14 16:11:19 +00:00
bail!("{}", output.status)
2020-02-14 14:59:19 +00:00
}
2020-02-14 17:59:26 +00:00
Ok(stdout.trim().to_string())
2020-02-14 14:59:19 +00:00
}
// FIXME: some real shell lexing here
fn shelx(cmd: &str) -> Vec<String> {
2020-07-07 16:38:34 +00:00
let mut res = Vec::new();
for (string_piece, in_quotes) in cmd.split('\'').zip([false, true].iter().copied().cycle()) {
if in_quotes {
res.push(string_piece.to_string())
} else {
if !string_piece.is_empty() {
res.extend(string_piece.split_ascii_whitespace().map(|it| it.to_string()))
}
}
}
res
2020-02-14 14:59:19 +00:00
}
struct Env {
pushd_stack: Vec<PathBuf>,
pushenv_stack: Vec<(OsString, Option<OsString>)>,
2020-02-14 14:59:19 +00:00
}
impl Env {
fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T {
thread_local! {
2020-03-05 10:15:55 +00:00
static ENV: RefCell<Env> = RefCell::new(Env {
pushd_stack: vec![env::current_dir().unwrap()],
pushenv_stack: vec![],
2020-03-05 10:15:55 +00:00
});
2020-02-14 14:59:19 +00:00
}
ENV.with(|it| f(&mut *it.borrow_mut()))
}
fn pushd(&mut self, dir: PathBuf) {
2020-02-27 09:54:20 +00:00
let dir = self.cwd().join(dir);
2020-03-05 10:15:55 +00:00
self.pushd_stack.push(dir);
env::set_current_dir(self.cwd()).unwrap();
2020-02-14 14:59:19 +00:00
}
fn popd(&mut self) {
self.pushd_stack.pop().unwrap();
2020-03-05 10:15:55 +00:00
env::set_current_dir(self.cwd()).unwrap();
2020-02-14 14:59:19 +00:00
}
fn pushenv(&mut self, var: OsString, value: OsString) {
self.pushenv_stack.push((var.clone(), env::var_os(&var)));
env::set_var(var, value)
}
fn popenv(&mut self) {
let (var, value) = self.pushenv_stack.pop().unwrap();
match value {
None => env::remove_var(var),
Some(value) => env::set_var(var, value),
}
}
2020-03-05 10:15:55 +00:00
fn cwd(&self) -> &Path {
self.pushd_stack.last().unwrap()
2020-02-14 14:59:19 +00:00
}
}