我有一个现有项目,需要在每个源文件的开头使用许可证头。问题是许可证标头不是静态的:
#+======================================================================
# \$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
#
#-======================================================================
我有一个现有的 perl 脚本,它扫描文件列表以确定文件类型(C、Java、bash 等)并进行基本检查以查看是否存在许可证序言。
如果没有,它可以插入必须手动更新的空白许可证标题。
但我想知道我该怎么做:
- 检测具有非静态信息的现有许可证,以及
- 扩展现有的 perl processFile($fileName, $type)函数(如下)以保留现有的“项目”、“描述”和“作者”信息?
我怀疑我可能需要在许可证模板中放置标记以指示动态文本,这应该保留在重新生成的标题中..?
您能否给我一些关于如何使用 perl 正则表达式或模式匹配器来获取当前变量信息的指示,以便我可以将其重新插入标题并更新年份?
我可以看到所有的魔法都需要在“for ($i = 0; $i < 5; ++$i)”循环中发生......
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;
}