rust/crates/tools/src/main.rs

105 lines
3.4 KiB
Rust
Raw Normal View History

use clap::{App, SubCommand};
2019-03-05 15:19:36 -06:00
use core::str;
use failure::bail;
2018-12-31 07:14:06 -06:00
use tools::{
2019-03-05 15:19:36 -06:00
generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt,
Overwrite, Result, run_fuzzer,
2018-12-31 07:14:06 -06:00
};
2019-03-18 14:18:54 -05:00
use std::{path::{PathBuf}, env};
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"))
2018-07-30 14:17:33 -05:00
.subcommand(SubCommand::with_name("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"))
2018-07-30 06:06:22 -05:00
.get_matches();
2019-02-08 05:49:43 -06:00
match matches.subcommand_name().expect("Subcommand must be specified") {
2019-03-18 12:27:31 -05:00
"install-code" => {
2019-03-18 14:18:54 -05:00
if cfg!(target_os = "macos") {
fix_path_for_mac()?;
}
2019-03-18 12:27:31 -05:00
install_code_extension()?;
}
"gen-tests" => gen_tests(Overwrite)?,
2018-12-09 04:29:13 -06:00
"gen-syntax" => generate(Overwrite)?,
"format" => run_rustfmt(Overwrite)?,
2018-12-09 04:29:13 -06:00
"format-hook" => install_format_hook()?,
2018-12-31 07:14:06 -06:00
"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
}
2018-07-30 14:17:33 -05:00
fn install_code_extension() -> Result<()> {
2018-09-16 04:54:24 -05:00
run("cargo install --path crates/ra_lsp_server --force", ".")?;
2018-09-15 18:02:25 -05:00
if cfg!(windows) {
2019-01-23 12:23:15 -06:00
run(r"cmd.exe /c npm.cmd ci", "./editors/code")?;
2018-12-17 02:26:41 -06:00
run(r"cmd.exe /c npm.cmd run package", "./editors/code")?;
2018-09-15 19:06:56 -05:00
} else {
2019-01-23 12:23:15 -06:00
run(r"npm ci", "./editors/code")?;
2018-12-17 02:26:41 -06:00
run(r"npm run package", "./editors/code")?;
2018-09-15 18:02:25 -05:00
}
if cfg!(windows) {
run(
2019-01-12 09:00:03 -06:00
r"cmd.exe /c code.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
"./editors/code",
)?;
2018-09-15 18:02:25 -05:00
} else {
2019-02-08 05:49:43 -06:00
run(r"code --install-extension ./ra-lsp-0.0.1.vsix --force", "./editors/code")?;
2018-09-15 18:02:25 -05:00
}
2019-03-05 15:19:36 -06:00
verify_installed_extensions()?;
Ok(())
}
fn verify_installed_extensions() -> Result<()> {
let exts = if cfg!(windows) {
run_with_output(r"cmd.exe /c code.cmd --list-extensions", ".")?
} else {
run_with_output(r"code --list-extensions", ".")?
};
if !str::from_utf8(&exts.stdout)?.contains("ra-lsp") {
bail!(
"Could not install the Visual Studio Code extension. Please make sure you \
have at least NodeJS 10.x installed and try again."
);
}
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,
Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
};
2019-03-18 14:18:54 -05:00
[ROOT_DIR, &home_dir]
.iter()
.map(|dir| String::from(dir.clone()) + COMMON_APP_PATH)
.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,
None => bail!("Could not get PATH variable from env."),
};
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(())
}