Skip to content

Commit bf4aaa4

Browse files
committed
Underengineer cargo xtask install --client
1 parent 142f9a0 commit bf4aaa4

File tree

3 files changed

+37
-70
lines changed

3 files changed

+37
-70
lines changed

xtask/src/flags.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ impl Xtask {
141141
// generated end
142142

143143
impl Install {
144-
pub(crate) fn validate(&self) -> xflags::Result<()> {
145-
if let Some(code_bin) = &self.code_bin {
146-
if let Err(err) = code_bin.parse::<ClientOpt>() {
147-
return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err)));
148-
}
149-
}
150-
Ok(())
151-
}
152144
pub(crate) fn server(&self) -> Option<ServerOpt> {
153145
if self.client && !self.server {
154146
return None;
@@ -166,7 +158,6 @@ impl Install {
166158
if !self.client && self.server {
167159
return None;
168160
}
169-
let client_opt = self.code_bin.as_ref().and_then(|it| it.parse().ok()).unwrap_or_default();
170-
Some(client_opt)
161+
Some(ClientOpt { code_bin: self.code_bin.clone() })
171162
}
172163
}

xtask/src/install.rs

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,12 @@ impl flags::Install {
2525
}
2626
}
2727

28-
#[derive(Clone, Copy)]
29-
pub(crate) enum ClientOpt {
30-
VsCode,
31-
VsCodeExploration,
32-
VsCodeInsiders,
33-
VsCodium,
34-
VsCodeOss,
35-
Any,
28+
#[derive(Clone)]
29+
pub(crate) struct ClientOpt {
30+
pub(crate) code_bin: Option<String>,
3631
}
3732

38-
impl ClientOpt {
39-
pub(crate) const fn as_cmds(&self) -> &'static [&'static str] {
40-
match self {
41-
ClientOpt::VsCode => &["code"],
42-
ClientOpt::VsCodeExploration => &["code-exploration"],
43-
ClientOpt::VsCodeInsiders => &["code-insiders"],
44-
ClientOpt::VsCodium => &["codium"],
45-
ClientOpt::VsCodeOss => &["code-oss"],
46-
ClientOpt::Any => &["code", "code-exploration", "code-insiders", "codium", "code-oss"],
47-
}
48-
}
49-
}
50-
51-
impl Default for ClientOpt {
52-
fn default() -> Self {
53-
ClientOpt::Any
54-
}
55-
}
56-
57-
impl std::str::FromStr for ClientOpt {
58-
type Err = anyhow::Error;
59-
60-
fn from_str(s: &str) -> Result<Self, Self::Err> {
61-
[
62-
ClientOpt::VsCode,
63-
ClientOpt::VsCodeExploration,
64-
ClientOpt::VsCodeInsiders,
65-
ClientOpt::VsCodium,
66-
ClientOpt::VsCodeOss,
67-
]
68-
.iter()
69-
.copied()
70-
.find(|c| [s] == c.as_cmds())
71-
.ok_or_else(|| anyhow::format_err!("no such client"))
72-
}
73-
}
33+
const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"];
7434

7535
pub(crate) struct ServerOpt {
7636
pub(crate) malloc: Malloc,
@@ -118,30 +78,49 @@ fn fix_path_for_mac() -> Result<()> {
11878
fn install_client(client_opt: ClientOpt) -> Result<()> {
11979
let _dir = pushd("./editors/code");
12080

121-
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
122-
client_opt.as_cmds().iter().copied().find(|bin| f(bin)).ok_or_else(|| {
123-
format_err!("Can't execute `code --version`. Perhaps it is not in $PATH?")
124-
})
125-
};
126-
127-
let installed_extensions = if cfg!(unix) {
81+
// Package extension.
82+
if cfg!(unix) {
12883
cmd!("npm --version").run().context("`npm` is required to build the VS Code plugin")?;
12984
cmd!("npm ci").run()?;
13085

13186
cmd!("npm run package --scripts-prepend-node-path").run()?;
132-
133-
let code = find_code(|bin| cmd!("{bin} --version").read().is_ok())?;
134-
cmd!("{code} --install-extension rust-analyzer.vsix --force").run()?;
135-
cmd!("{code} --list-extensions").read()?
13687
} else {
13788
cmd!("cmd.exe /c npm --version")
13889
.run()
13990
.context("`npm` is required to build the VS Code plugin")?;
14091
cmd!("cmd.exe /c npm ci").run()?;
14192

14293
cmd!("cmd.exe /c npm run package").run()?;
94+
};
95+
96+
// Find the appropriate VS Code binary.
97+
let lifetime_extender;
98+
let candidates: &[&str] = match client_opt.code_bin.as_deref() {
99+
Some(it) => {
100+
lifetime_extender = [it];
101+
&lifetime_extender[..]
102+
}
103+
None => VS_CODES,
104+
};
105+
let code = candidates
106+
.iter()
107+
.copied()
108+
.find(|&bin| {
109+
if cfg!(unix) {
110+
cmd!("{bin} --version").read().is_ok()
111+
} else {
112+
cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok()
113+
}
114+
})
115+
.ok_or_else(|| {
116+
format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0])
117+
})?;
143118

144-
let code = find_code(|bin| cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok())?;
119+
// Install & verify.
120+
let installed_extensions = if cfg!(unix) {
121+
cmd!("{code} --install-extension rust-analyzer.vsix --force").run()?;
122+
cmd!("{code} --list-extensions").read()?
123+
} else {
145124
cmd!("cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
146125
cmd!("cmd.exe /c {code}.cmd --list-extensions").read()?
147126
};

xtask/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ fn main() -> Result<()> {
3939
println!("{}", flags::Xtask::HELP);
4040
return Ok(());
4141
}
42-
flags::XtaskCmd::Install(cmd) => {
43-
cmd.validate()?;
44-
cmd.run()
45-
}
42+
flags::XtaskCmd::Install(cmd) => cmd.run(),
4643
flags::XtaskCmd::Codegen(cmd) => cmd.run(),
4744
flags::XtaskCmd::Lint(_) => run_clippy(),
4845
flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),

0 commit comments

Comments
 (0)