Skip to content

Commit 63e7678

Browse files
committed
copied from update-docs.pl
1 parent 20db016 commit 63e7678

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

tool/gen_mackerel_check.pl

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,95 @@
66
use IO::File;
77
use JSON::PP;
88

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+
957
# file utility
1058
sub slurp_utf8 {
1159
my $filename = shift;
1260
my $fh = IO::File->new($filename, "<:utf8");
1361
local $/;
1462
<$fh>;
1563
}
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+
####
1696

17-
my @plugins = sort @{ decode_json(slurp_utf8('packaging/config.json'))->{plugins}};
97+
my @plugins = sort @{ load_packaging_confg()->{plugins}};
1898

1999
my $imports = "";
20100
my $case = "";

0 commit comments

Comments
 (0)