More declarative fs massaging

This commit is contained in:
Aleksey Kladov 2020-02-14 17:05:56 +01:00
parent ce29497e43
commit 269e2f22a9
2 changed files with 35 additions and 18 deletions

View File

@ -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)?;
}

View File

@ -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<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..])
}
}
#[doc(hidden)]
pub fn run_process(cmd: String, echo: bool) -> Result<String> {
run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd))