autoformat/process.pl
2017-01-07 23:52:59 -05:00

60 lines
1.7 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
use autodie;
use warnings;
use File::Compare;
use File::Basename;
no warnings 'once';
# For every argument (file)
foreach my $file (@ARGV) {
# If it exists
if (-e $file) {
# Load the autoformatted data into $data
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";
}
}
}
# Creates the automatically formatted file
sub process_file {
my($filename) = "$_[0]";
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) = $_;
# If it is not a whitespace only line, append it to $data
if($line !~ /^\s*$/) {
$data = $data . "$line\n";
}
}
close(MYINPUTFILE);
# Get rid of final newline
chomp($data);
if ($data !~ /^\s*\/\*\*/) {
# Get the basename of the file, this prevents making the block comment say:
# @file src/folders/code.c
# And instead uses
# @file code.c
my $basename = basename($filename);
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
}
$data .= "\n";
return ($data, $old_data);
}