2020-01-07 14:33:51 +00:00
|
|
|
|
//! Installs rust-analyzer language server and/or editor plugin.
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
2020-02-14 16:05:56 +00:00
|
|
|
|
use std::{env, path::PathBuf, str};
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
2020-02-14 14:06:10 +00:00
|
|
|
|
use anyhow::{bail, format_err, Context, Result};
|
2020-10-16 17:46:03 +00:00
|
|
|
|
use xshell::{cmd, pushd};
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
|
|
|
|
// Latest stable, feel free to send a PR if this lags behind.
|
2020-08-03 10:57:04 +00:00
|
|
|
|
const REQUIRED_RUST_VERSION: u32 = 47;
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
|
|
|
|
pub struct InstallCmd {
|
|
|
|
|
pub client: Option<ClientOpt>,
|
|
|
|
|
pub server: Option<ServerOpt>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 22:35:06 +00:00
|
|
|
|
#[derive(Clone, Copy)]
|
2020-01-07 13:42:56 +00:00
|
|
|
|
pub enum ClientOpt {
|
|
|
|
|
VsCode,
|
2020-12-08 18:23:25 +00:00
|
|
|
|
VsCodeExploration,
|
2020-10-16 22:35:06 +00:00
|
|
|
|
VsCodeInsiders,
|
|
|
|
|
VsCodium,
|
|
|
|
|
VsCodeOss,
|
|
|
|
|
Any,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClientOpt {
|
|
|
|
|
pub const fn as_cmds(&self) -> &'static [&'static str] {
|
|
|
|
|
match self {
|
|
|
|
|
ClientOpt::VsCode => &["code"],
|
2020-12-08 18:23:25 +00:00
|
|
|
|
ClientOpt::VsCodeExploration => &["code-exploration"],
|
2020-10-16 22:35:06 +00:00
|
|
|
|
ClientOpt::VsCodeInsiders => &["code-insiders"],
|
|
|
|
|
ClientOpt::VsCodium => &["codium"],
|
|
|
|
|
ClientOpt::VsCodeOss => &["code-oss"],
|
2020-12-08 18:23:25 +00:00
|
|
|
|
ClientOpt::Any => &["code", "code-exploration", "code-insiders", "codium", "code-oss"],
|
2020-10-16 22:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<Self, Self::Err> {
|
2020-12-08 18:23:25 +00:00
|
|
|
|
[
|
|
|
|
|
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"))
|
2020-10-16 22:35:06 +00:00
|
|
|
|
}
|
2020-01-07 13:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ServerOpt {
|
2020-07-14 00:12:49 +00:00
|
|
|
|
pub malloc: Malloc,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum Malloc {
|
|
|
|
|
System,
|
|
|
|
|
Mimalloc,
|
2020-01-07 13:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InstallCmd {
|
|
|
|
|
pub fn run(self) -> Result<()> {
|
|
|
|
|
if cfg!(target_os = "macos") {
|
|
|
|
|
fix_path_for_mac().context("Fix path for mac")?
|
|
|
|
|
}
|
|
|
|
|
if let Some(server) = self.server {
|
|
|
|
|
install_server(server).context("install server")?;
|
|
|
|
|
}
|
|
|
|
|
if let Some(client) = self.client {
|
|
|
|
|
install_client(client).context("install client")?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fix_path_for_mac() -> Result<()> {
|
|
|
|
|
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 = "";
|
|
|
|
|
let home_dir = match env::var("HOME") {
|
|
|
|
|
Ok(home) => home,
|
2020-02-14 14:06:10 +00:00
|
|
|
|
Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
|
2020-01-07 13:42:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[ROOT_DIR, &home_dir]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|dir| String::from(*dir) + COMMON_APP_PATH)
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.filter(|path| path.exists())
|
|
|
|
|
.collect()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !vscode_path.is_empty() {
|
|
|
|
|
let vars = match env::var_os("PATH") {
|
|
|
|
|
Some(path) => path,
|
2020-02-14 14:06:10 +00:00
|
|
|
|
None => bail!("Could not get PATH variable from env."),
|
2020-01-07 13:42:56 +00: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")?;
|
|
|
|
|
env::set_var("PATH", &new_paths);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 22:35:06 +00:00
|
|
|
|
fn install_client(client_opt: ClientOpt) -> Result<()> {
|
|
|
|
|
let _dir = pushd("./editors/code");
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
2020-02-14 14:59:19 +00:00
|
|
|
|
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
|
2020-10-16 22:35:06 +00:00
|
|
|
|
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?")
|
|
|
|
|
})
|
2020-02-14 14:59:19 +00:00
|
|
|
|
};
|
2020-02-14 14:29:19 +00:00
|
|
|
|
|
2020-02-18 13:32:19 +00:00
|
|
|
|
let installed_extensions = if cfg!(unix) {
|
2020-10-16 17:46:03 +00:00
|
|
|
|
cmd!("npm --version").run().context("`npm` is required to build the VS Code plugin")?;
|
|
|
|
|
cmd!("npm install").run()?;
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
2020-10-16 17:46:03 +00:00
|
|
|
|
cmd!("npm run package --scripts-prepend-node-path").run()?;
|
2020-02-14 14:59:19 +00:00
|
|
|
|
|
2020-10-16 17:46:03 +00:00
|
|
|
|
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()?
|
2020-02-14 14:59:19 +00:00
|
|
|
|
} else {
|
2020-10-16 17:46:03 +00:00
|
|
|
|
cmd!("cmd.exe /c npm --version")
|
|
|
|
|
.run()
|
2020-02-14 14:59:19 +00:00
|
|
|
|
.context("`npm` is required to build the VS Code plugin")?;
|
2020-10-16 17:46:03 +00:00
|
|
|
|
cmd!("cmd.exe /c npm install").run()?;
|
2020-02-14 14:59:19 +00:00
|
|
|
|
|
2020-10-16 17:46:03 +00:00
|
|
|
|
cmd!("cmd.exe /c npm run package").run()?;
|
2020-02-14 14:59:19 +00:00
|
|
|
|
|
2020-10-16 17:46:03 +00:00
|
|
|
|
let code = find_code(|bin| cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok())?;
|
|
|
|
|
cmd!("cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
|
|
|
|
|
cmd!("cmd.exe /c {code}.cmd --list-extensions").read()?
|
2020-02-18 13:32:19 +00:00
|
|
|
|
};
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
2020-01-08 16:21:18 +00:00
|
|
|
|
if !installed_extensions.contains("rust-analyzer") {
|
2020-02-14 14:06:10 +00:00
|
|
|
|
bail!(
|
2020-03-01 13:07:16 +00:00
|
|
|
|
"Could not install the Visual Studio Code extension. \
|
2020-11-13 15:17:52 +00: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 13:07:16 +00: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 13:42:56 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn install_server(opts: ServerOpt) -> Result<()> {
|
|
|
|
|
let mut old_rust = false;
|
2020-10-16 17:46:03 +00:00
|
|
|
|
if let Ok(stdout) = cmd!("cargo --version").read() {
|
2020-02-10 14:16:07 +00:00
|
|
|
|
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
|
|
|
|
|
old_rust = true;
|
2020-01-07 13:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if old_rust {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
|
|
|
|
|
REQUIRED_RUST_VERSION,
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-10-16 17:46:03 +00:00
|
|
|
|
let features = match opts.malloc {
|
|
|
|
|
Malloc::System => &[][..],
|
|
|
|
|
Malloc::Mimalloc => &["--features", "mimalloc"],
|
2020-07-14 00:12:49 +00:00
|
|
|
|
};
|
2020-10-16 17:46:03 +00:00
|
|
|
|
|
|
|
|
|
let cmd = cmd!("cargo install --path crates/rust-analyzer --locked --force {features...}");
|
|
|
|
|
let res = cmd.run();
|
2020-01-07 13:42:56 +00:00
|
|
|
|
|
|
|
|
|
if res.is_err() && old_rust {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
|
|
|
|
|
REQUIRED_RUST_VERSION,
|
2020-02-14 14:59:19 +00:00
|
|
|
|
);
|
2020-01-07 13:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:46:03 +00:00
|
|
|
|
res?;
|
|
|
|
|
Ok(())
|
2020-01-07 13:42:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_version(version_output: &str, min_minor_version: u32) -> bool {
|
|
|
|
|
// Parse second the number out of
|
|
|
|
|
// cargo 1.39.0-beta (1c6ec66d5 2019-09-30)
|
|
|
|
|
let minor: Option<u32> = version_output.split('.').nth(1).and_then(|it| it.parse().ok());
|
|
|
|
|
match minor {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(minor) => minor >= min_minor_version,
|
|
|
|
|
}
|
|
|
|
|
}
|