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-07-31 18:21:44 -05:00
|
|
|
#![feature(path_ext)]
|
|
|
|
#![feature(rustc_private)]
|
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-05-01 11:43:36 -05:00
|
|
|
|
|
|
|
use rustfmt::{WriteMode, run};
|
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;
|
|
|
|
use std::fs::{File, PathExt};
|
|
|
|
use std::io::{self, Read};
|
|
|
|
use std::path::PathBuf;
|
2015-08-18 14:10:30 -05:00
|
|
|
use std::str::FromStr;
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
// Try to find a project file in the current directory and its parents.
|
|
|
|
fn lookup_project_file() -> io::Result<PathBuf> {
|
|
|
|
let mut current = try!(env::current_dir());
|
|
|
|
loop {
|
|
|
|
let config_file = current.join("rustfmt.toml");
|
|
|
|
if config_file.exists() {
|
|
|
|
return Ok(config_file);
|
|
|
|
} else {
|
|
|
|
current = match current.parent() {
|
|
|
|
// if the current directory has no parent, we're done searching
|
|
|
|
None => return Err(io::Error::new(io::ErrorKind::NotFound, "config not found")),
|
|
|
|
Some(path) => path.to_path_buf(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a project file. If it's found, read it.
|
|
|
|
fn lookup_and_read_project_file() -> io::Result<(PathBuf, String)> {
|
|
|
|
let path = try!(lookup_project_file());
|
|
|
|
let mut file = try!(File::open(&path));
|
|
|
|
let mut toml = String::new();
|
|
|
|
try!(file.read_to_string(&mut toml));
|
|
|
|
Ok((path, toml))
|
|
|
|
}
|
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
fn execute() -> i32 {
|
|
|
|
let (args, write_mode) = match determine_params(std::env::args()) {
|
2015-08-28 16:51:26 -05:00
|
|
|
Some(params) => params,
|
2015-08-28 03:28:28 -05:00
|
|
|
None => return 1,
|
|
|
|
};
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
let config = match lookup_and_read_project_file() {
|
|
|
|
Ok((path, toml)) => {
|
|
|
|
println!("Project config file: {}", path.display());
|
|
|
|
Config::from_toml(&toml)
|
|
|
|
}
|
|
|
|
Err(_) => Default::default(),
|
|
|
|
};
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2015-09-17 13:21:06 -05:00
|
|
|
run(args, write_mode, &config);
|
2015-08-28 03:28:28 -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() {
|
|
|
|
use std::io::Write;
|
2015-10-22 16:34:05 -05:00
|
|
|
let _ = env_logger::init();
|
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
let exit_code = execute();
|
|
|
|
// Make sure standard output is flushed before we exit
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
// 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-08-28 03:28:28 -05:00
|
|
|
fn print_usage<S: Into<String>>(reason: S) {
|
2015-09-10 17:52:57 -05:00
|
|
|
println!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[replace|overwrite|display|diff]] \
|
|
|
|
<file_name>",
|
|
|
|
reason.into());
|
2015-09-28 18:38:19 -05:00
|
|
|
Config::print_docs();
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn determine_params<I>(args: I) -> Option<(Vec<String>, WriteMode)>
|
2015-08-18 14:10:30 -05:00
|
|
|
where I: Iterator<Item = String>
|
|
|
|
{
|
2015-08-26 17:02:06 -05:00
|
|
|
let arg_prefix = "-";
|
|
|
|
let write_mode_prefix = "--write-mode=";
|
|
|
|
let help_mode = "-h";
|
|
|
|
let long_help_mode = "--help";
|
2015-08-18 14:10:30 -05:00
|
|
|
let mut write_mode = WriteMode::Replace;
|
2015-08-28 06:07:08 -05:00
|
|
|
let mut rustc_args = Vec::new();
|
2015-08-18 14:10:30 -05:00
|
|
|
|
|
|
|
// The NewFile option currently isn't supported because it requires another
|
|
|
|
// parameter, but it can be added later.
|
2015-08-28 06:07:08 -05:00
|
|
|
for arg in args {
|
2015-08-26 17:02:06 -05:00
|
|
|
if arg.starts_with(write_mode_prefix) {
|
2015-08-28 06:07:08 -05:00
|
|
|
match FromStr::from_str(&arg[write_mode_prefix.len()..]) {
|
|
|
|
Ok(mode) => write_mode = mode,
|
2015-08-28 03:28:28 -05:00
|
|
|
Err(_) => {
|
|
|
|
print_usage("Unrecognized write mode");
|
2015-08-28 06:07:08 -05:00
|
|
|
return None;
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
2015-08-28 06:07:08 -05:00
|
|
|
}
|
2015-08-26 17:02:06 -05:00
|
|
|
} else if arg.starts_with(help_mode) || arg.starts_with(long_help_mode) {
|
2015-08-28 03:28:28 -05:00
|
|
|
print_usage("");
|
2015-08-28 06:07:08 -05:00
|
|
|
return None;
|
2015-08-26 17:02:06 -05:00
|
|
|
} else if arg.starts_with(arg_prefix) {
|
2015-08-28 03:28:28 -05:00
|
|
|
print_usage("Invalid argument");
|
2015-08-28 06:07:08 -05:00
|
|
|
return None;
|
2015-08-28 03:28:28 -05:00
|
|
|
} else {
|
2015-08-28 06:07:08 -05:00
|
|
|
// Pass everything else to rustc
|
|
|
|
rustc_args.push(arg);
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 06:07:08 -05:00
|
|
|
if rustc_args.len() < 2 {
|
2015-08-28 03:28:28 -05:00
|
|
|
print_usage("Please provide a file to be formatted");
|
|
|
|
return None;
|
2015-08-26 17:02:06 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 06:07:08 -05:00
|
|
|
Some((rustc_args, write_mode))
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|