216 lines
6.5 KiB
Rust
Raw Normal View History

mod help;
use core::fmt::Write;
2019-03-05 18:19:36 -03:00
use core::str;
use pico_args::Arguments;
use ra_tools::{
2019-08-19 12:26:34 +03:00
gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt,
Cmd, Overwrite, Result,
2018-12-31 13:14:06 +00:00
};
use std::{env, path::PathBuf};
2018-07-30 14:06:22 +03:00
struct InstallOpt {
client: Option<ClientOpt>,
server: Option<ServerOpt>,
}
enum ClientOpt {
VsCode,
}
struct ServerOpt {
jemalloc: bool,
}
2018-07-30 14:06:22 +03:00
fn main() -> Result<()> {
let subcommand = std::env::args_os().nth(1);
if subcommand.is_none() {
help::print_global_help();
return Ok(());
}
let subcommand = subcommand.unwrap();
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
let subcommand = &*subcommand.to_string_lossy();
match subcommand {
"install-ra" | "install-code" => {
if matches.contains(["-h", "--help"]) {
help::print_install_ra_help();
return Ok(());
} else {
let server = matches.contains("--server");
let client_code = matches.contains("--client-code");
if server && client_code {
help::print_install_ra_conflict();
return Ok(());
}
let jemalloc = matches.contains("--jemalloc");
matches.finish().or_else(handle_extra_flags)?;
let opts = InstallOpt {
client: if server { None } else { Some(ClientOpt::VsCode) },
server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) },
};
install(opts)?
}
}
"gen-tests" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
gen_tests(Overwrite)?
}
}
"gen-syntax" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
generate_boilerplate(Overwrite)?
}
}
"format" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
run_rustfmt(Overwrite)?
}
}
"format-hook" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
install_format_hook()?
}
2019-03-18 20:27:31 +03:00
}
"lint" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
run_clippy()?
}
}
"fuzz-tests" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
} else {
run_fuzzer()?
}
}
_ => help::print_global_help(),
2018-07-30 14:06:22 +03:00
}
2018-08-09 17:43:39 +03:00
Ok(())
2018-07-30 14:06:22 +03:00
}
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
if let pico_args::Error::UnusedArgsLeft(flags) = e {
let mut invalid_flags = String::new();
for flag in flags {
write!(&mut invalid_flags, "{}, ", flag).expect("Error on write");
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
} else {
Err(e.to_string().into())
}
}
fn install(opts: InstallOpt) -> Result<()> {
if cfg!(target_os = "macos") {
fix_path_for_mac()?
2018-09-16 00:02:25 +01:00
}
if let Some(server) = opts.server {
install_server(server)?;
2019-03-05 18:19:36 -03:00
}
2019-08-07 12:12:23 +02:00
if let Some(client) = opts.client {
install_client(client)?;
}
2018-09-16 02:12:53 +03:00
Ok(())
}
2019-03-18 22:18:54 +03:00
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,
2019-06-16 00:48:50 +06:00
Err(e) => Err(format!("Failed getting HOME from environment with error: {}.", e))?,
};
2019-03-18 22:18:54 +03:00
[ROOT_DIR, &home_dir]
.iter()
2019-06-03 10:00:51 -04:00
.map(|dir| String::from(*dir) + COMMON_APP_PATH)
2019-03-18 22:18:54 +03:00
.map(PathBuf::from)
.filter(|path| path.exists())
.collect()
};
2019-03-18 22:18:54 +03:00
if !vscode_path.is_empty() {
let vars = match env::var_os("PATH") {
Some(path) => path,
2019-06-16 00:48:50 +06:00
None => Err("Could not get PATH variable from env.")?,
2019-03-18 22:18:54 +03:00
};
2019-03-18 22:18:54 +03:00
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
paths.append(&mut vscode_path);
let new_paths = env::join_paths(paths)?;
env::set_var("PATH", &new_paths);
}
2019-03-18 22:18:54 +03:00
Ok(())
}
fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
Cmd { unix: r"npm ci", windows: r"cmd.exe /c npm.cmd ci", work_dir: "./editors/code" }.run()?;
Cmd {
unix: r"npm run package",
windows: r"cmd.exe /c npm.cmd run package",
work_dir: "./editors/code",
}
.run()?;
let code_in_path = Cmd {
unix: r"code --version",
windows: r"cmd.exe /c code.cmd --version",
work_dir: "./editors/code",
}
.run()
.is_ok();
if !code_in_path {
Err("Can't execute `code --version`. Perhaps it is not in $PATH?")?;
}
Cmd {
unix: r"code --install-extension ./ra-lsp-0.0.1.vsix --force",
windows: r"cmd.exe /c code.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
work_dir: "./editors/code",
}
.run()?;
let output = Cmd {
unix: r"code --list-extensions",
windows: r"cmd.exe /c code.cmd --list-extensions",
work_dir: ".",
}
.run_with_output()?;
if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
Err("Could not install the Visual Studio Code extension. \
Please make sure you have at least NodeJS 10.x installed and try again.")?;
}
Ok(())
}
fn install_server(opts: ServerOpt) -> Result<()> {
if opts.jemalloc {
run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
} else {
run("cargo install --path crates/ra_lsp_server --locked --force", ".")
}
}