I have an existing project that requires license headers to be used at the beginning of every source file. The problem is that the license header is not static:
#+======================================================================
# \$HeadURL [filled in by svn] \$
# \$Id [filled in by svn] \$
#
# Project : Project blah blah - only one line
#
# Description : A longer description
# which may or may not span multiple lines
#
# Author(s) : some author text
# but there may be a few more authors, too!
#
# Copyright (c) : 2010-YYYY Some company,
# and a few fixed lines of
# address
#
# and a few more lines of fixed license code
# that does not change
#
#-======================================================================
I have an existing perl script that scans a list of files to determine file type (C, Java, bash, etc.) and does a rudimentary check to see if a license preamble exists.
If it does not, it can insert a blank license header which must be manually updated.
But I would like to know how I can:
- Detect an existing license with non-static information, and
- Extend the existing perl processFile($fileName, $type) function (below) to preserve the existing "Project", "Description" and "Author(s)" information?
I suspect that I may need to place markers in the license templates to indicate dynamic text, which should be preserved in the regenerated header..?
Can you please give me pointers on how to use perl regex or pattern matchers to grab the current variable information so that I can re-insert it into the header and update the year?
I can see that all the magic needs to happen in the "for ($i = 0; $i < 5; ++$i)" loop...
sub processFile {
my $i;
my $lineno = 0;
my $filename = $_[0];
my $type = $_[1];
my @license = split(/\n/, $licenses{$type});
my @contents;
#print "$filename is a $type file\n";
tie @contents, 'Tie::File', $filename or die $!;
if ($prolog{$type}) { # should not insert license at line 0
my $len = scalar(@contents);
while ($lineno < $len) {
if ($contents[$lineno] =~ /^$prolog{$type}$/) {
last;
} else {
$lineno++;
}
}
if ($lineno >= $len) {
# no prolog, so let's just insert it into the start
$lineno = 0;
} else {
$lineno = $lineno + 1;
}
} else {
$lineno = 0;
}
# Compare the first 5 lines excluding prolog with the license
# header. If they match, the license header won't be inserted.
for ($i = 0; $i < 5; ++$i) {
my $line = $contents[$i + $lineno];
$line =~ s/\$(\w+)\:.*\$/\$$1\$/;
if ($line ne $license[$i]) {
splice @contents, $lineno, 0, @license;
push @processedFiles, $filename;
last
}
}
untie @contents;
}