]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/xtask/src/install.rs
Add 'src/tools/rust-analyzer/' from commit '977e12a0bdc3e329af179ef3a9d466af9eb613bb'
[rust.git] / src / tools / rust-analyzer / xtask / src / install.rs
1 //! Installs rust-analyzer language server and/or editor plugin.
2
3 use std::{env, path::PathBuf, str};
4
5 use anyhow::{bail, format_err, Context, Result};
6 use xshell::{cmd, Shell};
7
8 use crate::flags;
9
10 impl flags::Install {
11     pub(crate) fn run(self, sh: &Shell) -> Result<()> {
12         if cfg!(target_os = "macos") {
13             fix_path_for_mac(sh).context("Fix path for mac")?;
14         }
15         if let Some(server) = self.server() {
16             install_server(sh, server).context("install server")?;
17         }
18         if let Some(client) = self.client() {
19             install_client(sh, client).context("install client")?;
20         }
21         Ok(())
22     }
23 }
24
25 #[derive(Clone)]
26 pub(crate) struct ClientOpt {
27     pub(crate) code_bin: Option<String>,
28 }
29
30 const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"];
31
32 pub(crate) struct ServerOpt {
33     pub(crate) malloc: Malloc,
34 }
35
36 pub(crate) enum Malloc {
37     System,
38     Mimalloc,
39     Jemalloc,
40 }
41
42 fn fix_path_for_mac(sh: &Shell) -> Result<()> {
43     let mut vscode_path: Vec<PathBuf> = {
44         const COMMON_APP_PATH: &str =
45             r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
46         const ROOT_DIR: &str = "";
47         let home_dir = sh.var("HOME").map_err(|err| {
48             format_err!("Failed getting HOME from environment with error: {}.", err)
49         })?;
50
51         [ROOT_DIR, &home_dir]
52             .into_iter()
53             .map(|dir| dir.to_string() + COMMON_APP_PATH)
54             .map(PathBuf::from)
55             .filter(|path| path.exists())
56             .collect()
57     };
58
59     if !vscode_path.is_empty() {
60         let vars = sh.var_os("PATH").context("Could not get PATH variable from env.")?;
61
62         let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
63         paths.append(&mut vscode_path);
64         let new_paths = env::join_paths(paths).context("build env PATH")?;
65         sh.set_var("PATH", &new_paths);
66     }
67
68     Ok(())
69 }
70
71 fn install_client(sh: &Shell, client_opt: ClientOpt) -> Result<()> {
72     let _dir = sh.push_dir("./editors/code");
73
74     // Package extension.
75     if cfg!(unix) {
76         cmd!(sh, "npm --version").run().context("`npm` is required to build the VS Code plugin")?;
77         cmd!(sh, "npm ci").run()?;
78
79         cmd!(sh, "npm run package --scripts-prepend-node-path").run()?;
80     } else {
81         cmd!(sh, "cmd.exe /c npm --version")
82             .run()
83             .context("`npm` is required to build the VS Code plugin")?;
84         cmd!(sh, "cmd.exe /c npm ci").run()?;
85
86         cmd!(sh, "cmd.exe /c npm run package").run()?;
87     };
88
89     // Find the appropriate VS Code binary.
90     let lifetime_extender;
91     let candidates: &[&str] = match client_opt.code_bin.as_deref() {
92         Some(it) => {
93             lifetime_extender = [it];
94             &lifetime_extender[..]
95         }
96         None => VS_CODES,
97     };
98     let code = candidates
99         .iter()
100         .copied()
101         .find(|&bin| {
102             if cfg!(unix) {
103                 cmd!(sh, "{bin} --version").read().is_ok()
104             } else {
105                 cmd!(sh, "cmd.exe /c {bin}.cmd --version").read().is_ok()
106             }
107         })
108         .ok_or_else(|| {
109             format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0])
110         })?;
111
112     // Install & verify.
113     let installed_extensions = if cfg!(unix) {
114         cmd!(sh, "{code} --install-extension rust-analyzer.vsix --force").run()?;
115         cmd!(sh, "{code} --list-extensions").read()?
116     } else {
117         cmd!(sh, "cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
118         cmd!(sh, "cmd.exe /c {code}.cmd --list-extensions").read()?
119     };
120
121     if !installed_extensions.contains("rust-analyzer") {
122         bail!(
123             "Could not install the Visual Studio Code extension. \
124             Please make sure you have at least NodeJS 12.x together with the latest version of VS Code installed and try again. \
125             Note that installing via xtask install does not work for VS Code Remote, instead you’ll need to install the .vsix manually."
126         );
127     }
128
129     Ok(())
130 }
131
132 fn install_server(sh: &Shell, opts: ServerOpt) -> Result<()> {
133     let features = match opts.malloc {
134         Malloc::System => &[][..],
135         Malloc::Mimalloc => &["--features", "mimalloc"],
136         Malloc::Jemalloc => &["--features", "jemalloc"],
137     };
138
139     let cmd = cmd!(sh, "cargo install --path crates/rust-analyzer --locked --force --features force-always-assert {features...}");
140     cmd.run()?;
141     Ok(())
142 }