rust-analyzer/xtask/src/not_bash.rs

166 lines
4.2 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
use std::{
cell::RefCell,
env,
2020-02-14 16:05:56 +00:00
ffi::OsStr,
fs,
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 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()))
}
}
2020-02-14 14:59:19 +00:00
macro_rules! _run {
($($expr:expr),*) => {
run!($($expr),*; echo = true)
};
($($expr:expr),* ; echo = $echo:expr) => {
$crate::not_bash::run_process(format!($($expr),*), $echo)
};
}
pub(crate) use _run as run;
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())
}
}
2020-02-14 16:05:56 +00:00
pub fn rm(glob: &str) -> Result<()> {
let cwd = Env::with(|env| env.cwd());
ls(glob)?.into_iter().try_for_each(|it| fs::remove_file(cwd.join(it)))?;
Ok(())
}
2020-02-14 18:03:45 +00:00
pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
if path.is_file() {
fs2::remove_file(path)
} else {
fs2::remove_dir_all(path)
}
}
2020-02-14 16:05:56 +00:00
pub fn ls(glob: &str) -> Result<Vec<PathBuf>> {
let cwd = Env::with(|env| env.cwd());
let mut res = Vec::new();
for entry in fs::read_dir(&cwd)? {
let entry = entry?;
if matches(&entry.file_name(), glob) {
let path = entry.path();
let path = path.strip_prefix(&cwd).unwrap();
res.push(path.to_path_buf())
}
}
return Ok(res);
fn matches(file_name: &OsStr, glob: &str) -> bool {
assert!(glob.starts_with('*'));
file_name.to_string_lossy().ends_with(&glob[1..])
}
}
2020-02-14 14:59:19 +00:00
#[doc(hidden)]
pub fn run_process(cmd: String, echo: bool) -> Result<String> {
run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd))
}
fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
let cwd = Env::with(|env| env.cwd());
let mut args = shelx(cmd);
let binary = args.remove(0);
if echo {
println!("> {}", cmd)
}
let output = Command::new(binary)
.args(args)
.current_dir(cwd)
.stdin(Stdio::null())
.stderr(Stdio::inherit())
.output()?;
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> {
cmd.split_whitespace().map(|it| it.to_string()).collect()
}
#[derive(Default)]
struct Env {
pushd_stack: Vec<PathBuf>,
}
impl Env {
fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T {
thread_local! {
static ENV: RefCell<Env> = Default::default();
}
ENV.with(|it| f(&mut *it.borrow_mut()))
}
fn pushd(&mut self, dir: PathBuf) {
self.pushd_stack.push(dir)
}
fn popd(&mut self) {
self.pushd_stack.pop().unwrap();
}
fn cwd(&self) -> PathBuf {
self.pushd_stack.last().cloned().unwrap_or_else(|| env::current_dir().unwrap())
}
}