2020-02-27 04:07:26 -06:00
|
|
|
//! Support library for `cargo xtask` command.
|
|
|
|
//!
|
|
|
|
//! See https://github.com/matklad/cargo-xtask/
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-02-14 08:59:19 -06:00
|
|
|
pub mod not_bash;
|
2020-01-07 07:42:56 -06:00
|
|
|
pub mod install;
|
2020-03-04 11:36:16 -06:00
|
|
|
pub mod dist;
|
2020-01-07 07:42:56 -06:00
|
|
|
pub mod pre_commit;
|
2020-01-07 08:33:51 -06:00
|
|
|
|
|
|
|
pub mod codegen;
|
2020-01-03 13:37:02 -06:00
|
|
|
mod ast_src;
|
2019-08-18 13:33:31 -05:00
|
|
|
|
2019-11-13 13:51:57 -06:00
|
|
|
use anyhow::Context;
|
2018-10-14 09:32:57 -05:00
|
|
|
use std::{
|
2020-02-14 12:03:45 -06:00
|
|
|
env,
|
2020-01-10 04:23:11 -06:00
|
|
|
io::Write,
|
2018-10-15 13:54:27 -05:00
|
|
|
path::{Path, PathBuf},
|
2020-01-07 08:33:09 -06:00
|
|
|
process::{Command, Stdio},
|
2018-10-14 09:32:57 -05:00
|
|
|
};
|
2020-03-21 10:04:28 -05:00
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2018-10-14 09:32:57 -05:00
|
|
|
|
2020-02-10 08:32:03 -06:00
|
|
|
use crate::{
|
|
|
|
codegen::Mode,
|
2020-04-08 04:47:40 -05:00
|
|
|
not_bash::{date_iso, fs2, pushd, rm_rf, run},
|
2020-02-10 08:32:03 -06:00
|
|
|
};
|
2019-08-18 13:34:55 -05:00
|
|
|
|
2020-01-07 08:33:09 -06:00
|
|
|
pub use anyhow::Result;
|
|
|
|
|
2019-01-24 06:58:38 -06:00
|
|
|
const TOOLCHAIN: &str = "stable";
|
2018-10-15 13:54:27 -05:00
|
|
|
|
|
|
|
pub fn project_root() -> PathBuf {
|
2019-11-01 15:20:44 -05:00
|
|
|
Path::new(
|
|
|
|
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
|
|
|
|
)
|
|
|
|
.ancestors()
|
|
|
|
.nth(1)
|
|
|
|
.unwrap()
|
|
|
|
.to_path_buf()
|
2018-10-15 13:54:27 -05:00
|
|
|
}
|
2018-10-31 14:50:43 -05:00
|
|
|
|
2020-03-21 10:04:28 -05:00
|
|
|
pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> {
|
|
|
|
let iter = WalkDir::new(path);
|
|
|
|
return iter
|
|
|
|
.into_iter()
|
|
|
|
.filter_entry(|e| !is_hidden(e))
|
|
|
|
.map(|e| e.unwrap())
|
|
|
|
.filter(|e| !e.file_type().is_dir())
|
|
|
|
.map(|e| e.into_path())
|
|
|
|
.filter(|path| path.extension().map(|it| it == "rs").unwrap_or(false));
|
|
|
|
|
|
|
|
fn is_hidden(entry: &DirEntry) -> bool {
|
|
|
|
entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 14:50:43 -05:00
|
|
|
pub fn run_rustfmt(mode: Mode) -> Result<()> {
|
2020-03-13 06:28:13 -05:00
|
|
|
let _dir = pushd(project_root());
|
2020-01-10 04:23:11 -06:00
|
|
|
ensure_rustfmt()?;
|
2018-10-31 15:57:38 -05:00
|
|
|
|
2020-03-13 06:28:13 -05:00
|
|
|
let check = if mode == Mode::Verify { "--check" } else { "" };
|
|
|
|
run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?;
|
2018-10-31 14:50:43 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-31 15:57:38 -05:00
|
|
|
|
2020-01-10 04:23:11 -06:00
|
|
|
fn reformat(text: impl std::fmt::Display) -> Result<String> {
|
|
|
|
ensure_rustfmt()?;
|
|
|
|
let mut rustfmt = Command::new("rustup")
|
|
|
|
.args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"])
|
|
|
|
.arg(project_root().join("rustfmt.toml"))
|
2020-04-09 11:03:03 -05:00
|
|
|
.args(&["--config", "fn_single_line=true"])
|
2020-01-10 04:23:11 -06:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
|
|
|
|
let output = rustfmt.wait_with_output()?;
|
|
|
|
let stdout = String::from_utf8(output.stdout)?;
|
2020-02-07 15:35:34 -06:00
|
|
|
let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`";
|
2020-01-10 04:23:11 -06:00
|
|
|
Ok(format!("//! {}\n\n{}", preamble, stdout))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_rustfmt() -> Result<()> {
|
|
|
|
match Command::new("rustup")
|
|
|
|
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.status()
|
|
|
|
{
|
|
|
|
Ok(status) if status.success() => return Ok(()),
|
|
|
|
_ => (),
|
|
|
|
};
|
2020-02-14 08:59:19 -06:00
|
|
|
run!("rustup toolchain install {}", TOOLCHAIN)?;
|
|
|
|
run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?;
|
|
|
|
Ok(())
|
2018-10-31 15:57:38 -05:00
|
|
|
}
|
2018-12-09 04:29:13 -06:00
|
|
|
|
2019-06-03 08:43:06 -05:00
|
|
|
pub fn run_clippy() -> Result<()> {
|
|
|
|
match Command::new("rustup")
|
|
|
|
.args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"])
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.status()
|
|
|
|
{
|
|
|
|
Ok(status) if status.success() => (),
|
2019-11-13 13:51:57 -06:00
|
|
|
_ => install_clippy().context("install clippy")?,
|
2019-06-03 08:43:06 -05:00
|
|
|
};
|
|
|
|
|
2019-06-04 01:24:47 -05:00
|
|
|
let allowed_lints = [
|
|
|
|
"clippy::collapsible_if",
|
2019-06-04 17:05:38 -05:00
|
|
|
"clippy::needless_pass_by_value",
|
|
|
|
"clippy::nonminimal_bool",
|
|
|
|
"clippy::redundant_pattern_matching",
|
2019-06-04 01:24:47 -05:00
|
|
|
];
|
2020-02-14 08:59:19 -06:00
|
|
|
run!(
|
|
|
|
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
|
|
|
|
TOOLCHAIN,
|
|
|
|
allowed_lints.join(" -A ")
|
2019-06-03 08:43:06 -05:00
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-07 08:33:51 -06:00
|
|
|
fn install_clippy() -> Result<()> {
|
2020-02-14 08:59:19 -06:00
|
|
|
run!("rustup toolchain install {}", TOOLCHAIN)?;
|
|
|
|
run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?;
|
|
|
|
Ok(())
|
2019-06-03 08:43:06 -05:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:14:06 -06:00
|
|
|
pub fn run_fuzzer() -> Result<()> {
|
2020-02-14 08:59:19 -06:00
|
|
|
let _d = pushd("./crates/ra_syntax");
|
2020-02-14 12:13:26 -06:00
|
|
|
if run!("cargo fuzz --help").is_err() {
|
|
|
|
run!("cargo install cargo-fuzz")?;
|
2018-12-31 07:14:06 -06:00
|
|
|
};
|
|
|
|
|
2020-02-14 08:59:19 -06:00
|
|
|
run!("rustup run nightly -- cargo fuzz run parser")?;
|
|
|
|
Ok(())
|
2018-12-31 07:14:06 -06:00
|
|
|
}
|
2020-01-07 09:01:41 -06:00
|
|
|
|
|
|
|
/// Cleans the `./target` dir after the build such that only
|
|
|
|
/// dependencies are cached on CI.
|
|
|
|
pub fn run_pre_cache() -> Result<()> {
|
|
|
|
let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
|
|
|
|
if !slow_tests_cookie.exists() {
|
|
|
|
panic!("slow tests were skipped on CI!")
|
|
|
|
}
|
|
|
|
rm_rf(slow_tests_cookie)?;
|
|
|
|
|
|
|
|
for entry in Path::new("./target/debug").read_dir()? {
|
|
|
|
let entry = entry?;
|
|
|
|
if entry.file_type().map(|it| it.is_file()).ok() == Some(true) {
|
|
|
|
// Can't delete yourself on windows :-(
|
|
|
|
if !entry.path().ends_with("xtask.exe") {
|
|
|
|
rm_rf(&entry.path())?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:03:45 -06:00
|
|
|
fs2::remove_file("./target/.rustc_info.json")?;
|
2020-03-02 22:30:14 -06:00
|
|
|
let to_delete = ["ra_", "heavy_test", "xtask"];
|
2020-01-07 09:01:41 -06:00
|
|
|
for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() {
|
|
|
|
for entry in Path::new(dir).read_dir()? {
|
|
|
|
let entry = entry?;
|
|
|
|
if to_delete.iter().any(|&it| entry.path().display().to_string().contains(it)) {
|
2020-03-02 22:30:14 -06:00
|
|
|
// Can't delete yourself on windows :-(
|
|
|
|
if !entry.path().ends_with("xtask.exe") {
|
|
|
|
rm_rf(&entry.path())?
|
|
|
|
}
|
2020-01-07 09:01:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:33:30 -06:00
|
|
|
pub fn run_release(dry_run: bool) -> Result<()> {
|
|
|
|
if !dry_run {
|
|
|
|
run!("git switch release")?;
|
2020-04-07 04:37:59 -05:00
|
|
|
run!("git fetch upstream --tags --force")?;
|
|
|
|
run!("git reset --hard tags/nightly")?;
|
2020-02-14 11:33:30 -06:00
|
|
|
run!("git push")?;
|
|
|
|
}
|
2020-02-10 08:32:03 -06:00
|
|
|
|
2020-02-14 11:36:42 -06:00
|
|
|
let website_root = project_root().join("../rust-analyzer.github.io");
|
2020-02-14 11:56:07 -06:00
|
|
|
let changelog_dir = website_root.join("./thisweek/_posts");
|
2020-02-10 08:32:03 -06:00
|
|
|
|
2020-04-08 04:47:40 -05:00
|
|
|
let today = date_iso()?;
|
2020-02-14 08:59:19 -06:00
|
|
|
let commit = run!("git rev-parse HEAD")?;
|
2020-02-14 11:56:07 -06:00
|
|
|
let changelog_n = fs2::read_dir(changelog_dir.as_path())?.count();
|
2020-02-10 08:32:03 -06:00
|
|
|
|
|
|
|
let contents = format!(
|
|
|
|
"\
|
|
|
|
= Changelog #{}
|
|
|
|
:sectanchors:
|
|
|
|
:page-layout: post
|
|
|
|
|
|
|
|
Commit: commit:{}[] +
|
|
|
|
Release: release:{}[]
|
|
|
|
|
|
|
|
== New Features
|
|
|
|
|
|
|
|
* pr:[] .
|
|
|
|
|
|
|
|
== Fixes
|
|
|
|
|
|
|
|
== Internal Improvements
|
|
|
|
",
|
|
|
|
changelog_n, commit, today
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = changelog_dir.join(format!("{}-changelog-{}.adoc", today, changelog_n));
|
2020-02-14 11:56:07 -06:00
|
|
|
fs2::write(&path, &contents)?;
|
2020-02-10 08:32:03 -06:00
|
|
|
|
2020-02-14 11:56:07 -06:00
|
|
|
fs2::copy(project_root().join("./docs/user/readme.adoc"), website_root.join("manual.adoc"))?;
|
2020-02-14 11:36:42 -06:00
|
|
|
|
2020-03-02 07:54:55 -06:00
|
|
|
let tags = run!("git tag --list"; echo = false)?;
|
|
|
|
let prev_tag = tags.lines().filter(|line| is_release_tag(line)).last().unwrap();
|
|
|
|
|
|
|
|
println!("\n git log {}..HEAD --merges --reverse", prev_tag);
|
|
|
|
|
2020-02-10 08:32:03 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-02 07:54:55 -06:00
|
|
|
|
|
|
|
fn is_release_tag(tag: &str) -> bool {
|
|
|
|
tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
|
|
|
|
}
|