| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Ukigumo::Client::YamlConfig; | 
| 2 | 4 |  |  | 4 |  | 21 | use strict; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 147 |  | 
| 3 | 4 |  |  | 4 |  | 20 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 96 |  | 
| 4 | 4 |  |  | 4 |  | 3750 | use YAML::Tiny; | 
|  | 4 |  |  |  |  | 27072 |  | 
|  | 4 |  |  |  |  | 291 |  | 
| 5 | 4 |  |  | 4 |  | 40 | use Ukigumo::Constants; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 276 |  | 
| 6 | 4 |  |  | 4 |  | 3834 | use String::CamelCase qw/camelize/; | 
|  | 4 |  |  |  |  | 3053 |  | 
|  | 4 |  |  |  |  | 301 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 33 | use Mouse; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 45 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # Arguments | 
| 11 |  |  |  |  |  |  | has c => ( | 
| 12 |  |  |  |  |  |  | is       => 'ro', | 
| 13 |  |  |  |  |  |  | isa      => 'Ukigumo::Client', | 
| 14 |  |  |  |  |  |  | required => 1, | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has ukigumo_yml_file => ( | 
| 18 |  |  |  |  |  |  | is       => 'ro', | 
| 19 |  |  |  |  |  |  | isa      => 'Str', | 
| 20 |  |  |  |  |  |  | default  => '.ukigumo.yml', | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has travis_yml_file => ( | 
| 24 |  |  |  |  |  |  | is       => 'ro', | 
| 25 |  |  |  |  |  |  | isa      => 'Str', | 
| 26 |  |  |  |  |  |  | default  => '.travis.yml', | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Generates automatically | 
| 30 |  |  |  |  |  |  | has config => ( | 
| 31 |  |  |  |  |  |  | is      => 'ro', | 
| 32 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 33 |  |  |  |  |  |  | builder => '_build_config', | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | has notifiers => ( | 
| 37 |  |  |  |  |  |  | is      => 'ro', | 
| 38 |  |  |  |  |  |  | isa     => 'ArrayRef', | 
| 39 |  |  |  |  |  |  | lazy    => 1, | 
| 40 |  |  |  |  |  |  | builder => '_build_notifiers', | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Configurable by yml | 
| 44 |  |  |  |  |  |  | has env => ( | 
| 45 |  |  |  |  |  |  | is      => 'ro', | 
| 46 |  |  |  |  |  |  | lazy    => 1, | 
| 47 |  |  |  |  |  |  | default => sub { shift->config->{env} }, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | has project_name => ( | 
| 51 |  |  |  |  |  |  | is      => 'ro', | 
| 52 |  |  |  |  |  |  | isa     => 'Maybe[Str]', | 
| 53 |  |  |  |  |  |  | lazy    => 1, | 
| 54 |  |  |  |  |  |  | default => sub { shift->config->{project_name} }, | 
| 55 |  |  |  |  |  |  | ); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | has notifications => ( | 
| 58 |  |  |  |  |  |  | is      => 'ro', | 
| 59 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 60 |  |  |  |  |  |  | lazy    => 1, | 
| 61 |  |  |  |  |  |  | default => sub { shift->config->{notifications} || {} }, | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | ## Executable commands | 
| 65 |  |  |  |  |  |  | has before_install => ( | 
| 66 |  |  |  |  |  |  | is      => 'ro', | 
| 67 |  |  |  |  |  |  | isa     => 'Maybe[ArrayRef[Str]]', | 
| 68 |  |  |  |  |  |  | lazy    => 1, | 
| 69 |  |  |  |  |  |  | default => sub { shift->_array_of_str_from_config('before_install') }, | 
| 70 |  |  |  |  |  |  | ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | has install => ( | 
| 73 |  |  |  |  |  |  | is      => 'ro', | 
| 74 |  |  |  |  |  |  | isa     => 'Maybe[ArrayRef[Str]]', | 
| 75 |  |  |  |  |  |  | lazy    => 1, | 
| 76 |  |  |  |  |  |  | default => sub { shift->_array_of_str_from_config('install') }, | 
| 77 |  |  |  |  |  |  | ); | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | has before_script => ( | 
| 80 |  |  |  |  |  |  | is      => 'ro', | 
| 81 |  |  |  |  |  |  | isa     => 'Maybe[ArrayRef[Str]]', | 
| 82 |  |  |  |  |  |  | lazy    => 1, | 
| 83 |  |  |  |  |  |  | default => sub { shift->_array_of_str_from_config('before_script') }, | 
| 84 |  |  |  |  |  |  | ); | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | has script => ( | 
| 87 |  |  |  |  |  |  | is      => 'ro', | 
| 88 |  |  |  |  |  |  | isa     => 'Maybe[ArrayRef[Str]]', | 
| 89 |  |  |  |  |  |  | lazy    => 1, | 
| 90 |  |  |  |  |  |  | default => sub { shift->_array_of_str_from_config('script') }, | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | has after_script => ( | 
| 94 |  |  |  |  |  |  | is      => 'ro', | 
| 95 |  |  |  |  |  |  | isa     => 'Maybe[ArrayRef[Str]]', | 
| 96 |  |  |  |  |  |  | lazy    => 1, | 
| 97 |  |  |  |  |  |  | default => sub { shift->_array_of_str_from_config('after_script') }, | 
| 98 |  |  |  |  |  |  | ); | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 4 |  |  | 4 |  | 3359 | no Mouse; | 
|  | 4 |  |  |  |  | 21 |  | 
|  | 4 |  |  |  |  | 28 |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub apply_environment_variables { | 
| 103 | 3 |  |  | 3 | 0 | 173 | my ($self) = @_; | 
| 104 | 3 |  |  |  |  | 15 | my $c = $self->c; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 3 |  |  |  |  | 25 | my $env = $self->env; | 
| 107 | 3 | 100 | 66 |  |  | 29 | if ($env && (my $ref = ref $env) ne 'ARRAY') { | 
| 108 | 2 |  | 100 |  |  | 11 | $ref ||= 'SCALAR'; | 
| 109 | 2 |  |  |  |  | 17 | $c->logger->warnf("`env` must be array reference: in spite of it was given `$ref`"); | 
| 110 | 2 |  |  |  |  | 9 | $c->reflect_result(STATUS_FAIL); | 
| 111 | 2 |  |  |  |  | 17 | die "`env` must be array reference: in spite of it was given `$ref`\n"; | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 1 |  |  |  |  | 3 | for my $e (@$env) { | 
| 114 | 3 |  |  |  |  | 14 | my ($k, $v) = %$e; | 
| 115 | 3 |  |  |  |  | 21 | $ENV{$k} = $v; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub effective_yml_file { | 
| 120 | 9 |  |  | 9 | 0 | 18 | my ($self) = @_; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 9 |  |  |  |  | 52 | my @files = ($self->ukigumo_yml_file, $self->travis_yml_file); | 
| 123 | 9 |  |  |  |  | 30 | for my $file (@files) { | 
| 124 | 10 | 100 |  |  |  | 213 | return $file if -f $file; | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 1 |  |  |  |  | 4 | return; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub _build_config { | 
| 131 | 9 |  |  | 9 |  | 19832 | my ($self) = @_; | 
| 132 | 9 |  |  |  |  | 43 | my $c = $self->c; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 9 |  |  |  |  | 38 | my $ukigumo_yml = $self->effective_yml_file; | 
| 135 | 9 | 100 |  |  |  | 35 | if ($ukigumo_yml) { | 
| 136 | 8 |  |  |  |  | 20 | my $y = eval { YAML::Tiny->read($ukigumo_yml) }; | 
|  | 8 |  |  |  |  | 84 |  | 
| 137 | 8 | 50 |  |  |  | 24958 | if (my $e = $@) { | 
| 138 | 0 |  |  |  |  | 0 | $c->logger->warnf("YAML syntax error in $ukigumo_yml: $e"); | 
| 139 | 0 |  |  |  |  | 0 | $c->reflect_result(STATUS_FAIL); | 
| 140 | 0 |  |  |  |  | 0 | die "$ukigumo_yml: $e\n"; | 
| 141 |  |  |  |  |  |  | } | 
| 142 | 8 | 100 |  |  |  | 41 | unless (defined $y->[0]) { | 
| 143 | 1 |  |  |  |  | 10 | $c->logger->warnf("$ukigumo_yml: does not contain anything"); | 
| 144 | 1 |  |  |  |  | 8 | $c->reflect_result(STATUS_FAIL); | 
| 145 | 1 |  |  |  |  | 13 | die "$ukigumo_yml: does not contain anything\n"; | 
| 146 |  |  |  |  |  |  | } | 
| 147 | 7 |  |  |  |  | 183 | return $y->[0]; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 1 |  |  |  |  | 9 | $c->logger->infof("There is no yaml file"); | 
| 151 | 1 |  |  |  |  | 14 | return +{}; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | sub _array_of_str_from_config { | 
| 155 | 16 |  |  | 16 |  | 58 | my ($self, $key) = @_; | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 16 |  |  |  |  | 77 | my $str = $self->config->{$key}; | 
| 158 | 16 | 100 | 66 |  |  | 434 | (defined $str && not ref $str) ? [$str] : $str; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub _build_notifiers { | 
| 162 | 1 |  |  | 1 |  | 10 | my ($self) = @_; | 
| 163 |  |  |  |  |  |  |  | 
| 164 | 1 |  |  |  |  | 2 | my @notifiers; | 
| 165 | 1 |  |  |  |  | 3 | for my $type (keys %{$self->notifications}) { | 
|  | 1 |  |  |  |  | 6 |  | 
| 166 | 2 | 100 |  |  |  | 8 | if ($type eq 'ikachan') { | 
|  |  | 50 |  |  |  |  |  | 
| 167 | 1 |  |  |  |  | 1 | push @notifiers, @{$self->_load_notifier_class($type, NOTIFIER_IKACHAN)}; | 
|  | 1 |  |  |  |  | 3 |  | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  | elsif ($type eq 'github_statuses') { | 
| 170 | 1 |  |  |  |  | 3 | push @notifiers, @{$self->_load_notifier_class($type, NOTIFIER_GITHUBSTATUSES)}; | 
|  | 1 |  |  |  |  | 4 |  | 
| 171 |  |  |  |  |  |  | } else { | 
| 172 | 0 |  |  |  |  | 0 | push @notifiers, @{$self->_load_notifier_class($type)}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  | } | 
| 175 | 1 |  |  |  |  | 21 | return \@notifiers; | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | sub _load_notifier_class { | 
| 179 | 2 |  |  | 2 |  | 5 | my ($self, $type, $module_name) = @_; | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 2 | 50 |  |  |  | 6 | unless ($module_name) { | 
| 182 | 0 |  |  |  |  | 0 | $module_name = 'Ukigumo::Client::Notify::' . camelize($type); | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 2 |  |  |  |  | 9 | (my $module_path = $module_name) =~ s!::!/!g; | 
| 186 | 2 |  |  |  |  | 3 | $module_path .= '.pm'; | 
| 187 | 2 |  |  |  |  | 1247 | require $module_path; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 2 |  |  |  |  | 25 | my $notifier_config = $self->notifications->{$type}; | 
| 190 | 2 | 50 |  |  |  | 12 | $notifier_config = [$notifier_config] unless ref $notifier_config eq 'ARRAY'; | 
| 191 |  |  |  |  |  |  |  | 
| 192 | 2 |  |  |  |  | 3 | my @notifiers; | 
| 193 | 2 |  |  |  |  | 7 | for my $args (@$notifier_config) { | 
| 194 | 2 |  |  |  |  | 31 | push @notifiers, $module_name->new($args); | 
| 195 |  |  |  |  |  |  | } | 
| 196 |  |  |  |  |  |  |  | 
| 197 | 2 |  |  |  |  | 152 | return \@notifiers; | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | 1; |