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
|
|
|
|
2015-11-02 13:45:45 -06:00
|
|
|
use rustfmt::{WriteMode, run, run_from_stdin};
|
2015-09-14 15:43:55 -05:00
|
|
|
use rustfmt::config::Config;
|
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};
|
2015-10-24 08:30:26 -05:00
|
|
|
use std::io::{self, Read, Write};
|
2015-11-15 21:37:08 -06:00
|
|
|
use std::process::Command;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to find a project file in the input file directory and its parents.
|
2015-10-16 03:08:47 -05:00
|
|
|
fn lookup_project_file(input_file: &Path) -> io::Result<PathBuf> {
|
|
|
|
let mut current = if input_file.is_relative() {
|
|
|
|
try!(env::current_dir()).join(input_file)
|
|
|
|
} else {
|
|
|
|
input_file.to_path_buf()
|
|
|
|
};
|
|
|
|
|
2015-10-24 08:30:26 -05:00
|
|
|
// FIXME: We should canonize path to properly handle its parents,
|
2015-10-16 03:08:47 -05:00
|
|
|
// but `canonicalize` function is unstable now (recently added API)
|
|
|
|
// current = try!(fs::canonicalize(current));
|
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
loop {
|
|
|
|
let config_file = current.join("rustfmt.toml");
|
2015-10-22 16:35:42 -05:00
|
|
|
if fs::metadata(&config_file).is_ok() {
|
2015-07-31 18:21:44 -05:00
|
|
|
return Ok(config_file);
|
|
|
|
}
|
2015-11-09 14:41:25 -06:00
|
|
|
|
|
|
|
// If the current directory has no parent, we're done searching.
|
|
|
|
if !current.pop() {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::NotFound, "Config not found"));
|
|
|
|
}
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Try to find a project file. If it's found, read it.
|
2015-10-16 03:08:47 -05:00
|
|
|
fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, String)> {
|
|
|
|
let path = try!(lookup_project_file(input_file));
|
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));
|
|
|
|
Ok((path, toml))
|
|
|
|
}
|
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
fn update_config(config: &mut Config, matches: &Matches) {
|
|
|
|
config.verbose = matches.opt_present("verbose");
|
|
|
|
}
|
|
|
|
|
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-15 21:37:08 -06:00
|
|
|
opts.optflag("", "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)",
|
|
|
|
"[replace|overwrite|display|diff|coverage]");
|
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
|
|
|
|
let config = match lookup_and_read_project_file(&Path::new(".")) {
|
2015-11-04 17:14:55 -06:00
|
|
|
Ok((_, toml)) => {
|
2015-11-02 13:45:45 -06:00
|
|
|
Config::from_toml(&toml)
|
|
|
|
}
|
|
|
|
Err(_) => Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2015-11-15 00:41:41 -06:00
|
|
|
let mut config = match lookup_and_read_project_file(&file) {
|
2015-11-12 18:13:25 -06:00
|
|
|
Ok((path, toml)) => {
|
|
|
|
println!("Using rustfmt config file {} for {}",
|
|
|
|
path.display(),
|
|
|
|
file.display());
|
|
|
|
Config::from_toml(&toml)
|
|
|
|
}
|
|
|
|
Err(_) => Default::default(),
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
env::current_exe().unwrap().display());
|
|
|
|
println!("{}", opts.usage(&reason));
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
|
|
|
|
2015-11-15 21:37:08 -06:00
|
|
|
fn print_version() {
|
|
|
|
let cmd = Command::new("git")
|
|
|
|
.arg("rev-parse")
|
|
|
|
.arg("--short")
|
|
|
|
.arg("HEAD")
|
|
|
|
.output();
|
|
|
|
match cmd {
|
|
|
|
Ok(output) => print!("{}", String::from_utf8(output.stdout).unwrap()),
|
|
|
|
Err(e) => panic!("Unable te get version: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-03 02:16:33 -06:00
|
|
|
if matches.free.len() == 0 {
|
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
|
|
|
}
|
2015-10-22 16:38:16 -05:00
|
|
|
None => WriteMode::Replace,
|
|
|
|
};
|
2015-08-28 03:28:28 -05:00
|
|
|
|
2015-11-12 19:08:57 -06:00
|
|
|
let files: Vec<_> = matches.free.iter().map(|a| PathBuf::from(a)).collect();
|
2015-11-12 18:13:25 -06:00
|
|
|
|
|
|
|
Operation::Format(files, write_mode)
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|