Skip to content

Commit ce29497

Browse files
committed
Replace Cmd with not-bash
1 parent bd3a41c commit ce29497

File tree

5 files changed

+171
-154
lines changed

5 files changed

+171
-154
lines changed

xtask/src/cmd.rs

Lines changed: 0 additions & 56 deletions
This file was deleted.

xtask/src/install.rs

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{env, fs, path::PathBuf, str};
55
use anyhow::{bail, format_err, Context, Result};
66
use walkdir::WalkDir;
77

8-
use crate::cmd::{run, run_with_output, Cmd};
8+
use crate::not_bash::{pushd, run};
99

1010
// Latest stable, feel free to send a PR if this lags behind.
1111
const REQUIRED_RUST_VERSION: u32 = 41;
@@ -83,21 +83,9 @@ fn fix_path_for_mac() -> Result<()> {
8383
}
8484

8585
fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
86-
let npm_version = Cmd {
87-
unix: r"npm --version",
88-
windows: r"cmd.exe /c npm --version",
89-
work_dir: "./editors/code",
90-
}
91-
.run();
92-
93-
if npm_version.is_err() {
94-
bail!("`npm --version` failed, `npm` is required to build the VS Code plugin")
95-
}
86+
let _dir = pushd("./editors/code");
9687

97-
Cmd { unix: r"npm install", windows: r"cmd.exe /c npm install", work_dir: "./editors/code" }
98-
.run()?;
99-
100-
let vsixes = || {
88+
let list_vsixes = || {
10189
WalkDir::new("./editors/code")
10290
.max_depth(1)
10391
.into_iter()
@@ -106,50 +94,45 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
10694
.filter(|it| it.file_name().unwrap_or_default().to_string_lossy().ends_with(".vsix"))
10795
};
10896

109-
for path in vsixes() {
110-
fs::remove_file(path)?
111-
}
97+
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
98+
["code", "code-insiders", "codium", "code-oss"]
99+
.iter()
100+
.copied()
101+
.find(|bin| f(bin))
102+
.ok_or_else(|| {
103+
format_err!("Can't execute `code --version`. Perhaps it is not in $PATH?")
104+
})
105+
};
112106

113-
Cmd {
114-
unix: r"npm run package --scripts-prepend-node-path",
115-
windows: r"cmd.exe /c npm run package",
116-
work_dir: "./editors/code",
117-
}
118-
.run()?;
119-
120-
let extension = vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string();
121-
122-
let code_binary = ["code", "code-insiders", "codium", "code-oss"]
123-
.iter()
124-
.find(|bin| {
125-
Cmd {
126-
unix: &format!("{} --version", bin),
127-
windows: &format!("cmd.exe /c {}.cmd --version", bin),
128-
work_dir: "./editors/code",
129-
}
130-
.run()
131-
.is_ok()
132-
})
133-
.ok_or_else(|| {
134-
format_err!("Can't execute `code --version`. Perhaps it is not in $PATH?")
135-
})?;
136-
137-
Cmd {
138-
unix: &format!(r"{} --install-extension ./{} --force", code_binary, extension),
139-
windows: &format!(
140-
r"cmd.exe /c {}.cmd --install-extension ./{} --force",
141-
code_binary, extension
142-
),
143-
work_dir: "./editors/code",
144-
}
145-
.run()?;
107+
let installed_extensions;
108+
if cfg!(unix) {
109+
run!("npm --version").context("`npm` is required to build the VS Code plugin")?;
110+
run!("npm install")?;
146111

147-
let installed_extensions = Cmd {
148-
unix: &format!(r"{} --list-extensions", code_binary),
149-
windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
150-
work_dir: ".",
112+
let vsix_pkg = {
113+
list_vsixes().try_for_each(fs::remove_file)?;
114+
run!("npm run package --scripts-prepend-node-path")?;
115+
list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string()
116+
};
117+
118+
let code = find_code(|bin| run!("{} --version", bin).is_ok())?;
119+
run!("{} --install-extension ./{} --force", code, vsix_pkg)?;
120+
installed_extensions = run!("{} --list-extensions", code; echo = false)?;
121+
} else {
122+
run!("cmd.exe /c npm --version")
123+
.context("`npm` is required to build the VS Code plugin")?;
124+
run!("cmd.exe /c npm install")?;
125+
126+
let vsix_pkg = {
127+
list_vsixes().try_for_each(fs::remove_file)?;
128+
run!("cmd.exe /c npm run package")?;
129+
list_vsixes().next().unwrap().file_name().unwrap().to_string_lossy().to_string()
130+
};
131+
132+
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)?;
134+
installed_extensions = run!("cmd.exe /c {}.cmd --list-extensions", code; echo = false)?;
151135
}
152-
.run_with_output()?;
153136

