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/)
17 extern crate serde_json as json;
21 use std::path::PathBuf;
22 use std::process::{Command, ExitStatus};
24 use std::collections::HashSet;
25 use std::iter::FromIterator;
29 use getopts::{Options, Matches};
32 let exit_status = execute();
33 std::io::stdout().flush().unwrap();
34 std::process::exit(exit_status);
41 let mut opts = getopts::Options::new();
42 opts.optflag("h", "help", "show this message");
43 opts.optflag("q", "quiet", "no output printed to stdout");
44 opts.optflag("v", "verbose", "use verbose output");
48 "specify package to format (only usable in workspaces)",
51 opts.optflag("", "all", "format all packages (only usable in workspaces)");
53 // If there is any invalid argument passed to `cargo fmt`, return without formatting.
54 if let Some(arg) = env::args()
56 .take_while(|a| a != "--")
57 .find(|a| !a.starts_with('-'))
59 print_usage(&opts, &format!("Invalid argument: `{}`.", arg));
63 let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
66 print_usage(&opts, &e.to_string());
71 let verbosity = match (matches.opt_present("v"), matches.opt_present("q")) {
72 (false, false) => Verbosity::Normal,
73 (false, true) => Verbosity::Quiet,
74 (true, false) => Verbosity::Verbose,
76 print_usage(&opts, "quiet mode and verbose mode are not compatible");
81 if matches.opt_present("h") {
82 print_usage(&opts, "");
86 let workspace_hitlist = WorkspaceHitlist::from_matches(&matches);
88 match format_crate(verbosity, workspace_hitlist) {
90 print_usage(&opts, &e.to_string());
97 status.code().unwrap_or(failure)
103 fn print_usage(opts: &Options, reason: &str) {
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.",
112 #[derive(Debug, Clone, Copy, PartialEq)]
120 verbosity: Verbosity,
121 workspace_hitlist: WorkspaceHitlist,
122 ) -> Result<ExitStatus, std::io::Error> {
123 let targets = get_targets(workspace_hitlist)?;
125 // Currently only bin and lib files get formatted
126 let files: Vec<_> = targets
128 .filter(|t| t.kind.should_format())
129 .inspect(|t| if verbosity == Verbosity::Verbose {
130 println!("[{:?}] {:?}", t.kind, t.path)
135 format_files(&files, &get_fmt_args(), verbosity)
138 fn get_fmt_args() -> Vec<String> {
139 // All arguments after -- are passed to rustfmt
140 env::args().skip_while(|a| a != "--").skip(1).collect()
145 Lib, // dylib, staticlib, lib
147 Example, // example file
150 CustomBuild, // build script
151 ProcMacro, // a proc macro implementation
156 fn should_format(&self) -> bool {
160 TargetKind::Example |
163 TargetKind::CustomBuild |
164 TargetKind::ProcMacro => true,
176 #[derive(Debug, PartialEq, Eq)]
177 pub enum WorkspaceHitlist {
183 impl WorkspaceHitlist {
184 pub fn get_some<'a>(&'a self) -> Option<&'a [String]> {
185 if let &WorkspaceHitlist::Some(ref hitlist) = self {
192 pub fn from_matches(matches: &Matches) -> WorkspaceHitlist {
193 match (matches.opt_present("all"), matches.opt_present("p")) {
194 (false, false) => WorkspaceHitlist::None,
195 (true, _) => WorkspaceHitlist::All,
196 (false, true) => WorkspaceHitlist::Some(matches.opt_strs("p")),
201 // Returns a vector of all compile targets of a crate
202 fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::io::Error> {
203 let mut targets: Vec<Target> = vec![];
204 if workspace_hitlist == WorkspaceHitlist::None {
205 let output = Command::new("cargo").arg("read-manifest").output()?;
206 if output.status.success() {
207 // None of the unwraps should fail if output of `cargo read-manifest` is correct
208 let data = &String::from_utf8(output.stdout).unwrap();
209 let json: Value = json::from_str(data).unwrap();
210 let json_obj = json.as_object().unwrap();
211 let jtargets = json_obj.get("targets").unwrap().as_array().unwrap();
212 for jtarget in jtargets {
213 targets.push(target_from_json(jtarget));
218 return Err(std::io::Error::new(
219 std::io::ErrorKind::NotFound,
220 str::from_utf8(&output.stderr).unwrap(),
223 // This happens when cargo-fmt is not used inside a crate or
224 // is used inside a workspace.
225 // To ensure backward compatability, we only use `cargo metadata` for workspaces.
226 // TODO: Is it possible only use metadata or read-manifest
227 let output = Command::new("cargo")
231 if output.status.success() {
232 let data = &String::from_utf8(output.stdout).unwrap();
233 let json: Value = json::from_str(data).unwrap();
234 let json_obj = json.as_object().unwrap();
235 let mut hitlist: HashSet<&String> = if workspace_hitlist != WorkspaceHitlist::All {
236 HashSet::from_iter(workspace_hitlist.get_some().unwrap())
238 HashSet::new() // Unused
240 let members: Vec<&Value> = json_obj
246 .filter(|member| if workspace_hitlist == WorkspaceHitlist::All {
249 let member_obj = member.as_object().unwrap();
250 let member_name = member_obj.get("name").unwrap().as_str().unwrap();
251 hitlist.take(&member_name.to_string()).is_some()
254 if hitlist.len() != 0 {
255 // Mimick cargo of only outputting one <package> spec.
256 return Err(std::io::Error::new(
257 std::io::ErrorKind::InvalidInput,
259 "package `{}` is not a member of the workspace",
260 hitlist.iter().next().unwrap()
264 for member in members {
265 let member_obj = member.as_object().unwrap();
266 let jtargets = member_obj.get("targets").unwrap().as_array().unwrap();
267 for jtarget in jtargets {
268 targets.push(target_from_json(jtarget));
273 Err(std::io::Error::new(
274 std::io::ErrorKind::NotFound,
275 str::from_utf8(&output.stderr).unwrap(),
280 fn target_from_json(jtarget: &Value) -> Target {
281 let jtarget = jtarget.as_object().unwrap();
282 let path = PathBuf::from(jtarget.get("src_path").unwrap().as_str().unwrap());
283 let kinds = jtarget.get("kind").unwrap().as_array().unwrap();
284 let kind = match kinds[0].as_str().unwrap() {
285 "bin" => TargetKind::Bin,
286 "lib" | "dylib" | "staticlib" | "cdylib" | "rlib" => TargetKind::Lib,
287 "test" => TargetKind::Test,
288 "example" => TargetKind::Example,
289 "bench" => TargetKind::Bench,
290 "custom-build" => TargetKind::CustomBuild,
291 "proc-macro" => TargetKind::ProcMacro,
292 _ => TargetKind::Other,
304 verbosity: Verbosity,
305 ) -> Result<ExitStatus, std::io::Error> {
306 let stdout = if verbosity == Verbosity::Quiet {
307 std::process::Stdio::null()
309 std::process::Stdio::inherit()
311 if verbosity == Verbosity::Verbose {
313 for a in fmt_args.iter() {
316 for f in files.iter() {
317 print!(" {}", f.display());
321 let mut command = Command::new("rustfmt")
326 .map_err(|e| match e.kind() {
327 std::io::ErrorKind::NotFound => {
329 std::io::ErrorKind::Other,
330 "Could not run rustfmt, please make sure it is in your PATH.",