|
6 | 6 | use IO::File;
|
7 | 7 | use JSON::PP;
|
8 | 8 |
|
| 9 | +my $PLUGIN_PREFIX = 'check-'; |
| 10 | +my $PACKAGE_NAME = 'mackerel-check-plugins'; |
| 11 | + |
| 12 | +# refer Mackerel::ReleaseUtils |
| 13 | +sub replace { |
| 14 | + my ($glob, $code) = @_; |
| 15 | + for my $file (glob $glob) { |
| 16 | + my $content = $code->(path($file)->slurp_utf8, $file); |
| 17 | + $content .= "\n" if $content !~ /\n\z/ms; |
| 18 | + |
| 19 | + my $f = path($file); |
| 20 | + # for keeping permission |
| 21 | + $f->append_utf8({truncate => 1}, $content); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +sub retrieve_plugins { |
| 27 | + sort map {s/^$PLUGIN_PREFIX//; $_} <$PLUGIN_PREFIX*>; |
| 28 | +} |
| 29 | + |
| 30 | +sub update_readme { |
| 31 | + my @plugins = @_; |
| 32 | + |
| 33 | + my $doc_links = ''; |
| 34 | + for my $plug (@plugins) { |
| 35 | + $doc_links .= "* [$PLUGIN_PREFIX$plug](./$PLUGIN_PREFIX$plug/README.md)\n" |
| 36 | + } |
| 37 | + replace 'README.md' => sub { |
| 38 | + my $readme = shift; |
| 39 | + my $plu_reg = qr/$PLUGIN_PREFIX[-0-9a-zA-Z_]+/; |
| 40 | + $readme =~ s!(?:\* \[$plu_reg\]\(\./$plu_reg/README\.md\)\n)+!$doc_links!ms; |
| 41 | + $readme; |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +sub update_packaging_specs { |
| 46 | + my @plugins = @_; |
| 47 | + my $for_in = 'for i in ' . join(' ', @plugins) . '; do'; |
| 48 | + |
| 49 | + my $replace_sub = sub { |
| 50 | + my $content = shift; |
| 51 | + $content =~ s/for i in.*?;\s*do/$for_in/ms; |
| 52 | + $content; |
| 53 | + }; |
| 54 | + replace $_, $replace_sub for ("packaging/rpm/$PACKAGE_NAME*.spec", "packaging/deb-v2/debian/rules"); |
| 55 | +} |
| 56 | + |
9 | 57 | # file utility
|
10 | 58 | sub slurp_utf8 {
|
11 | 59 | my $filename = shift;
|
12 | 60 | my $fh = IO::File->new($filename, "<:utf8");
|
13 | 61 | local $/;
|
14 | 62 | <$fh>;
|
15 | 63 | }
|
| 64 | +sub write_file { |
| 65 | + my $filename = shift; |
| 66 | + my $content = shift; |
| 67 | + my $fh = IO::File->new($filename, ">:utf8"); |
| 68 | + print $fh $content; |
| 69 | + $fh->close; |
| 70 | +} |
| 71 | +sub append_file { |
| 72 | + my $filename = shift; |
| 73 | + my $content = shift; |
| 74 | + my $fh = IO::File->new($filename, "+>:utf8"); |
| 75 | + print $fh $content; |
| 76 | + $fh->close; |
| 77 | +} |
| 78 | + |
| 79 | +sub load_packaging_confg { |
| 80 | + decode_json(slurp_utf8('packaging/config.json')); |
| 81 | +} |
| 82 | + |
| 83 | +sub subtask { |
| 84 | + my @plugins = retrieve_plugins; |
| 85 | + update_readme(@plugins); |
| 86 | + my $config = load_packaging_confg; |
| 87 | + update_packaging_specs(@{ $config->{plugins} }); |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +subtask(); |
| 92 | + |
| 93 | +#### |
| 94 | +# go:generate task |
| 95 | +#### |
16 | 96 |
|
17 |
| -my @plugins = sort @{ decode_json(slurp_utf8('packaging/config.json'))->{plugins}}; |
| 97 | +my @plugins = sort @{ load_packaging_confg()->{plugins}}; |
18 | 98 |
|
19 | 99 | my $imports = "";
|
20 | 100 | my $case = "";
|
|
0 commit comments