2019-10-26 09:20:44 -05:00
|
|
|
//! See https://github.com/matklad/cargo-xtask/.
|
|
|
|
//!
|
|
|
|
//! This binary defines various auxiliary build commands, which are not
|
|
|
|
//! expressible with just `cargo`. Notably, it provides `cargo xtask codegen`
|
2019-10-30 15:17:27 -05:00
|
|
|
//! for code generation and `cargo xtask install` for installation of
|
2019-10-26 09:20:44 -05:00
|
|
|
//! rust-analyzer server and client.
|
|
|
|
//!
|
|
|
|
//! This binary is integrated into the `cargo` command line by using an alias in
|
|
|
|
//! `.cargo/config`.
|
2019-09-10 06:56:45 -05:00
|
|
|
mod help;
|
|
|
|
|
2019-11-20 00:47:14 -06:00
|
|
|
use std::{env, fmt::Write, path::PathBuf, str};
|
|
|
|
|
2019-11-13 13:51:57 -06:00
|
|
|
use anyhow::Context;
|
2019-09-10 06:56:45 -05:00
|
|
|
use pico_args::Arguments;
|
2019-10-17 11:36:55 -05:00
|
|
|
use xtask::{
|
2019-10-23 10:13:40 -05:00
|
|
|
codegen::{self, Mode},
|
2019-11-18 06:22:51 -06:00
|
|
|
install_pre_commit_hook, reformat_staged_files, run, run_clippy, run_fuzzer, run_rustfmt,
|
|
|
|
run_with_output, Cmd, Result,
|
2018-12-31 07:14:06 -06:00
|
|
|
};
|
2018-07-30 06:06:22 -05:00
|
|
|
|
2019-10-21 05:40:40 -05:00
|
|
|
// Latest stable, feel free to send a PR if this lags behind.
|
2019-11-14 02:11:32 -06:00
|
|
|
const REQUIRED_RUST_VERSION: u32 = 39;
|
2019-10-21 05:40:40 -05:00
|
|
|
|
2019-07-27 06:35:18 -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<()> {
|
2019-11-20 00:47:14 -06:00
|
|
|
if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
|
2019-11-18 06:22:51 -06:00
|
|
|
return reformat_staged_files();
|
|
|
|
}
|
|
|
|
|
2019-09-10 10:17:11 -05:00
|
|
|
let subcommand = match std::env::args_os().nth(1) {
|
|
|
|
None => {
|
|
|
|
eprintln!("{}", help::GLOBAL_HELP);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some(s) => s,
|
|
|
|
};
|
2019-09-10 06:56:45 -05:00
|
|
|
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
|
|
|
|
let subcommand = &*subcommand.to_string_lossy();
|
|
|
|
match subcommand {
|
2019-10-17 11:36:55 -05:00
|
|
|
"install" => {
|
2019-09-10 06:56:45 -05:00
|
|
|
if matches.contains(["-h", "--help"]) {
|
2019-10-17 11:36:55 -05:00
|
|
|
eprintln!("{}", help::INSTALL_HELP);
|
2019-09-10 10:17:11 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let server = matches.contains("--server");
|
|
|
|
let client_code = matches.contains("--client-code");
|
|
|
|
if server && client_code {
|
|
|
|
eprintln!("{}", help::INSTALL_RA_CONFLICT);
|
2019-09-10 06:56:45 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-09-10 10:17:11 -05:00
|
|
|
let jemalloc = matches.contains("--jemalloc");
|
|
|
|
matches.finish().or_else(handle_extra_flags)?;
|
|
|
|
let opts = InstallOpt {
|
|
|
|
client: if server { None } else { Some(ClientOpt::VsCode) },
|
2019-10-30 12:36:37 -05:00
|
|
|
server: if client_code { None } else { Some(ServerOpt { jemalloc }) },
|
2019-09-10 10:17:11 -05:00
|
|
|
};
|
|
|
|
install(opts)?
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
2019-10-17 11:36:55 -05:00
|
|
|
"codegen" => {
|
2019-09-10 06:56:45 -05:00
|
|
|
if matches.contains(["-h", "--help"]) {
|
|
|
|
help::print_no_param_subcommand_help(&subcommand);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-10-23 10:17:24 -05:00
|
|
|
codegen::generate_syntax(Mode::Overwrite)?;
|
|
|
|
codegen::generate_parser_tests(Mode::Overwrite)?;
|
2019-10-25 06:16:46 -05:00
|
|
|
codegen::generate_assists_docs(Mode::Overwrite)?;
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
|
|
|
"format" => {
|
|
|
|
if matches.contains(["-h", "--help"]) {
|
|
|
|
help::print_no_param_subcommand_help(&subcommand);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-10-23 10:13:40 -05:00
|
|
|
run_rustfmt(Mode::Overwrite)?
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
2019-11-18 06:22:51 -06:00
|
|
|
"install-pre-commit-hook" => {
|
2019-09-10 06:56:45 -05:00
|
|
|
if matches.contains(["-h", "--help"]) {
|
|
|
|
help::print_no_param_subcommand_help(&subcommand);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-11-18 06:22:51 -06:00
|
|
|
install_pre_commit_hook()?
|
2019-03-18 12:27:31 -05:00
|
|
|
}
|
2019-09-10 06:56:45 -05:00
|
|
|
"lint" => {
|
|
|
|
if matches.contains(["-h", "--help"]) {
|
|
|
|
help::print_no_param_subcommand_help(&subcommand);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-09-10 10:17:11 -05:00
|
|
|
run_clippy()?
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
|
|
|
"fuzz-tests" => {
|
|
|
|
if matches.contains(["-h", "--help"]) {
|
|
|
|
help::print_no_param_subcommand_help(&subcommand);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-09-10 10:17:11 -05:00
|
|
|
run_fuzzer()?
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
2019-09-10 10:17:11 -05:00
|
|
|
_ => eprintln!("{}", help::GLOBAL_HELP),
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
2018-08-09 09:43:39 -05:00
|
|
|
Ok(())
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 06:56:45 -05: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 {
|
2019-09-10 10:17:11 -05:00
|
|
|
write!(&mut invalid_flags, "{}, ", flag)?;
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
|
|
|
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
|
2019-11-13 13:51:57 -06:00
|
|
|
anyhow::bail!("Invalid flags: {}", invalid_flags)
|
2019-09-10 06:56:45 -05:00
|
|
|
} else {
|
2019-11-13 13:51:57 -06:00
|
|
|
anyhow::bail!(e.to_string())
|
2019-09-10 06:56:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 06:35:18 -05:00
|
|
|
fn install(opts: InstallOpt) -> Result<()> {
|
|
|
|
if cfg!(target_os = "macos") {
|
2019-11-13 13:51:57 -06:00
|
|
|
fix_path_for_mac().context("Fix path for mac")?
|
2018-09-15 18:02:25 -05:00
|
|
|
}
|
2019-07-27 06:35:18 -05:00
|
|
|
if let Some(server) = opts.server {
|
2019-11-13 13:51:57 -06:00
|
|
|
install_server(server).context("install server")?;
|
2019-03-05 15:19:36 -06:00
|
|
|
}
|
2019-08-07 05:12:23 -05:00
|
|
|
if let Some(client) = opts.client {
|
2019-11-13 13:51:57 -06:00
|
|
|
install_client(client).context("install client")?;
|
2019-08-07 05:12:23 -05:00
|
|
|
}
|
2018-09-15 18:12:53 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-18 12:27:11 -05:00
|
|
|
|
2019-03-18 14:18:54 -05:00
|
|
|
fn fix_path_for_mac() -> Result<()> {
|
|
|
|
let mut vscode_path: Vec<PathBuf> = {
|
2019-03-18 12:27:11 -05:00
|
|
|
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-11-13 13:51:57 -06:00
|
|
|
Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
|
2019-03-18 12:27:11 -05:00
|
|
|
};
|
|
|
|
|
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 12:27:11 -05:00
|
|
|
|
2019-03-18 14:18:54 -05:00
|
|
|
if !vscode_path.is_empty() {
|
|
|
|
let vars = match env::var_os("PATH") {
|
|
|
|
Some(path) => path,
|
2019-11-13 13:51:57 -06:00
|
|
|
None => anyhow::bail!("Could not get PATH variable from env."),
|
2019-03-18 14:18:54 -05:00
|
|
|
};
|
2019-03-18 12:27:11 -05:00
|
|
|
|
2019-03-18 14:18:54 -05:00
|
|
|
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
|
|
|
|
paths.append(&mut vscode_path);
|
2019-11-13 13:51:57 -06:00
|
|
|
let new_paths = env::join_paths(paths).context("build env PATH")?;
|
2019-03-18 14:18:54 -05:00
|
|
|
env::set_var("PATH", &new_paths);
|
2019-03-18 12:27:11 -05:00
|
|
|
}
|
2019-03-18 14:18:54 -05:00
|
|
|
|
|
|
|
Ok(())
|
2019-03-18 12:27:11 -05:00
|
|
|
}
|
2019-07-27 06:35:18 -05:00
|
|
|
|
|
|
|
fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
|
2019-10-26 09:12:56 -05:00
|
|
|
let npm_version = Cmd {
|
|
|
|
unix: r"npm --version",
|
|
|
|
windows: r"cmd.exe /c npm.cmd --version",
|
|
|
|
work_dir: "./editors/code",
|
|
|
|
}
|
|
|
|
.run();
|
|
|
|
|
|
|
|
if npm_version.is_err() {
|
|
|
|
eprintln!("\nERROR: `npm --version` failed, `npm` is required to build the VS Code plugin")
|
|
|
|
}
|
|
|
|
|
2019-07-27 06:35:18 -05:00
|
|
|
Cmd { unix: r"npm ci", windows: r"cmd.exe /c npm.cmd ci", work_dir: "./editors/code" }.run()?;
|
2019-07-27 15:02:03 -05:00
|
|
|
Cmd {
|
2019-11-11 13:51:07 -06:00
|
|
|
unix: r"npm run package --scripts-prepend-node-path",
|
2019-07-27 15:02:03 -05:00
|
|
|
windows: r"cmd.exe /c npm.cmd run package",
|
|
|
|
work_dir: "./editors/code",
|
|
|
|
}
|
|
|
|
.run()?;
|
2019-07-27 06:35:18 -05:00
|
|
|
|
2019-09-21 03:24:41 -05:00
|
|
|
let code_binary = ["code", "code-insiders", "codium"].iter().find(|bin| {
|
2019-09-18 06:24:20 -05:00
|
|
|
Cmd {
|
|
|
|
unix: &format!("{} --version", bin),
|
|
|
|
windows: &format!("cmd.exe /c {}.cmd --version", bin),
|
|
|
|
work_dir: "./editors/code",
|
|
|
|
}
|
|
|
|
.run()
|
|
|
|
.is_ok()
|
|
|
|
});
|
|
|
|
|
|
|
|
let code_binary = match code_binary {
|
|
|
|
Some(it) => it,
|
2019-11-13 13:51:57 -06:00
|
|
|
None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
|
2019-09-18 06:24:20 -05:00
|
|
|
};
|
2019-07-27 06:35:18 -05:00
|
|
|
|
|
|
|
Cmd {
|
2019-09-18 06:24:20 -05:00
|
|
|
unix: &format!(r"{} --install-extension ./ra-lsp-0.0.1.vsix --force", code_binary),
|
|
|
|
windows: &format!(
|
|
|
|
r"cmd.exe /c {}.cmd --install-extension ./ra-lsp-0.0.1.vsix --force",
|
|
|
|
code_binary
|
|
|
|
),
|
2019-07-27 06:35:18 -05:00
|
|
|
work_dir: "./editors/code",
|
|
|
|
}
|
|
|
|
.run()?;
|
|
|
|
|
|
|
|
let output = Cmd {
|
2019-09-18 06:24:20 -05:00
|
|
|
unix: &format!(r"{} --list-extensions", code_binary),
|
|
|
|
windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
|
2019-07-27 06:35:18 -05:00
|
|
|
work_dir: ".",
|
|
|
|
}
|
|
|
|
.run_with_output()?;
|
|
|
|
|
|
|
|
if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
|
2019-11-13 13:51:57 -06:00
|
|
|
anyhow::bail!(
|
|
|
|
"Could not install the Visual Studio Code extension. \
|
|
|
|
Please make sure you have at least NodeJS 10.x installed and try again."
|
|
|
|
);
|
2019-07-27 06:35:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install_server(opts: ServerOpt) -> Result<()> {
|
2019-11-14 02:11:32 -06:00
|
|
|
let mut old_rust = false;
|
|
|
|
if let Ok(output) = run_with_output("cargo --version", ".") {
|
|
|
|
if let Ok(stdout) = String::from_utf8(output.stdout) {
|
|
|
|
println!("{}", stdout);
|
|
|
|
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
|
|
|
|
old_rust = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 05:40:40 -05:00
|
|
|
|
|
|
|
if old_rust {
|
|
|
|
eprintln!(
|
2019-11-14 02:11:32 -06:00
|
|
|
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
|
|
|
|
REQUIRED_RUST_VERSION,
|
2019-10-21 05:40:40 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = if opts.jemalloc {
|
2019-08-20 11:27:33 -05:00
|
|
|
run("cargo install --path crates/ra_lsp_server --locked --force --features jemalloc", ".")
|
2019-07-27 06:35:18 -05:00
|
|
|
} else {
|
2019-08-20 11:27:33 -05:00
|
|
|
run("cargo install --path crates/ra_lsp_server --locked --force", ".")
|
2019-10-21 05:40:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if res.is_err() && old_rust {
|
|
|
|
eprintln!(
|
2019-11-14 02:11:32 -06:00
|
|
|
"\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
|
|
|
|
REQUIRED_RUST_VERSION,
|
2019-10-21 05:40:40 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2019-11-14 02:11:32 -06:00
|
|
|
|
|
|
|
fn check_version(version_output: &str, min_minor_version: u32) -> bool {
|
|
|
|
// Parse second the number out of
|
|
|
|
// cargo 1.39.0-beta (1c6ec66d5 2019-09-30)
|
|
|
|
let minor: Option<u32> = version_output.split('.').nth(1).and_then(|it| it.parse().ok());
|
|
|
|
match minor {
|
|
|
|
None => true,
|
|
|
|
Some(minor) => minor >= min_minor_version,
|
|
|
|
}
|
|
|
|
}
|