Skip to content

Commit 269e2f2

Browse files
committed
More declarative fs massaging
1 parent ce29497 commit 269e2f2

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

xtask/src/install.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Installs rust-analyzer language server and/or editor plugin.
22
3-
use std::{env, fs, path::PathBuf, str};
3+
use std::{env, path::PathBuf, str};
44

55
use anyhow::{bail, format_err, Context, Result};
6-
use walkdir::WalkDir;
76

8-
use crate::not_bash::{pushd, run};
7+
use crate::not_bash::{ls, pushd, rm, run};
98

109
// Latest stable, feel free to send a PR if this lags behind.
1110
const REQUIRED_RUST_VERSION: u32 = 41;
@@ -85,15 +84,6 @@ fn fix_path_for_mac() -> Result<()> {
8584
fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
8685
let _dir = pushd("./editors/code");
8786

88-
let list_vsixes = || {
89-
WalkDir::new("./editors/code")
90-
.max_depth(1)
91-
.into_iter()
92-
.map(|it| it.unwrap())
93-
.map(|it| it.path().to_owned())
94-
.filter(|it| it.file_name().unwrap_or_default().to_string_lossy().ends_with(".vsix"))
95-
};
96-
9787
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
9888
["code", "code-insiders", "codium", "code-oss"]
9989
.iter()
@@ -110,27 +100,27 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
110100
run!("npm install")?;
111101

112102
let vsix_pkg = {
113-
list_vsixes().try_for_each(fs::remove_file)?;
103+
rm("*.vsix")?;
114104
run!("npm run package --scripts-prepend-node-path")?;
115-
list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string()
105+
ls("*.vsix")?.pop().unwrap()
116106
};
117107

118108
let code = find_code(|bin| run!("{} --version", bin).is_ok())?;
119-
run!("{} --install-extension ./{} --force", code, vsix_pkg)?;
109+
run!("{} --install-extension {} --force", code, vsix_pkg.display())?;
120110
installed_extensions = run!("{} --list-extensions", code; echo = false)?;
121111
} else {
122112
run!("cmd.exe /c npm --version")
123113
.context("`npm` is required to build the VS Code plugin")?;
124114
run!("cmd.exe /c npm install")?;
125115

126116
let vsix_pkg = {
127-
list_vsixes().try_for_each(fs::remove_file)?;
117+
rm("*.vsix")?;
128118
run!("cmd.exe /c npm run package")?;
129-
list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string()
119+
ls("*.vsix")?.pop().unwrap()
130120
};
131121

132122
let code = find_code(|bin| run!("cmd.exe /c {}.cmd --version", bin).is_ok())?;
133-
run!(r"cmd.exe /c {}.cmd --install-extension ./{} --force", code, vsix_pkg)?;
123+
run!(r"cmd.exe /c {}.cmd --install-extension {} --force", code, vsix_pkg.display())?;
134124
installed_extensions = run!("cmd.exe /c {}.cmd --list-extensions", code; echo = false)?;
135125
}
136126

xtask/src/not_bash.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
use std::{
33
cell::RefCell,
44
env,
5+
ffi::OsStr,
6+
fs,
57
path::PathBuf,
68
process::{Command, Stdio},
79
};
@@ -33,6 +35,31 @@ impl Drop for Pushd {
3335
}
3436
}
3537

38+
pub fn rm(glob: &str) -> Result<()> {
39+
let cwd = Env::with(|env| env.cwd());
40+
ls(glob)?.into_iter().try_for_each(|it| fs::remove_file(cwd.join(it)))?;
41+
Ok(())
42+
}
43+
44+
pub fn ls(glob: &str) -> Result<Vec<PathBuf>> {
45+
let cwd = Env::with(|env| env.cwd());
46+
let mut res = Vec::new();
47+
for entry in fs::read_dir(&cwd)? {
48+
let entry = entry?;
49+
if matches(&entry.file_name(), glob) {
50+
let path = entry.path();
51+
let path = path.strip_prefix(&cwd).unwrap();
52+
res.push(path.to_path_buf())
53+
}
54+
}
55+
return Ok(res);
56+
57+
fn matches(file_name: &OsStr, glob: &str) -> bool {
58+
assert!(glob.starts_with('*'));
59+
file_name.to_string_lossy().ends_with(&glob[1..])
60+
}
61+
}
62+
3663
#[doc(hidden)]
3764
pub fn run_process(cmd: String, echo: bool) -> Result<String> {
3865
run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd))

0 commit comments

Comments
 (0)