1 // Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Inspired by Paul Woolcock's cargo-fmt (https://github.com/pwoolcoc/cargo-fmt/)
16 extern crate cargo_metadata;
18 extern crate serde_json as json;
22 use std::hash::{Hash, Hasher};
23 use std::io::{self, Write};
24 use std::path::{Path, PathBuf};
25 use std::process::{Command, ExitStatus};
27 use std::collections::HashSet;
28 use std::iter::FromIterator;
30 use getopts::{Matches, Options};
33 let exit_status = execute();
34 std::io::stdout().flush().unwrap();
35 std::process::exit(exit_status);
42 let mut opts = getopts::Options::new();
43 opts.optflag("h", "help", "show this message");
44 opts.optflag("q", "quiet", "no output printed to stdout");
45 opts.optflag("v", "verbose", "use verbose output");
49 "specify package to format (only usable in workspaces)",
52 opts.optflag("", "all", "format all packages (only usable in workspaces)");
54 // If there is any invalid argument passed to `cargo fmt`, return without formatting.
55 if let Some(arg) = env::args()
57 .take_while(|a| a != "--")
58 .find(|a| !a.starts_with('-'))
60 print_usage_to_stderr(&opts, &format!("Invalid argument: `{}`.", arg));
64 let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
67 print_usage_to_stderr(&opts, &e.to_string());
72 let verbosity = match (matches.opt_present("v"), matches.opt_present("q")) {
73 (false, false) => Verbosity::Normal,
74 (false, true) => Verbosity::Quiet,
75 (true, false) => Verbosity::Verbose,
77 print_usage_to_stderr(&opts, "quiet mode and verbose mode are not compatible");
82 if matches.opt_present("h") {
83 print_usage_to_stdout(&opts, "");
87 let strategy = CargoFmtStrategy::from_matches(&matches);
89 match format_crate(verbosity, &strategy) {
91 print_usage_to_stderr(&opts, &e.to_string());
94 Ok(status) => if status.success() {
97 status.code().unwrap_or(failure)
102 macro_rules! print_usage {
103 ($print:ident, $opts:ident, $reason:expr) => ({
104 let msg = format!("{}\nusage: cargo fmt [options]", $reason);
106 "{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \
107 Arguments after `--` are passed to rustfmt.",
113 fn print_usage_to_stdout(opts: &Options, reason: &str) {
114 print_usage!(println, opts, reason);
117 fn print_usage_to_stderr(opts: &Options, reason: &str) {
118 print_usage!(eprintln, opts, reason);
121 #[derive(Debug, Clone, Copy, PartialEq)]
129 verbosity: Verbosity,
130 strategy: &CargoFmtStrategy,
131 ) -> Result<ExitStatus, io::Error> {
132 let targets = get_targets(strategy)?;
134 // Currently only bin and lib files get formatted
135 let files: Vec<_> = targets
137 .filter(|t| t.kind.should_format())
139 if verbosity == Verbosity::Verbose {
140 println!("[{:?}] {:?}", t.kind, t.path)
146 format_files(&files, &get_fmt_args(), verbosity)
149 fn get_fmt_args() -> Vec<String> {
150 // All arguments after -- are passed to rustfmt
151 env::args().skip_while(|a| a != "--").skip(1).collect()
156 Lib, // dylib, staticlib, lib
158 Example, // example file
161 CustomBuild, // build script
162 ProcMacro, // a proc macro implementation
167 fn should_format(&self) -> bool {
171 | TargetKind::Example
174 | TargetKind::CustomBuild
175 | TargetKind::ProcMacro => true,
180 fn from_str(s: &str) -> Self {
182 "bin" => TargetKind::Bin,
183 "lib" | "dylib" | "staticlib" | "cdylib" | "rlib" => TargetKind::Lib,
184 "test" => TargetKind::Test,
185 "example" => TargetKind::Example,
186 "bench" => TargetKind::Bench,
187 "custom-build" => TargetKind::CustomBuild,
188 "proc-macro" => TargetKind::ProcMacro,
189 _ => TargetKind::Other,
194 /// Target uses a `path` field for equality and hashing.
197 /// A path to the main source file of the target.
199 /// A kind of target (e.g. lib, bin, example, ...).
204 pub fn from_target(target: &cargo_metadata::Target) -> Self {
205 let path = PathBuf::from(&target.src_path);
206 let canonicalized = fs::canonicalize(&path).unwrap_or(path);
210 kind: TargetKind::from_str(&target.kind[0]),
215 impl PartialEq for Target {
216 fn eq(&self, other: &Target) -> bool {
217 self.path == other.path
221 impl Eq for Target {}
223 impl Hash for Target {
224 fn hash<H: Hasher>(&self, state: &mut H) {
225 self.path.hash(state);
229 #[derive(Debug, PartialEq, Eq)]
230 pub enum CargoFmtStrategy {
231 /// Format every packages and dependencies.
233 /// Format pacakges that are specified by the command line argument.
235 /// Format the root packages only.
239 impl CargoFmtStrategy {
240 pub fn from_matches(matches: &Matches) -> CargoFmtStrategy {
241 match (matches.opt_present("all"), matches.opt_present("p")) {
242 (false, false) => CargoFmtStrategy::Root,
243 (true, _) => CargoFmtStrategy::All,
244 (false, true) => CargoFmtStrategy::Some(matches.opt_strs("p")),
249 /// Based on the specified CargoFmtStrategy, returns a set of main source files.
250 fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error> {
251 let mut targets = HashSet::new();
254 CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?,
255 CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut HashSet::new())?,
256 CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
259 if targets.is_empty() {
261 io::ErrorKind::Other,
262 format!("Failed to find targets"),
269 fn get_targets_root_only(targets: &mut HashSet<Target>) -> Result<(), io::Error> {
270 let metadata = get_cargo_metadata(None)?;
272 for package in metadata.packages {
273 for target in package.targets {
274 if target.name == package.name {
275 targets.insert(Target::from_target(&target));
283 fn get_targets_recursive(
284 manifest_path: Option<&Path>,
285 mut targets: &mut HashSet<Target>,
286 visited: &mut HashSet<String>,
287 ) -> Result<(), io::Error> {
288 let metadata = get_cargo_metadata(manifest_path)?;
290 for package in metadata.packages {
291 add_targets(&package.targets, &mut targets);
293 // Look for local dependencies.
294 for dependency in package.dependencies {
295 if dependency.source.is_some() || visited.contains(&dependency.name) {
299 let mut manifest_path = PathBuf::from(&package.manifest_path);
302 manifest_path.push(&dependency.name);
303 manifest_path.push("Cargo.toml");
305 if manifest_path.exists() {
306 visited.insert(dependency.name);
307 get_targets_recursive(Some(&manifest_path), &mut targets, visited)?;
315 fn get_targets_with_hitlist(
317 targets: &mut HashSet<Target>,
318 ) -> Result<(), io::Error> {
319 let metadata = get_cargo_metadata(None)?;
321 let mut workspace_hitlist: HashSet<&String> = HashSet::from_iter(hitlist);
323 for package in metadata.packages {
324 for target in package.targets {
325 if workspace_hitlist.remove(&target.name) {
326 targets.insert(Target::from_target(&target));
331 if workspace_hitlist.is_empty() {
334 let package = workspace_hitlist.iter().next().unwrap();
336 io::ErrorKind::InvalidInput,
337 format!("package `{}` is not a member of the workspace", package),
342 fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet<Target>) {
343 for target in target_paths {
344 targets.insert(Target::from_target(&target));
351 verbosity: Verbosity,
352 ) -> Result<ExitStatus, io::Error> {
353 let stdout = if verbosity == Verbosity::Quiet {
354 std::process::Stdio::null()
356 std::process::Stdio::inherit()
359 if verbosity == Verbosity::Verbose {
365 print!(" {}", f.display());
370 let mut command = Command::new("rustfmt")
375 .map_err(|e| match e.kind() {
376 io::ErrorKind::NotFound => io::Error::new(
377 io::ErrorKind::Other,
378 "Could not run rustfmt, please make sure it is in your PATH.",
386 fn get_cargo_metadata(manifest_path: Option<&Path>) -> Result<cargo_metadata::Metadata, io::Error> {
387 match cargo_metadata::metadata(manifest_path) {
388 Ok(metadata) => Ok(metadata),
389 Err(..) => Err(io::Error::new(
390 io::ErrorKind::Other,
391 "`cargo manifest` failed.",