2020-01-07 08:33:51 -06:00
|
|
|
|
//! Installs rust-analyzer language server and/or editor plugin.
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2020-02-14 10:05:56 -06:00
|
|
|
|
use std::{env, path::PathBuf, str};
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2023-06-19 07:01:47 -05:00
|
|
|
|
use anyhow::{bail, format_err, Context};
|
2022-03-13 16:20:51 -05:00
|
|
|
|
use xshell::{cmd, Shell};
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2021-03-05 02:51:32 -06:00
|
|
|
|
use crate::flags;
|
|
|
|
|
|
|
|
|
|
impl flags::Install {
|
2023-06-19 07:01:47 -05:00
|
|
|
|
pub(crate) fn run(self, sh: &Shell) -> anyhow::Result<()> {
|
2021-03-05 02:51:32 -06:00
|
|
|
|
if cfg!(target_os = "macos") {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
fix_path_for_mac(sh).context("Fix path for mac")?;
|
2021-03-05 02:51:32 -06:00
|
|
|
|
}
|
|
|
|
|
if let Some(server) = self.server() {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
install_server(sh, server).context("install server")?;
|
2021-03-05 02:51:32 -06:00
|
|
|
|
}
|
|
|
|
|
if let Some(client) = self.client() {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
install_client(sh, client).context("install client")?;
|
2021-03-05 02:51:32 -06:00
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-01-07 07:42:56 -06:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 11:42:41 -06:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) struct ClientOpt {
|
|
|
|
|
pub(crate) code_bin: Option<String>,
|
2020-10-16 17:35:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 11:42:41 -06:00
|
|
|
|
const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"];
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2021-03-01 11:16:23 -06:00
|
|
|
|
pub(crate) struct ServerOpt {
|
|
|
|
|
pub(crate) malloc: Malloc,
|
2020-07-13 19:12:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 11:16:23 -06:00
|
|
|
|
pub(crate) enum Malloc {
|
2020-07-13 19:12:49 -05:00
|
|
|
|
System,
|
|
|
|
|
Mimalloc,
|
2021-01-18 12:25:55 -06:00
|
|
|
|
Jemalloc,
|
2020-01-07 07:42:56 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 07:01:47 -05:00
|
|
|
|
fn fix_path_for_mac(sh: &Shell) -> anyhow::Result<()> {
|
2020-01-07 07:42:56 -06:00
|
|
|
|
let mut vscode_path: Vec<PathBuf> = {
|
|
|
|
|
const COMMON_APP_PATH: &str =
|
|
|
|
|
r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
|
|
|
|
|
const ROOT_DIR: &str = "";
|
2022-03-13 16:20:51 -05:00
|
|
|
|
let home_dir = sh.var("HOME").map_err(|err| {
|
|
|
|
|
format_err!("Failed getting HOME from environment with error: {}.", err)
|
|
|
|
|
})?;
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
|
|
|
|
[ROOT_DIR, &home_dir]
|
2021-11-03 00:13:47 -05:00
|
|
|
|
.into_iter()
|
2024-02-09 09:56:07 -06:00
|
|
|
|
.map(|dir| dir.to_owned() + COMMON_APP_PATH)
|
2020-01-07 07:42:56 -06:00
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.filter(|path| path.exists())
|
|
|
|
|
.collect()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !vscode_path.is_empty() {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
let vars = sh.var_os("PATH").context("Could not get PATH variable from env.")?;
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
|
|
|
|
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
|
|
|
|
|
paths.append(&mut vscode_path);
|
|
|
|
|
let new_paths = env::join_paths(paths).context("build env PATH")?;
|
2022-12-24 15:09:08 -06:00
|
|
|
|
sh.set_var("PATH", new_paths);
|
2020-01-07 07:42:56 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 07:01:47 -05:00
|
|
|
|
fn install_client(sh: &Shell, client_opt: ClientOpt) -> anyhow::Result<()> {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
let _dir = sh.push_dir("./editors/code");
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2021-03-05 11:42:41 -06:00
|
|
|
|
// Package extension.
|
|
|
|
|
if cfg!(unix) {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "npm --version").run().context("`npm` is required to build the VS Code plugin")?;
|
|
|
|
|
cmd!(sh, "npm ci").run()?;
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "npm run package --scripts-prepend-node-path").run()?;
|
2020-02-14 08:59:19 -06:00
|
|
|
|
} else {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "cmd.exe /c npm --version")
|
2020-10-16 12:46:03 -05:00
|
|
|
|
.run()
|
2020-02-14 08:59:19 -06:00
|
|
|
|
.context("`npm` is required to build the VS Code plugin")?;
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "cmd.exe /c npm ci").run()?;
|
2020-02-14 08:59:19 -06:00
|
|
|
|
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "cmd.exe /c npm run package").run()?;
|
2021-03-05 11:42:41 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "{bin} --version").read().is_ok()
|
2021-03-05 11:42:41 -06:00
|
|
|
|
} else {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "cmd.exe /c {bin}.cmd --version").read().is_ok()
|
2021-03-05 11:42:41 -06:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0])
|
|
|
|
|
})?;
|
2020-02-14 08:59:19 -06:00
|
|
|
|
|
2021-03-05 11:42:41 -06:00
|
|
|
|
// Install & verify.
|
|
|
|
|
let installed_extensions = if cfg!(unix) {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "{code} --install-extension rust-analyzer.vsix --force").run()?;
|
|
|
|
|
cmd!(sh, "{code} --list-extensions").read()?
|
2021-03-05 11:42:41 -06:00
|
|
|
|
} else {
|
2022-03-13 16:20:51 -05:00
|
|
|
|
cmd!(sh, "cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
|
|
|
|
|
cmd!(sh, "cmd.exe /c {code}.cmd --list-extensions").read()?
|
2020-02-18 07:32:19 -06:00
|
|
|
|
};
|
2020-01-07 07:42:56 -06:00
|
|
|
|
|
2020-01-08 10:21:18 -06:00
|
|
|
|
if !installed_extensions.contains("rust-analyzer") {
|
2020-02-14 08:06:10 -06:00
|
|
|
|
bail!(
|
2020-03-01 07:07:16 -06:00
|
|
|
|
"Could not install the Visual Studio Code extension. \
|
2020-11-13 09:17:52 -06:00
|
|
|
|
Please make sure you have at least NodeJS 12.x together with the latest version of VS Code installed and try again. \
|
2020-03-01 07:07:16 -06:00
|
|
|
|
Note that installing via xtask install does not work for VS Code Remote, instead you’ll need to install the .vsix manually."
|
2020-01-07 07:42:56 -06:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 07:01:47 -05:00
|
|
|
|
fn install_server(sh: &Shell, opts: ServerOpt) -> anyhow::Result<()> {
|
2020-10-16 12:46:03 -05:00
|
|
|
|
let features = match opts.malloc {
|
|
|
|
|
Malloc::System => &[][..],
|
|
|
|
|
Malloc::Mimalloc => &["--features", "mimalloc"],
|
2021-01-18 12:25:55 -06:00
|
|
|
|
Malloc::Jemalloc => &["--features", "jemalloc"],
|
2020-07-13 19:12:49 -05:00
|
|
|
|
};
|
2020-10-16 12:46:03 -05:00
|
|
|
|
|
2022-03-13 16:20:51 -05:00
|
|
|
|
let cmd = cmd!(sh, "cargo install --path crates/rust-analyzer --locked --force --features force-always-assert {features...}");
|
2021-10-23 07:07:11 -05:00
|
|
|
|
cmd.run()?;
|
2020-10-16 12:46:03 -05:00
|
|
|
|
Ok(())
|
2020-01-07 07:42:56 -06:00
|
|
|
|
}
|