| 1 | 4 | ▶ | use 5.006; # our | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Dist::Zilla::Role::Bootstrap; | ||||||||||
| 6 | |||||||||||
| 7 | 4 | our $VERSION = '1.001004'; | |||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: Shared logic for bootstrap things. | ||||||||||
| 10 | |||||||||||
| 11 | 4 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use Moose::Role qw( with has around requires ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use List::UtilsBy qw( max_by nmax_by ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 4 | ▶ | use version qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 4 | with 'Dist::Zilla::Role::Plugin'; | |||||||||
| 30 | |||||||||||
| 31 | 3 | ▶ | around 'dump_config' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 3 | my ( $orig, $self, @args ) = @_; | |||||||||
| 33 | 3 | my $config = $self->$orig(@args); | |||||||||
| 34 | 3 | my $localconf = $config->{ +__PACKAGE__ } = {}; | |||||||||
| 35 | |||||||||||
| 36 | 3 | $localconf->{distname} = $self->distname; | |||||||||
| 37 | 3 | $localconf->{try_built} = $self->try_built; | |||||||||
| 38 | 3 | $localconf->{try_built_method} = $self->try_built_method; | |||||||||
| 39 | 3 | $localconf->{fallback} = $self->fallback; | |||||||||
| 40 | |||||||||||
| 41 | 3 | $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION; | |||||||||
| 42 | |||||||||||
| 43 | 3 | return $config; | |||||||||
| 44 | 4 | }; | |||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 4 | has distname => ( isa => 'Str', is => ro =>, lazy_build => 1 ); | |||||||||
| 61 | |||||||||||
| 62 | 3 | ▶ | sub _build_distname { $_[0]->zilla->name } | ||||||||
|
Subroutine
_build_distname: called CC=1 SCAR=0.0
| |||||||||||
| 63 | |||||||||||
| 64 | 4 | has _cwd => ( is => ro =>, lazy_build => 1, ); | |||||||||
| 65 | |||||||||||
| 66 | 3 | ▶ | sub _build__cwd { | ||||||||
|
Subroutine
_build__cwd: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 3 | require Path::Tiny; | |||||||||
| 68 | 3 | return Path::Tiny::path( $_[0]->zilla->root ); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | 4 | has try_built => ( isa => 'Bool', is => ro =>, lazy_build => 1, ); | |||||||||
| 86 | 1 | ▶ | sub _build_try_built { return } | ||||||||
|
Subroutine
_build_try_built: called CC=1 SCAR=0.0
| |||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | 4 | has fallback => ( isa => 'Bool', is => ro =>, lazy_build => 1 ); | |||||||||
| 103 | 3 | ▶ | sub _build_fallback { 1 } | ||||||||
|
Subroutine
_build_fallback: called CC=1 SCAR=0.0
| |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | 4 | has try_built_method => ( isa => 'Str', is => ro =>, lazy_build => 1, ); | |||||||||
| 124 | 1 | ▶ | sub _build_try_built_method { 'mtime' } | ||||||||
|
Subroutine
_build_try_built_method: called CC=1 SCAR=0.0
| |||||||||||
| 125 | |||||||||||
| 126 | 1 | ▶ | sub _pick_latest_mtime { | ||||||||
|
Subroutine
_pick_latest_mtime: called CC=1 SCAR=0.0
| |||||||||||
| 127 | 1 | my ( undef, @candidates ) = @_; | |||||||||
| 128 | 1 | ▶ | return max_by { $_->stat->mtime } @candidates; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 3 | ▶ | sub _get_candidate_version { | ||||||||
|
Subroutine
_get_candidate_version: called CC=2 SCAR=7.0
| |||||||||||
| 132 | 3 | my ( $self, $candidate ) = @_; | |||||||||
| 133 | 3 | my $distname = $self->distname; | |||||||||
| 134 | 3 | ▶ | if ( $candidate->basename =~ /\A\Q$distname\E-(.+\z)/msx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 3 | my $version = $1; | |||||||||
| 136 | 3 | $version =~ s/-TRIAL\z//msx; | |||||||||
| 137 | 3 | return version->parse($version); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 1 | ▶ | sub _pick_latest_parseversion { | ||||||||
|
Subroutine
_pick_latest_parseversion: called CC=1 SCAR=0.0
| |||||||||||
| 143 | 1 | my ( $self, @candidates ) = @_; | |||||||||
| 144 | 1 | ▶ | return max_by { $self->_get_candidate_version($_) } @candidates; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 4 | my (%methods) = ( | |||||||||
| 148 | mtime => _pick_latest_mtime =>, | ||||||||||
| 149 | parseversion => _pick_latest_parseversion =>, | ||||||||||
| 150 | ); | ||||||||||
| 151 | |||||||||||
| 152 | 2 | ▶ | sub _pick_candidate { | ||||||||
|
Subroutine
_pick_candidate: called CC=2 SCAR=7.6
| |||||||||||
| 153 | 2 | my ( $self, @candidates ) = @_; | |||||||||
| 154 | 2 | my $method = $self->try_built_method; | |||||||||
| 155 | 2 | ▶ | if ( not exists $methods{$method} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 0 | require Carp; | |||||||||
| 157 | 0 | Carp::croak("No such candidate picking method $method"); | |||||||||
| 158 | } | ||||||||||
| 159 | 2 | $method = $methods{$method}; | |||||||||
| 160 | 2 | return $self->$method(@candidates); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 4 | has _bootstrap_root => ( is => ro =>, lazy_build => 1 ); | |||||||||
| 164 | |||||||||||
| 165 | 3 | ▶ | sub _build__bootstrap_root { | ||||||||
|
Subroutine
_build__bootstrap_root: called CC=5 SCAR=18.9
| |||||||||||
| 166 | 3 | my ($self) = @_; | |||||||||
| 167 | 3 | ▶ | if ( not $self->try_built ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 1 | return $self->_cwd; | |||||||||
| 169 | } | ||||||||||
| 170 | 2 | my $distname = $self->distname; | |||||||||
| 171 | |||||||||||
| 172 | 2 | my (@candidates) = grep { $_->basename =~ /\A\Q$distname\E-/msx } grep { $_->is_dir } $self->_cwd->children; | |||||||||
| 173 | |||||||||||
| 174 | 2 | ▶ | if ( 1 == scalar @candidates ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | return $candidates[0]; | |||||||||
| 176 | } | ||||||||||
| 177 | 2 | ▶ | if ( scalar @candidates < 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 178 | 0 | ▶ | if ( not $self->fallback ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | 0 | $self->log( [ 'candidates for bootstrap (%s) == 0, and fallback disabled. not bootstrapping', 0 + @candidates ] ); | |||||||||
| 180 | 0 | return; | |||||||||
| 181 | } | ||||||||||
| 182 | else { | ||||||||||
| 183 | 0 | $self->log( [ 'candidates for bootstrap (%s) == 0, fallback to boostrapping <distname>/', 0 + @candidates ] ); | |||||||||
| 184 | 0 | return $self->_cwd; | |||||||||
| 185 | } | ||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 2 | $self->log_debug( [ '>1 candidates, picking one by method %s', $self->try_built_method ] ); | |||||||||
| 189 | 2 | return $self->_pick_candidate(@candidates); | |||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 0 | ▶ | sub _add_inc { | ||||||||
|
Subroutine
_add_inc: not called CC=2 SCAR=17.9
| |||||||||||
| 193 | 0 | my ( undef, $import ) = @_; | |||||||||
| 194 | 0 | ▶ | if ( not ref $import ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | 0 | require lib; | |||||||||
| 196 | 0 | return lib->import($import); | |||||||||
| 197 | } | ||||||||||
| 198 | 0 | require Carp; | |||||||||
| 199 | 0 | return Carp::croak('At this time, _add_inc(arg) only supports scalar values of arg'); | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | |||||||||||
| 203 | |||||||||||
| 204 | |||||||||||
| 205 | |||||||||||
| 206 | |||||||||||
| 207 | |||||||||||
| 208 | |||||||||||
| 209 | |||||||||||
| 210 | |||||||||||
| 211 | |||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | 4 | requires 'bootstrap'; | |||||||||
| 215 | |||||||||||
| 216 | 3 | ▶ | around plugin_from_config => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 217 | 3 | my ( $orig, $plugin_class, $name, $payload, $section ) = @_; | |||||||||
| 218 | |||||||||||
| 219 | 3 | my $instance = $plugin_class->$orig( $name, $payload, $section ); | |||||||||
| 220 | |||||||||||
| 221 | 3 | $instance->bootstrap; | |||||||||
| 222 | |||||||||||
| 223 | 3 | return $instance; | |||||||||
| 224 | 4 | }; | |||||||||
| 225 | |||||||||||
| 226 | 4 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 227 | |||||||||||
| 228 | 4 | 1; | |||||||||
| 229 | |||||||||||
| 230 | __END__ | ||||||||||