2020-03-31 13:09:11 +00:00
|
|
|
use crate::clippy_project_root;
|
2019-06-25 04:43:38 +00:00
|
|
|
use shell_escape::escape;
|
|
|
|
use std::ffi::OsStr;
|
2020-01-30 20:29:21 +00:00
|
|
|
use std::path::Path;
|
2019-06-25 04:43:38 +00:00
|
|
|
use std::process::{self, Command};
|
2020-12-13 14:49:48 +00:00
|
|
|
use std::{fs, io};
|
2019-06-25 04:43:38 +00:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CliError {
|
2021-01-31 10:17:37 +00:00
|
|
|
CommandFailed(String, String),
|
2019-06-25 04:43:38 +00:00
|
|
|
IoError(io::Error),
|
2019-07-28 04:41:13 +00:00
|
|
|
RustfmtNotInstalled,
|
2019-06-25 04:43:38 +00:00
|
|
|
WalkDirError(walkdir::Error),
|
2020-12-13 14:49:48 +00:00
|
|
|
RaSetupActive,
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for CliError {
|
|
|
|
fn from(error: io::Error) -> Self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::IoError(error)
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<walkdir::Error> for CliError {
|
|
|
|
fn from(error: walkdir::Error) -> Self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::WalkDirError(error)
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FmtContext {
|
|
|
|
check: bool,
|
|
|
|
verbose: bool,
|
|
|
|
}
|
|
|
|
|
2020-12-13 14:49:48 +00:00
|
|
|
// the "main" function of cargo dev fmt
|
2019-06-25 04:43:38 +00:00
|
|
|
pub fn run(check: bool, verbose: bool) {
|
|
|
|
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
|
|
|
|
let mut success = true;
|
|
|
|
|
2020-01-30 20:29:21 +00:00
|
|
|
let project_root = clippy_project_root();
|
2019-06-25 04:43:38 +00:00
|
|
|
|
2020-12-13 14:49:48 +00:00
|
|
|
// if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
|
|
|
|
// format because rustfmt would also format the entire rustc repo as it is a local
|
|
|
|
// dependency
|
|
|
|
if fs::read_to_string(project_root.join("Cargo.toml"))
|
|
|
|
.expect("Failed to read clippy Cargo.toml")
|
|
|
|
.contains(&"[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
|
|
|
|
{
|
|
|
|
return Err(CliError::RaSetupActive);
|
|
|
|
}
|
|
|
|
|
2019-07-28 04:41:13 +00:00
|
|
|
rustfmt_test(context)?;
|
|
|
|
|
2019-06-25 04:43:38 +00:00
|
|
|
success &= cargo_fmt(context, project_root.as_path())?;
|
|
|
|
success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
|
|
|
|
success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
|
2021-03-11 14:18:56 +00:00
|
|
|
success &= cargo_fmt(context, &project_root.join("lintcheck"))?;
|
2019-06-25 04:43:38 +00:00
|
|
|
|
|
|
|
for entry in WalkDir::new(project_root.join("tests")) {
|
|
|
|
let entry = entry?;
|
|
|
|
let path = entry.path();
|
|
|
|
|
2021-06-21 15:50:36 +00:00
|
|
|
if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" {
|
2019-06-25 04:43:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:35:32 +00:00
|
|
|
success &= rustfmt(context, path)?;
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_err(err: CliError) {
|
|
|
|
match err {
|
2021-01-31 10:17:37 +00:00
|
|
|
CliError::CommandFailed(command, stderr) => {
|
|
|
|
eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
|
2019-06-25 04:43:38 +00:00
|
|
|
},
|
|
|
|
CliError::IoError(err) => {
|
|
|
|
eprintln!("error: {}", err);
|
|
|
|
},
|
2019-07-28 04:41:13 +00:00
|
|
|
CliError::RustfmtNotInstalled => {
|
|
|
|
eprintln!("error: rustfmt nightly is not installed.");
|
|
|
|
},
|
2019-06-25 04:43:38 +00:00
|
|
|
CliError::WalkDirError(err) => {
|
|
|
|
eprintln!("error: {}", err);
|
|
|
|
},
|
2020-12-13 14:49:48 +00:00
|
|
|
CliError::RaSetupActive => {
|
|
|
|
eprintln!(
|
2021-03-17 09:12:20 +00:00
|
|
|
"error: a local rustc repo is enabled as path dependency via `cargo dev ide_setup`.
|
2020-12-13 14:49:48 +00:00
|
|
|
Not formatting because that would format the local repo as well!
|
|
|
|
Please revert the changes to Cargo.tomls first."
|
|
|
|
);
|
|
|
|
},
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let context = FmtContext { check, verbose };
|
|
|
|
let result = try_run(&context);
|
|
|
|
let code = match result {
|
|
|
|
Ok(true) => 0,
|
|
|
|
Ok(false) => {
|
|
|
|
eprintln!();
|
|
|
|
eprintln!("Formatting check failed.");
|
2020-01-30 07:33:48 +00:00
|
|
|
eprintln!("Run `cargo dev fmt` to update formatting.");
|
2019-06-25 04:43:38 +00:00
|
|
|
1
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
output_err(err);
|
|
|
|
1
|
|
|
|
},
|
|
|
|
};
|
|
|
|
process::exit(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
|
2019-09-16 15:50:36 +00:00
|
|
|
let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
|
2019-06-25 04:43:38 +00:00
|
|
|
|
|
|
|
format!(
|
|
|
|
"cd {} && {} {}",
|
|
|
|
escape(dir.as_ref().to_string_lossy()),
|
|
|
|
escape(program.as_ref().to_string_lossy()),
|
|
|
|
arg_display.join(" ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec(
|
|
|
|
context: &FmtContext,
|
|
|
|
program: impl AsRef<OsStr>,
|
|
|
|
dir: impl AsRef<Path>,
|
|
|
|
args: &[impl AsRef<OsStr>],
|
|
|
|
) -> Result<bool, CliError> {
|
|
|
|
if context.verbose {
|
|
|
|
println!("{}", format_command(&program, &dir, args));
|
|
|
|
}
|
|
|
|
|
2021-01-31 10:17:37 +00:00
|
|
|
let child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
|
|
|
|
let output = child.wait_with_output()?;
|
|
|
|
let success = output.status.success();
|
2019-06-25 04:43:38 +00:00
|
|
|
|
|
|
|
if !context.check && !success {
|
2021-01-31 10:17:37 +00:00
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
|
|
|
|
return Err(CliError::CommandFailed(
|
|
|
|
format_command(&program, &dir, args),
|
|
|
|
String::from(stderr),
|
|
|
|
));
|
2019-06-25 04:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
|
|
|
|
let mut args = vec!["+nightly", "fmt", "--all"];
|
|
|
|
if context.check {
|
|
|
|
args.push("--");
|
|
|
|
args.push("--check");
|
|
|
|
}
|
2019-10-08 05:20:24 +00:00
|
|
|
let success = exec(context, "cargo", path, &args)?;
|
2019-06-25 04:43:38 +00:00
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
|
2019-07-28 04:41:13 +00:00
|
|
|
fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
|
2019-10-08 05:20:24 +00:00
|
|
|
let program = "rustfmt";
|
2019-07-28 04:41:13 +00:00
|
|
|
let dir = std::env::current_dir()?;
|
|
|
|
let args = &["+nightly", "--version"];
|
|
|
|
|
|
|
|
if context.verbose {
|
|
|
|
println!("{}", format_command(&program, &dir, args));
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
|
|
|
|
|
|
|
|
if output.status.success() {
|
|
|
|
Ok(())
|
|
|
|
} else if std::str::from_utf8(&output.stderr)
|
|
|
|
.unwrap_or("")
|
|
|
|
.starts_with("error: 'rustfmt' is not installed")
|
|
|
|
{
|
|
|
|
Err(CliError::RustfmtNotInstalled)
|
|
|
|
} else {
|
2021-01-31 10:17:37 +00:00
|
|
|
Err(CliError::CommandFailed(
|
|
|
|
format_command(&program, &dir, args),
|
|
|
|
std::str::from_utf8(&output.stderr).unwrap_or("").to_string(),
|
|
|
|
))
|
2019-07-28 04:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:43:38 +00:00
|
|
|
fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
|
|
|
|
let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
|
|
|
|
if context.check {
|
|
|
|
args.push("--check".as_ref());
|
|
|
|
}
|
2019-10-08 05:20:24 +00:00
|
|
|
let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
|
2019-06-25 04:43:38 +00:00
|
|
|
if !success {
|
|
|
|
eprintln!("rustfmt failed on {}", path.display());
|
|
|
|
}
|
|
|
|
Ok(success)
|
|
|
|
}
|