2016-08-19 11:09:23 -04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use autodie;
|
|
|
|
use warnings;
|
|
|
|
use File::Compare;
|
|
|
|
use File::Basename;
|
|
|
|
no warnings 'once';
|
2016-08-19 11:14:56 -04:00
|
|
|
# For every argument (file)
|
2016-08-19 11:09:23 -04:00
|
|
|
foreach my $file (@ARGV) {
|
2016-08-19 11:14:56 -04:00
|
|
|
# If it exists
|
2016-08-19 11:09:23 -04:00
|
|
|
if (-e $file) {
|
2016-08-19 11:14:56 -04:00
|
|
|
# Load the autoformatted data into $data
|
2016-08-19 11:23:25 -04:00
|
|
|
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
|
2016-08-19 11:09:23 -04:00
|
|
|
print "Processed $file\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 11:14:56 -04:00
|
|
|
# Creates the automatically formatted file
|
2016-08-19 11:09:23 -04:00
|
|
|
sub process_file {
|
|
|
|
my($filename) = "$_[0]";
|
|
|
|
open(MYINPUTFILE, "<$filename") or die "x $!";
|
2016-08-19 11:14:56 -04:00
|
|
|
# The formatted file
|
2016-08-19 11:09:23 -04:00
|
|
|
my($data) = "";
|
2016-08-19 11:23:25 -04:00
|
|
|
# The original file (used for comparison to see if changes were made)
|
|
|
|
my($old_data) = "";
|
2016-08-19 11:14:56 -04:00
|
|
|
# Loop through every line in the file
|
2016-08-19 11:09:23 -04:00
|
|
|
while(<MYINPUTFILE>) {
|
2016-08-19 11:23:25 -04:00
|
|
|
# Add data to $old_data
|
|
|
|
$old_data .= $_;
|
2016-08-19 11:14:56 -04:00
|
|
|
# Get rid of trailing whitespace
|
2016-08-19 11:09:23 -04:00
|
|
|
s/\s*$//;
|
|
|
|
my($line) = $_;
|
2016-08-19 11:14:56 -04:00
|
|
|
# If it is not a whitespace only line, append it to $data
|
2016-08-19 11:09:23 -04:00
|
|
|
if($line !~ /^\s*$/) {
|
|
|
|
$data = $data . "$line\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(MYINPUTFILE);
|
2016-08-19 11:14:56 -04:00
|
|
|
# Get rid of final newline
|
2016-08-19 11:09:23 -04:00
|
|
|
chomp($data);
|
|
|
|
if ($data !~ /^\s*\/\*\*/) {
|
2016-08-19 11:14:56 -04:00
|
|
|
# Get the basename of the file, this prevents making the block comment say:
|
|
|
|
# @file src/folders/code.c
|
|
|
|
# And instead uses
|
|
|
|
# @file code.c
|
2016-08-19 11:09:23 -04:00
|
|
|
my $basename = basename($filename);
|
|
|
|
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
|
|
|
|
}
|
2016-08-20 19:26:41 -04:00
|
|
|
$data .= "\n";
|
2016-08-19 11:23:25 -04:00
|
|
|
return ($data, $old_data);
|
2016-08-19 11:09:23 -04:00
|
|
|
}
|