File Coverage

blib/lib/App/CISetup/Role/ConfigUpdater.pm
Criterion Covered Total %
statement 49 80 61.2
branch 3 22 13.6
condition n/a
subroutine 15 22 68.1
pod 0 1 0.0
total 67 125 53.6


line stmt bran cond sub pod time code
1             package App::CISetup::Role::ConfigUpdater;
2              
3 1     1   950 use strict;
  1         3  
  1         38  
4 1     1   8 use warnings;
  1         3  
  1         36  
5 1     1   7 use namespace::autoclean;
  1         2  
  1         12  
6 1     1   88 use autodie qw( :all );
  1         3  
  1         9  
7              
8             our $VERSION = '0.18';
9              
10 1     1   6178 use App::CISetup::Travis::ConfigFile;
  1         3  
  1         40  
11 1     1   6 use App::CISetup::Types qw( Bool CodeRef Dir Str );
  1         2  
  1         18  
12 1     1   8557 use File::pushd qw( pushd );
  1         3  
  1         94  
13 1     1   572 use Git::Sub qw( remote );
  1         31244  
  1         10  
14 1     1   53 use Path::Iterator::Rule;
  1         2  
  1         33  
15 1     1   5 use Path::Tiny qw( path );
  1         3  
  1         51  
16 1     1   6 use Try::Tiny;
  1         3  
  1         53  
17 1     1   7 use YAML qw( Load );
  1         2  
  1         49  
18              
19 1     1   6 use Moose::Role;
  1         3  
  1         13  
20              
21             requires qw(
22             _config_file_class
23             _config_filename
24             _cli_params
25             );
26              
27             has create => (
28             is => 'ro',
29             isa => Bool,
30             default => 0,
31             );
32              
33             has dir => (
34             is => 'ro',
35             isa => Dir,
36             required => 1,
37             coerce => 1,
38             );
39              
40             has _config_file_iterator => (
41             is => 'ro',
42             isa => CodeRef,
43             lazy => 1,
44             builder => '_build_config_file_iterator',
45             );
46              
47             with 'MooseX::Getopt::Dashes';
48              
49             sub run {
50 0     0 0 0 my $self = shift;
51              
52 0 0       0 return $self->create
53             ? $self->_create_file
54             : $self->_update_files;
55             }
56              
57             sub _create_file {
58 0     0   0 my $self = shift;
59              
60 0         0 my $file = $self->dir->child( $self->_config_filename );
61 0         0 $self->_config_file_class->new( $self->_cf_params($file) )->create_file;
62              
63 0 0       0 print "Created $file\n" or die $!;
64              
65 0         0 return 0;
66             }
67              
68             sub _update_files {
69 0     0   0 my $self = shift;
70              
71 0         0 my $iter = $self->_config_file_iterator;
72              
73 0         0 my $count = 0;
74 0         0 while ( my $file = $iter->() ) {
75 0         0 $file = path($file);
76              
77 0         0 $count++;
78 0         0 my $updated;
79             try {
80 0     0   0 $updated
81             = $self->_config_file_class->new( $self->_cf_params($file) )
82             ->update_file;
83             }
84             catch {
85 0 0   0   0 print "\n\n\n" . $file . "\n" or die $!;
86 0 0       0 print $_ or die $!;
87 0         0 };
88              
89 0 0       0 next unless $updated;
90              
91 0 0       0 print "Updated $file\n" or die $!;
92             }
93              
94 0 0       0 warn sprintf( "WARNING: No %s files found\n", $self->_config_filename )
95             unless $count;
96              
97 0         0 return 0;
98             }
99              
100             sub _cf_params {
101 1     1   10 my $self = shift;
102 1         2 my $file = shift;
103              
104             return (
105 1         5 file => $file,
106             $self->_stored_params_from_file($file),
107             $self->_cli_params,
108             );
109             }
110              
111             sub _stored_params_from_file {
112 2     2   6 my $self = shift;
113 2         3 my $file = shift;
114              
115 2 50       10 return unless $file->exists;
116              
117             return
118 2 50       51 unless my ($yaml)
119             = $file->slurp_utf8
120             =~ /### __app_cisetup__\r?\n(.+)### __app_cisetup__/s;
121              
122 2         409 $yaml =~ s/^# //mg;
123              
124 2 50       5 return %{ Load($yaml) || {} };
  2         10  
125             }
126              
127             sub _build_config_file_iterator {
128 0     0     my $self = shift;
129              
130 0           my $rule = Path::Iterator::Rule->new;
131 0           $rule->file->name( $self->_config_filename );
132              
133             $rule->and(
134             sub {
135 0     0     my $path = path(shift);
136              
137 0 0         return unless -e $path->parent->child('.git');
138 0           my $pushed = pushd( $path->parent );
139              
140             ## no critic (Modules::RequireExplicitInclusion, Subroutines::ProhibitCallsToUnexportedSubs)
141             # XXX - make this configurable?
142             # my @origin = git::remote(qw( show -n origin ));
143             # return unless grep {m{Push +URL: .+(:|/)maxmind/}} @origin;
144              
145 0           return 1;
146             }
147 0           );
148              
149 0           return $rule->iter( $self->dir );
150             }
151              
152             1;