File Coverage

blib/lib/Dist/Zilla/Plugin/Author/CSSON/GithubActions.pm
Criterion Covered Total %
statement 68 70 97.1
branch 9 14 64.2
condition 4 6 66.6
subroutine 17 17 100.0
pod 0 3 0.0
total 98 110 89.0


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