rust/crates/ra_tools/src/main.rs

157 lines
4.7 KiB
Rust
Raw Normal View History

use clap::{App, Arg, SubCommand};
2019-03-05 15:19:36 -06:00
use core::str;
use ra_tools::{
gen_tests, generate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, Cmd,
Overwrite, Result,
2018-12-31 07:14:06 -06:00
};
use std::{env, path::PathBuf};
2018-07-30 06:06:22 -05:00
struct InstallOpt {
client: Option<ClientOpt>,
server: Option<ServerOpt>,
}
enum ClientOpt {
VsCode,
}
struct ServerOpt {
jemalloc: bool,
}
2018-07-30 06:06:22 -05:00
fn main() -> Result<()> {
let matches = App::new("tasks")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
2018-10-16 13:09:22 -05:00
.subcommand(SubCommand::with_name("gen-syntax"))
2018-07-30 06:06:22 -05:00
.subcommand(SubCommand::with_name("gen-tests"))
.subcommand(
SubCommand::with_name("install-ra")
.arg(Arg::with_name("server").long("--server"))
.arg(Arg::with_name("jemalloc").long("jemalloc"))
.arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")),
)
.alias("install-code")
2018-10-26 08:08:21 -05:00
.subcommand(SubCommand::with_name("format"))
2018-12-09 04:29:13 -06:00
.subcommand(SubCommand::with_name("format-hook"))
2018-12-31 07:14:06 -06:00
.subcommand(SubCommand::with_name("fuzz-tests"))
2019-06-03 08:43:06 -05:00
.subcommand(SubCommand::with_name("lint"))
2018-07-30 06:06:22 -05:00
.get_matches();
match matches.subcommand() {
("install-ra", Some(matches)) => {
let opts = InstallOpt {
client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) },
server: if matches.is_present("client-code") {
None
} else {
Some(ServerOpt { jemalloc: matches.is_present("jemalloc") })
},
};
install(opts)?
2019-03-18 12:27:31 -05:00
}
("gen-tests", _) => gen_tests(Overwrite)?,
("gen-syntax", _) => generate(Overwrite)?,
("format", _) => run_rustfmt(Overwrite)?,
("format-hook", _) => install_format_hook()?,
("lint", _) => run_clippy()?,
("fuzz-tests", _) => run_fuzzer()?,
2018-07-30 06:06:22 -05:00
_ => unreachable!(),
}
2018-08-09 09:43:39 -05:00
Ok(())
2018-07-30 06:06:22 -05:00
}
fn install(opts: InstallOpt) -> Result<()> {
if cfg!(target_os = "macos") {
fix_path_for_mac()?
2018-09-15 18:02:25 -05:00
}
if let Some(server) = opts.server {
install_server(server)?;
2019-03-05 15:19:36 -06:00
}
2019-08-07 05:12:23 -05:00
if let Some(client) = opts.client {
install_client(client)?;
}
2018-09-15 18:12:53 -05:00
Ok(())
}
2019-03-18 14:18:54 -05: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-15 13:48:50 -05:00
Err(e) => Err(format!("Failed getting HOME from environment with error: {}.", e))?,
};
2019-03-18 14:18:54 -05:00
[ROOT_DIR, &home_dir]
.iter()
2019-06-03 09:00:51 -05:00
.map(|dir| String::from(*dir) + COMMON_APP_PATH)
2019-03-18 14:18:54 -05:00
.map(PathBuf::from)
.filter(|path| path.exists())
.collect()
};
2019-03-18 14:18:54 -05:00
if !vscode_path.is_empty() {
let vars = match env::var_os("PATH") {
Some(path) => path,
2019-06-15 13:48:50 -05:00
None => Err("Could not get PATH variable from env.")?,
2019-03-18 14:18:54 -05:00
};
2019-03-18 14:18:54 -05: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 14:18:54 -05: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 --force --features jemalloc", ".")
} else {
run("cargo install --path crates/ra_lsp_server --force", ".")
}
}