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:09:23 -04:00
|
|
|
my $data = process_file($file);
|
|
|
|
open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!";
|
2016-08-19 11:14:56 -04:00
|
|
|
# Write the data to $file.bak
|
|
|
|
#TODO: This could be done with no .bak
|
2016-08-19 11:09:23 -04:00
|
|
|
print MYOUTPUTFILE $data;
|
|
|
|
close(MYOUTPUTFILE);
|
|
|
|
if (compare($file, "$file.bak") != 0) {
|
2016-08-19 11:14:56 -04:00
|
|
|
# If there were changes, then tell the user
|
2016-08-19 11:09:23 -04:00
|
|
|
print "Processed $file\n";
|
|
|
|
}
|
2016-08-19 11:14:56 -04:00
|
|
|
# Replace the original file with this file
|
|
|
|
# Delete it
|
2016-08-19 11:09:23 -04:00
|
|
|
unlink $file;
|
2016-08-19 11:14:56 -04:00
|
|
|
# Overwrite the old one
|
2016-08-19 11:09:23 -04:00
|
|
|
rename "$file.bak", $file;
|
|
|
|
}
|
|
|
|
}
|
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: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: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";
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|