|
| 1 | +use shell_escape::escape; |
| 2 | +use std::ffi::OsStr; |
| 3 | +use std::io; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | +use std::process::{self, Command}; |
| 6 | +use walkdir::WalkDir; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub enum CliError { |
| 10 | + CommandFailed(String), |
| 11 | + IoError(io::Error), |
| 12 | + ProjectRootNotFound, |
| 13 | + WalkDirError(walkdir::Error), |
| 14 | +} |
| 15 | + |
| 16 | +impl From<io::Error> for CliError { |
| 17 | + fn from(error: io::Error) -> Self { |
| 18 | + CliError::IoError(error) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl From<walkdir::Error> for CliError { |
| 23 | + fn from(error: walkdir::Error) -> Self { |
| 24 | + CliError::WalkDirError(error) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +struct FmtContext { |
| 29 | + check: bool, |
| 30 | + verbose: bool, |
| 31 | +} |
| 32 | + |
| 33 | +pub fn run(check: bool, verbose: bool) { |
| 34 | + fn try_run(context: &FmtContext) -> Result<bool, CliError> { |
| 35 | + let mut success = true; |
| 36 | + |
| 37 | + let project_root = project_root()?; |
| 38 | + |
| 39 | + success &= cargo_fmt(context, project_root.as_path())?; |
| 40 | + success &= cargo_fmt(context, &project_root.join("clippy_dev"))?; |
| 41 | + success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?; |
| 42 | + |
| 43 | + for entry in WalkDir::new(project_root.join("tests")) { |
| 44 | + let entry = entry?; |
| 45 | + let path = entry.path(); |
| 46 | + |
| 47 | + if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + success &= rustfmt(context, &path)?; |
| 52 | + } |
| 53 | + |
| 54 | + Ok(success) |
| 55 | + } |
| 56 | + |
| 57 | + fn output_err(err: CliError) { |
| 58 | + match err { |
| 59 | + CliError::CommandFailed(command) => { |
| 60 | + eprintln!("error: A command failed! `{}`", command); |
| 61 | + }, |
| 62 | + CliError::IoError(err) => { |
| 63 | + eprintln!("error: {}", err); |
| 64 | + }, |
| 65 | + CliError::ProjectRootNotFound => { |
| 66 | + eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir."); |
| 67 | + }, |
| 68 | + CliError::WalkDirError(err) => { |
| 69 | + eprintln!("error: {}", err); |
| 70 | + }, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + let context = FmtContext { check, verbose }; |
| 75 | + let result = try_run(&context); |
| 76 | + let code = match result { |
| 77 | + Ok(true) => 0, |
| 78 | + Ok(false) => { |
| 79 | + eprintln!(); |
| 80 | + eprintln!("Formatting check failed."); |
| 81 | + eprintln!("Run `./util/dev fmt` to update formatting."); |
| 82 | + 1 |
| 83 | + }, |
| 84 | + Err(err) => { |
| 85 | + output_err(err); |
| 86 | + 1 |
| 87 | + }, |
| 88 | + }; |
| 89 | + process::exit(code); |
| 90 | +} |
| 91 | + |
| 92 | +fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String { |
| 93 | + let arg_display: Vec<_> = args |
| 94 | + .iter() |
| 95 | + .map(|a| escape(a.as_ref().to_string_lossy()).to_owned()) |
| 96 | + .collect(); |
| 97 | + |
| 98 | + format!( |
| 99 | + "cd {} && {} {}", |
| 100 | + escape(dir.as_ref().to_string_lossy()), |
| 101 | + escape(program.as_ref().to_string_lossy()), |
| 102 | + arg_display.join(" ") |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +fn exec( |
| 107 | + context: &FmtContext, |
| 108 | + program: impl AsRef<OsStr>, |
| 109 | + dir: impl AsRef<Path>, |
| 110 | + args: &[impl AsRef<OsStr>], |
| 111 | +) -> Result<bool, CliError> { |
| 112 | + if context.verbose { |
| 113 | + println!("{}", format_command(&program, &dir, args)); |
| 114 | + } |
| 115 | + |
| 116 | + let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?; |
| 117 | + let code = child.wait()?; |
| 118 | + let success = code.success(); |
| 119 | + |
| 120 | + if !context.check && !success { |
| 121 | + return Err(CliError::CommandFailed(format_command(&program, &dir, args))); |
| 122 | + } |
| 123 | + |
| 124 | + Ok(success) |
| 125 | +} |
| 126 | + |
| 127 | +fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> { |
| 128 | + let mut args = vec!["+nightly", "fmt", "--all"]; |
| 129 | + if context.check { |
| 130 | + args.push("--"); |
| 131 | + args.push("--check"); |
| 132 | + } |
| 133 | + let success = exec(context, "cargo", path, &args)?; |
| 134 | + |
| 135 | + Ok(success) |
| 136 | +} |
| 137 | + |
| 138 | +fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> { |
| 139 | + let mut args = vec!["+nightly".as_ref(), path.as_os_str()]; |
| 140 | + if context.check { |
| 141 | + args.push("--check".as_ref()); |
| 142 | + } |
| 143 | + let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?; |
| 144 | + if !success { |
| 145 | + eprintln!("rustfmt failed on {}", path.display()); |
| 146 | + } |
| 147 | + Ok(success) |
| 148 | +} |
| 149 | + |
| 150 | +fn project_root() -> Result<PathBuf, CliError> { |
| 151 | + let current_dir = std::env::current_dir()?; |
| 152 | + for path in current_dir.ancestors() { |
| 153 | + let result = std::fs::read_to_string(path.join("Cargo.toml")); |
| 154 | + if let Err(err) = &result { |
| 155 | + if err.kind() == io::ErrorKind::NotFound { |
| 156 | + continue; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + let content = result?; |
| 161 | + if content.contains("[package]\nname = \"clippy\"") { |
| 162 | + return Ok(path.to_path_buf()); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + Err(CliError::ProjectRootNotFound) |
| 167 | +} |
0 commit comments