|
3 | 3 | use warnings;
|
4 | 4 | use utf8;
|
5 | 5 | use autodie;
|
| 6 | +use IO::File; |
6 | 7 | use JSON::PP;
|
7 | 8 |
|
8 |
| -my $json = do { |
| 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->(slurp_utf8($file), $file); |
| 17 | + $content .= "\n" if $content !~ /\n\z/ms; |
| 18 | + |
| 19 | + # for keeping permission |
| 20 | + append_file($file, $content); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +sub retrieve_plugins { |
| 25 | + sort map {s/^$PLUGIN_PREFIX//; $_} <$PLUGIN_PREFIX*>; |
| 26 | +} |
| 27 | + |
| 28 | +sub update_readme { |
| 29 | + my @plugins = @_; |
| 30 | + |
| 31 | + my $doc_links = ''; |
| 32 | + for my $plug (@plugins) { |
| 33 | + $doc_links .= "* [$PLUGIN_PREFIX$plug](./$PLUGIN_PREFIX$plug/README.md)\n" |
| 34 | + } |
| 35 | + replace 'README.md' => sub { |
| 36 | + my $readme = shift; |
| 37 | + my $plu_reg = qr/$PLUGIN_PREFIX[-0-9a-zA-Z_]+/; |
| 38 | + $readme =~ s!(?:\* \[$plu_reg\]\(\./$plu_reg/README\.md\)\n)+!$doc_links!ms; |
| 39 | + $readme; |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +sub update_packaging_specs { |
| 44 | + my @plugins = @_; |
| 45 | + my $for_in = 'for i in ' . join(' ', @plugins) . '; do'; |
| 46 | + |
| 47 | + my $replace_sub = sub { |
| 48 | + my $content = shift; |
| 49 | + $content =~ s/for i in.*?;\s*do/$for_in/ms; |
| 50 | + $content; |
| 51 | + }; |
| 52 | + replace $_, $replace_sub for ("packaging/rpm/$PACKAGE_NAME*.spec", "packaging/deb-v2/debian/rules"); |
| 53 | +} |
| 54 | + |
| 55 | +#### |
| 56 | +# file utility |
| 57 | +#### |
| 58 | +sub slurp_utf8 { |
| 59 | + my $filename = shift; |
| 60 | + my $fh = IO::File->new($filename, "<:utf8"); |
9 | 61 | local $/;
|
10 |
| - open my $fh, '<', 'packaging/config.json'; |
11 |
| - <$fh> |
12 |
| -}; |
| 62 | + <$fh>; |
| 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 | + |
| 84 | +#### |
| 85 | +# some file generate task |
| 86 | +#### |
| 87 | + |
| 88 | +sub subtask { |
| 89 | + my @plugins = retrieve_plugins; |
| 90 | + update_readme(@plugins); |
| 91 | + my $config = load_packaging_confg; |
| 92 | + update_packaging_specs(@{ $config->{plugins} }); |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +subtask(); |
| 97 | + |
| 98 | +#### |
| 99 | +# go:generate task |
| 100 | +#### |
13 | 101 |
|
14 |
| -my @plugins = sort @{ decode_json($json)->{plugins}}; |
| 102 | +my @plugins = sort @{ load_packaging_confg()->{plugins}}; |
15 | 103 |
|
16 | 104 | my $imports = "";
|
17 | 105 | my $case = "";
|
|
0 commit comments