首页 > 代码库 > 用prel实现的文件头注释工具

用prel实现的文件头注释工具

最近想开源一套软件,对于一些代码都要在文件头上加上固定格式的版本信息。自己用perl写了一个小工具,拿出来分享。

 

  1 #!/usr/bin/perl -W  2 #  3 # Copyright (c) 2014 Nijie. All rights reserved.  4 # License: GPL-2  5 #  6 # File: comments.pl  用于增加c/c++文件注释  7 # Create by Nijie 2014.07.23  8 #  9  10 use strict; 11 use warnings; 12 use File::Copy; 13  14 my $author=Nijie;            # 作者 - 需修改 15 my $createyear=2014;        # 版权时间 - 需修改 16 my $date="2014.07.23";        # 文件创建时间 - 需修改 17  18 # 以下版权信息 - 需修改 19 my $comments="// Created by $author on $date. 20 // Copyright (c) $createyear $author. All rights reserved. 21 // Use of this source code is governed by a GPL-2 license that can be found in the LICENSE file.  22 // 23 "; 24  25 my @filterDir = ("./lib3rd/");    # 需要过滤的文件,比如不属于你开发的目录等 - 需修改 26  27 my %filterList = (); 28 foreach (@filterDir) 29 { 30     $filterList{$_} = 1; 31 } 32  33 my ($dircnt, $filecnt) = (0, 0); 34  35 sub lsr_s($) { 36     my $cwd = shift; 37     my @dirs = ($cwd./); 38  39     my ($dir, $file); 40     while ($dir = pop(@dirs)) { 41         if (exists($filterList{$dir})) 42         { 43             print "skip dir : $dir\n"; 44             next; 45         } 46         local *DH; 47         print "open $dir\n"; 48         if (!opendir(DH, $dir)) { 49             warn "Cannot opendir $dir: $! $^E"; 50             next; 51         } 52         foreach (readdir(DH)) { 53             if ($_ eq . || $_ eq ..) { 54                 next; 55             } 56             $file = $dir.$_;          57             if (!-l $file && -d _) { 58                 $file .= /; 59                 push(@dirs, $file); 60             } 61             process($file, $dir); 62         } 63         closedir(DH); 64     } 65 } 66  67 sub process($) { 68     my $file = shift; 69     print "process $file"; 70     if (substr($file, length($file)-1, 1) eq /) { 71         $dircnt++; 72     } 73     else { 74         $filecnt++; 75         if ($file =~ /\.(h$|c$|cpp$|hpp$|cc$)/) 76         { 77             addComments($file); 78         } 79     } 80     print "\n"; 81 } 82  83 sub addComments($) 84 { 85     my $line = 0; 86     my $file = shift; 87     open FILE, $file or die die ($!); 88     open OUTFILE, ">$file~~~" or die "Open the file $file~~~ failed!\n"; 89  90     print OUTFILE $comments; 91     while (<FILE>) 92     { 93         print OUTFILE $_; 94     } 95     close FILE; 96     close OUTFILE; 97     move("$file~~~", $file); 98 } 99 100 lsr_s(.);101 print "$filecnt files, $dircnt directory.\n";102 `pause`;