Don't write .bak file, write only if there were changes
This commit is contained in:
parent
b00004f73e
commit
9c691994d0
28
process.pl
28
process.pl
@ -10,21 +10,17 @@ foreach my $file (@ARGV) {
|
||||
# If it exists
|
||||
if (-e $file) {
|
||||
# Load the autoformatted data into $data
|
||||
my $data = process_file($file);
|
||||
open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!";
|
||||
# Write the data to $file.bak
|
||||
#TODO: This could be done with no .bak
|
||||
print MYOUTPUTFILE $data;
|
||||
close(MYOUTPUTFILE);
|
||||
if (compare($file, "$file.bak") != 0) {
|
||||
# If there were changes, then tell the user
|
||||
my ($data, $old_data) = process_file($file);
|
||||
# Check if original data matches new data, if it doesn't, write it
|
||||
# If it matches (there were no changes made), do nothing
|
||||
if (not $data eq $old_data) {
|
||||
# Write out the data
|
||||
open(MYOUTPUTFILE, "+>$file") or die "Error opening output file $!";
|
||||
print MYOUTPUTFILE $data;
|
||||
close(MYOUTPUTFILE);
|
||||
# Tell the user that we changed the file
|
||||
print "Processed $file\n";
|
||||
}
|
||||
# Replace the original file with this file
|
||||
# Delete it
|
||||
unlink $file;
|
||||
# Overwrite the old one
|
||||
rename "$file.bak", $file;
|
||||
}
|
||||
}
|
||||
# Creates the automatically formatted file
|
||||
@ -33,8 +29,12 @@ sub process_file {
|
||||
open(MYINPUTFILE, "<$filename") or die "x $!";
|
||||
# The formatted file
|
||||
my($data) = "";
|
||||
# The original file (used for comparison to see if changes were made)
|
||||
my($old_data) = "";
|
||||
# Loop through every line in the file
|
||||
while(<MYINPUTFILE>) {
|
||||
# Add data to $old_data
|
||||
$old_data .= $_;
|
||||
# Get rid of trailing whitespace
|
||||
s/\s*$//;
|
||||
my($line) = $_;
|
||||
@ -54,5 +54,5 @@ sub process_file {
|
||||
my $basename = basename($filename);
|
||||
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
|
||||
}
|
||||
return $data;
|
||||
return ($data, $old_data);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user