1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#!/usr/bin/perl -i
#
# build/replace_license_blocks file ...
#
# Replace a <@LICENSE> ... </@LICENSE> delimited block of comment text inside
# an arbitrary number of files with the contents of the file named
# 'newlicense'. The comment character to use at start-of-line is read
# from the file being worked on, e.g.
#
# * <@LICENSE>
#
# will result in all lines of the license text being prefixed with " * ".
#
# do something like the following:
# - create a "newlicense" file with the license text as appropriate
# - run something like:
# grep -rl @LICENSE * | egrep -v 'replace_license_blocks|\.svn' | xargs build/replace_license_blocks
#
# read the new license text; die if not available
open (IN, "<newlicense") or die "cannot read 'newlicense'";
my $license = join ('', <IN>);
close IN;
# remove final comment if present
$license =~ s/\n \*\/$//gs;
# remove C comment start-of-line markers
$license =~ s/^(?:\/\* | \* | \*\/| \*)//gm;
# and fixup in-place
$in_license_block = 0;
while (<>) {
if ($in_license_block) {
# we're waiting until the end-of-license "closing tag"
if (s/^.+<\/\@LICENSE>//) {
$in_license_block = 0;
}
} else {
if (s{^(.+)<\@LICENSE>\s*$}{ license_with_prefix($1); }eg) {
$in_license_block = 1;
} else {
# a non-block form -- will be replaced with a block
s{^(.+)\@LICENSE\s*$}{ license_with_prefix($1); }eg;
}
print;
}
}
sub license_with_prefix {
my $prefix = shift;
# if the prefix is "/*" replace with " *"
my $orig = $prefix;
$prefix =~ s@^(\s*)/\*(.*)$@$1 *$2@;
my $text = $license; $text =~ s/^/$prefix/gm;
return $orig."<\@LICENSE>\n".
$text.
$prefix."</\@LICENSE>\n";
}
|