-
Notifications
You must be signed in to change notification settings - Fork 138
/
dedeprecator.nqp
executable file
·137 lines (122 loc) · 3.46 KB
/
dedeprecator.nqp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env parrot-nqp
# Copyright (C) 2011-2012, Parrot Foundation.
=begin README
dedepracator: find deprecations in your Parrot code
This script, fed with a yaml file with Parrot deprecations
(you probably want it to be Parrot's api.yaml), will scan your code
for a deprecations and warn you about them.
Usage:
# scan the current directory for deprecations
$ dedeprecator.nqp
# scan test.pir for deprecations, using api.yaml
$ dedeprecator.nqp test.pir
# scan bar.pir and baz.pir using the alternate api.yaml file
$ dedeprecator.nqp --apiyaml /somewhere/else/foo.yaml bar.pir baz.pir
# in case you forgot everything
$ dedeprecator.nqp -h
# or
$ dedeprecator.nqp --help
=end README
INIT {
pir::load_bytecode("YAML/Tiny.pbc");
}
sub check_pir($file, @regexes) {
my @deprecations;
my $fh := pir::new('FileHandle');
$fh.open($file);
$fh.encoding('utf8');
my $line := 1;
while $fh.readline -> $l {
for @regexes -> $regex {
my $r := $regex[0];
if $l ~~ / $r / {
@deprecations.push("$file:$line: { $regex[2] }");
}
}
$line++;
}
$fh.close;
return @deprecations;
}
MAIN(pir::getinterp()[2]);
sub MAIN(@ARGS) {
my $name := @ARGS.shift;
my @files;
my $apiyaml := 'api.yaml';
# getopt
my $arg;
while pir::elements(@ARGS) {
$arg := @ARGS.shift;
if $arg eq '--help' || $arg eq '-h' {
USAGE($name);
} elsif $arg eq '--apiyaml' {
unless pir::elements(@ARGS) {
say("--apiyaml requires an argument");
USAGE($name);
}
$apiyaml := @ARGS.shift;
} else {
@files.push($arg);
}
}
# prepare the regexes
my $parser := YAML::Tiny.new;
my $api;
try {
$api := $parser.read_string(slurp($apiyaml));
CATCH {
say("Failed parsing '$apiyaml'\n"
~ "Make sure that it exists and is a valid YAML");
pir::exit(1);
}
}
my @regs_pir;
for $api[0] {
if $_<detection> && $_<detection><regex> {
if $_<detection><regex><pir> {
my $r := $_<detection><regex><pir>;
@regs_pir.push(
[Regex::P6Regex::Compiler.compile($r), $r, $_<name>]
);
}
}
}
# if no files are given, find all files in the current directory,
# recursively
unless pir::elements(@files) {
my $os := pir::new('OS');
my @candidates := $os.readdir('.');
while pir::elements(@candidates) {
my $file := @candidates.shift;
# this probably can be written better
next if $file ~~ /[^ '.' $] | [^ '..' $]
| ['/..' $] | ['/.' $]/;
@files.push($file);
try { # try is isdir said funny
for $os.readdir($file) {
@candidates.push("$file/$_");
}
}
}
}
# check the given files
for @files -> $f {
if $f ~~ / '.pir' $ / {
my @d;
try {
@d := check_pir($f, @regs_pir);
CATCH {
say("Failed checking $f: $!");
}
}
if pir::elements(@d) {
say(@d.join("\n"));
}
}
}
}
sub USAGE($name) {
say("Usage: $name [--apiyaml <api.yaml file>] [<files to check>]");
pir::exit(1);
}
# vim: ft=perl6