File Coverage

blib/lib/Dist/Zilla/Plugin/TravisCI.pm
Criterion Covered Total %
statement 89 110 80.9
branch 25 44 56.8
condition n/a
subroutine 10 11 90.9
pod 1 5 20.0
total 125 170 73.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::TravisCI;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Integrating the generation of .travis.yml into your dzil
4             $Dist::Zilla::Plugin::TravisCI::VERSION = '0.014';
5 4     4   5297098 use Moose;
  4         381497  
  4         28  
6 4     4   26117 use Path::Tiny qw( path );
  4         9257  
  4         261  
7 4     4   1922 use Dist::Zilla::File::FromCode;
  4         912174  
  4         5170  
8              
9             with 'Dist::Zilla::Role::FileGatherer','Dist::Zilla::Role::AfterBuild', 'Beam::Emitter';
10              
11             our @phases = ( ( map { my $phase = $_; ('before_'.$phase, $phase, 'after_'.$phase) } qw( install script ) ), 'after_success', 'after_failure' );
12             our @emptymvarrayattr = qw( notify_email notify_irc requires env script_env extra_dep apt_package );
13              
14             has $_ => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] } ) for (@phases, @emptymvarrayattr);
15              
16             our @bools = qw( verbose test_deps test_authordeps no_notify_email coveralls );
17              
18             has $_ => ( is => 'ro', isa => 'Bool', default => sub { 0 } ) for @bools;
19              
20             has irc_template => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [
21             "%{branch}#%{build_number} by %{author}: %{message} (%{build_url})",
22             ] } );
23              
24             has perl_version => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [
25             "5.30",
26             "5.28",
27             "5.26",
28             "5.24",
29             "5.22",
30             "5.20",
31             "5.18",
32             "5.16",
33             "5.14",
34             ] } );
35              
36              
37             has 'write_to' => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [ 'root' ] } );
38              
39             our @core_env = ("HARNESS_OPTIONS=j10:c HARNESS_TIMER=1");
40              
41             around mvp_multivalue_args => sub {
42             my ($orig, $self) = @_;
43              
44             my @start = $self->$orig;
45             return @start, @phases, @emptymvarrayattr, qw( irc_template perl_version write_to );
46             };
47              
48             sub gather_files {
49 5     5 0 194979 my $self = shift;
50 5 50       12 return unless grep { $_ eq 'build' } @{ $self->write_to };
  5         29  
  5         181  
51 0         0 require YAML;
52             my $file = Dist::Zilla::File::FromCode->new(
53             name => '.travis.yml',
54             code_return_type => 'text', # YAML::Dump returns text
55             code => sub {
56 0     0   0 return $self->build_travis_yml_str;
57             },
58 0         0 );
59 0         0 $self->add_file($file);
60 0         0 return;
61             }
62              
63             sub after_build {
64 5     5 0 120507 my $self = shift;
65 5 50       11 return unless grep { $_ eq 'root' } @{ $self->write_to };
  5         28  
  5         164  
66 5         131 path($self->zilla->root,'.travis.yml')->spew_utf8($self->build_travis_yml_str);
67 5         76640 return;
68             }
69              
70 15     15   32 sub _get_exports { shift; map { "export ".$_ } @_ }
  15         30  
  5         23  
