-
Notifications
You must be signed in to change notification settings - Fork 138
/
find_hacks.pl
executable file
·120 lines (96 loc) · 3.94 KB
/
find_hacks.pl
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! perl
# Copyright (C) 2010, Parrot Foundation.
use strict;
use warnings;
use HTML::Entities;
=head1 NAME
tools/dev/find_hacks.pl - Generates a TracWiki formatted list of 'hack'
comments in Parrot source.
=head1 SYNOPSIS
$ perl tools/dev/find_hacks.pl
=head1 DESCRIPTION
This script greps through all text files in the directory that it's run in, and
looks for all instances of the word 'hack' in the source. It then outputs a
TracWiki formatted page that can be copied and pasted into Trac directly, complete
with links to the relevant source file on github.
If run with a non-clean source tree, generated files have a chance of matching, so
this tool is best run after a `make realclean'.
=cut
# Open our grep stream and then print a header.
open(my $hacks, '-|', 'grep -rIn hack .') || die "Could not grep! $!\n";
print '= Parrot HACK List =' . "\n\n";
while(<$hacks>) {
if(/^.\/(.+?):([0-9]+):(.*hack.*)$/i) {
# Save the regex data.
my $filename = $1, my $linenum = $2, my $context = $3;
# Make sure we're not triggering for 'hacker', though.
next if $context =~ /hacker/;
# And this script should not trigger.
next if $filename =~ /find_hacks.pl$/;
# Process context for HTML, then do some stuff to it too.
$context = encode_entities($context);
$context =~ s/hack/<strong><font color="red">hack<\/font><\/strong>/ig;
# See if we have an incomplete multi-line comment.
my $begin = $context =~ /\/\*/ && !($context =~ /\*\//);
my $end = $context =~ /\*\// && !($context =~ /\/\*/);
# Print out the main line data.
print " * [https://github.com/parrot/parrot/blob/master/$filename#L$linenum $filename:$linenum][[br]]\n{{{\n#!html\n";
print "<div class='wikipage' style='font-family: Consolas,Lucida Console,DejaVu Sans Mono,Bitstream Vera Sans Mono,monospace;'>";
if($begin) {
# First print out the beginning
$context =~ s/ / /g;
print $context . "<br>\n";
# Open the file and just slurp the entirety.
open(my $SOURCEFILE, '<', $filename) or die $!;
my $terminator = $/; undef $/;
my $sourcefile = <$SOURCEFILE>;
$/ = $terminator;
# Split based on newline and then keep printing until we get an end.
my @lines = split(/$terminator/, $sourcefile);
my $someline = $linenum;
do {
$lines[$someline] = encode_entities($lines[$someline]);
$lines[$someline] =~ s/ / /g;
print $lines[$someline] . "<br>\n";
} while !($lines[$someline++] =~ /\*\//);
close $SOURCEFILE;
}
elsif($end) {
# Open the file and read fully.
open(my $SOURCEFILE, '<', $filename) or die $!;
my $terminator = $/; undef $/;
my $sourcefile = <$SOURCEFILE>;
$/ = $terminator;
# Go back until we found the beginning of the comment.
my @lines = split(/$terminator/, $sourcefile);
my $someline = $linenum;
while (!($lines[--$someline] =~ /\/\*/)) { }
# And print from there to just before our line.
for(; $someline < ($linenum - 1); $someline++) {
$lines[$someline] = encode_entities($lines[$someline]);
$lines[$someline] =~ s/ / /g;
print $lines[$someline] . "<br>\n";
}
# Print out the last line.
print $context . "<br>\n";
close $SOURCEFILE;
}
else {
$context =~ s/^\s+//;
$context =~ s/\s+$//;
print $context . "<br>\n";
}
print "</div>\n}}}\n\n";
}
}
# Print a footer.
print '{{{
#!comment
Automatically generated by tools/dev/find_hacks.pl
}}}';
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: