-
Notifications
You must be signed in to change notification settings - Fork 901
/
Copy pathcargo-fmt.rs
215 lines (189 loc) · 6.27 KB
/
cargo-fmt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Inspired by Paul Woolcock's cargo-fmt (https://github.com/pwoolcoc/cargo-fmt/)
#![cfg(not(test))]
#![deny(warnings)]
extern crate getopts;
extern crate rustc_serialize;
use std::env;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, ExitStatus};
use std::str;
use getopts::Options;
use rustc_serialize::json::Json;
fn main() {
let exit_status = execute();
std::io::stdout().flush().unwrap();
std::process::exit(exit_status);
}
fn execute() -> i32 {
let success = 0;
let failure = 1;
let mut opts = getopts::Options::new();
opts.optflag("h", "help", "show this message");
opts.optflag("q", "quiet", "no output printed to stdout");
opts.optflag("v", "verbose", "use verbose output");
let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
Ok(m) => m,
Err(e) => {
print_usage(&opts, &e.to_string());
return failure;
}
};
let verbosity = match (matches.opt_present("v"), matches.opt_present("q")) {
(false, false) => Verbosity::Normal,
(false, true) => Verbosity::Quiet,
(true, false) => Verbosity::Verbose,
(true, true) => {
print_usage(&opts, "quiet mode and verbose mode are not compatible");
return failure;
}
};
if matches.opt_present("h") {
print_usage(&opts, "");
return success;
}
match format_crate(verbosity) {
Err(e) => {
print_usage(&opts, &e.to_string());
failure
}
Ok(status) => {
if status.success() {
success
} else {
status.code().unwrap_or(failure)
}
}
}
}
fn print_usage(opts: &Options, reason: &str) {
let msg = format!("{}\nusage: cargo fmt [options]", reason);
println!("{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \
Arguments after `--` are passed to rustfmt.",
opts.usage(&msg));
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Verbosity {
Verbose,
Normal,
Quiet,
}
fn format_crate(verbosity: Verbosity) -> Result<ExitStatus, std::io::Error> {
let targets = try!(get_targets());
// Currently only bin and lib files get formatted
let files: Vec<_> = targets.into_iter()
.filter(|t| t.kind.should_format())
.inspect(|t| if verbosity == Verbosity::Verbose {
println!("[{:?}] {:?}", t.kind, t.path)
})
.map(|t| t.path)
.collect();
format_files(&files, &get_fmt_args(), verbosity)
}
fn get_fmt_args() -> Vec<String> {
// All arguments after -- are passed to rustfmt
env::args().skip_while(|a| a != "--").skip(1).collect()
}
#[derive(Debug)]
enum TargetKind {
Lib, // dylib, staticlib, lib
Bin, // bin
Example, // example file
Test, // test file
Bench, // bench file
CustomBuild, // build script
Other, // plugin,...
}
impl TargetKind {
fn should_format(&self) -> bool {
match *self {
TargetKind::Lib | TargetKind::Bin | TargetKind::Example | TargetKind::Test |
TargetKind::Bench | TargetKind::CustomBuild => true,
_ => false,
}
}
}
#[derive(Debug)]
pub struct Target {
path: PathBuf,
kind: TargetKind,
}
// Returns a vector of all compile targets of a crate
fn get_targets() -> Result<Vec<Target>, std::io::Error> {
let mut targets: Vec<Target> = vec![];
let output = try!(Command::new("cargo").arg("read-manifest").output());
if output.status.success() {
// None of the unwraps should fail if output of `cargo read-manifest` is correct
let data = &String::from_utf8(output.stdout).unwrap();
let json = Json::from_str(data).unwrap();
let jtargets = json.find("targets").unwrap().as_array().unwrap();
for jtarget in jtargets {
targets.push(target_from_json(jtarget));
}
Ok(targets)
} else {
// This happens when cargo-fmt is not used inside a crate
Err(std::io::Error::new(std::io::ErrorKind::NotFound,
str::from_utf8(&output.stderr).unwrap()))
}
}
fn target_from_json(jtarget: &Json) -> Target {
let jtarget = jtarget.as_object().unwrap();
let path = PathBuf::from(jtarget.get("src_path").unwrap().as_string().unwrap());
let kinds = jtarget.get("kind").unwrap().as_array().unwrap();
let kind = match kinds[0].as_string().unwrap() {
"bin" => TargetKind::Bin,
"lib" | "dylib" | "staticlib" | "cdylib" | "rlib" => TargetKind::Lib,
"test" => TargetKind::Test,
"example" => TargetKind::Example,
"bench" => TargetKind::Bench,
"custom-build" => TargetKind::CustomBuild,
_ => TargetKind::Other,
};
Target {
path: path,
kind: kind,
}
}
fn format_files(files: &[PathBuf],
fmt_args: &[String],
verbosity: Verbosity)
-> Result<ExitStatus, std::io::Error> {
let stdout = if verbosity == Verbosity::Quiet {
std::process::Stdio::null()
} else {
std::process::Stdio::inherit()
};
if verbosity == Verbosity::Verbose {
print!("rustfmt");
for a in fmt_args.iter() {
print!(" {}", a);
}
for f in files.iter() {
print!(" {}", f.display());
}
println!("");
}
let mut command = try!(Command::new("rustfmt")
.stdout(stdout)
.args(files)
.args(fmt_args)
.spawn()
.map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => {
std::io::Error::new(std::io::ErrorKind::Other,
"Could not run rustfmt, please make sure it is in your PATH.")
}
_ => e,
}));
command.wait()
}