/
generate_single_file_script.p6
63 lines (49 loc) · 1.66 KB
/
generate_single_file_script.p6
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
use File::Find;
my @files = find(dir => "lib", name => *.ends-with("pm6"));
my %modules;
my %dependencies;
for @files -> $fn {
next if $fn.IO.d;
my $c = $fn.IO.slurp;
note "working on $fn";
my $modname;
$c.=subst(/« use \s+ v6.*?\;/, "");
if $c.contains: "unit " {
$c = $c.subst(/« unit \s* (.*?) [":ver<".*?">:auth<".*?">"]?\;/,
{ $modname = $_[0].Str.words.tail; $_[0].Str ~ q[ {] }) ~ "\n}"
}
else {
$modname = $c.match(/« module <("App::MoarVM::ConfprogCompiler".*?)>\;/).Str;
}
die "could not determine what module this is" unless $modname;
note " module ($modname)\n";
%dependencies{$modname} = [];
#$c .= subst(/^^ "=begin " (\w+) .*? "=end " $0 $/, "");
$c .= subst(/\s*use\s+("App::MoarVM::ConfprogCompiler".*?)\;/, {
if $_[0].Str ne $modname {
note " depends on $_[0].Str()";
%dependencies{$modname}.push: $_[0].Str;
"\nimport $_[0].Str();"
}
else {
"\nuse $_[0].Str()";
}
}, :g);
%modules{$modname} = $c;
note "";
}
note "writing out modules in dependency order...";
my %written_out;
while %modules > %written_out {
for %dependencies.pairs.grep({ .value (<=) %written_out }) {
if .key !(elem) %written_out {
say %modules{.key};
note " $_.key()";
}
%written_out{.key} = 1
}
}
note "done\n";
note "concatenating script from bin/";
"bin/confprog-compile".IO.slurp.subst(/ ^^ "#!" .*? $$ /, "").subst(/« use \s+ v6 .*? \;/, "").subst(/« <( use )> \s+ "App::MoarVM::ConfprogCompiler" /, "import").say;
note "single-file script created!";