rust/xtask/src/install.rs

174 lines
5.2 KiB
Rust
Raw Normal View History

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
2020-02-14 08:06:10 -06:00
use anyhow::{bail, format_err, Context, Result};
2020-01-07 07:42:56 -06:00
2020-02-14 10:05:56 -06:00
use crate::not_bash::{ls, pushd, rm, run};
2020-01-07 07:42:56 -06:00
// Latest stable, feel free to send a PR if this lags behind.
2020-01-30 11:03:24 -06:00
const REQUIRED_RUST_VERSION: u32 = 41;
2020-01-07 07:42:56 -06:00
pub struct InstallCmd {
pub client: Option<ClientOpt>,
pub server: Option<ServerOpt>,
}
pub enum ClientOpt {
VsCode,
}
pub struct ServerOpt {
pub jemalloc: bool,
}
impl InstallCmd {
pub fn run(self) -> Result<()> {
2020-02-10 03:53:31 -06:00
let both = self.server.is_some() && self.client.is_some();
2020-01-07 07:42:56 -06:00
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")?;
}
2020-02-10 03:53:31 -06:00
if both {
eprintln!(
"
Installation complete.
Add `\"rust-analyzer.raLspServerPath\": \"ra_lsp_server\",` to VS Code settings,
otherwise it will use the latest release from GitHub.
"
)
}
2020-01-07 07:42:56 -06:00
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 08:06:10 -06:00
Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
2020-01-07 07:42:56 -06: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 08:06:10 -06:00
None => bail!("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")?;
env::set_var("PATH", &new_paths);
}
Ok(())
}
fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
2020-02-14 08:59:19 -06:00
let _dir = pushd("./editors/code");
2020-01-07 07:42:56 -06:00
2020-02-14 08:59:19 -06:00
let find_code = |f: fn(&str) -> bool| -> Result<&'static str> {
["code", "code-insiders", "codium", "code-oss"]
.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 08:29:19 -06:00
2020-02-14 08:59:19 -06:00
let installed_extensions;
if cfg!(unix) {
run!("npm --version").context("`npm` is required to build the VS Code plugin")?;
run!("npm install")?;
2020-01-07 07:42:56 -06:00
2020-02-14 08:59:19 -06:00
let vsix_pkg = {
2020-02-14 10:05:56 -06:00
rm("*.vsix")?;
2020-02-14 08:59:19 -06:00
run!("npm run package --scripts-prepend-node-path")?;
2020-02-14 10:05:56 -06:00
ls("*.vsix")?.pop().unwrap()
2020-02-14 08:59:19 -06:00
};
let code = find_code(|bin| run!("{} --version", bin).is_ok())?;
2020-02-14 10:05:56 -06:00
run!("{} --install-extension {} --force", code, vsix_pkg.display())?;
2020-02-14 08:59:19 -06:00
installed_extensions = run!("{} --list-extensions", code; echo = false)?;
} else {
run!("cmd.exe /c npm --version")
.context("`npm` is required to build the VS Code plugin")?;
run!("cmd.exe /c npm install")?;
let vsix_pkg = {
2020-02-14 10:05:56 -06:00
rm("*.vsix")?;
2020-02-14 08:59:19 -06:00
run!("cmd.exe /c npm run package")?;
2020-02-14 10:05:56 -06:00
ls("*.vsix")?.pop().unwrap()
2020-02-14 08:59:19 -06:00
};
let code = find_code(|bin| run!("cmd.exe /c {}.cmd --version", bin).is_ok())?;
2020-02-14 10:05:56 -06:00
run!(r"cmd.exe /c {}.cmd --install-extension {} --force", code, vsix_pkg.display())?;
2020-02-14 08:59:19 -06:00
installed_extensions = run!("cmd.exe /c {}.cmd --list-extensions", code; echo = false)?;
2020-02-10 08:16:07 -06:00
}
2020-01-07 07:42:56 -06:00
if !installed_extensions.contains("rust-analyzer") {
2020-02-14 08:06:10 -06:00
bail!(
2020-01-07 07:42:56 -06:00
"Could not install the Visual Studio Code extension. \
Please make sure you have at least NodeJS 10.x together with the latest version of VS Code installed and try again."
);
}
Ok(())
}
fn install_server(opts: ServerOpt) -> Result<()> {
let mut old_rust = false;
2020-02-14 08:59:19 -06:00
if let Ok(stdout) = run!("cargo --version") {
2020-02-10 08:16:07 -06:00
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
old_rust = true;
2020-01-07 07:42:56 -06:00
}
}
if old_rust {
eprintln!(
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
REQUIRED_RUST_VERSION,
)
}
2020-02-14 08:59:19 -06:00
let jemalloc = if opts.jemalloc { "--features jemalloc" } else { "" };
let res = run!("cargo install --path crates/ra_lsp_server --locked --force {}", jemalloc);
2020-01-07 07:42:56 -06: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 08:59:19 -06:00
);
2020-01-07 07:42:56 -06:00
}
2020-02-14 08:59:19 -06:00
res.map(drop)
2020-01-07 07:42:56 -06: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,
}
}