2015-08-01 15:02:59 +02: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 20:21:06 +02:00
|
|
|
// TODO: add tests
|
2015-08-01 15:02:59 +02:00
|
|
|
|
2015-08-02 14:49:35 +02:00
|
|
|
use std::fs::{self, File};
|
2017-07-13 18:42:14 +09:00
|
|
|
use std::io::{self, BufWriter, Read, Write};
|
2017-12-08 14:16:47 +01:00
|
|
|
use std::path::Path;
|
2017-07-13 18:42:14 +09:00
|
|
|
|
|
|
|
use checkstyle::{output_checkstyle_file, output_footer, output_header};
|
|
|
|
use config::{Config, NewlineStyle, WriteMode};
|
2018-01-20 20:23:25 +00:00
|
|
|
use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch};
|
2017-12-08 14:16:47 +01:00
|
|
|
use syntax::codemap::FileName;
|
2015-08-01 15:02:59 +02:00
|
|
|
|
2018-02-07 22:48:05 +09:00
|
|
|
use FileRecord;
|
2015-08-01 15:02:59 +02:00
|
|
|
|
|
|
|
// Append a newline to the end of each file.
|
2017-12-10 17:40:51 +09:00
|
|
|
pub fn append_newline(s: &mut String) {
|
2016-05-15 21:41:05 +12:00
|
|
|
s.push_str("\n");
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-30 15:04:19 +01:00
|
|
|
pub fn write_all_files<T>(
|
|
|
|
file_map: &[FileRecord],
|
|
|
|
out: &mut T,
|
|
|
|
config: &Config,
|
|
|
|
) -> Result<(), io::Error>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-19 00:02:21 -05:00
|
|
|
{
|
2017-05-16 15:47:09 +07:00
|
|
|
output_header(out, config.write_mode()).ok();
|
2016-05-15 21:41:05 +12:00
|
|
|
for &(ref filename, ref text) in file_map {
|
2017-05-08 13:13:49 +09:00
|
|
|
write_file(text, filename, out, config)?;
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|
2017-05-16 15:47:09 +07:00
|
|
|
output_footer(out, config.write_mode()).ok();
|
2015-08-01 15:02:59 +02:00
|
|
|
|
2015-12-29 23:54:35 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:12:48 -05:00
|
|
|
// Prints all newlines either as `\n` or as `\r\n`.
|
2017-12-12 13:48:12 +09:00
|
|
|
pub fn write_system_newlines<T>(writer: T, text: &str, config: &Config) -> Result<(), io::Error>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-12 18:12:48 -05:00
|
|
|
{
|
|
|
|
// Buffer output, since we're writing a since char at a time.
|
|
|
|
let mut writer = BufWriter::new(writer);
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
let style = if config.newline_style() == NewlineStyle::Native {
|
2016-01-12 18:12:48 -05:00
|
|
|
if cfg!(windows) {
|
|
|
|
NewlineStyle::Windows
|
2015-11-15 03:34:04 -08:00
|
|
|
} else {
|
2016-01-12 18:12:48 -05:00
|
|
|
NewlineStyle::Unix
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-16 15:47:09 +07:00
|
|
|
config.newline_style()
|
2016-01-12 18:12:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match style {
|
|
|
|
NewlineStyle::Unix => write!(writer, "{}", text),
|
|
|
|
NewlineStyle::Windows => {
|
2017-12-10 17:40:51 +09:00
|
|
|
for c in text.chars() {
|
2016-01-12 18:12:48 -05:00
|
|
|
match c {
|
2017-05-08 13:13:49 +09:00
|
|
|
'\n' => write!(writer, "\r\n")?,
|
2016-01-12 18:12:48 -05:00
|
|
|
'\r' => continue,
|
2017-05-08 13:13:49 +09:00
|
|
|
c => write!(writer, "{}", c)?,
|
2015-08-16 15:58:17 +12:00
|
|
|
}
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|
2016-01-12 18:12:48 -05:00
|
|
|
Ok(())
|
2015-08-16 15:58:17 +12:00
|
|
|
}
|
2016-01-12 18:12:48 -05:00
|
|
|
NewlineStyle::Native => unreachable!(),
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|
2016-01-12 18:12:48 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn write_file<T>(
|
2017-12-12 13:48:12 +09:00
|
|
|
text: &str,
|
2017-12-08 14:16:47 +01:00
|
|
|
filename: &FileName,
|
2017-06-12 15:58:58 +12:00
|
|
|
out: &mut T,
|
|
|
|
config: &Config,
|
|
|
|
) -> Result<bool, io::Error>
|
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-19 00:02:21 -05:00
|
|
|
{
|
2017-06-12 15:58:58 +12:00
|
|
|
fn source_and_formatted_text(
|
2017-12-12 13:48:12 +09:00
|
|
|
text: &str,
|
2017-12-08 14:16:47 +01:00
|
|
|
filename: &Path,
|
2017-06-12 15:58:58 +12:00
|
|
|
config: &Config,
|
|
|
|
) -> Result<(String, String), io::Error> {
|
2017-05-08 13:13:49 +09:00
|
|
|
let mut f = File::open(filename)?;
|
2016-01-02 23:21:55 -05:00
|
|
|
let mut ori_text = String::new();
|
2017-05-08 13:13:49 +09:00
|
|
|
f.read_to_string(&mut ori_text)?;
|
2016-01-02 23:21:55 -05:00
|
|
|
let mut v = Vec::new();
|
2017-05-08 13:13:49 +09:00
|
|
|
write_system_newlines(&mut v, text, config)?;
|
2016-01-02 23:21:55 -05:00
|
|
|
let fmt_text = String::from_utf8(v).unwrap();
|
|
|
|
Ok((ori_text, fmt_text))
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
fn create_diff(
|
2017-12-08 14:16:47 +01:00
|
|
|
filename: &Path,
|
2017-12-12 13:48:12 +09:00
|
|
|
text: &str,
|
2017-06-12 15:58:58 +12:00
|
|
|
config: &Config,
|
|
|
|
) -> Result<Vec<Mismatch>, io::Error> {
|
2017-05-08 13:13:49 +09:00
|
|
|
let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
|
2016-01-13 00:22:30 -05:00
|
|
|
Ok(make_diff(&ori, &fmt, 3))
|
2016-01-08 22:38:27 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 14:16:47 +01:00
|
|
|
let filename_to_path = || match *filename {
|
|
|
|
FileName::Real(ref path) => path,
|
|
|
|
_ => panic!("cannot format `{}` with WriteMode::Replace", filename),
|
|
|
|
};
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
match config.write_mode() {
|
2015-08-18 21:10:30 +02:00
|
|
|
WriteMode::Replace => {
|
2017-12-08 14:16:47 +01:00
|
|
|
let filename = filename_to_path();
|
2017-12-24 00:28:58 +09:00
|
|
|
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
2016-01-02 23:21:55 -05:00
|
|
|
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.
|
2017-12-08 14:16:47 +01:00
|
|
|
let tmp_name = filename.with_extension("tmp");
|
|
|
|
let bk_name = filename.with_extension("bk");
|
2016-01-02 23:21:55 -05:00
|
|
|
{
|
|
|
|
// Write text to temp file
|
2017-05-08 13:13:49 +09:00
|
|
|
let tmp_file = File::create(&tmp_name)?;
|
|
|
|
write_system_newlines(tmp_file, text, config)?;
|
2016-01-02 23:21:55 -05:00
|
|
|
}
|
2015-08-18 21:10:30 +02:00
|
|
|
|
2017-05-08 13:13:49 +09:00
|
|
|
fs::rename(filename, bk_name)?;
|
|
|
|
fs::rename(tmp_name, filename)?;
|
2016-01-02 23:21:55 -05:00
|
|
|
}
|
|
|
|
}
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|
2015-08-18 21:10:30 +02:00
|
|
|
WriteMode::Overwrite => {
|
2017-06-06 15:33:46 +09:00
|
|
|
// Write text directly over original file if there is a diff.
|
2017-12-08 14:16:47 +01:00
|
|
|
let filename = filename_to_path();
|
2017-12-24 00:28:58 +09:00
|
|
|
let (source, formatted) = source_and_formatted_text(text, filename, config)?;
|
2017-06-06 15:33:46 +09:00
|
|
|
if source != formatted {
|
|
|
|
let file = File::create(filename)?;
|
|
|
|
write_system_newlines(file, text, config)?;
|
|
|
|
}
|
2015-08-18 21:10:30 +02:00
|
|
|
}
|
2015-11-02 20:45:45 +01:00
|
|
|
WriteMode::Plain => {
|
2017-05-08 13:13:49 +09:00
|
|
|
write_system_newlines(out, text, config)?;
|
2015-11-02 20:45:45 +01:00
|
|
|
}
|
2015-10-21 12:56:24 +05:30
|
|
|
WriteMode::Display | WriteMode::Coverage => {
|
2015-08-18 21:10:30 +02:00
|
|
|
println!("{}:\n", filename);
|
2017-05-08 13:13:49 +09:00
|
|
|
write_system_newlines(out, text, config)?;
|
2015-08-18 21:10:30 +02:00
|
|
|
}
|
2015-09-10 18:27:22 -04:00
|
|
|
WriteMode::Diff => {
|
2017-12-08 14:16:47 +01:00
|
|
|
let filename = filename_to_path();
|
2017-12-24 00:28:58 +09:00
|
|
|
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
2016-06-20 20:42:29 +02:00
|
|
|
let mismatch = make_diff(&ori, &fmt, 3);
|
|
|
|
let has_diff = !mismatch.is_empty();
|
2017-11-10 07:23:12 +11:00
|
|
|
print_diff(
|
|
|
|
mismatch,
|
2017-12-08 14:16:47 +01:00
|
|
|
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
|
2017-11-10 07:23:12 +11:00
|
|
|
config.color(),
|
|
|
|
);
|
2016-06-20 20:42:29 +02:00
|
|
|
return Ok(has_diff);
|
2016-01-02 23:21:55 -05:00
|
|
|
}
|
2015-09-10 18:27:22 -04:00
|
|
|
}
|
2018-01-20 20:23:25 +00:00
|
|
|
WriteMode::Modified => {
|
|
|
|
let filename = filename_to_path();
|
|
|
|
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
|
|
|
let mismatch = make_diff(&ori, &fmt, 0);
|
|
|
|
let has_diff = !mismatch.is_empty();
|
|
|
|
output_modified(out, mismatch);
|
|
|
|
return Ok(has_diff);
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 23:13:32 -05:00
|
|
|
WriteMode::Checkstyle => {
|
2017-12-08 14:16:47 +01:00
|
|
|
let filename = filename_to_path();
|
2017-12-24 00:28:58 +09:00
|
|
|
let diff = create_diff(filename, text, config)?;
|
2017-05-08 13:13:49 +09:00
|
|
|
output_checkstyle_file(out, filename, diff)?;
|
2015-12-27 23:13:32 -05:00
|
|
|
}
|
2015-08-18 21:10:30 +02:00
|
|
|
}
|
2015-08-01 15:02:59 +02:00
|
|
|
|
2016-06-20 20:42:29 +02:00
|
|
|
// when we are not in diff mode, don't indicate differing files
|
|
|
|
Ok(false)
|
2015-08-01 15:02:59 +02:00
|
|
|
}
|