Skip to content

Commit 94323c8

Browse files
bors[bot]matklad
andauthored
Merge #3136
3136: Not bash r=matklad a=matklad More declarative installation Co-authored-by: Aleksey Kladov <[email protected]>
2 parents d46b555 + f2e8dfc commit 94323c8

File tree

7 files changed

+202
-162
lines changed

7 files changed

+202
-162
lines changed

editors/code/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editors/code/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"preview": true,
66
"private": true,
77
"icon": "icon.png",
8-
"version": "0.1.0",
8+
"//": "The real version is in release.yaml, this one just needs to be bigger",
9+
"version": "0.2.0-dev",
910
"publisher": "matklad",
1011
"repository": {
1112
"url": "https://github.com/rust-analyzer/rust-analyzer.git",

xtask/src/cmd.rs

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

xtask/src/install.rs

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use std::{env, path::PathBuf, str};
44

5-
use anyhow::{Context, Result};
5+
use anyhow::{bail, format_err, Context, Result};
66

7-
use crate::cmd::{run, run_with_output, Cmd};
7+
use crate::not_bash::{ls, pushd, rm, run};
88

99
// Latest stable, feel free to send a PR if this lags behind.
1010
const REQUIRED_RUST_VERSION: u32 = 41;
@@ -55,7 +55,7 @@ fn fix_path_for_mac() -> Result<()> {
5555
const ROOT_DIR: &str = "";
5656
let home_dir = match env::var("HOME") {
5757
Ok(home) => home,
58-
Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
58+
Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
5959
};
6060

6161
[ROOT_DIR, &home_dir]
@@ -69,7 +69,7 @@ fn fix_path_for_mac() -> Result<()> {
6969
if !vscode_path.is_empty() {
7070
let vars = match env::var_os("PATH") {
7171
Some(path) => path,
72-
None => anyhow::bail!("Could not get PATH variable from env."),
72+
None => bail!("Could not get PATH variable from env."),
7373
};
7474

7575
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
@@ -82,84 +82,61 @@ fn fix_path_for_mac() -> Result<()> {
8282
}
8383

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

96-
Cmd { unix: r"npm install", windows: r"cmd.exe /c npm install", work_dir: "./editors/code" }
97-
.run()?;
98-
Cmd {
99-
unix: r"npm run package --scripts-prepend-node-path",
100-
windows: r"cmd.exe /c npm run package",
101-
work_dir: "./editors/code",
102-
}
103-
.run()?;
87+
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
88+
["code", "code-insiders", "codium", "code-oss"]
89+
.iter()
90+
.copied()
91+
.find(|bin| f(bin))
92+
.ok_or_else(|| {
93+
format_err!("Can't execute `code --version`. Perhaps it is not in $PATH?")
94+
})
95+
};
10496

105-
let code_binary = ["code", "code-insiders", "codium", "code-oss"].iter().find(|bin| {
106-
Cmd {
107-
unix: &format!("{} --version", bin),
108-
windows: &format!("cmd.exe /c {}.cmd --version", bin),
109-
work_dir: "./editors/code",
110-
}
111-
.run()
112-
.is_ok()
113-
});
97+
let installed_extensions;
98+
if cfg!(unix) {
99+
run!("npm --version").context("`npm` is required to build the VS Code plugin")?;
100+
run!("npm install")?;
114101

115-
let code_binary = match code_binary {
116-
Some(it) => it,
117-
None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
118-
};
102+
let vsix_pkg = {
103+
rm("*.vsix")?;
104+
run!("npm run package --scripts-prepend-node-path")?;
105+
ls("*.vsix")?.pop().unwrap()
106+
};
119107

120-
Cmd {
121-
unix: &format!(r"{} --install-extension ./rust-analyzer-0.1.0.vsix --force", code_binary),
122-
windows: &format!(
123-
r"cmd.exe /c {}.cmd --install-extension ./rust-analyzer-0.1.0.vsix --force",
124-
code_binary
125-
),
126-
work_dir: "./editors/code",
127-
}
128-
.run()?;
108+
let code = find_code(|bin| run!("{} --version", bin).is_ok())?;
109+
run!("{} --install-extension {} --force", code, vsix_pkg.display())?;
110+
installed_extensions = run!("{} --list-extensions", code; echo = false)?;
111+
} else {
112+
run!("cmd.exe /c npm --version")
113+
.context("`npm` is required to build the VS Code plugin")?;
114+
run!("cmd.exe /c npm install")?;
115+
116+
let vsix_pkg = {
117+
rm("*.vsix")?;
118+
run!("cmd.exe /c npm run package")?;
119+
ls("*.vsix")?.pop().unwrap()
120+
};
129121

130-
let installed_extensions = Cmd {
131-
unix: &format!(r"{} --list-extensions", code_binary),
132-
windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
133-
work_dir: ".",
122+
let code = find_code(|bin| run!("cmd.exe /c {}.cmd --version", bin).is_ok())?;
123+
run!(r"cmd.exe /c {}.cmd --install-extension {} --force", code, vsix_pkg.display())?;
124+
installed_extensions = run!("cmd.exe /c {}.cmd --list-extensions", code; echo = false)?;
134125
}
135-
.run_with_output()?;
136126

137127
if !installed_extensions.contains("rust-analyzer") {
138-
anyhow::bail!(
128+
bail!(
139129
"Could not install the Visual Studio Code extension. \
140130
Please make sure you have at least NodeJS 10.x together with the latest version of VS Code installed and try again."
141131
);
142132
}
143133

144-
if installed_extensions.contains("ra-lsp") {
145-
Cmd {
146-
unix: &format!(r"{} --uninstall-extension matklad.ra-lsp", code_binary),
147-
windows: &format!(
148-
r"cmd.exe /c {}.cmd --uninstall-extension matklad.ra-lsp",
149-
code_binary
150-
),
151-
work_dir: "./editors/code",
152-
}
153-
.run()?;
154-
}
155-
156134
Ok(())
157135
}
158136

159137
fn install_server(opts: ServerOpt) -> Result<()> {
160138
let mut old_rust = false;
161-
if let Ok(stdout) = run_with_output("cargo --version", ".") {
162-
println!("{}", stdout);
139+
if let Ok(stdout) = run!("cargo --version") {
163140
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
164141
old_rust = true;
165142
}
@@ -172,20 +149,17 @@ fn install_server(opts: ServerOpt) -> Result<()> {
172149
)
173150
}
174151

175-
let res = if opts.jemalloc {
176-
run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
177-
} else {
178-
run("cargo install --path crates/ra_lsp_server --locked --force", ".")
179-
};
152+
let jemalloc = if opts.jemalloc { "--features jemalloc" } else { "" };
153+
let res = run!("cargo install --path crates/ra_lsp_server --locked --force {}", jemalloc);
180154

181155
if res.is_err() && old_rust {
182156
eprintln!(
183157
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
184158
REQUIRED_RUST_VERSION,
185-
)
159+
);
186160
}
187161

188-
res
162+
res.map(drop)
189163
}
190164

191165
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)