2015-08-01 08:02:59 -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-09-17 13:21:06 -05:00
|
|
|
// TODO: add tests
|
2015-08-01 08:02:59 -05:00
|
|
|
|
|
|
|
use strings::string_buffer::StringBuffer;
|
2015-09-17 13:21:06 -05:00
|
|
|
|
2015-08-01 08:02:59 -05:00
|
|
|
use std::collections::HashMap;
|
2015-08-02 07:49:35 -05:00
|
|
|
use std::fs::{self, File};
|
2016-01-12 17:12:48 -06:00
|
|
|
use std::io::{self, Write, Read, stdout, BufWriter};
|
2015-09-17 13:21:06 -05:00
|
|
|
|
2016-01-12 17:12:48 -06:00
|
|
|
use config::{NewlineStyle, Config, WriteMode};
|
2015-12-29 22:54:35 -06:00
|
|
|
use rustfmt_diff::{make_diff, print_diff, Mismatch, DiffLine};
|
2015-08-01 08:02:59 -05:00
|
|
|
|
|
|
|
// A map of the files of a crate, with their new content
|
|
|
|
pub type FileMap = HashMap<String, StringBuffer>;
|
|
|
|
|
|
|
|
// Append a newline to the end of each file.
|
|
|
|
pub fn append_newlines(file_map: &mut FileMap) {
|
|
|
|
for (_, s) in file_map.iter_mut() {
|
|
|
|
s.push_str("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_all_files(file_map: &FileMap,
|
|
|
|
mode: WriteMode,
|
|
|
|
config: &Config)
|
2016-01-12 17:12:48 -06:00
|
|
|
-> Result<(), io::Error> {
|
2015-12-30 23:33:51 -06:00
|
|
|
output_heading(mode).ok();
|
2015-08-01 08:02:59 -05:00
|
|
|
for filename in file_map.keys() {
|
2016-01-12 17:12:48 -06:00
|
|
|
try!(write_file(&file_map[filename], filename, mode, config));
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
2015-12-30 23:33:51 -06:00
|
|
|
output_trailing(mode).ok();
|
2015-08-01 08:02:59 -05:00
|
|
|
|
2015-12-29 22:54:35 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-12-30 23:33:51 -06:00
|
|
|
pub fn output_heading(mode: WriteMode) -> Result<(), io::Error> {
|
2015-12-29 22:54:35 -06:00
|
|
|
let stdout = stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
match mode {
|
|
|
|
WriteMode::Checkstyle => {
|
|
|
|
let mut xml_heading = String::new();
|
|
|
|
xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
|
|
|
xml_heading.push_str("\n");
|
|
|
|
xml_heading.push_str("<checkstyle version=\"4.3\">");
|
|
|
|
try!(write!(stdout, "{}", xml_heading));
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-12-30 23:33:07 -06:00
|
|
|
_ => Ok(()),
|
2015-12-29 22:54:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 23:33:51 -06:00
|
|
|
pub fn output_trailing(mode: WriteMode) -> Result<(), io::Error> {
|
2015-12-29 22:54:35 -06:00
|
|
|
let stdout = stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
match mode {
|
|
|
|
WriteMode::Checkstyle => {
|
|
|
|
let mut xml_tail = String::new();
|
|
|
|
xml_tail.push_str("</checkstyle>");
|
|
|
|
try!(write!(stdout, "{}", xml_tail));
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-12-30 23:33:07 -06:00
|
|
|
_ => Ok(()),
|
2015-12-29 22:54:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn output_checkstyle_file<T>(mut writer: T,
|
|
|
|
filename: &str,
|
2015-12-30 23:33:07 -06:00
|
|
|
diff: Vec<Mismatch>)
|
|
|
|
-> Result<(), io::Error>
|
2015-12-29 22:54:35 -06:00
|
|
|
where T: Write
|
|
|
|
{
|
|
|
|
try!(write!(writer, "<file name=\"{}\">", filename));
|
|
|
|
for mismatch in diff {
|
|
|
|
for line in mismatch.lines {
|
|
|
|
match line {
|
|
|
|
DiffLine::Expected(ref str) => {
|
2016-01-03 22:17:49 -06:00
|
|
|
let message = xml_escape_str(&str);
|
|
|
|
// TODO XML encode str here.
|
2015-12-30 23:33:07 -06:00
|
|
|
try!(write!(writer,
|
2016-01-08 21:38:27 -06:00
|
|
|
"<error line=\"{}\" severity=\"warning\" message=\"Should be \
|
2015-12-30 23:33:07 -06:00
|
|
|
`{}`\" />",
|
|
|
|
mismatch.line_number,
|
2016-01-03 22:17:49 -06:00
|
|
|
message));
|
2015-12-29 22:54:35 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Do nothing with context and expected.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try!(write!(writer, "</file>"));
|
2016-01-12 17:12:48 -06:00
|
|
|
Ok(())
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 22:17:49 -06:00
|
|
|
// Convert special characters into XML entities.
|
|
|
|
// This is needed for checkstyle output.
|
|
|
|
fn xml_escape_str(string: &str) -> String {
|
|
|
|
let mut out = String::new();
|
|
|
|
for c in string.chars() {
|
|
|
|
match c {
|
|
|
|
'<' => out.push_str("<"),
|
|
|
|
'>' => out.push_str(">"),
|
|
|
|
'"' => out.push_str("""),
|
|
|
|
'\'' => out.push_str("'"),
|
|
|
|
'&' => out.push_str("&"),
|
|
|
|
_ => out.push(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2016-01-12 17:12:48 -06:00
|
|
|
// Prints all newlines either as `\n` or as `\r\n`.
|
|
|
|
pub fn write_system_newlines<T>(writer: T,
|
2015-08-01 08:02:59 -05:00
|
|
|
text: &StringBuffer,
|
|
|
|
config: &Config)
|
2015-08-02 07:49:35 -05:00
|
|
|
-> Result<(), io::Error>
|
2016-01-12 17:12:48 -06:00
|
|
|
where T: Write
|
|
|
|
{
|
|
|
|
// Buffer output, since we're writing a since char at a time.
|
|
|
|
let mut writer = BufWriter::new(writer);
|
|
|
|
|
|
|
|
let style = if config.newline_style == NewlineStyle::Native {
|
|
|
|
if cfg!(windows) {
|
|
|
|
NewlineStyle::Windows
|
2015-11-15 05:34:04 -06:00
|
|
|
} else {
|
2016-01-12 17:12:48 -06:00
|
|
|
NewlineStyle::Unix
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.newline_style
|
|
|
|
};
|
|
|
|
|
|
|
|
match style {
|
|
|
|
NewlineStyle::Unix => write!(writer, "{}", text),
|
|
|
|
NewlineStyle::Windows => {
|
|
|
|
for (c, _) in text.chars() {
|
|
|
|
match c {
|
|
|
|
'\n' => try!(write!(writer, "\r\n")),
|
|
|
|
'\r' => continue,
|
|
|
|
c => try!(write!(writer, "{}", c)),
|
2015-08-15 22:58:17 -05:00
|
|
|
}
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
2016-01-12 17:12:48 -06:00
|
|
|
Ok(())
|
2015-08-15 22:58:17 -05:00
|
|
|
}
|
2016-01-12 17:12:48 -06:00
|
|
|
NewlineStyle::Native => unreachable!(),
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
2016-01-12 17:12:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_file(text: &StringBuffer,
|
|
|
|
filename: &str,
|
|
|
|
mode: WriteMode,
|
|
|
|
config: &Config)
|
|
|
|
-> Result<Option<String>, io::Error> {
|
2015-08-01 08:02:59 -05:00
|
|
|
|
2016-01-02 22:21:55 -06:00
|
|
|
fn source_and_formatted_text(text: &StringBuffer,
|
|
|
|
filename: &str,
|
|
|
|
config: &Config)
|
|
|
|
-> Result<(String, String), io::Error> {
|
|
|
|
let mut f = try!(File::open(filename));
|
|
|
|
let mut ori_text = String::new();
|
|
|
|
try!(f.read_to_string(&mut ori_text));
|
|
|
|
let mut v = Vec::new();
|
|
|
|
try!(write_system_newlines(&mut v, text, config));
|
|
|
|
let fmt_text = String::from_utf8(v).unwrap();
|
|
|
|
Ok((ori_text, fmt_text))
|
|
|
|
}
|
|
|
|
|
2016-01-08 21:38:27 -06:00
|
|
|
fn create_diff(filename: &str,
|
|
|
|
text: &StringBuffer,
|
|
|
|
config: &Config)
|
|
|
|
-> Result<Vec<Mismatch>, io::Error> {
|
|
|
|
let ori_text, fmt_text = try!(source_and_formatted_text(text, filename, config));
|
|
|
|
Ok(make_diff(&ori_text, &fmt_text, 3))
|
|
|
|
}
|
|
|
|
|
2015-08-01 08:02:59 -05:00
|
|
|
match mode {
|
2015-08-18 14:10:30 -05:00
|
|
|
WriteMode::Replace => {
|
2016-01-02 22:21:55 -06:00
|
|
|
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
|
|
|
if fmt != ori {
|
|
|
|
// Do a little dance to make writing safer - write to a temp file
|
|
|
|
// rename the original to a .bk, then rename the temp file to the
|
|
|
|
// original.
|
|
|
|
let tmp_name = filename.to_owned() + ".tmp";
|
|
|
|
let bk_name = filename.to_owned() + ".bk";
|
|
|
|
{
|
|
|
|
// Write text to temp file
|
|
|
|
let tmp_file = try!(File::create(&tmp_name));
|
|
|
|
try!(write_system_newlines(tmp_file, text, config));
|
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
|
2016-01-02 22:21:55 -06:00
|
|
|
try!(fs::rename(filename, bk_name));
|
|
|
|
try!(fs::rename(tmp_name, filename));
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
WriteMode::Overwrite => {
|
|
|
|
// Write text directly over original file.
|
|
|
|
let file = try!(File::create(filename));
|
|
|
|
try!(write_system_newlines(file, text, config));
|
|
|
|
}
|
2015-11-02 13:45:45 -06:00
|
|
|
WriteMode::Plain => {
|
|
|
|
let stdout = stdout();
|
2015-11-03 02:16:33 -06:00
|
|
|
let stdout = stdout.lock();
|
|
|
|
try!(write_system_newlines(stdout, text, config));
|
2015-11-02 13:45:45 -06:00
|
|
|
}
|
2015-10-21 02:26:24 -05:00
|
|
|
WriteMode::Display | WriteMode::Coverage => {
|
2015-08-18 14:10:30 -05:00
|
|
|
println!("{}:\n", filename);
|
|
|
|
let stdout = stdout();
|
2015-11-03 02:16:33 -06:00
|
|
|
let stdout = stdout.lock();
|
|
|
|
try!(write_system_newlines(stdout, text, config));
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|
2015-09-10 17:27:22 -05:00
|
|
|
WriteMode::Diff => {
|
|
|
|
println!("Diff of {}:\n", filename);
|
2016-01-02 22:21:55 -06:00
|
|
|
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
|
|
|
print_diff(make_diff(&ori, &fmt, 3),
|
|
|
|
|line_num| format!("\nDiff at line {}:", line_num));
|
|
|
|
}
|
2015-09-10 17:27:22 -05:00
|
|
|
}
|
2016-01-12 17:12:48 -06:00
|
|
|
WriteMode::Default => {
|
|
|
|
unreachable!("The WriteMode should NEVER Be default at this point!");
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|
2015-12-27 22:13:32 -06:00
|
|
|
WriteMode::Checkstyle => {
|
2015-12-29 22:54:35 -06:00
|
|
|
let stdout = stdout();
|
|
|
|
let stdout = stdout.lock();
|
2016-01-08 21:38:27 -06:00
|
|
|
let diff = try!(create_diff(filename, text, config));
|
2015-12-27 22:13:32 -06:00
|
|
|
// Output the XML tags for the lines that are different.
|
2016-01-08 21:38:27 -06:00
|
|
|
try!(output_checkstyle_file(stdout, filename, diff));
|
2015-12-27 22:13:32 -06:00
|
|
|
}
|
|
|
|
WriteMode::Return => {
|
|
|
|
// io::Write is not implemented for String, working around with
|
|
|
|
// Vec<u8>
|
|
|
|
let mut v = Vec::new();
|
|
|
|
try!(write_system_newlines(&mut v, text, config));
|
|
|
|
// won't panic, we are writing correct utf8
|
|
|
|
return Ok(Some(String::from_utf8(v).unwrap()));
|
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|
2015-08-01 08:02:59 -05:00
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|