From 269e2f22a942919d421b89287d5669b2c2607917 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 14 Feb 2020 17:05:56 +0100 Subject: More declarative fs massaging --- xtask/src/install.rs | 26 ++++++++------------------ xtask/src/not_bash.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'xtask') diff --git a/xtask/src/install.rs b/xtask/src/install.rs index f89c939b5..540a66130 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -1,11 +1,10 @@ //! Installs rust-analyzer language server and/or editor plugin. -use std::{env, fs, path::PathBuf, str}; +use std::{env, path::PathBuf, str}; use anyhow::{bail, format_err, Context, Result}; -use walkdir::WalkDir; -use crate::not_bash::{pushd, run}; +use crate::not_bash::{ls, pushd, rm, run}; // Latest stable, feel free to send a PR if this lags behind. const REQUIRED_RUST_VERSION: u32 = 41; @@ -85,15 +84,6 @@ fn fix_path_for_mac() -> Result<()> { fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { let _dir = pushd("./editors/code"); - let list_vsixes = || { - WalkDir::new("./editors/code") - .max_depth(1) - .into_iter() - .map(|it| it.unwrap()) - .map(|it| it.path().to_owned()) - .filter(|it| it.file_name().unwrap_or_default().to_string_lossy().ends_with(".vsix")) - }; - let find_code = |f: fn(&str) -> bool| -> Result<&'static str> { ["code", "code-insiders", "codium", "code-oss"] .iter() @@ -110,13 +100,13 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { run!("npm install")?; let vsix_pkg = { - list_vsixes().try_for_each(fs::remove_file)?; + rm("*.vsix")?; run!("npm run package --scripts-prepend-node-path")?; - list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string() + ls("*.vsix")?.pop().unwrap() }; let code = find_code(|bin| run!("{} --version", bin).is_ok())?; - run!("{} --install-extension ./{} --force", code, vsix_pkg)?; + run!("{} --install-extension {} --force", code, vsix_pkg.display())?; installed_extensions = run!("{} --list-extensions", code; echo = false)?; } else { run!("cmd.exe /c npm --version") @@ -124,13 +114,13 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { run!("cmd.exe /c npm install")?; let vsix_pkg = { - list_vsixes().try_for_each(fs::remove_file)?; + rm("*.vsix")?; run!("cmd.exe /c npm run package")?; - list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string() + ls("*.vsix")?.pop().unwrap() }; let code = find_code(|bin| run!("cmd.exe /c {}.cmd --version", bin).is_ok())?; - run!(r"cmd.exe /c {}.cmd --install-extension ./{} --force", code, vsix_pkg)?; + run!(r"cmd.exe /c {}.cmd --install-extension {} --force", code, vsix_pkg.display())?; installed_extensions = run!("cmd.exe /c {}.cmd --list-extensions", code; echo = false)?; } diff --git a/xtask/src/not_bash.rs b/xtask/src/not_bash.rs index a2e47c7af..4ec1efa73 100644 --- a/xtask/src/not_bash.rs +++ b/xtask/src/not_bash.rs @@ -2,6 +2,8 @@ use std::{ cell::RefCell, env, + ffi::OsStr, + fs, path::PathBuf, process::{Command, Stdio}, }; @@ -33,6 +35,31 @@ impl Drop for Pushd { } } +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(()) +} + +pub fn ls(glob: &str) -> Result> { + 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..]) + } +} + #[doc(hidden)] pub fn run_process(cmd: String, echo: bool) -> Result { run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd)) -- cgit v1.2.3