diff options
Diffstat (limited to 'crates/ra_tools/src/main.rs')
-rw-r--r-- | crates/ra_tools/src/main.rs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs new file mode 100644 index 000000000..285071ea5 --- /dev/null +++ b/crates/ra_tools/src/main.rs | |||
@@ -0,0 +1,106 @@ | |||
1 | use clap::{App, SubCommand}; | ||
2 | use core::str; | ||
3 | use failure::bail; | ||
4 | use ra_tools::{ | ||
5 | generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt, | ||
6 | Overwrite, Result, run_fuzzer, run_clippy, | ||
7 | }; | ||
8 | use std::{path::{PathBuf}, env}; | ||
9 | |||
10 | fn main() -> Result<()> { | ||
11 | let matches = App::new("tasks") | ||
12 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | ||
13 | .subcommand(SubCommand::with_name("gen-syntax")) | ||
14 | .subcommand(SubCommand::with_name("gen-tests")) | ||
15 | .subcommand(SubCommand::with_name("install-code")) | ||
16 | .subcommand(SubCommand::with_name("format")) | ||
17 | .subcommand(SubCommand::with_name("format-hook")) | ||
18 | .subcommand(SubCommand::with_name("fuzz-tests")) | ||
19 | .subcommand(SubCommand::with_name("lint")) | ||
20 | .get_matches(); | ||
21 | match matches.subcommand_name().expect("Subcommand must be specified") { | ||
22 | "install-code" => { | ||
23 | if cfg!(target_os = "macos") { | ||
24 | fix_path_for_mac()?; | ||
25 | } | ||
26 | install_code_extension()?; | ||
27 | } | ||
28 | "gen-tests" => gen_tests(Overwrite)?, | ||
29 | "gen-syntax" => generate(Overwrite)?, | ||
30 | "format" => run_rustfmt(Overwrite)?, | ||
31 | "format-hook" => install_format_hook()?, | ||
32 | "lint" => run_clippy()?, | ||
33 | "fuzz-tests" => run_fuzzer()?, | ||
34 | _ => unreachable!(), | ||
35 | } | ||
36 | Ok(()) | ||
37 | } | ||
38 | |||
39 | fn install_code_extension() -> Result<()> { | ||
40 | run("cargo install --path crates/ra_lsp_server --force", ".")?; | ||
41 | if cfg!(windows) { | ||
42 | run(r"cmd.exe /c npm.cmd ci", "./editors/code")?; | ||
43 | run(r"cmd.exe /c npm.cmd run package", "./editors/code")?; | ||
44 | } else { | ||
45 | run(r"npm ci", "./editors/code")?; | ||
46 | run(r"npm run package", "./editors/code")?; | ||
47 | } | ||
48 | if cfg!(windows) { | ||
49 | run( | ||
50 | r"cmd.exe /c code.cmd --install-extension ./ra-lsp-0.0.1.vsix --force", | ||
51 | "./editors/code", | ||
52 | )?; | ||
53 | } else { | ||
54 | run(r"code --install-extension ./ra-lsp-0.0.1.vsix --force", "./editors/code")?; | ||
55 | } | ||
56 | verify_installed_extensions()?; | ||
57 | Ok(()) | ||
58 | } | ||
59 | |||
60 | fn verify_installed_extensions() -> Result<()> { | ||
61 | let exts = if cfg!(windows) { | ||
62 | run_with_output(r"cmd.exe /c code.cmd --list-extensions", ".")? | ||
63 | } else { | ||
64 | run_with_output(r"code --list-extensions", ".")? | ||
65 | }; | ||
66 | if !str::from_utf8(&exts.stdout)?.contains("ra-lsp") { | ||
67 | bail!( | ||
68 | "Could not install the Visual Studio Code extension. Please make sure you \ | ||
69 | have at least NodeJS 10.x installed and try again." | ||
70 | ); | ||
71 | } | ||
72 | Ok(()) | ||
73 | } | ||
74 | |||
75 | fn fix_path_for_mac() -> Result<()> { | ||
76 | let mut vscode_path: Vec<PathBuf> = { | ||
77 | const COMMON_APP_PATH: &str = | ||
78 | r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin"; | ||
79 | const ROOT_DIR: &str = ""; | ||
80 | let home_dir = match env::var("HOME") { | ||
81 | Ok(home) => home, | ||
82 | Err(e) => bail!("Failed getting HOME from environment with error: {}.", e), | ||
83 | }; | ||
84 | |||
85 | [ROOT_DIR, &home_dir] | ||
86 | .iter() | ||
87 | .map(|dir| String::from(*dir) + COMMON_APP_PATH) | ||
88 | .map(PathBuf::from) | ||
89 | .filter(|path| path.exists()) | ||
90 | .collect() | ||
91 | }; | ||
92 | |||
93 | if !vscode_path.is_empty() { | ||
94 | let vars = match env::var_os("PATH") { | ||
95 | Some(path) => path, | ||
96 | None => bail!("Could not get PATH variable from env."), | ||
97 | }; | ||
98 | |||
99 | let mut paths = env::split_paths(&vars).collect::<Vec<_>>(); | ||
100 | paths.append(&mut vscode_path); | ||
101 | let new_paths = env::join_paths(paths)?; | ||
102 | env::set_var("PATH", &new_paths); | ||
103 | } | ||
104 | |||
105 | Ok(()) | ||
106 | } | ||