aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_tools/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_tools/src/main.rs')
-rw-r--r--crates/ra_tools/src/main.rs129
1 files changed, 94 insertions, 35 deletions
diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs
index 33badf290..5410edea9 100644
--- a/crates/ra_tools/src/main.rs
+++ b/crates/ra_tools/src/main.rs
@@ -1,5 +1,8 @@
1use clap::{App, Arg, SubCommand}; 1mod help;
2
3use core::fmt::Write;
2use core::str; 4use core::str;
5use pico_args::Arguments;
3use ra_tools::{ 6use ra_tools::{
4 gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, 7 gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt,
5 Cmd, Overwrite, Result, 8 Cmd, Overwrite, Result,
@@ -20,45 +23,101 @@ struct ServerOpt {
20} 23}
21 24
22fn main() -> Result<()> { 25fn main() -> Result<()> {
23 let matches = App::new("tasks") 26 let subcommand = std::env::args_os().nth(1);
24 .setting(clap::AppSettings::SubcommandRequiredElseHelp) 27 if subcommand.is_none() {
25 .subcommand(SubCommand::with_name("gen-syntax")) 28 help::print_global_help();
26 .subcommand(SubCommand::with_name("gen-tests")) 29 return Ok(());
27 .subcommand( 30 }
28 SubCommand::with_name("install-ra") 31 let subcommand = subcommand.unwrap();
29 .arg(Arg::with_name("server").long("--server")) 32 let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
30 .arg(Arg::with_name("jemalloc").long("jemalloc")) 33 let subcommand = &*subcommand.to_string_lossy();
31 .arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")), 34 match subcommand {
32 ) 35 "install-ra" | "install-code" => {
33 .alias("install-code") 36 if matches.contains(["-h", "--help"]) {
34 .subcommand(SubCommand::with_name("format")) 37 help::print_install_ra_help();
35 .subcommand(SubCommand::with_name("format-hook")) 38 return Ok(());
36 .subcommand(SubCommand::with_name("fuzz-tests")) 39 } else {
37 .subcommand(SubCommand::with_name("lint")) 40 let server = matches.contains("--server");
38 .get_matches(); 41 let client_code = matches.contains("--client-code");
39 match matches.subcommand() { 42 if server && client_code {
40 ("install-ra", Some(matches)) => { 43 help::print_install_ra_conflict();
41 let opts = InstallOpt { 44 return Ok(());
42 client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) }, 45 }
43 server: if matches.is_present("client-code") { 46 let jemalloc = matches.contains("--jemalloc");
44 None 47 matches.finish().or_else(handle_extra_flags)?;
45 } else { 48 let opts = InstallOpt {
46 Some(ServerOpt { jemalloc: matches.is_present("jemalloc") }) 49 client: if server { None } else { Some(ClientOpt::VsCode) },
47 }, 50 server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) },
48 }; 51 };
49 install(opts)? 52 install(opts)?
53 }
54 }
55 "gen-tests" => {
56 if matches.contains(["-h", "--help"]) {
57 help::print_no_param_subcommand_help(&subcommand);
58 return Ok(());
59 } else {
60 gen_tests(Overwrite)?
61 }
62 }
63 "gen-syntax" => {
64 if matches.contains(["-h", "--help"]) {
65 help::print_no_param_subcommand_help(&subcommand);
66 return Ok(());
67 } else {
68 generate_boilerplate(Overwrite)?
69 }
70 }
71 "format" => {
72 if matches.contains(["-h", "--help"]) {
73 help::print_no_param_subcommand_help(&subcommand);
74 return Ok(());
75 } else {
76 run_rustfmt(Overwrite)?
77 }
78 }
79 "format-hook" => {
80 if matches.contains(["-h", "--help"]) {
81 help::print_no_param_subcommand_help(&subcommand);
82 return Ok(());
83 } else {
84 install_format_hook()?
85 }
86 }
87 "lint" => {
88 if matches.contains(["-h", "--help"]) {
89 help::print_no_param_subcommand_help(&subcommand);
90 return Ok(());
91 } else {
92 run_clippy()?
93 }
50 } 94 }
51 ("gen-tests", _) => gen_tests(Overwrite)?, 95 "fuzz-tests" => {
52 ("gen-syntax", _) => generate_boilerplate(Overwrite)?, 96 if matches.contains(["-h", "--help"]) {
53 ("format", _) => run_rustfmt(Overwrite)?, 97 help::print_no_param_subcommand_help(&subcommand);
54 ("format-hook", _) => install_format_hook()?, 98 return Ok(());
55 ("lint", _) => run_clippy()?, 99 } else {
56 ("fuzz-tests", _) => run_fuzzer()?, 100 run_fuzzer()?
57 _ => unreachable!(), 101 }
102 }
103 _ => help::print_global_help(),
58 } 104 }
59 Ok(()) 105 Ok(())
60} 106}
61 107
108fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
109 if let pico_args::Error::UnusedArgsLeft(flags) = e {
110 let mut invalid_flags = String::new();
111 for flag in flags {
112 write!(&mut invalid_flags, "{}, ", flag).expect("Error on write");
113 }
114 let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
115 Err(format!("Invalid flags: {}", invalid_flags).into())
116 } else {
117 Err(e.to_string().into())
118 }
119}
120
62fn install(opts: InstallOpt) -> Result<()> { 121fn install(opts: InstallOpt) -> Result<()> {
63 if cfg!(target_os = "macos") { 122 if cfg!(target_os = "macos") {
64 fix_path_for_mac()? 123 fix_path_for_mac()?