| 1 | package Dist::Zilla::Plugin::ReadmeAddDevInfo; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Dist::Zilla::Plugin::ReadmeAddDevInfo - add info how to contribute to the project | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Moose::Util::TypeConstraints qw(enum role_type); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 5 | ▶ | use Dist::Zilla::File::OnDisk; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use Dist::Zilla::File::InMemory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 5 | our $VERSION = '0.03'; | |||||||||
| 18 | |||||||||||
| 19 | # same as Dist::Zilla::Plugin::ReadmeAnyFromPod | ||||||||||
| 20 | 5 | with qw( | |||||||||
| 21 | Dist::Zilla::Role::AfterBuild | ||||||||||
| 22 | Dist::Zilla::Role::AfterRelease | ||||||||||
| 23 | Dist::Zilla::Role::FileMunger | ||||||||||
| 24 | Dist::Zilla::Role::FileInjector | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 5 | has _file_obj => ( | |||||||||
| 28 | is => 'rw', isa => role_type('Dist::Zilla::Role::File'), | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 5 | has phase => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | isa => enum([qw(build release filemunge)]), | ||||||||||
| 34 | default => 'build', | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 5 | has before => ( | |||||||||
| 38 | is => 'ro', | ||||||||||
| 39 | isa => 'Str', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | 5 | has add_contribution_file => ( | |||||||||
| 43 | is => 'ro', | ||||||||||
| 44 | isa => 'Str', | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | has contribution_file => ( | ||||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | lazy => 1, | ||||||||||
| 50 | 5 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.2
| |||||||||||
| 51 | 5 | my $self = shift; | |||||||||
| 52 | 5 | my @candidates = qw/ CONTRIBUTING.md CONTRIBUTING /; | |||||||||
| 53 | |||||||||||
| 54 | # only for the filemunge phase do we look | ||||||||||
| 55 | # in the slurped files | ||||||||||
| 56 | 5 | ▶ | if( $self->phase eq 'filemunge' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | for my $file ( @{ $self->zilla->files } ) { | |||||||||
| 58 | 2 | ▶ | return $file if grep { $file->name eq $_ } @candidates; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | else { | ||||||||||
| 62 | # for the other phases we look on disk | ||||||||||
| 63 | 4 | my $root = path($self->zilla->root); | |||||||||
| 64 | |||||||||||
| 65 | return Dist::Zilla::File::OnDisk->new( | ||||||||||
| 66 | name => "$_", | ||||||||||
| 67 | content => $_->slurp_raw, | ||||||||||
| 68 | encoding => 'bytes', | ||||||||||
| 69 | 4 | ) for grep { -f $_ } map { $root->child($_) } @candidates | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | if ( $self->add_contribution_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | my $file = Dist::Zilla::File::InMemory->new( | |||||||||
| 74 | content => '', | ||||||||||
| 75 | name => 'CONTRIBUTING.md', | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | 0 | $self->add_file( $self->_file_obj( $file ) ); | |||||||||
| 79 | |||||||||||
| 80 | 0 | return $file; | |||||||||
| 81 | } | ||||||||||
| 82 | }, | ||||||||||
| 83 | 5 | ); | |||||||||
| 84 | |||||||||||
| 85 | has readme_file => ( | ||||||||||
| 86 | is => 'ro', | ||||||||||
| 87 | lazy => 1, | ||||||||||
| 88 | 9 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.6
| |||||||||||
| 89 | 9 | my $self = shift; | |||||||||
| 90 | 9 | my @candidates = qw/ README.md README.mkdn README.markdown /; | |||||||||
| 91 | |||||||||||
| 92 | # only for the filemunge phase do we look | ||||||||||
| 93 | # in the slurped files | ||||||||||
| 94 | 9 | ▶ | if( $self->phase eq 'filemunge' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 1 | for my $file ( @{ $self->zilla->files } ) { | |||||||||
| 96 | 1 | ▶ | return $file if grep { $file->name eq $_ } @candidates; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | else { | ||||||||||
| 100 | # for the other phases we look on disk | ||||||||||
| 101 | 8 | my $root = path($self->zilla->root); | |||||||||
| 102 | |||||||||||
| 103 | return Dist::Zilla::File::OnDisk->new( | ||||||||||
| 104 | name => "$_", | ||||||||||
| 105 | content => $_->slurp_raw, | ||||||||||
| 106 | encoding => 'bytes', | ||||||||||
| 107 | 8 | ) for grep { -f $_ } map { $root->child($_) } @candidates | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | $self->log_fatal('README file not found') if !$self->{contributing_file_only}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | }, | ||||||||||
| 112 | 5 | ); | |||||||||
| 113 | |||||||||||
| 114 | 11 | ▶ | sub after_build { | ||||||||
|
Subroutine
after_build: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 115 | 11 | my ($self) = @_; | |||||||||
| 116 | 11 | ▶ | $self->add_info if $self->phase eq 'build'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 8 | ▶ | sub after_release { | ||||||||
|
Subroutine
after_release: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 120 | 8 | my ($self) = @_; | |||||||||
| 121 | 8 | ▶ | $self->add_info if $self->phase eq 'release'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 11 | ▶ | sub munge_files { | ||||||||
|
Subroutine
munge_files: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 125 | 11 | my $self = shift; | |||||||||
| 126 | |||||||||||
| 127 | 11 | ▶ | $self->add_info if $self->phase eq 'filemunge'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 11 | ▶ | sub add_info { | ||||||||
|
Subroutine
add_info: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 131 | 11 | my $self = shift; | |||||||||
| 132 | |||||||||||
| 133 | 11 | my $distname = $self->zilla->name; | |||||||||
| 134 | 11 | my $distmeta = $self->zilla->distmeta; | |||||||||
| 135 | 11 | my $repository = $distmeta->{resources}->{repository}->{url}; | |||||||||
| 136 | |||||||||||
| 137 | 11 | ▶ | return if !$repository; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | |||||||||||
| 139 | 10 | my ($base_url, $user_name, $repository_name) = ($repository =~ m{^\w+://(.*)/([^\/]+)/(.*?)(\.git|\/|$)}); | |||||||||
| 140 | |||||||||||
| 141 | 10 | ▶ | return if !$repository_name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | 9 | ▶ | if ( '.git' ne substr $repository, -4 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 8 | $repository .= '.git'; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 9 | my $info = qq~ | |||||||||
| 148 | # Development | ||||||||||
| 149 | |||||||||||
| 150 | The distribution is contained in a Git repository, so simply clone the | ||||||||||
| 151 | repository | ||||||||||
| 152 | |||||||||||
| 153 | ``` | ||||||||||
| 154 | \$ git clone $repository | ||||||||||
| 155 | ``` | ||||||||||
| 156 | |||||||||||
| 157 | and change into the newly-created directory. | ||||||||||
| 158 | |||||||||||
| 159 | ``` | ||||||||||
| 160 | \$ cd $repository_name | ||||||||||
| 161 | ``` | ||||||||||
| 162 | |||||||||||
| 163 | The project uses [`Dist::Zilla`](https://metacpan.org/pod/Dist::Zilla) to | ||||||||||
| 164 | build the distribution, hence this will need to be installed before | ||||||||||
| 165 | continuing: | ||||||||||
| 166 | |||||||||||
| 167 | ``` | ||||||||||
| 168 | \$ cpanm Dist::Zilla | ||||||||||
| 169 | ``` | ||||||||||
| 170 | |||||||||||
| 171 | To install the required prequisite packages, run the following set of | ||||||||||
| 172 | commands: | ||||||||||
| 173 | |||||||||||
| 174 | ``` | ||||||||||
| 175 | \$ dzil authordeps --missing | cpanm | ||||||||||
| 176 | \$ dzil listdeps --author --missing | cpanm | ||||||||||
| 177 | ``` | ||||||||||
| 178 | |||||||||||
| 179 | The distribution can be tested like so: | ||||||||||
| 180 | |||||||||||
| 181 | ``` | ||||||||||
| 182 | \$ dzil test | ||||||||||
| 183 | ``` | ||||||||||
| 184 | |||||||||||
| 185 | To run the full set of tests (including author and release-process tests), | ||||||||||
| 186 | add the `--author` and `--release` options: | ||||||||||
| 187 | |||||||||||
| 188 | ``` | ||||||||||
| 189 | \$ dzil test --author --release | ||||||||||
| 190 | ``` | ||||||||||
| 191 | ~; | ||||||||||
| 192 | |||||||||||
| 193 | 9 | ▶ | if ( $self->add_contribution_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 5 | my $contribution_file = $self->contribution_file; | |||||||||
| 195 | 5 | $contribution_file->content( $info ); | |||||||||
| 196 | |||||||||||
| 197 | 5 | ▶ | path( $contribution_file->name )->spew_raw( $contribution_file->encoded_content ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | if $self->phase ne 'filemunge'; | ||||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | 9 | my $readme = $self->readme_file; | |||||||||
| 202 | 9 | my $content = $readme->encoded_content; | |||||||||
| 203 | |||||||||||
| 204 | 9 | ▶ | if ( !$self->before ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | 4 | $content .= "\n\n$info"; | |||||||||
| 206 | } | ||||||||||
| 207 | else { | ||||||||||
| 208 | 5 | my $before = $self->before; | |||||||||
| 209 | 5 | $content =~ s{^(\Q$before\E)}{\n$info\n$before}m; | |||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 9 | $readme->content($content); | |||||||||
| 213 | |||||||||||
| 214 | # need to write it to disk if we're in a | ||||||||||
| 215 | # phase that is not filemunge | ||||||||||
| 216 | 9 | ▶ | path( $readme->name )->spew_raw( $readme->encoded_content ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 217 | if $self->phase ne 'filemunge'; | ||||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | 5 | 1; | |||||||||
| 221 | |||||||||||
| 222 | __END__ | ||||||||||