154137
if !installed_extensions.contains("rust-analyzer") {
155138
bail!(
@@ -163,8 +146,7 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
163146

164147
fn install_server(opts: ServerOpt) -> Result<()> {
165148
let mut old_rust = false;
166-
if let Ok(stdout) = run_with_output("cargo --version", ".") {
167-
println!("{}", stdout);
149+
if let Ok(stdout) = run!("cargo --version") {
168150
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
169151
old_rust = true;
170152
}
@@ -177,20 +159,17 @@ fn install_server(opts: ServerOpt) -> Result<()> {
177159
)
178160
}
179161

180-
let res = if opts.jemalloc {
181-
run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
182-
} else {
183-
run("cargo install --path crates/ra_lsp_server --locked --force", ".")
184-
};
162+
let jemalloc = if opts.jemalloc { "--features jemalloc" } else { "" };
163+
let res = run!("cargo install --path crates/ra_lsp_server --locked --force {}", jemalloc);
185164

186165
if res.is_err() && old_rust {
187166
eprintln!(
188167
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
189168
REQUIRED_RUST_VERSION,
190-
)
169+
);
191170
}
192171

193-
res
172+
res.map(drop)
194173
}
195174

196175
fn check_version(version_output: &str, min_minor_version: u32) -> bool {

xtask/src/lib.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! FIXME: write short doc here
22
3-
mod cmd;
3+
pub mod not_bash;
44
pub mod install;
55
pub mod pre_commit;
66

@@ -16,8 +16,8 @@ use std::{
1616
};
1717

1818
use crate::{
19-
cmd::{run, run_with_output},
2019
codegen::Mode,
20+
not_bash::{pushd, run},
2121
};
2222

2323
pub use anyhow::Result;
@@ -38,9 +38,9 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
3838
ensure_rustfmt()?;
3939

4040
if mode == Mode::Verify {
41-
run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?;
41+
run!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN)?;
4242
} else {
43-
run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
43+
run!("rustup run {} -- cargo fmt", TOOLCHAIN)?;
4444
}
4545
Ok(())
4646
}
@@ -70,8 +70,9 @@ fn ensure_rustfmt() -> Result<()> {
7070
Ok(status) if status.success() => return Ok(()),
7171
_ => (),
7272
};
73-
run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
74-
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
73+
run!("rustup toolchain install {}", TOOLCHAIN)?;
74+
run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?;
75+
Ok(())
7576
}
7677

7778
pub fn run_clippy() -> Result<()> {
@@ -92,34 +93,31 @@ pub fn run_clippy() -> Result<()> {
9293
"clippy::nonminimal_bool",
9394
"clippy::redundant_pattern_matching",
9495
];
95-
run(
96-
&format!(
97-
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
98-
TOOLCHAIN,
99-
allowed_lints.join(" -A ")
100-
),
101-
".",
96+
run!(
97+
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
98+
TOOLCHAIN,
99+
allowed_lints.join(" -A ")
102100
)?;
103101
Ok(())
104102
}
105103

106104
fn install_clippy() -> Result<()> {
107-
run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
108-
run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".")
105+
run!("rustup toolchain install {}", TOOLCHAIN)?;
106+
run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?;
107+
Ok(())
109108
}
110109

111110
pub fn run_fuzzer() -> Result<()> {
112-
match Command::new("cargo")
113-
.args(&["fuzz", "--help"])
114-
.stderr(Stdio::null())
115-
.stdout(Stdio::null())
116-
.status()
117-
{
118-
Ok(status) if status.success() => (),
119-
_ => run("cargo install cargo-fuzz", ".")?,
111+
let _d = pushd("./crates/ra_syntax");
112+
match run!("cargo fuzz --help") {
113+
Ok(_) => (),
114+
_ => {
115+
run!("cargo install cargo-fuzz")?;
116+
}
120117
};
121118

122-
run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
119+
run!("rustup run nightly -- cargo fuzz run parser")?;
120+
Ok(())
123121
}
124122

125123
/// Cleans the `./target` dir after the build such that only
@@ -161,15 +159,15 @@ fn rm_rf(path: &Path) -> Result<()> {
161159
}
162160

163161
pub fn run_release() -> Result<()> {
164-
run("git switch release", ".")?;
165-
run("git fetch upstream", ".")?;
166-
run("git reset --hard upstream/master", ".")?;
167-
run("git push", ".")?;
162+
run!("git switch release")?;
163+
run!("git fetch upstream")?;
164+
run!("git reset --hard upstream/master")?;
165+
run!("git push")?;
168166

169167
let changelog_dir = project_root().join("../rust-analyzer.github.io/thisweek/_posts");
170168

171-
let today = run_with_output("date --iso", ".")?;
172-
let commit = run_with_output("git rev-parse HEAD", ".")?;
169+
let today = run!("date --iso")?;
170+
let commit = run!("git rev-parse HEAD")?;
173171
let changelog_n = fs::read_dir(changelog_dir.as_path())?.count();
174172

175173
let contents = format!(

0 commit comments

Comments
 (0)