2015-05-01 11:43:36 -05:00
|
|
|
// Copyright 2015 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.
|
2015-10-22 16:35:42 -05:00
|
|
|
|
2015-05-03 17:12:39 -05:00
|
|
|
#![cfg(not(test))]
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2015-05-01 11:43:36 -05:00
|
|
|
extern crate rustfmt;
|
2015-07-31 18:21:44 -05:00
|
|
|
extern crate toml;
|
2015-10-22 16:34:05 -05:00
|
|
|
extern crate env_logger;
|
2015-10-22 16:38:16 -05:00
|
|
|
extern crate getopts;
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2016-01-12 17:12:48 -06:00
|
|
|
use rustfmt::{run, run_from_stdin};
|
|
|
|
use rustfmt::config::{Config, WriteMode};
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
use std::env;
|
2015-10-22 16:35:42 -05:00
|
|
|
use std::fs::{self, File};
|
2016-01-31 09:21:48 -06:00
|
|
|
use std::io::{self, ErrorKind, Read, Write};
|
2015-10-16 03:08:47 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-10-22 16:38:16 -05:00
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
use getopts::{Matches, Options};
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Rustfmt operations.
|
|
|
|
enum Operation {
|
2015-11-12 19:08:57 -06:00
|
|
|
/// Format files and their child modules.
|
2015-11-12 18:13:25 -06:00
|
|
|
Format(Vec<PathBuf>, WriteMode),
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Print the help message.
|
|
|
|
Help,
|
2015-11-15 21:37:08 -06:00
|
|
|
// Print version information
|
|
|
|
Version,
|
2015-11-07 18:03:25 -06:00
|
|
|
/// Print detailed configuration help.
|
|
|
|
ConfigHelp,
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Invalid program input, including reason.
|
|
|
|
InvalidInput(String),
|
2015-11-02 13:45:45 -06:00
|
|
|
/// No file specified, read from stdin
|
|
|
|
Stdin(String, WriteMode),
|
2015-10-24 08:30:26 -05:00
|
|
|
}
|
|
|
|
|
2016-02-01 11:55:12 -06:00
|
|
|
/// Try to find a project file in the given directory and its parents. Returns the path of a the
|
|
|
|
/// nearest project file if one exists, or `None` if no project file was found.
|
2016-01-31 09:21:48 -06:00
|
|
|
fn lookup_project_file(dir: &Path) -> io::Result<Option<PathBuf>> {
|
|
|
|
let mut current = if dir.is_relative() {
|
|
|
|
try!(env::current_dir()).join(dir)
|
2015-10-16 03:08:47 -05:00
|
|
|
} else {
|
2016-01-31 09:21:48 -06:00
|
|
|
dir.to_path_buf()
|
2015-10-16 03:08:47 -05:00
|
|
|
};
|
|
|
|
|
2016-01-31 01:01:54 -06:00
|
|
|
current = try!(fs::canonicalize(current));
|
2015-10-16 03:08:47 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
loop {
|
|
|
|
let config_file = current.join("rustfmt.toml");
|
2016-01-31 09:21:48 -06:00
|
|
|
match fs::metadata(&config_file) {
|
|
|
|
Ok(md) => {
|
|
|
|
// Properly handle unlikely situation of a directory named `rustfmt.toml`.
|
|
|
|
if md.is_file() {
|
|
|
|
return Ok(Some(config_file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If it's not found, we continue searching; otherwise something went wrong and we
|
|
|
|
// return the error.
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() != ErrorKind::NotFound {
|
|
|
|
return Err(e);
|
|
|
|
}
|
2016-01-31 00:48:14 -06:00
|
|
|
}
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
2015-11-09 14:41:25 -06:00
|
|
|
|
|
|
|
// If the current directory has no parent, we're done searching.
|
|
|
|
if !current.pop() {
|
2016-01-31 09:21:48 -06:00
|
|
|
return Ok(None);
|
2015-11-09 14:41:25 -06:00
|
|
|
}
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 09:21:48 -06:00
|
|
|
/// Resolve the config for input in `dir`.
|
|
|
|
///
|
|
|
|
/// Returns the `Config` to use, and the path of the project file if there was
|
|
|
|
/// one.
|
|
|
|
fn resolve_config(dir: &Path) -> io::Result<(Config, Option<PathBuf>)> {
|
|
|
|
let path = try!(lookup_project_file(dir));
|
|
|
|
if path.is_none() {
|
|
|
|
return Ok((Config::default(), None));
|
|
|
|
}
|
|
|
|
let path = path.unwrap();
|
2015-07-31 18:21:44 -05:00
|
|
|
let mut file = try!(File::open(&path));
|
|
|
|
let mut toml = String::new();
|
|
|
|
try!(file.read_to_string(&mut toml));
|
2016-01-31 09:21:48 -06:00
|
|
|
Ok((Config::from_toml(&toml), Some(path)))
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
fn update_config(config: &mut Config, matches: &Matches) {
|
|
|
|
config.verbose = matches.opt_present("verbose");
|
2015-12-23 08:25:49 -06:00
|
|
|
config.skip_children = matches.opt_present("skip-children");
|
2015-11-15 00:41:41 -06:00
|
|
|
}
|
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
fn execute() -> i32 {
|
2015-10-24 08:30:26 -05:00
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optflag("h", "help", "show this message");
|
2015-11-16 07:52:14 -06:00
|
|
|
opts.optflag("V", "version", "show version information");
|
2015-11-15 00:41:41 -06:00
|
|
|
opts.optflag("v", "verbose", "show progress");
|
2015-10-24 08:30:26 -05:00
|
|
|
opts.optopt("",
|
|
|
|
"write-mode",
|
2015-11-03 02:16:33 -06:00
|
|
|
"mode to write in (not usable when piping from stdin)",
|
2015-12-27 22:13:32 -06:00
|
|
|
"[replace|overwrite|display|diff|coverage|checkstyle]");
|
2015-12-23 08:25:49 -06:00
|
|
|
opts.optflag("", "skip-children", "don't reformat child modules");
|
2015-10-24 08:30:26 -05:00
|
|
|
|
2015-11-07 18:03:25 -06:00
|
|
|
opts.optflag("",
|
|
|
|
"config-help",
|
|
|
|
"show details of rustfmt configuration options");
|
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
let matches = match opts.parse(env::args().skip(1)) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => {
|
|
|
|
print_usage(&opts, &e.to_string());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let operation = determine_operation(&matches);
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2015-10-24 08:30:26 -05:00
|
|
|
match operation {
|
|
|
|
Operation::InvalidInput(reason) => {
|
|
|
|
print_usage(&opts, &reason);
|
|
|
|
1
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
2015-10-24 08:30:26 -05:00
|
|
|
Operation::Help => {
|
|
|
|
print_usage(&opts, "");
|
|
|
|
0
|
|
|
|
}
|
2015-11-15 21:37:08 -06:00
|
|
|
Operation::Version => {
|
|
|
|
print_version();
|
|
|
|
0
|
|
|
|
}
|
2015-11-07 18:03:25 -06:00
|
|
|
Operation::ConfigHelp => {
|
|
|
|
Config::print_docs();
|
|
|
|
0
|
|
|
|
}
|
2015-11-02 13:45:45 -06:00
|
|
|
Operation::Stdin(input, write_mode) => {
|
|
|
|
// try to read config from local directory
|
2016-01-31 09:21:48 -06:00
|
|
|
let (config, _) = resolve_config(&env::current_dir().unwrap())
|
|
|
|
.expect("Error resolving config");
|
2015-11-02 13:45:45 -06:00
|
|
|
|
|
|
|
run_from_stdin(input, write_mode, &config);
|
|
|
|
0
|
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
Operation::Format(files, write_mode) => {
|
|
|
|
for file in files {
|
2016-01-31 09:21:48 -06:00
|
|
|
let (mut config, path) = resolve_config(file.parent().unwrap())
|
|
|
|
.expect(&format!("Error resolving config for {}",
|
|
|
|
file.display()));
|
|
|
|
if let Some(path) = path {
|
|
|
|
println!("Using rustfmt config file {} for {}",
|
|
|
|
path.display(),
|
|
|
|
file.display());
|
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
update_config(&mut config, &matches);
|
2015-11-12 18:13:25 -06:00
|
|
|
run(&file, write_mode, &config);
|
|
|
|
}
|
2015-10-24 08:30:26 -05:00
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 11:43:36 -05:00
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
fn main() {
|
2015-10-22 16:34:05 -05:00
|
|
|
let _ = env_logger::init();
|
2015-08-28 03:28:28 -05:00
|
|
|
let exit_code = execute();
|
2015-10-24 08:30:26 -05:00
|
|
|
|
|
|
|
// Make sure standard output is flushed before we exit.
|
2015-08-28 03:28:28 -05:00
|
|
|
std::io::stdout().flush().unwrap();
|
2015-10-24 08:30:26 -05:00
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
// Exit with given exit code.
|
|
|
|
//
|
|
|
|
// NOTE: This immediately terminates the process without doing any cleanup,
|
|
|
|
// so make sure to finish all necessary cleanup before this is called.
|
|
|
|
std::process::exit(exit_code);
|
2015-08-26 17:02:06 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:38:16 -05:00
|
|
|
fn print_usage(opts: &Options, reason: &str) {
|
2015-11-12 18:13:25 -06:00
|
|
|
let reason = format!("{}\nusage: {} [options] <file>...",
|
2015-10-22 16:38:16 -05:00
|
|
|
reason,
|
2015-11-19 19:56:37 -06:00
|
|
|
env::args_os().next().unwrap().to_string_lossy());
|
2015-10-22 16:38:16 -05:00
|
|
|
println!("{}", opts.usage(&reason));
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
|
|
|
|
2015-11-15 21:37:08 -06:00
|
|
|
fn print_version() {
|
2015-11-17 17:51:15 -06:00
|
|
|
println!("{}.{}.{}{}",
|
|
|
|
option_env!("CARGO_PKG_VERSION_MAJOR").unwrap_or("X"),
|
|
|
|
option_env!("CARGO_PKG_VERSION_MINOR").unwrap_or("X"),
|
|
|
|
option_env!("CARGO_PKG_VERSION_PATCH").unwrap_or("X"),
|
|
|
|
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
|
2015-11-15 21:37:08 -06:00
|
|
|
}
|
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
fn determine_operation(matches: &Matches) -> Operation {
|
2015-10-22 16:38:16 -05:00
|
|
|
if matches.opt_present("h") {
|
2015-10-24 08:30:26 -05:00
|
|
|
return Operation::Help;
|
2015-10-22 16:38:16 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 18:03:25 -06:00
|
|
|
if matches.opt_present("config-help") {
|
|
|
|
return Operation::ConfigHelp;
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:37:08 -06:00
|
|
|
if matches.opt_present("version") {
|
|
|
|
return Operation::Version;
|
|
|
|
}
|
|
|
|
|
2015-11-02 13:45:45 -06:00
|
|
|
// if no file argument is supplied, read from stdin
|
2015-12-04 06:32:19 -06:00
|
|
|
if matches.free.is_empty() {
|
2015-11-02 13:45:45 -06:00
|
|
|
|
|
|
|
let mut buffer = String::new();
|
|
|
|
match io::stdin().read_to_string(&mut buffer) {
|
|
|
|
Ok(..) => (),
|
|
|
|
Err(e) => return Operation::InvalidInput(e.to_string()),
|
|
|
|
}
|
|
|
|
|
2015-11-03 02:16:33 -06:00
|
|
|
// WriteMode is always plain for Stdin
|
2015-11-02 13:45:45 -06:00
|
|
|
return Operation::Stdin(buffer, WriteMode::Plain);
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:38:16 -05:00
|
|
|
let write_mode = match matches.opt_str("write-mode") {
|
|
|
|
Some(mode) => {
|
|
|
|
match mode.parse() {
|
|
|
|
Ok(mode) => mode,
|
2015-10-24 08:30:26 -05:00
|
|
|
Err(..) => return Operation::InvalidInput("Unrecognized write mode".into()),
|
2015-08-28 06:07:08 -05:00
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|
2016-01-12 17:12:48 -06:00
|
|
|
None => WriteMode::Default,
|
2015-10-22 16:38:16 -05:00
|
|
|
};
|
2015-08-28 03:28:28 -05:00
|
|
|
|
2015-12-04 06:32:19 -06:00
|
|
|
let files: Vec<_> = matches.free.iter().map(PathBuf::from).collect();
|
2015-11-12 18:13:25 -06:00
|
|
|
|
|
|
|
Operation::Format(files, write_mode)
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|