File Coverage

blib/lib/Dist/Zilla/Plugin/Author/CSSON/GithubActions.pm
Criterion Covered Total %
statement 75 77 97.4
branch 10 16 62.5
condition 4 6 66.6
subroutine 19 19 100.0
pod 0 4 0.0
total 108 122 88.5


line stmt bran cond sub pod time code
1 1     1   105981 use 5.14.0;
  1         16  
2 1     1   6 use strict;
  1         2  
  1         22  
3 1     1   5 use warnings;
  1         2  
  1         90  
4              
5             package Dist::Zilla::Plugin::Author::CSSON::GithubActions;
6              
7             # ABSTRACT: Ease creation of common Github Actions workflows
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0103';
10              
11 1     1   578 use Moose;
  1         497521  
  1         8  
12 1     1   8419 use namespace::autoclean;
  1         8410  
  1         4  
13 1     1   921 use Path::Tiny;
  1         11658  
  1         120  
14 1     1   625 use Types::Standard qw/ArrayRef Bool Str/;
  1         78153  
  1         8  
15 1     1   1561 use Module::Load qw/load/;
  1         1074  
  1         8  
16 1     1   476 use YAML::XS qw/Dump/;
  1         2883  
  1         91  
17 1     1   677 use List::AllUtils qw/first/;
  1         11068  
  1         96  
18 1     1   505 use Path::Class::File;
  1         37222  
  1         32  
19 1     1   484 use Dist::Zilla::File::InMemory;
  1         487395  
  1         1004  
