Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine file rewrite process #970

Merged
merged 7 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/create-release-pr.yml
Expand Up @@ -34,10 +34,6 @@ jobs:
NEXT=${{ steps.start.outputs.nextVersion }}
mv packaging/mackerel-agent-plugins_$CURRENT.orig.tar.gz packaging/mackerel-agent-plugins_$NEXT.orig.tar.gz

- run: |
cpanm -qn Path::Tiny
perl tool/update-docs.pl

- uses: mackerelio/mackerel-create-release-pull-request-action@main
with:
github_token: ${{ secrets.MACKERELBOT_GITHUB_TOKEN }}
Expand Down
112 changes: 107 additions & 5 deletions tool/gen_mackerel_plugin.pl
Expand Up @@ -3,15 +3,117 @@
use warnings;
use utf8;
use autodie;
use IO::File;
use JSON::PP;

my $json = do {
my $PLUGIN_PREFIX = 'mackerel-plugin-';
my $PACKAGE_NAME = 'mackerel-agent-plugins';

# refer Mackerel::ReleaseUtils
sub replace {
my ($glob, $code) = @_;
for my $file (glob $glob) {
my $content = $code->(slurp_utf8($file), $file);
$content .= "\n" if $content !~ /\n\z/ms;

# for keeping permission
append_file($file, $content);
}
}

sub retrieve_plugins {
# exclude plugins which has been moved to other repositories
sort map {s/^$PLUGIN_PREFIX//; $_} grep { -e "$_/lib" } <$PLUGIN_PREFIX*>;
}

sub update_readme {
my @plugins = @_;

my $doc_links = '';
for my $plug (@plugins) {
$doc_links .= "* [$PLUGIN_PREFIX$plug](./$PLUGIN_PREFIX$plug/README.md)\n"
}
replace 'README.md' => sub {
my $readme = shift;
my $plu_reg = qr/$PLUGIN_PREFIX[-0-9a-zA-Z_]+/;
$readme =~ s!(?:\* \[$plu_reg\]\(\./$plu_reg/README\.md\)\n)+!$doc_links!ms;
$readme;
};
}

sub update_packaging_specs {
my @plugins = @_;
my $for_in = 'for i in ' . join(' ', @plugins) . '; do';

my $replace_sub = sub {
my $content = shift;
$content =~ s/for i in.*?;\s*do/$for_in/ms;
$content;
};
replace $_, $replace_sub for ("packaging/rpm/$PACKAGE_NAME*.spec", "packaging/deb*/debian/rules");

write_file(
'packaging/deb/debian/source/include-binaries',
join("\n", map { "debian/$PLUGIN_PREFIX$_" } @plugins) . "\n"
);
}

sub update_packaging_binaries_list {
my @plugins = @_;
write_file(
'packaging/plugin-lists',
join("\n", map { "$PLUGIN_PREFIX$_" } @plugins) . "\n"
);
}

####
# file utility
####

sub slurp_utf8 {
my $filename = shift;
my $fh = IO::File->new($filename, "<:utf8");
local $/;
open my $fh, '<', 'packaging/config.json';
<$fh>
};
<$fh>;
}
sub write_file {
my $filename = shift;
my $content = shift;
my $fh = IO::File->new($filename, ">:utf8");
print $fh $content;
$fh->close;
}
sub append_file {
my $filename = shift;
my $content = shift;
my $fh = IO::File->new($filename, "+>:utf8");
print $fh $content;
$fh->close;
}

sub load_packaging_confg {
decode_json(slurp_utf8('packaging/config.json'));
}

####
# some file generate task
####

sub subtask {
my @plugins = retrieve_plugins;
update_readme(@plugins);
my $config = load_packaging_confg;
update_packaging_specs(sort @{ $config->{plugins} });
update_packaging_binaries_list(sort @{ $config->{plugins} });
}

subtask();

####
# go:generate task
####

my @plugins = sort @{ decode_json($json)->{plugins}};
my @plugins = sort @{ load_packaging_confg()->{plugins}};

sub resolve_package {
my $plug = shift;
Expand Down
79 changes: 0 additions & 79 deletions tool/update-docs.pl

This file was deleted.