Skip to content

Commit

Permalink
Merge pull request #30 from yanick/filemunge
Browse files Browse the repository at this point in the history
add a filemunge phase
  • Loading branch information
fayland committed Feb 18, 2018
2 parents 9e2f9ab + 8bc7c81 commit ff5f23d
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions lib/Dist/Zilla/Plugin/GitHubREADME/Badge.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use Path::Tiny;
with qw(
Dist::Zilla::Role::AfterBuild
Dist::Zilla::Role::AfterRelease
Dist::Zilla::Role::FileMunger
);

has badges => (
Expand All @@ -28,10 +29,40 @@ has 'place' => ( is => 'rw', isa => 'Str', default => sub { 'top' } );

has phase => (
is => 'ro',
isa => enum([qw(build release)]),
isa => enum([qw(build release filemunge)]),
default => 'build',
);

has readme_file => (
is => 'ro',
lazy => 1,
default => sub {
my $self = shift;
my @candidates = qw/ README.md README.mkdn README.markdown /;


# only for the filemunge phase do we look
# in the slurped files
if( $self->phase eq 'filemunge' ) {
for my $file ( @{ $self->zilla->files } ) {
return $file if grep { $file->name eq $_ } @candidates;
}
}
else {
# for the other phases we look on disk
my $root = path($self->zilla->root);

return Dist::Zilla::File::OnDisk->new(
name => "$_",
content => $_->slurp_raw,
encoding => 'bytes',
) for grep { -f $_ } map { $root->child($_) } @candidates
}

$self->log_fatal('README file not found');
},
);

sub after_build {
my ($self) = @_;
$self->add_badges if $self->phase eq 'build';
Expand All @@ -42,6 +73,13 @@ sub after_release {
$self->add_badges if $self->phase eq 'release';
}

sub munge_files {
my $self = shift;

$self->add_badges if $self->phase eq 'filemunge';
}


sub add_badges {
my ($self) = @_;

Expand All @@ -52,19 +90,6 @@ sub add_badges {
my ($base_url, $user_name, $repository_name) = ($repository =~ m{^\w+://(.*)/([^\/]+)/(.*?)(\.git|\/|$)});
return unless $repository_name;

my $file;
foreach my $filename ('README.md', 'README.mkdn', 'README.markdown') {
$file = path($self->zilla->root)->child($filename);
last if -e "$file";
}
$self->log_fatal('README file not found') if ! -e "$file";

my $readme = $file;

# We are lazy and dealing with only encoded bytes.
# If we need to decode we could probably get the encoding from the zilla file object (if Dist::Zilla->VERSION >= 5).
my $content = $readme->slurp_raw;

my @badges;
foreach my $badge (@{$self->badges}) {
if ($badge eq 'travis') {
Expand Down Expand Up @@ -94,13 +119,22 @@ sub add_badges {
}
}

my $readme = $self->readme_file;

my $content = $readme->encoded_content;

if ($self->place eq 'bottom') {
$content = $content . "\n\n" . join("\n", @badges);
} else {
$content = join("\n", @badges) . "\n\n" . $content;
}

$readme->spew_raw($content);
$readme->content($content);

# need to write it to disk if we're in a
# phase that is not filemunge
path( $readme->name )->spew_raw( $readme->encoded_content )
if $self->phase ne 'filemunge';
}

1;
Expand Down Expand Up @@ -165,8 +199,12 @@ Place the badges at the top or bottom of the README. Defaults to top.
[GitHubREADME::Badge]
phase = release
Which Dist::Zilla phase to add the badges: build or release.
The default is build.
Which Dist::Zilla phase to add the badges: C<build>, C<release> or C<filemunge>.
For the C<build> and C<release> phases, the README that is on disk will
be modified, whereas for the C<filemunge> it's the internal zilla version of
the README that will be modified.
The default is C<build>.
=head1 SEE ALSO
Expand Down

0 comments on commit ff5f23d

Please sign in to comment.