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
|
|
|
|
2016-04-05 23:34:29 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
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-04-02 13:56:37 -05:00
|
|
|
use rustfmt::{run, Input};
|
2016-01-12 17:12:48 -06:00
|
|
|
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};
|
2016-02-05 14:59:41 -06:00
|
|
|
use std::str::FromStr;
|
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
|
|
|
|
2016-03-08 23:52:58 -06: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.
|
2016-02-25 15:27:37 -06:00
|
|
|
Format {
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
config_path: Option<PathBuf>,
|
|
|
|
},
|
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,
|
2016-02-25 15:27:37 -06:00
|
|
|
/// Invalid program input.
|
|
|
|
InvalidInput {
|
|
|
|
reason: String,
|
|
|
|
},
|
2015-11-02 13:45:45 -06:00
|
|
|
/// No file specified, read from stdin
|
2016-02-25 15:27:37 -06:00
|
|
|
Stdin {
|
|
|
|
input: String,
|
|
|
|
config_path: Option<PathBuf>,
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2016-01-22 04:43:54 -06:00
|
|
|
/// read the given config file path recursively if present else read the project file path
|
|
|
|
fn match_cli_path_or_file(config_path: Option<PathBuf>,
|
|
|
|
input_file: &Path)
|
|
|
|
-> io::Result<(Config, Option<PathBuf>)> {
|
|
|
|
|
|
|
|
if let Some(config_file) = config_path {
|
|
|
|
let (toml, path) = try!(resolve_config(config_file.as_ref()));
|
|
|
|
if path.is_some() {
|
|
|
|
return Ok((toml, path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve_config(input_file)
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:59:41 -06:00
|
|
|
fn update_config(config: &mut Config, matches: &Matches) -> Result<(), String> {
|
2015-11-15 00:41:41 -06:00
|
|
|
config.verbose = matches.opt_present("verbose");
|
2015-12-23 08:25:49 -06:00
|
|
|
config.skip_children = matches.opt_present("skip-children");
|
2016-02-05 14:59:41 -06:00
|
|
|
|
|
|
|
let write_mode = matches.opt_str("write-mode");
|
|
|
|
match matches.opt_str("write-mode").map(|wm| WriteMode::from_str(&wm)) {
|
|
|
|
None => Ok(()),
|
|
|
|
Some(Ok(write_mode)) => {
|
|
|
|
config.write_mode = write_mode;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Some(Err(_)) => Err(format!("Invalid write-mode: {}", write_mode.expect("cannot happen"))),
|
|
|
|
}
|
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");
|
2016-01-22 04:43:54 -06:00
|
|
|
opts.optopt("",
|
|
|
|
"config-path",
|
|
|
|
"Recursively searches the given path for the rustfmt.toml config file. If not \
|
|
|
|
found reverts to the input file path",
|
|
|
|
"[Path for the configuration file]");
|
2015-11-07 18:03:25 -06:00
|
|
|
|
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 {
|
2016-02-25 15:27:37 -06:00
|
|
|
Operation::InvalidInput { reason } => {
|
2015-10-24 08:30:26 -05:00
|
|
|
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
|
|
|
|
}
|
2016-02-25 15:27:37 -06:00
|
|
|
Operation::Stdin { input, config_path } => {
|
2015-11-02 13:45:45 -06:00
|
|
|
// try to read config from local directory
|
2016-02-05 14:59:41 -06:00
|
|
|
let (mut config, _) = match_cli_path_or_file(config_path, &env::current_dir().unwrap())
|
|
|
|
.expect("Error resolving config");
|
2015-11-02 13:45:45 -06:00
|
|
|
|
2016-02-05 14:59:41 -06:00
|
|
|
// write_mode is always Plain for Stdin.
|
|
|
|
config.write_mode = WriteMode::Plain;
|
|
|
|
|
2016-04-02 13:56:37 -05:00
|
|
|
run(Input::Text(input), &config);
|
2015-11-02 13:45:45 -06:00
|
|
|
0
|
|
|
|
}
|
2016-02-25 15:27:37 -06:00
|
|
|
Operation::Format { files, config_path } => {
|
2016-01-22 04:43:54 -06:00
|
|
|
let mut config = Config::default();
|
|
|
|
let mut path = None;
|
|
|
|
// Load the config path file if provided
|
|
|
|
if let Some(config_file) = config_path {
|
|
|
|
let (cfg_tmp, path_tmp) = resolve_config(config_file.as_ref())
|
|
|
|
.expect(&format!("Error resolving config for {:?}",
|
|
|
|
config_file));
|
|
|
|
config = cfg_tmp;
|
|
|
|
path = path_tmp;
|
|
|
|
};
|
|
|
|
if let Some(path) = path.as_ref() {
|
2016-04-04 05:49:16 -05:00
|
|
|
println!("Using rustfmt config file {}", path.display());
|
2016-01-22 04:43:54 -06:00
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
for file in files {
|
2016-01-22 04:43:54 -06:00
|
|
|
// Check the file directory if the config-path could not be read or not provided
|
|
|
|
if path.is_none() {
|
|
|
|
let (config_tmp, path_tmp) = resolve_config(file.parent().unwrap())
|
|
|
|
.expect(&format!("Error resolving config \
|
|
|
|
for {}",
|
|
|
|
file.display()));
|
|
|
|
if let Some(path) = path_tmp.as_ref() {
|
2016-04-04 05:49:16 -05:00
|
|
|
println!("Using rustfmt config file {} for {}",
|
|
|
|
path.display(),
|
|
|
|
file.display());
|
2016-01-22 04:43:54 -06:00
|
|
|
}
|
|
|
|
config = config_tmp;
|
2016-01-31 09:21:48 -06:00
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
|
2016-02-05 14:59:41 -06:00
|
|
|
if let Err(e) = update_config(&mut config, &matches) {
|
|
|
|
print_usage(&opts, &e);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-02 13:56:37 -05:00
|
|
|
run(Input::File(file), &config);
|
2015-11-12 18:13:25 -06:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-22 04:43:54 -06:00
|
|
|
// Read the config_path and convert to parent dir if a file is provided.
|
|
|
|
let config_path: Option<PathBuf> = matches.opt_str("config-path")
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.and_then(|dir| {
|
|
|
|
if dir.is_file() {
|
|
|
|
return dir.parent().map(|v| v.into());
|
|
|
|
}
|
|
|
|
Some(dir)
|
|
|
|
});
|
|
|
|
|
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(..) => (),
|
2016-02-25 15:27:37 -06:00
|
|
|
Err(e) => return Operation::InvalidInput { reason: e.to_string() },
|
2015-11-02 13:45:45 -06:00
|
|
|
}
|
|
|
|
|
2016-02-25 15:27:37 -06:00
|
|
|
return Operation::Stdin {
|
|
|
|
input: buffer,
|
|
|
|
config_path: config_path,
|
|
|
|
};
|
2015-11-02 13:45:45 -06: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
|
|
|
|
2016-02-25 15:27:37 -06:00
|
|
|
Operation::Format {
|
|
|
|
files: files,
|
|
|
|
config_path: config_path,
|
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|