aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-01-07 14:37:27 +0000
committerGitHub <[email protected]>2020-01-07 14:37:27 +0000
commit9df1663c12f48df7f4921e68c928016877390565 (patch)
tree4b03a633fcb27c7fdeb5322b1bb3ca3889778b7f
parent6c1401404baca846103c85e048b5d0a959ddc81b (diff)
parent5e7995eeb7b7ab4cf0d80ddfa2d20e506216f895 (diff)
Merge #2755
2755: Cleanup r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--xtask/src/cmd.rs53
-rw-r--r--xtask/src/help.rs46
-rw-r--r--xtask/src/install.rs178
-rw-r--r--xtask/src/lib.rs103
-rw-r--r--xtask/src/main.rs299
-rw-r--r--xtask/src/pre_commit.rs36
6 files changed, 344 insertions, 371 deletions
diff --git a/xtask/src/cmd.rs b/xtask/src/cmd.rs
new file mode 100644
index 000000000..2027f4893
--- /dev/null
+++ b/xtask/src/cmd.rs
@@ -0,0 +1,53 @@
1use std::process::{Command, Output, Stdio};
2
3use anyhow::{Context, Result};
4
5use crate::project_root;
6
7pub struct Cmd<'a> {
8 pub unix: &'a str,
9 pub windows: &'a str,
10 pub work_dir: &'a str,
11}
12
13impl Cmd<'_> {
14 pub fn run(self) -> Result<()> {
15 if cfg!(windows) {
16 run(self.windows, self.work_dir)
17 } else {
18 run(self.unix, self.work_dir)
19 }
20 }
21 pub fn run_with_output(self) -> Result<Output> {
22 if cfg!(windows) {
23 run_with_output(self.windows, self.work_dir)
24 } else {
25 run_with_output(self.unix, self.work_dir)
26 }
27 }
28}
29
30pub fn run(cmdline: &str, dir: &str) -> Result<()> {
31 do_run(cmdline, dir, &mut |c| {
32 c.stdout(Stdio::inherit());
33 })
34 .map(|_| ())
35}
36
37pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
38 do_run(cmdline, dir, &mut |_| {})
39}
40
41fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result<Output> {
42 eprintln!("\nwill run: {}", cmdline);
43 let proj_dir = project_root().join(dir);
44 let mut args = cmdline.split_whitespace();
45 let exec = args.next().unwrap();
46 let mut cmd = Command::new(exec);
47 f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
48 let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
49 if !output.status.success() {
50 anyhow::bail!("`{}` exited with {}", cmdline, output.status);
51 }
52 Ok(output)
53}
diff --git a/xtask/src/help.rs b/xtask/src/help.rs
deleted file mode 100644
index f4e25dcde..000000000
--- a/xtask/src/help.rs
+++ /dev/null
@@ -1,46 +0,0 @@
1//! FIXME: write short doc here
2
3pub const GLOBAL_HELP: &str = "tasks
4
5USAGE:
6 ra_tools <SUBCOMMAND>
7
8FLAGS:
9 -h, --help Prints help information
10
11SUBCOMMANDS:
12 format
13 install-pre-commit-hook
14 fuzz-tests
15 codegen
16 install
17 lint";
18
19pub const INSTALL_HELP: &str = "ra_tools-install
20
21USAGE:
22 ra_tools.exe install [FLAGS]
23
24FLAGS:
25 --client-code
26 -h, --help Prints help information
27 --jemalloc
28 --server";
29
30pub fn print_no_param_subcommand_help(subcommand: &str) {
31 eprintln!(
32 "ra_tools-{}
33
34USAGE:
35 ra_tools {}
36
37FLAGS:
38 -h, --help Prints help information",
39 subcommand, subcommand
40 );
41}
42
43pub const INSTALL_RA_CONFLICT: &str =
44 "error: The argument `--server` cannot be used with `--client-code`
45
46For more information try --help";
diff --git a/xtask/src/install.rs b/xtask/src/install.rs
new file mode 100644
index 000000000..c5bb7351a
--- /dev/null
+++ b/xtask/src/install.rs
@@ -0,0 +1,178 @@
1//! Installs rust-analyzer language server and/or editor plugin.
2
3use std::{env, path::PathBuf, str};
4
5use anyhow::{Context, Result};
6
7use crate::cmd::{run, run_with_output, Cmd};
8
9// Latest stable, feel free to send a PR if this lags behind.
10const REQUIRED_RUST_VERSION: u32 = 40;
11
12pub struct InstallCmd {
13 pub client: Option<ClientOpt>,
14 pub server: Option<ServerOpt>,
15}
16
17pub enum ClientOpt {
18 VsCode,
19}
20
21pub struct ServerOpt {
22 pub jemalloc: bool,
23}
24
25impl InstallCmd {
26 pub fn run(self) -> Result<()> {
27 if cfg!(target_os = "macos") {
28 fix_path_for_mac().context("Fix path for mac")?
29 }
30 if let Some(server) = self.server {
31 install_server(server).context("install server")?;
32 }
33 if let Some(client) = self.client {
34 install_client(client).context("install client")?;
35 }
36 Ok(())
37 }
38}
39
40fn fix_path_for_mac() -> Result<()> {
41 let mut vscode_path: Vec<PathBuf> = {
42 const COMMON_APP_PATH: &str =
43 r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
44 const ROOT_DIR: &str = "";
45 let home_dir = match env::var("HOME") {
46 Ok(home) => home,
47 Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
48 };
49
50 [ROOT_DIR, &home_dir]
51 .iter()
52 .map(|dir| String::from(*dir) + COMMON_APP_PATH)
53 .map(PathBuf::from)
54 .filter(|path| path.exists())
55 .collect()
56 };
57
58 if !vscode_path.is_empty() {
59 let vars = match env::var_os("PATH") {
60 Some(path) => path,
61 None => anyhow::bail!("Could not get PATH variable from env."),
62 };
63
64 let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
65 paths.append(&mut vscode_path);
66 let new_paths = env::join_paths(paths).context("build env PATH")?;
67 env::set_var("PATH", &new_paths);
68 }
69
70 Ok(())
71}
72
73fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
74 let npm_version = Cmd {
75 unix: r"npm --version",
76 windows: r"cmd.exe /c npm --version",
77 work_dir: "./editors/code",
78 }
79 .run();
80
81 if npm_version.is_err() {
82 eprintln!("\nERROR: `npm --version` failed, `npm` is required to build the VS Code plugin")
83 }
84
85 Cmd { unix: r"npm install", windows: r"cmd.exe /c npm install", work_dir: "./editors/code" }
86 .run()?;
87 Cmd {
88 unix: r"npm run package --scripts-prepend-node-path",
89 windows: r"cmd.exe /c npm run package",
90 work_dir: "./editors/code",
91 }
92 .run()?;
93
94 let code_binary = ["code", "code-insiders", "codium"].iter().find(|bin| {
95 Cmd {
96 unix: &format!("{} --version", bin),
97 windows: &format!("cmd.exe /c {}.cmd --version", bin),
98 work_dir: "./editors/code",
99 }
100 .run()
101 .is_ok()
102 });
103
104 let code_binary = match code_binary {
105 Some(it) => it,
106 None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
107 };
108
109 Cmd {
110 unix: &format!(r"{} --install-extension ./ra-lsp-0.0.1.vsix --force", code_binary),
111 windows: &format!(
112 r"cmd.exe /c {}.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
113 code_binary
114 ),
115 work_dir: "./editors/code",
116 }
117 .run()?;
118
119 let output = Cmd {
120 unix: &format!(r"{} --list-extensions", code_binary),
121 windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
122 work_dir: ".",
123 }
124 .run_with_output()?;
125
126 if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
127 anyhow::bail!(
128 "Could not install the Visual Studio Code extension. \
129 Please make sure you have at least NodeJS 10.x together with the latest version of VS Code installed and try again."
130 );
131 }
132
133 Ok(())
134}
135
136fn install_server(opts: ServerOpt) -> Result<()> {
137 let mut old_rust = false;
138 if let Ok(output) = run_with_output("cargo --version", ".") {
139 if let Ok(stdout) = String::from_utf8(output.stdout) {
140 println!("{}", stdout);
141 if !check_version(&stdout, REQUIRED_RUST_VERSION) {
142 old_rust = true;
143 }
144 }
145 }
146
147 if old_rust {
148 eprintln!(
149 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
150 REQUIRED_RUST_VERSION,
151 )
152 }
153
154 let res = if opts.jemalloc {
155 run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
156 } else {
157 run("cargo install --path crates/ra_lsp_server --locked --force", ".")
158 };
159
160 if res.is_err() && old_rust {
161 eprintln!(
162 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
163 REQUIRED_RUST_VERSION,
164 )
165 }
166
167 res
168}
169
170fn check_version(version_output: &str, min_minor_version: u32) -> bool {
171 // Parse second the number out of
172 // cargo 1.39.0-beta (1c6ec66d5 2019-09-30)
173 let minor: Option<u32> = version_output.split('.').nth(1).and_then(|it| it.parse().ok());
174 match minor {
175 None => true,
176 Some(minor) => minor >= min_minor_version,
177 }
178}
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index 51a868dee..b76278635 100644
--- a/xtask/src/lib.rs
+++ b/xtask/src/lib.rs
@@ -1,18 +1,22 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3mod cmd;
4pub mod install;
5pub mod pre_commit;
6
3pub mod codegen; 7pub mod codegen;
4mod ast_src; 8mod ast_src;
5 9
6use anyhow::Context; 10use anyhow::Context;
7pub use anyhow::Result;
8use std::{ 11use std::{
9 env, fs, 12 env,
10 io::{Error as IoError, ErrorKind},
11 path::{Path, PathBuf}, 13 path::{Path, PathBuf},
12 process::{Command, Output, Stdio}, 14 process::{Command, Stdio},
13}; 15};
14 16
15use crate::codegen::Mode; 17use crate::{cmd::run, codegen::Mode};
18
19pub use anyhow::Result;
16 20
17const TOOLCHAIN: &str = "stable"; 21const TOOLCHAIN: &str = "stable";
18 22
@@ -26,40 +30,6 @@ pub fn project_root() -> PathBuf {
26 .to_path_buf() 30 .to_path_buf()
27} 31}
28 32
29pub struct Cmd<'a> {
30 pub unix: &'a str,
31 pub windows: &'a str,
32 pub work_dir: &'a str,
33}
34
35impl Cmd<'_> {
36 pub fn run(self) -> Result<()> {
37 if cfg!(windows) {
38 run(self.windows, self.work_dir)
39 } else {
40 run(self.unix, self.work_dir)
41 }
42 }
43 pub fn run_with_output(self) -> Result<Output> {
44 if cfg!(windows) {
45 run_with_output(self.windows, self.work_dir)
46 } else {
47 run_with_output(self.unix, self.work_dir)
48 }
49 }
50}
51
52pub fn run(cmdline: &str, dir: &str) -> Result<()> {
53 do_run(cmdline, dir, |c| {
54 c.stdout(Stdio::inherit());
55 })
56 .map(|_| ())
57}
58
59pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
60 do_run(cmdline, dir, |_| {})
61}
62
63pub fn run_rustfmt(mode: Mode) -> Result<()> { 33pub fn run_rustfmt(mode: Mode) -> Result<()> {
64 match Command::new("rustup") 34 match Command::new("rustup")
65 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) 35 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
@@ -79,23 +49,11 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
79 Ok(()) 49 Ok(())
80} 50}
81 51
82pub fn install_rustfmt() -> Result<()> { 52fn install_rustfmt() -> Result<()> {
83 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; 53 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
84 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") 54 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
85} 55}
86 56
87pub fn install_pre_commit_hook() -> Result<()> {
88 let result_path =
89 PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX));
90 if !result_path.exists() {
91 let me = std::env::current_exe()?;
92 fs::copy(me, result_path)?;
93 } else {
94 Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?;
95 }
96 Ok(())
97}
98
99pub fn run_clippy() -> Result<()> { 57pub fn run_clippy() -> Result<()> {
100 match Command::new("rustup") 58 match Command::new("rustup")
101 .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) 59 .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"])
@@ -125,7 +83,7 @@ pub fn run_clippy() -> Result<()> {
125 Ok(()) 83 Ok(())
126} 84}
127 85
128pub fn install_clippy() -> Result<()> { 86fn install_clippy() -> Result<()> {
129 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; 87 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
130 run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") 88 run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".")
131} 89}
@@ -143,42 +101,3 @@ pub fn run_fuzzer() -> Result<()> {
143 101
144 run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") 102 run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
145} 103}
146
147pub fn reformat_staged_files() -> Result<()> {
148 run_rustfmt(Mode::Overwrite)?;
149 let root = project_root();
150 let output = Command::new("git")
151 .arg("diff")
152 .arg("--diff-filter=MAR")
153 .arg("--name-only")
154 .arg("--cached")
155 .current_dir(&root)
156 .output()?;
157 if !output.status.success() {
158 anyhow::bail!(
159 "`git diff --diff-filter=MAR --name-only --cached` exited with {}",
160 output.status
161 );
162 }
163 for line in String::from_utf8(output.stdout)?.lines() {
164 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
165 }
166 Ok(())
167}
168
169fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
170where
171 F: FnMut(&mut Command),
172{
173 eprintln!("\nwill run: {}", cmdline);
174 let proj_dir = project_root().join(dir);
175 let mut args = cmdline.split_whitespace();
176 let exec = args.next().unwrap();
177 let mut cmd = Command::new(exec);
178 f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
179 let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
180 if !output.status.success() {
181 anyhow::bail!("`{}` exited with {}", cmdline, output.status);
182 }
183 Ok(output)
184}
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 9cefad925..9309b2fbd 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -7,272 +7,105 @@
7//! 7//!
8//! This binary is integrated into the `cargo` command line by using an alias in 8//! This binary is integrated into the `cargo` command line by using an alias in
9//! `.cargo/config`. 9//! `.cargo/config`.
10mod help;
11 10
12use std::{env, fmt::Write, path::PathBuf, str}; 11use std::env;
13 12
14use anyhow::Context;
15use pico_args::Arguments; 13use pico_args::Arguments;
16use xtask::{ 14use xtask::{
17 codegen::{self, Mode}, 15 codegen::{self, Mode},
18 install_pre_commit_hook, reformat_staged_files, run, run_clippy, run_fuzzer, run_rustfmt, 16 install::{ClientOpt, InstallCmd, ServerOpt},
19 run_with_output, Cmd, Result, 17 pre_commit, run_clippy, run_fuzzer, run_rustfmt, Result,
20}; 18};
21 19
22// Latest stable, feel free to send a PR if this lags behind.
23const REQUIRED_RUST_VERSION: u32 = 40;
24
25struct InstallOpt {
26 client: Option<ClientOpt>,
27 server: Option<ServerOpt>,
28}
29
30enum ClientOpt {
31 VsCode,
32}
33
34struct ServerOpt {
35 jemalloc: bool,
36}
37
38fn main() -> Result<()> { 20fn main() -> Result<()> {
39 if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) { 21 if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
40 return reformat_staged_files(); 22 return pre_commit::run_hook();
41 } 23 }
42 24
43 let subcommand = match std::env::args_os().nth(1) { 25 let subcommand = std::env::args().nth(1).unwrap_or_default();
44 None => { 26 let mut args = Arguments::from_vec(std::env::args_os().skip(2).collect());
45 eprintln!("{}", help::GLOBAL_HELP); 27
46 return Ok(()); 28 match subcommand.as_str() {
47 }
48 Some(s) => s,
49 };
50 let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
51 let subcommand = &*subcommand.to_string_lossy();
52 match subcommand {
53 "install" => { 29 "install" => {
54 if matches.contains(["-h", "--help"]) { 30 if args.contains(["-h", "--help"]) {
55 eprintln!("{}", help::INSTALL_HELP); 31 eprintln!(
32 "\
33cargo xtask install
34Install rust-analyzer server or editor plugin.
35
36USAGE:
37 cargo xtask install [FLAGS]
38
39FLAGS:
40 --client-code Install only VS Code plugin
41 --server Install only the language server
42 --jemalloc Use jemalloc for server
43 -h, --help Prints help information
44 "
45 );
56 return Ok(()); 46 return Ok(());
57 } 47 }
58 let server = matches.contains("--server"); 48 let server = args.contains("--server");
59 let client_code = matches.contains("--client-code"); 49 let client_code = args.contains("--client-code");
60 if server && client_code { 50 if server && client_code {
61 eprintln!("{}", help::INSTALL_RA_CONFLICT); 51 eprintln!(
52 "error: The argument `--server` cannot be used with `--client-code`\n\n\
53 For more information try --help"
54 );
62 return Ok(()); 55 return Ok(());
63 } 56 }
64 let jemalloc = matches.contains("--jemalloc"); 57
65 matches.finish().or_else(handle_extra_flags)?; 58 let jemalloc = args.contains("--jemalloc");
66 let opts = InstallOpt { 59
60 args.finish()?;
61
62 InstallCmd {
67 client: if server { None } else { Some(ClientOpt::VsCode) }, 63 client: if server { None } else { Some(ClientOpt::VsCode) },
68 server: if client_code { None } else { Some(ServerOpt { jemalloc }) }, 64 server: if client_code { None } else { Some(ServerOpt { jemalloc }) },
69 }; 65 }
70 install(opts)? 66 .run()
71 } 67 }
72 "codegen" => { 68 "codegen" => {
73 if matches.contains(["-h", "--help"]) { 69 args.finish()?;
74 help::print_no_param_subcommand_help(&subcommand);
75 return Ok(());
76 }
77 codegen::generate_syntax(Mode::Overwrite)?; 70 codegen::generate_syntax(Mode::Overwrite)?;
78 codegen::generate_parser_tests(Mode::Overwrite)?; 71 codegen::generate_parser_tests(Mode::Overwrite)?;
79 codegen::generate_assists_docs(Mode::Overwrite)?; 72 codegen::generate_assists_docs(Mode::Overwrite)?;
73 Ok(())
80 } 74 }
81 "format" => { 75 "format" => {
82 if matches.contains(["-h", "--help"]) { 76 args.finish()?;
83 help::print_no_param_subcommand_help(&subcommand); 77 run_rustfmt(Mode::Overwrite)
84 return Ok(());
85 }
86 run_rustfmt(Mode::Overwrite)?
87 } 78 }
88 "install-pre-commit-hook" => { 79 "install-pre-commit-hook" => {
89 if matches.contains(["-h", "--help"]) { 80 args.finish()?;
90 help::print_no_param_subcommand_help(&subcommand); 81 pre_commit::install_hook()
91 return Ok(());
92 }
93 install_pre_commit_hook()?
94 } 82 }
95 "lint" => { 83 "lint" => {
96 if matches.contains(["-h", "--help"]) { 84 args.finish()?;
97 help::print_no_param_subcommand_help(&subcommand); 85 run_clippy()
98 return Ok(());
99 }
100 run_clippy()?
101 } 86 }
102 "fuzz-tests" => { 87 "fuzz-tests" => {
103 if matches.contains(["-h", "--help"]) { 88 args.finish()?;
104 help::print_no_param_subcommand_help(&subcommand); 89 run_fuzzer()
105 return Ok(());
106 }
107 run_fuzzer()?
108 } 90 }
109 _ => eprintln!("{}", help::GLOBAL_HELP), 91 _ => {
110 } 92 eprintln!(
111 Ok(()) 93 "\
112} 94cargo xtask
113 95Run custom build command.
114fn handle_extra_flags(e: pico_args::Error) -> Result<()> { 96
115 if let pico_args::Error::UnusedArgsLeft(flags) = e { 97USAGE:
116 let mut invalid_flags = String::new(); 98 cargo xtask <SUBCOMMAND>
117 for flag in flags { 99
118 write!(&mut invalid_flags, "{}, ", flag)?; 100SUBCOMMANDS:
101 format
102 install-pre-commit-hook
103 fuzz-tests
104 codegen
105 install
106 lint"
107 );
108 Ok(())
119 } 109 }
120 let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
121 anyhow::bail!("Invalid flags: {}", invalid_flags)
122 } else {
123 anyhow::bail!(e.to_string())
124 }
125}
126
127fn install(opts: InstallOpt) -> Result<()> {
128 if cfg!(target_os = "macos") {
129 fix_path_for_mac().context("Fix path for mac")?
130 }
131 if let Some(server) = opts.server {
132 install_server(server).context("install server")?;
133 }
134 if let Some(client) = opts.client {
135 install_client(client).context("install client")?;
136 }
137 Ok(())
138}
139
140fn fix_path_for_mac() -> Result<()> {
141 let mut vscode_path: Vec<PathBuf> = {
142 const COMMON_APP_PATH: &str =
143 r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
144 const ROOT_DIR: &str = "";
145 let home_dir = match env::var("HOME") {
146 Ok(home) => home,
147 Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
148 };
149
150 [ROOT_DIR, &home_dir]
151 .iter()
152 .map(|dir| String::from(*dir) + COMMON_APP_PATH)
153 .map(PathBuf::from)
154 .filter(|path| path.exists())
155 .collect()
156 };
157
158 if !vscode_path.is_empty() {
159 let vars = match env::var_os("PATH") {
160 Some(path) => path,
161 None => anyhow::bail!("Could not get PATH variable from env."),
162 };
163
164 let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
165 paths.append(&mut vscode_path);
166 let new_paths = env::join_paths(paths).context("build env PATH")?;
167 env::set_var("PATH", &new_paths);
168 }
169
170 Ok(())
171}
172
173fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
174 let npm_version = Cmd {
175 unix: r"npm --version",
176 windows: r"cmd.exe /c npm --version",
177 work_dir: "./editors/code",
178 }
179 .run();
180
181 if npm_version.is_err() {
182 eprintln!("\nERROR: `npm --version` failed, `npm` is required to build the VS Code plugin")
183 }
184
185 Cmd { unix: r"npm install", windows: r"cmd.exe /c npm install", work_dir: "./editors/code" }
186 .run()?;
187 Cmd {
188 unix: r"npm run package --scripts-prepend-node-path",
189 windows: r"cmd.exe /c npm run package",
190 work_dir: "./editors/code",
191 }
192 .run()?;
193
194 let code_binary = ["code", "code-insiders", "codium"].iter().find(|bin| {
195 Cmd {
196 unix: &format!("{} --version", bin),
197 windows: &format!("cmd.exe /c {}.cmd --version", bin),
198 work_dir: "./editors/code",
199 }
200 .run()
201 .is_ok()
202 });
203
204 let code_binary = match code_binary {
205 Some(it) => it,
206 None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
207 };
208
209 Cmd {
210 unix: &format!(r"{} --install-extension ./ra-lsp-0.0.1.vsix --force", code_binary),
211 windows: &format!(
212 r"cmd.exe /c {}.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
213 code_binary
214 ),
215 work_dir: "./editors/code",
216 }
217 .run()?;
218
219 let output = Cmd {
220 unix: &format!(r"{} --list-extensions", code_binary),
221 windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
222 work_dir: ".",
223 }
224 .run_with_output()?;
225
226 if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
227 anyhow::bail!(
228 "Could not install the Visual Studio Code extension. \
229 Please make sure you have at least NodeJS 10.x together with the latest version of VS Code installed and try again."
230 );
231 }
232
233 Ok(())
234}
235
236fn install_server(opts: ServerOpt) -> Result<()> {
237 let mut old_rust = false;
238 if let Ok(output) = run_with_output("cargo --version", ".") {
239 if let Ok(stdout) = String::from_utf8(output.stdout) {
240 println!("{}", stdout);
241 if !check_version(&stdout, REQUIRED_RUST_VERSION) {
242 old_rust = true;
243 }
244 }
245 }
246
247 if old_rust {
248 eprintln!(
249 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
250 REQUIRED_RUST_VERSION,
251 )
252 }
253
254 let res = if opts.jemalloc {
255 run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
256 } else {
257 run("cargo install --path crates/ra_lsp_server --locked --force", ".")
258 };
259
260 if res.is_err() && old_rust {
261 eprintln!(
262 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
263 REQUIRED_RUST_VERSION,
264 )
265 }
266
267 res
268}
269
270fn check_version(version_output: &str, min_minor_version: u32) -> bool {
271 // Parse second the number out of
272 // cargo 1.39.0-beta (1c6ec66d5 2019-09-30)
273 let minor: Option<u32> = version_output.split('.').nth(1).and_then(|it| it.parse().ok());
274 match minor {
275 None => true,
276 Some(minor) => minor >= min_minor_version,
277 } 110 }
278} 111}
diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs
new file mode 100644
index 000000000..88e868ca6
--- /dev/null
+++ b/xtask/src/pre_commit.rs
@@ -0,0 +1,36 @@
1//! pre-commit hook for code formatting.
2
3use std::{fs, path::PathBuf};
4
5use anyhow::{bail, Result};
6
7use crate::{cmd::run_with_output, project_root, run, run_rustfmt, Mode};
8
9// FIXME: if there are changed `.ts` files, also reformat TypeScript (by
10// shelling out to `npm fmt`).
11pub fn run_hook() -> Result<()> {
12 run_rustfmt(Mode::Overwrite)?;
13
14 let diff = run_with_output("git diff --diff-filter=MAR --name-only --cached", ".")?;
15
16 let root = project_root();
17 for line in String::from_utf8(diff.stdout)?.lines() {
18 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
19 }
20
21 Ok(())
22}
23
24pub fn install_hook() -> Result<()> {
25 let hook_path: PathBuf =
26 format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into();
27
28 if hook_path.exists() {
29 bail!("Git hook already created");
30 }
31
32 let me = std::env::current_exe()?;
33 fs::copy(me, hook_path)?;
34
35 Ok(())
36}