20              
21             with qw/
22             Dist::Zilla::Role::Plugin
23             Dist::Zilla::Role::FileGatherer
24             Dist::Zilla::Role::FilePruner
25             /;
26              
27             sub mvp_multivalue_args {
28 5     5 0 2606080 qw/
29             on_push_branches
30             on_pull_request_branches
31             matrix_os
32             perl_versions
33             /;
34             }
35              
36             has workflow_class => (
37             is => 'ro',
38             required => 1,
39             documentation => q{The 'workflow_class' will be prefix with 'Dist::Zilla::Plugin' },
40             );
41             has clear_on_push_branches => (
42             is => 'ro',
43             isa => Bool,
44             default => sub { 0 },
45             documentation => q{Clears the on.push.branches setting from the base workflow (if that setting is used in the config)},
46             );
47             has clear_on_pull_request_branches => (
48             is => 'ro',
49             isa => Bool,
50             default => sub { 0 },
51             documentation => q{Clears the on.pull_request.branches setting from the base workflow (if that setting is used in the config)},
52             );
53              
54             for my $setting (qw/on_push_branches on_pull_request_branches/) {
55             has $setting => (
56             is => 'ro',
57             isa => ArrayRef,
58             default => sub { [] },
59             traits => ['Array'],
60             documentation => q{Add more branches to on.push.branches *or* on.pull_request.branches},
61             handles => {
62             "all_$setting" => 'elements',
63             "has_$setting" => 'count',
64             },
65             );
66             }
67             has matrix_os => (
68             is => 'ro',
69             isa => ArrayRef,
70             default => sub { [] },
71             documentation => q{If defined, replaces the matrix.os setting},
72             );
73             has perl_version => (
74             is => 'ro',
75             isa => ArrayRef,
76             default => sub { [] },
77             documentation => q{If defined, replaces the matrix.perl-version setting},
78             );
79              
80             has filename => (
81             is => 'rw',
82             isa => Str,
83             predicate => 'has_filename',
84              
85             );
86              
87             sub _prepare_yaml {
88 10     10   27 my $self = shift;
89              
90 10         542 my $class_name = 'Dist::Zilla::Plugin::' . $self->workflow_class;
91 10         66 load $class_name;
92              
93 10         863 my $workflow = $class_name->new;
94 10         2062 my $yaml = $workflow->yaml;
95              
96 10 50 66     4545 if ($self->clear_on_push_branches && exists $yaml->{'on'}{'push'}{'branches'}) {
97 2         9 $yaml->{'on'}{'push'}{'branches'} = [];
98             }
99 10 50 66     468 if ($self->clear_on_pull_request_branches && exists $yaml->{'on'}{'pull_request'}{'branches'}) {
100 4         16 $yaml->{'on'}{'pull_request'}{'branches'} = [];
101             }
102              
103 10 50       527 if ($self->has_on_push_branches) {
104 0         0 push @{ $yaml->{'on'}{'push'}{'branches'} } => $self->all_on_push_branches;
  0         0  
105             }
106 10 100       550 if ($self->has_on_pull_request_branches) {
107 4         8 push @{ $yaml->{'on'}{'pull_request'}{'branches'} } => $self->all_on_pull_request_branches;
  4         227  
108             }
109 10         53 return $yaml;
110             }
111              
112             sub _write_yaml {
113 5     5   16 my $self = shift;
114 5         11 my $yaml = shift;
115              
116 5         19 my $file = Path::Class::File->new(split m{/} => $self->filepath($yaml));
117 5         1572 $file->spew(Dump($yaml));
118 5         1948 return $file;
119             }
120              
121             sub filepath {
122 15     15 0 314 my $self = shift;
123 15         31 my $yaml = shift;
124              
125 15 100       795 if ($self->has_filename) {
    50          
126 10         33 delete $yaml->{'filename'};
127             }
128             elsif (exists $yaml->{'filename'}) {
129 5         217 $self->filename(delete $yaml->{'filename'});
130             }
131 15 50       476 my $path = path($self->zilla->built_in ? $self->zilla->built_in : (), '.github', 'workflows', $self->filename);
132 15         628 $path->touchpath;
133 15         4339 return $path->stringify;
134             }
135              
136             sub gather_files {
137 5     5 0 273642 my $self = shift;
138              
139 5         26 my $yaml = $self->_prepare_yaml;
140 5         581 $self->_write_yaml($yaml);
141              
142 5         29 my $generated_file = Dist::Zilla::File::InMemory->new({
143             name => $self->filepath($yaml),
144             content => Dump($yaml),
145             });
146 5         3175 $self->add_file($generated_file);
147              
148             }
149              
150             sub prune_files {
151 5     5 0 73231 my $self = shift;
152 5         60 $self->log(['Pruning...']);
153              
154 5         1876 my $yaml = $self->_prepare_yaml;
155 5     5   578 my $file = first { $_->name eq $self->filepath($yaml) } @{ $self->zilla->files };
  5         215  
  5         175  
156              
157 5 50       248 $self->zilla->prune_file($file) if $file;
158             }
159              
160             __PACKAGE__->meta->make_immutable;
161              
162             1;
163              
164             __END__
165              
166             =pod
167              
168             =encoding UTF-8
169              
170             =head1 NAME
171              
172             Dist::Zilla::Plugin::Author::CSSON::GithubActions - Ease creation of common Github Actions workflows
173              
174              
175              
176             =begin html
177              
178             <p>
179             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
180             <a href="http://cpants.cpanauthors.org/release/CSSON/Dist-Zilla-Plugin-Author-CSSON-GithubActions-0.0103"><img src="http://badgedepot.code301.com/badge/kwalitee/CSSON/Dist-Zilla-Plugin-Author-CSSON-GithubActions/0.0103" alt="Distribution kwalitee" /></a>
181             <a href="http://matrix.cpantesters.org/?dist=Dist-Zilla-Plugin-Author-CSSON-GithubActions%200.0103"><img src="http://badgedepot.code301.com/badge/cpantesters/Dist-Zilla-Plugin-Author-CSSON-GithubActions/0.0103" alt="CPAN Testers result" /></a>
182             <img src="https://img.shields.io/badge/coverage-91.7%-yellow.svg" alt="coverage 91.7%" />
183             </p>
184              
185             =end html
186              
187             =head1 VERSION
188              
189             Version 0.0103, released 2020-12-25.
190              
191             =head1 SYNOPSIS
192              
193             In dist.ini:
194              
195             [Author::CSSON::GithubActions]
196             ; workflow_class is mandatory (Dist::Zilla::Plugin is prepended when loading it)
197             workflow_class = Author::CSSON::GithubActions::BaseWorkflow
198              
199             ; the rest is optional, and customizes the workflow defined in the workflow_class
200              
201             ; set on.push.branches to an empty list
202             clear_on_push_branches = 1
203              
204             ; set on.pull_request.branches to an empty list
205             clear_on_pull_request_branches = 1
206              
207             ; add branches to on.push.branches
208             on_pull_request_branches = 'this-branch'
209             on_pull_request_branches = 'that-other-branch'
210              
211             ; add branches to on.pull_request.branches
212             on_pull_request_branches = 'my-pr-branch'
213             on_pull_request_branches = 'feature-branch'
214              
215             ; replace jobs.perl-job.strategy.matrix.os
216             matrix_os = ubuntu-latest
217             matrix_os = ubuntu-16.04
218              
219             ; replace jobs.perl-job.strategy.matrix.perl-version
220             perl_version = 5.32
221             perl_version = 5.24
222             perl_version = 5.18
223              
224             =head1 STATUS
225              
226             This is very early in development.
227              
228             =head1 DESCRIPTION
229              
230             This plugin creates a Github Actions workflow file in C<.github/workflows>.
231              
232             Note that, if you plan to use the customizations shown above, the following settings in the workflow YAML file are expected to be defined as lists and not strings:
233             * C<on.push.branches>
234             * C<on.pull_request.branches>
235             * C<jobs.perl-job.strategy.matrix.os>
236             * C<jobs.perl-job.strategy.matrix.perl-version>
237              
238             See L<Dist::Zilla::Plugin::Author::CSSON::GithubActions::Role::Workflow> for how to define a workflow.
239              
240             See L<Dist::Zilla::Plugin::Author::CSSON::GithubActions::BaseWorkflow> for an example workflow.
241              
242             =head1 SEE ALSO
243              
244             =over 4
245              
246             =item *
247              
248             L<Dist::Zilla::TravisCI>
249              
250             =back
251              
252             =head1 SOURCE
253              
254             L<https://github.com/Csson/p5-Dist-Zilla-Plugin-Author-CSSON-GithubActions>
255              
256             =head1 HOMEPAGE
257              
258             L<https://metacpan.org/release/Dist-Zilla-Plugin-Author-CSSON-GithubActions>
259              
260             =head1 AUTHOR
261              
262             Erik Carlsson <info@code301.com>
263              
264             =head1 COPYRIGHT AND LICENSE
265              
266             This software is copyright (c) 2020 by Erik Carlsson.
267              
268             This is free software; you can redistribute it and/or modify it under
269             the same terms as the Perl 5 programming language system itself.
270              
271             =cut