71              
72             sub build_travis_yml_str {
73 5     5 0 336 my ($self) = @_;
74 5         23 my $structure = $self->build_travis_yml;
75 5         149 require YAML;
76 5         5886 local $YAML::QuoteNumericStrings=1;
77 5         39 return YAML::Dump($structure);
78             }
79              
80             sub build_travis_yml {
81 5     5 0 14 my ($self, $is_build_branch) = @_;
82              
83 5         143 my $zilla = $self->zilla;
84             my %travisyml = (
85             language => "perl",
86             matrix => {
87             include => [ map {{
88 45 100       332 perl => sprintf('%.2f',$_),
89             ( $_ <= 5.20 ) ? ( dist => "trusty" ) : (),
90 5         39 }} @{$self->perl_version} ]
  5         153  
91             }
92             );
93              
94 5         143 my $rmeta = $zilla->distmeta->{resources};
95              
96 5         47 my %notifications;
97              
98 5         12 my @emails = grep { $_ } @{$self->notify_email};
  0         0  
  5         162  
99 5 50       149 if ($self->no_notify_email) {
    50          
100 0         0 $notifications{email} = \"false";
101             } elsif (scalar @emails) {
102 0         0 $notifications{email} = \@emails;
103             }
104              
105 5 50       17 if (%notifications) {
106 0         0 $travisyml{notifications} = \%notifications;
107             }
108              
109 5 100       10 if (@{$self->apt_package()}) {
  5         164  
110 2         64 $travisyml{addons}->{apt_packages} = $self->apt_package();
111             }
112              
113 5         17 my %phases_commands = map { $_ => $self->$_ } @phases;
  40         1218  
114              
115 5 50       203 my $verbose = $self->verbose ? ' --verbose ' : ' --quiet ';
116              
117 5         15 unshift @{$phases_commands{before_install}}, (
  5         21  
118             'git config --global user.name "Dist Zilla Plugin TravisCI"',
119             'git config --global user.email $HOSTNAME":not-for-mail@travis-ci.com"',
120             );
121              
122 5         46 my @extra_deps = @{$self->extra_dep};
  5         166  
123              
124 5         12 my $needs_cover;
125              
126 5 50       141 if ($self->coveralls) {
127 0         0 push @extra_deps, 'Devel::Cover::Report::Coveralls';
128 0         0 unshift @{$phases_commands{after_success}}, 'cover -report coveralls';
  0         0  
129 0         0 $needs_cover = 1;
130             }
131              
132 5 50       16 if ($needs_cover) {
133 0         0 push @{$self->env}, 'HARNESS_PERL_SWITCHES=-MDevel::Cover=-db,$TRAVIS_BUILD_DIR/cover_db';
  0         0  
134             }
135              
136 5         39 my @env_exports = $self->_get_exports(@core_env, @{$self->env});
  5         149  
137              
138 5 50       11 unless (@{$phases_commands{install}}) {
  5         22  
139 5 50       10 push @{$phases_commands{install}}, (
  5 50       168  
140             "cpanm ".$verbose." --notest --skip-installed Dist::Zilla",
141             "dzil authordeps | grep -ve '^\\W' | xargs -n 5 -P 10 cpanm ".$verbose." ".($self->test_authordeps ? "" : " --notest ")." --skip-installed",
142             "dzil listdeps | grep -ve '^\\W' | cpanm ".$verbose." ".($self->test_deps ? "" : " --notest ")." --skip-installed",
143             );
144 5 50       26 if (@extra_deps) {
145 0 0       0 push @{$phases_commands{install}}, (
  0         0  
146             "cpanm ".$verbose." ".($self->test_deps ? "" : " --notest ")." ".join(" ",@extra_deps),
147             );
148             }
149             }
150              
151 5 50       11 unless (@{$phases_commands{script}}) {
  5         18  
152 5         11 push @{$phases_commands{script}}, "dzil smoke --release --author";
  5         14  
153             }
154              
155 5         10 unshift @{$phases_commands{script}}, $self->_get_exports(@{$self->script_env});
  5         10  
  5         192  
156              
157 5 50       10 unless (@{$phases_commands{install}}) {
  5         18  
158             $phases_commands{install} = [
159 0 0       0 'cpanm --installdeps '.$verbose.' '.($self->test_deps ? "" : "--notest").' --skip-installed .',
160             ];
161             }
162              
163 5 50       8 if (@{$self->requires}) {
  5         147  
164 0         0 unshift @{$phases_commands{before_install}}, "sudo apt-get install -qq ".join(" ",@{$self->requires});
  0         0  
  0         0  
165             }
166              
167 5         19 push @{$phases_commands{install}}, @{delete $phases_commands{after_install}};
  5         19  
  5         14  
168              
169 5         9 unshift @{$phases_commands{script}}, $self->_get_exports(@{$self->script_env});
  5         12  
  5         146  
170              
171 5         12 my $first = 0;
172 5         15 for (@phases) {
173 40 100       128 next unless defined $phases_commands{$_};
174 35         47 my @commands = @{$phases_commands{$_}};
  35         60  
175 35 100       70 if (@commands) {
176 15 100       54 $travisyml{$_} = [
177             $first
178             ? ()
179             : (@env_exports),
180             @commands,
181             ];
182 15         81 $first = 1;
183             }
184             }
185              
186 5         34 return $self->emit(
187             'modify_travis_yml',
188             class => 'Dist::Zilla::Event::TravisCI::YML',
189             travis_yml => { $self->modify_travis_yml(%travisyml) }
190             )->travis_yml;
191             }
192              
193             sub modify_travis_yml {
194 4     4 1 18 my ( $self, %args ) = @_;
195 4         44 return %args;
196             }
197              
198             __PACKAGE__->meta->make_immutable;
199              
200             package # Hidden
201             Dist::Zilla::Event::TravisCI::YML;
202              
203 4     4   44 use Moose;
  4         10  
  4         34  
