-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-corpus.pl
executable file
·164 lines (122 loc) · 3.72 KB
/
build-corpus.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use lib 'tools';
use Local::MissingModule;
use Local::VimColor;
use Local::VimFolds;
use Local::Utils;
use File::Temp;
use JSON qw(encode_json);
sub insert_into_tree {
my ( $tree, $path, $content ) = @_;
my @path_components = split(qr{/}, $path);
my $filename = pop @path_components;
foreach my $component (@path_components) {
unless(exists $tree->{$component}) {
$tree->{$component} = {};
}
if(ref($tree->{$component}) ne 'HASH') { # if it's a leaf
die 'freak out';
}
$tree = $tree->{$component};
}
$tree->{$filename} = $content;
}
sub two_way_pipe {
my $input = pop;
my @cmd = @_;
my ( $child_read, $child_write, $parent_read, $parent_write );
pipe($child_read, $parent_write);
pipe($parent_read, $child_write);
my $pid = fork;
if($pid) {
close $child_read;
close $child_write;
print { $parent_write } $input;
close $parent_write;
my $output = do {
local $/;
<$parent_read>;
};
close $parent_read;
waitpid $pid, 0;
if($? != 0) {
local $" = ' ';
die "Running @cmd exited with a non-zero status";
}
return $output;
} else {
close $parent_read;
close $parent_write;
open STDIN, '<&', $child_read;
open STDOUT, '>&', $child_write;
exec @cmd;
exit 255;
}
}
sub create_blob {
my ( $contents ) = @_;
my $blob_id = two_way_pipe('git', 'hash-object', '-w', '--stdin', $contents);
chomp $blob_id;
return $blob_id;
}
sub create_tree {
my ( $tree ) = @_;
my @files;
foreach my $filename (sort keys %$tree) {
my $contents = $tree->{$filename};
if(ref($contents) eq 'HASH') {
my $tree_id = create_tree($contents);
push @files, "040000 tree $tree_id\t$filename";
} else {
my $blob_id = create_blob($contents);
push @files, "100644 blob $blob_id\t$filename";
}
}
my $content = join("\n", @files);
my $object_id = two_way_pipe('git', 'mktree', $content);
chomp $object_id;
return $object_id;
}
my $color = Local::VimColor->new(
language => 'perl',
);
my $fold = Local::VimFolds->new(
options => {
perl_fold => 1,
perl_nofold_packages => 1,
perl_fold_anonymous_subs => 1,
},
language => 'perl',
);
my $json = JSON->new->utf8->canonical;
my $iter = get_blob_iterator('origin/p5-corpus', 'corpus');
my $tree = {};
while(my ( $filename, $contents ) = $iter->()) {
next unless $filename =~ /(?:pm|pl)\z/;
my $source = File::Temp->new;
print { $source } $contents;
close $source;
my $html = $color->color_file($source->filename);
my @folds = $fold->_get_folds($source->filename);
my $html_filename = $filename;
$html_filename =~ s{\Acorpus/}{};
$html_filename .= '.html';
my $folds_filename = $filename;
$folds_filename =~ s{\Acorpus/}{};
$folds_filename .= '-folds.json';
insert_into_tree($tree, $html_filename, $html);
insert_into_tree($tree, $folds_filename, $json->encode(\@folds));
}
$tree = create_tree($tree);
my $corpus_tree = find_git_object('origin/p5-corpus', 'corpus');
$tree = two_way_pipe('git', 'mktree', "040000 tree $tree\tcorpus_html\n040000 tree $corpus_tree\tcorpus\n");
chomp $tree;
open my $pipe, '-|', 'git', 'commit-tree', '-m', 'Update Perl 5 corpus', $tree;
my $commit = <$pipe>;
close $pipe;
chomp $commit;
system 'git', 'update-ref', 'refs/heads/p5-corpus', $commit;
system 'git', 'push', 'origin', '--force', 'p5-corpus:p5-corpus';