Skip to content

Commit

Permalink
Merge pull request #89 from avencera/avoid-write
Browse files Browse the repository at this point in the history
Only print and write files if contents has changed
  • Loading branch information
praveenperera authored Dec 20, 2023
2 parents 19c2b9e + 6a7155a commit c251262
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,24 @@ fn run_on_file_paths(file_path: &Path, options: &Options) {
Ok(contents) => {
if sorter::has_classes(&contents, options) {
let sorted_content = sorter::sort_file_contents(&contents, options);
let contents_changed = sorted_content != contents;

match &options.write_mode {
WriteMode::ToStdOut => (),
WriteMode::DryRun => print_file_name(file_path, options),
WriteMode::ToFile => write_to_file(file_path, &sorted_content, options),
WriteMode::ToConsole => print_file_contents(&sorted_content),
WriteMode::CheckFormatted => {
print_changed_files(file_path, &sorted_content, &contents, options);
match (contents_changed, &options.write_mode) {
(_, WriteMode::ToStdOut) => (),
(_, WriteMode::DryRun) => print_file_name(file_path, options),

(true, WriteMode::ToFile) => write_to_file(file_path, &sorted_content, options),
(false, WriteMode::ToFile) => print_file_name(file_path, options),

// For now print the file contents to the console even if it hasn't changed to
// keep consistent with how rustywind has always worked. But in a later
// breaking release add a `--print-unchanged` flag to get the old behavior back
// but default to not printing unchanged files.
(true, WriteMode::ToConsole) => print_file_contents(&sorted_content),
(false, WriteMode::ToConsole) => print_file_contents(&sorted_content),

(_, WriteMode::CheckFormatted) => {
print_changed_files(file_path, contents_changed, options);
}
}
}
Expand All @@ -170,13 +180,8 @@ fn run_on_file_paths(file_path: &Path, options: &Options) {
}
}

fn print_changed_files(
file_path: &Path,
sorted_content: &str,
original_content: &str,
options: &Options,
) {
if sorted_content != original_content {
fn print_changed_files(file_path: &Path, contents_changed: bool, options: &Options) {
if contents_changed {
if !EXIT_ERROR.load(Ordering::Relaxed) {
EXIT_ERROR.store(true, Ordering::Relaxed);
}
Expand Down

0 comments on commit c251262

Please sign in to comment.