2019-01-26 23:03:52 +00:00
|
|
|
use clap::{App, SubCommand};
|
2019-03-05 21:19:36 +00:00
|
|
|
use core::str;
|
|
|
|
use failure::bail;
|
2018-12-31 13:14:06 +00:00
|
|
|
use tools::{
|
2019-03-05 21:19:36 +00:00
|
|
|
generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt,
|
2019-01-26 23:03:52 +00:00
|
|
|
Overwrite, Result, run_fuzzer,
|
2018-12-31 13:14:06 +00:00
|
|
|
};
|
2018-07-30 11:06:22 +00:00
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
let matches = App::new("tasks")
|
|
|
|
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
2018-10-16 18:09:22 +00:00
|
|
|
.subcommand(SubCommand::with_name("gen-syntax"))
|
2018-07-30 11:06:22 +00:00
|
|
|
.subcommand(SubCommand::with_name("gen-tests"))
|
2018-07-30 19:17:33 +00:00
|
|
|
.subcommand(SubCommand::with_name("install-code"))
|
2018-10-26 13:08:21 +00:00
|
|
|
.subcommand(SubCommand::with_name("format"))
|
2018-12-09 10:29:13 +00:00
|
|
|
.subcommand(SubCommand::with_name("format-hook"))
|
2018-12-31 13:14:06 +00:00
|
|
|
.subcommand(SubCommand::with_name("fuzz-tests"))
|
2018-07-30 11:06:22 +00:00
|
|
|
.get_matches();
|
2019-02-08 11:49:43 +00:00
|
|
|
match matches.subcommand_name().expect("Subcommand must be specified") {
|
2019-03-18 17:27:31 +00:00
|
|
|
"install-code" => {
|
|
|
|
setup_environment()?;
|
|
|
|
install_code_extension()?;
|
|
|
|
}
|
2019-01-26 23:03:52 +00:00
|
|
|
"gen-tests" => gen_tests(Overwrite)?,
|
2018-12-09 10:29:13 +00:00
|
|
|
"gen-syntax" => generate(Overwrite)?,
|
2019-01-26 23:03:52 +00:00
|
|
|
"format" => run_rustfmt(Overwrite)?,
|
2018-12-09 10:29:13 +00:00
|
|
|
"format-hook" => install_format_hook()?,
|
2018-12-31 13:14:06 +00:00
|
|
|
"fuzz-tests" => run_fuzzer()?,
|
2018-07-30 11:06:22 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2018-08-09 14:43:39 +00:00
|
|
|
Ok(())
|
2018-07-30 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:27:31 +00:00
|
|
|
fn setup_environment() -> Result<()> {
|
|
|
|
if cfg!(target_os = "macos") {
|
|
|
|
vscode_path_helpers::append_vscode_path()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-07-30 19:17:33 +00:00
|
|
|
fn install_code_extension() -> Result<()> {
|
2018-09-16 09:54:24 +00:00
|
|
|
run("cargo install --path crates/ra_lsp_server --force", ".")?;
|
2018-09-15 23:02:25 +00:00
|
|
|
if cfg!(windows) {
|
2019-01-23 18:23:15 +00:00
|
|
|
run(r"cmd.exe /c npm.cmd ci", "./editors/code")?;
|
2018-12-17 08:26:41 +00:00
|
|
|
run(r"cmd.exe /c npm.cmd run package", "./editors/code")?;
|
2018-09-16 00:06:56 +00:00
|
|
|
} else {
|
2019-01-23 18:23:15 +00:00
|
|
|
run(r"npm ci", "./editors/code")?;
|
2018-12-17 08:26:41 +00:00
|
|
|
run(r"npm run package", "./editors/code")?;
|
2018-09-15 23:02:25 +00:00
|
|
|
}
|
|
|
|
if cfg!(windows) {
|
2018-10-15 21:44:23 +00:00
|
|
|
run(
|
2019-01-12 15:00:03 +00:00
|
|
|
r"cmd.exe /c code.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
|
2018-10-15 21:44:23 +00:00
|
|
|
"./editors/code",
|
|
|
|
)?;
|
2018-09-15 23:02:25 +00:00
|
|
|
} else {
|
2019-02-08 11:49:43 +00:00
|
|
|
run(r"code --install-extension ./ra-lsp-0.0.1.vsix --force", "./editors/code")?;
|
2018-09-15 23:02:25 +00:00
|
|
|
}
|
2019-03-05 21:19:36 +00: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 23:12:53 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-18 17:27:11 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
mod vscode_path_helpers {
|
|
|
|
use super::Result;
|
|
|
|
use std::{path::{PathBuf}, env};
|
|
|
|
use failure::bail;
|
|
|
|
|
|
|
|
pub(crate) fn append_vscode_path() -> Result<()> {
|
|
|
|
let vars = match env::var_os("PATH") {
|
|
|
|
Some(path) => path,
|
|
|
|
None => bail!("Could not get PATH variable from env."),
|
|
|
|
};
|
|
|
|
|
|
|
|
let vscode_path = get_vscode_path()?;
|
|
|
|
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
|
|
|
|
paths.push(vscode_path);
|
|
|
|
let new_paths = env::join_paths(paths)?;
|
|
|
|
env::set_var("PATH", &new_paths);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_vscode_path() -> Result<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),
|
|
|
|
};
|
|
|
|
|
|
|
|
for dir in [ROOT_DIR, &home_dir].iter() {
|
|
|
|
let path = String::from(dir.clone()) + COMMON_APP_PATH;
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.exists() {
|
|
|
|
return Ok(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!(
|
|
|
|
"Could not find Visual Studio Code application. Please make sure you \
|
|
|
|
have Visual Studio Code installed and try again or install extension \
|
|
|
|
manually."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
mod vscode_path_helpers {
|
|
|
|
use super::Result;
|
|
|
|
pub(crate) fn append_vscode_path() -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|