From bf4aaa4fb0d40987587ce202c3cc743e2a5959ee Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Mar 2021 20:42:41 +0300 Subject: Underengineer `cargo xtask install --client` --- xtask/src/flags.rs | 11 +------ xtask/src/install.rs | 91 ++++++++++++++++++++-------------------------------- xtask/src/main.rs | 5 +-- 3 files changed, 37 insertions(+), 70 deletions(-) diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index 2ca05d3df..56eda5b1e 100644 --- a/xtask/src/flags.rs +++ b/xtask/src/flags.rs @@ -141,14 +141,6 @@ impl Xtask { // generated end impl Install { - pub(crate) fn validate(&self) -> xflags::Result<()> { - if let Some(code_bin) = &self.code_bin { - if let Err(err) = code_bin.parse::() { - return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err))); - } - } - Ok(()) - } pub(crate) fn server(&self) -> Option { if self.client && !self.server { return None; @@ -166,7 +158,6 @@ impl Install { if !self.client && self.server { return None; } - let client_opt = self.code_bin.as_ref().and_then(|it| it.parse().ok()).unwrap_or_default(); - Some(client_opt) + Some(ClientOpt { code_bin: self.code_bin.clone() }) } } diff --git a/xtask/src/install.rs b/xtask/src/install.rs index 3e8fbe0a6..177028b08 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -25,52 +25,12 @@ impl flags::Install { } } -#[derive(Clone, Copy)] -pub(crate) enum ClientOpt { - VsCode, - VsCodeExploration, - VsCodeInsiders, - VsCodium, - VsCodeOss, - Any, +#[derive(Clone)] +pub(crate) struct ClientOpt { + pub(crate) code_bin: Option, } -impl ClientOpt { - pub(crate) const fn as_cmds(&self) -> &'static [&'static str] { - match self { - ClientOpt::VsCode => &["code"], - ClientOpt::VsCodeExploration => &["code-exploration"], - ClientOpt::VsCodeInsiders => &["code-insiders"], - ClientOpt::VsCodium => &["codium"], - ClientOpt::VsCodeOss => &["code-oss"], - ClientOpt::Any => &["code", "code-exploration", "code-insiders", "codium", "code-oss"], - } - } -} - -impl Default for ClientOpt { - fn default() -> Self { - ClientOpt::Any - } -} - -impl std::str::FromStr for ClientOpt { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - [ - ClientOpt::VsCode, - ClientOpt::VsCodeExploration, - ClientOpt::VsCodeInsiders, - ClientOpt::VsCodium, - ClientOpt::VsCodeOss, - ] - .iter() - .copied() - .find(|c| [s] == c.as_cmds()) - .ok_or_else(|| anyhow::format_err!("no such client")) - } -} +const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"]; pub(crate) struct ServerOpt { pub(crate) malloc: Malloc, @@ -118,21 +78,12 @@ fn fix_path_for_mac() -> Result<()> { fn install_client(client_opt: ClientOpt) -> Result<()> { let _dir = pushd("./editors/code"); - let find_code = |f: fn(&str) -> bool| -> Result<&'static str> { - client_opt.as_cmds().iter().copied().find(|bin| f(bin)).ok_or_else(|| { - format_err!("Can't execute `code --version`. Perhaps it is not in $PATH?") - }) - }; - - let installed_extensions = if cfg!(unix) { + // Package extension. + if cfg!(unix) { cmd!("npm --version").run().context("`npm` is required to build the VS Code plugin")?; cmd!("npm ci").run()?; cmd!("npm run package --scripts-prepend-node-path").run()?; - - let code = find_code(|bin| cmd!("{bin} --version").read().is_ok())?; - cmd!("{code} --install-extension rust-analyzer.vsix --force").run()?; - cmd!("{code} --list-extensions").read()? } else { cmd!("cmd.exe /c npm --version") .run() @@ -140,8 +91,36 @@ fn install_client(client_opt: ClientOpt) -> Result<()> { cmd!("cmd.exe /c npm ci").run()?; cmd!("cmd.exe /c npm run package").run()?; + }; + + // Find the appropriate VS Code binary. + let lifetime_extender; + let candidates: &[&str] = match client_opt.code_bin.as_deref() { + Some(it) => { + lifetime_extender = [it]; + &lifetime_extender[..] + } + None => VS_CODES, + }; + let code = candidates + .iter() + .copied() + .find(|&bin| { + if cfg!(unix) { + cmd!("{bin} --version").read().is_ok() + } else { + cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok() + } + }) + .ok_or_else(|| { + format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0]) + })?; - let code = find_code(|bin| cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok())?; + // Install & verify. + let installed_extensions = if cfg!(unix) { + cmd!("{code} --install-extension rust-analyzer.vsix --force").run()?; + cmd!("{code} --list-extensions").read()? + } else { cmd!("cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?; cmd!("cmd.exe /c {code}.cmd --list-extensions").read()? }; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index ca27b6cec..3c4332f75 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -39,10 +39,7 @@ fn main() -> Result<()> { println!("{}", flags::Xtask::HELP); return Ok(()); } - flags::XtaskCmd::Install(cmd) => { - cmd.validate()?; - cmd.run() - } + flags::XtaskCmd::Install(cmd) => cmd.run(), flags::XtaskCmd::Codegen(cmd) => cmd.run(), flags::XtaskCmd::Lint(_) => run_clippy(), flags::XtaskCmd::FuzzTests(_) => run_fuzzer(), -- cgit v1.2.3