204             extends 'Beam::Event';
205              
206             has 'travis_yml' => ( is => 'rw', isa => 'HashRef', required => 1 );
207              
208             __PACKAGE__->meta->make_immutable;
209              
210             1;
211              
212             __END__
213              
214             =pod
215              
216             =head1 NAME
217              
218             Dist::Zilla::Plugin::TravisCI - Integrating the generation of .travis.yml into your dzil
219              
220             =head1 VERSION
221              
222             version 0.014
223              
224             =head1 SYNOPSIS
225              
226             [TravisCI]
227             perl_version = 5.14
228             perl_version = 5.16
229             perl_version = 5.18
230             perl_version = 5.20
231             perl_version = 5.22
232             perl_version = 5.24
233             perl_version = 5.26
234             perl_version = 5.28
235             perl_version = 5.30
236             notify_email = other@email.then.default
237             irc_template = %{branch}#%{build_number} by %{author}: %{message} (%{build_url})
238             requires = libdebian-package-dev
239             extra_dep = Extra::Module
240             env = KEY=VALUE
241             script_env = SCRIPTKEY=SCRIPTONLY
242             before_install = echo "After the installation of requirements before perl modules"
243             install = echo "Replace our procedure to install the perl modules"
244             after_install = echo "In the install phase after perl modules are installed"
245             before_script = echo "Do something before the dzil smoke is called"
246             script = echo "replace our call for dzil smoke"
247             after_script = echo "another test script to run, probably?"
248             after_success = echo "yeah!"
249             after_failure = echo "Buh!! :("
250             verbose = 0
251             test_deps = 0
252             test_authordeps = 0
253             no_notify_email = 0
254             coveralls = 0
255             apt_package = libzmq1-dev
256              
257             =head1 DESCRIPTION
258              
259             Adds a B<.travis.yml> to your repository on B<build> or B<release>.
260              
261             =head1 BASED ON
262              
263             This plugin is based on code of L<Dist::Zilla::TravisCI>.
264              
265             =head1 EVENTS
266              
267             This module provides an event to allow modifying the C<travis_yml> data structure
268             prior to writing it to file.
269              
270             =head2 C<modify_travis_yml>
271              
272             This event can be hooked with L<< C<[Beam::Connector]>|Dist::Zilla::Plugin::Beam::Connector >>
273             in order to allow 3rd party plugins to modify the C<YAML> data.
274              
275             ; Hook into another plugin from this
276             on = plugin:TravisCI#modify_travis_yml => plugin:AuthorTweaks#tweak_travis
277              
278             ; Hook into an arbitrary class loaded by Beam
279             container = inc/beam.yml
280             on = plugin:TravisCI#modify_travis_yml => container:disttweaks#tweak_travis
281              
282             The recieving method(s) will recieve a C<Dist::Zilla::Event::TravisCI::YML> event to modify
283             directly.
284              
285             sub event_hander {
286             my ( $self , $event ) = @_;
287             push @{ $event->travis_yml->{env} }, 'AUTHOR_TESTING=1';
288             }
289              
290             B<< See L<< C<[Beam::Connector]>|Dist::Zilla::Plugin::Beam::Connector/Receiving Events >> for details. >>
291              
292             =head1 SUPPORT
293              
294             IRC
295              
296             Join #distzilla on irc.perl.org. Highlight Getty for fast reaction :).
297              
298             Repository
299              
300             https://github.com/Getty/p5-dist-zilla-plugin-travisci
301             Pull request and additional contributors are welcome
302              
303             Issue Tracker
304              
305             https://github.com/Getty/p5-dist-zilla-plugin-travisci/issues
306              
307             =head1 AUTHOR
308              
309             Torsten Raudssus <torsten@raudss.us> L<https://raudss.us/>
310              
311             =head1 COPYRIGHT AND LICENSE
312              
313             This software is copyright (c) 2013 by L<Raudssus Social Software|https://raudss.us/>.
314              
315             This is free software; you can redistribute it and/or modify it under
316             the same terms as the Perl 5 programming language system itself.
317              
318             =cut