| 1 | package CPAN::Maker::Bootstrapper::Role::Importer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use Carp; | ||
| 8 | use Cwd qw(abs_path); | ||
| 9 | use English qw(-no_match_vars); | ||
| 10 | use List::Util qw(none); | ||
| 11 | use Scalar::Util qw(reftype); | ||
| 12 | |||
| 13 | # not used yet | ||
| 14 | use CLI::Simple::Utils qw(slurp choose); | ||
| 15 | use Data::Dumper; | ||
| 16 | use File::Basename qw(basename dirname); | ||
| 17 | use File::Copy qw(copy); | ||
| 18 | use File::Find; | ||
| 19 | use File::Path qw(make_path remove_tree); | ||
| 20 | use File::Spec; | ||
| 21 | use File::Temp; | ||
| 22 | |||
| 23 | use Role::Tiny; | ||
| 24 | |||
| 25 | our $VERSION = '2.6.2'; | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | sub cmd_import { | ||
| 29 | ######################################################################## | ||
| 30 | my ($self) = @_; | ||
| 31 | |||
| 32 | my ($import_manifest) = $self->get_args; | ||
| 33 | |||
| 34 | die "usage: import manifest-file\n" | ||
| 35 | if !$import_manifest; | ||
| 36 | |||
| 37 | die sprintf "ERROR: %s does not exist or is not readable\n", $import_manifest | ||
| 38 | if !-r $import_manifest; | ||
| 39 | |||
| 40 | my $manifest = $self->_load_import_manifest($import_manifest); | ||
| 41 | |||
| 42 | foreach my $key (qw(base-dir module import)) { | ||
| 43 | next if exists $manifest->{$key}; | ||
| 44 | |||
| 45 | die "ERROR: import manifests must contain $key\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $base_dir = abs_path( $manifest->{'base-dir'} ); | ||
| 49 | |||
| 50 | die sprintf "ERROR: %s does not specify a valid import path\n", $manifest->{'base-dir'} | ||
| 51 | if !-d $base_dir; | ||
| 52 | |||
| 53 | $manifest->{'base-dir'} = $base_dir; | ||
| 54 | |||
| 55 | my $import_list = $manifest->{import}; | ||
| 56 | |||
| 57 | $self->_test_type( import => $import_list, 'ARRAY' ); | ||
| 58 | |||
| 59 | die "ERROR: import list must include at least a lib source\n" | ||
| 60 | if none { ref $_ eq 'HASH' && exists $_->{lib} } @{$import_list}; | ||
| 61 | |||
| 62 | my $listing = $self->_manifest_file_listing($manifest); | ||
| 63 | |||
| 64 | $self->set_import_file_listing($listing); | ||
| 65 | |||
| 66 | if ( $self->get_dry_run ) { | ||
| 67 | $self->_report_import_plan; | ||
| 68 | return $SUCCESS; | ||
| 69 | } | ||
| 70 | |||
| 71 | local $ENV{SYNTAX_CHECKING} = 'off'; | ||
| 72 | |||
| 73 | return $self->_import_project( | ||
| 74 | module => $manifest->{module}, | ||
| 75 | import_paths => [$base_dir] | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | ######################################################################## | ||
| 80 | sub _manifest_file_listing { | ||
| 81 | ######################################################################## | ||
| 82 | my ( $self, $manifest ) = @_; | ||
| 83 | |||
| 84 | warn sprintf "IMPORTER: %s\n", $INC{'CPAN/Maker/Bootstrapper/Role/Importer.pm'}; | ||
| 85 | |||
| 86 | my $base_dir = $manifest->{'base-dir'}; | ||
| 87 | my @files; | ||
| 88 | |||
| 89 | foreach my $entry ( @{ $manifest->{import} } ) { | ||
| 90 | $self->_test_type( import => $entry, 'HASH' ); | ||
| 91 | |||
| 92 | die "ERROR: each import entry must contain exactly one destination\n" | ||
| 93 | if keys %{$entry} != 1; | ||
| 94 | |||
| 95 | foreach my $destination ( sort keys %{$entry} ) { | ||
| 96 | my $spec = $entry->{$destination}; | ||
| 97 | |||
| 98 | $self->_test_type( $destination => $spec, 'HASH' ); | ||
| 99 | |||
| 100 | $self->_importer_validate_import_destination( $destination, 'destination' ); | ||
| 101 | |||
| 102 | die sprintf "ERROR: import destination '%s' requires a path\n", $destination | ||
| 103 | if !defined $spec->{path}; | ||
| 104 | |||
| 105 | if ( defined $spec->{dest} ) { | ||
| 106 | $self->_importer_validate_import_destination( $spec->{dest}, 'dest' ); | ||
| 107 | |||
| 108 | $destination = File::Spec->catdir( $destination, $spec->{dest} ); | ||
| 109 | } | ||
| 110 | |||
| 111 | my $extensions = $spec->{ext}; | ||
| 112 | |||
| 113 | if ( defined $extensions ) { | ||
| 114 | my @extensions | ||
| 115 | = ( reftype($extensions) // q{} ) eq 'ARRAY' | ||
| 116 | ? @{$extensions} | ||
| 117 | : ($extensions); | ||
| 118 | |||
| 119 | foreach my $extension (@extensions) { | ||
| 120 | next if $extension eq q{*}; | ||
| 121 | |||
| 122 | $self->_validate_import_regexp( $extension, 'ext' ); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | my $paths = $spec->{path}; | ||
| 127 | |||
| 128 | if ( ( reftype($paths) // q{} ) ne 'ARRAY' ) { | ||
| 129 | $paths = [$paths]; | ||
| 130 | } | ||
| 131 | |||
| 132 | foreach my $path ( @{$paths} ) { | ||
| 133 | my $source = File::Spec->file_name_is_absolute($path) ? $path : File::Spec->catfile( $base_dir, $path ); | ||
| 134 | |||
| 135 | $source = abs_path($source); | ||
| 136 | |||
| 137 | croak sprintf 'ERROR: import source does not exist: %s', $path | ||
| 138 | if !defined $source; | ||
| 139 | |||
| 140 | my $relative_source = File::Spec->abs2rel( $source, $base_dir ); | ||
| 141 | $relative_source =~ s{\\}{/}gxsm; | ||
| 142 | |||
| 143 | croak sprintf 'ERROR: import source is outside base-dir: %s', $path | ||
| 144 | if $relative_source eq q{..} | ||
| 145 | || $relative_source =~ m{\A[.][.]/}xsm; | ||
| 146 | |||
| 147 | if ( -f $source ) { | ||
| 148 | $self->_add_import_file( | ||
| 149 | files => \@files, | ||
| 150 | source => $source, | ||
| 151 | destination => $destination, | ||
| 152 | base_dir => $base_dir | ||
| 153 | ); | ||
| 154 | |||
| 155 | next; | ||
| 156 | } | ||
| 157 | elsif ( -d $source ) { | ||
| 158 | my $recurse = $spec->{recurse} // $FALSE; | ||
| 159 | |||
| 160 | my @exclusions; | ||
| 161 | |||
| 162 | foreach my $exclude_set ( $manifest->{exclude}, $spec->{exclude} ) { | ||
| 163 | next if !defined $exclude_set; | ||
| 164 | |||
| 165 | if ( ( reftype($exclude_set) // q{} ) eq 'ARRAY' ) { | ||
| 166 | push @exclusions, @{$exclude_set}; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | push @exclusions, $exclude_set; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | foreach my $exclude (@exclusions) { | ||
| 174 | next if index( $exclude, q{/} ) >= 0; | ||
| 175 | |||
| 176 | $self->_validate_import_regexp( $exclude, 'exclude' ); | ||
| 177 | } | ||
| 178 | |||
| 179 | File::Find::find( | ||
| 180 | { no_chdir => $TRUE, | ||
| 181 | wanted => sub { | ||
| 182 | my $file = $File::Find::name; | ||
| 183 | |||
| 184 | if ( -d $file ) { | ||
| 185 | if ( | ||
| 186 | $file ne $source | ||
| 187 | && (!$recurse | ||
| 188 | || $self->_excluded_import_path( $file, $base_dir, \@exclusions ) ) | ||
| 189 | ) { | ||
| 190 | $File::Find::prune = $TRUE; | ||
| 191 | } | ||
| 192 | |||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | return if !-f $file; | ||
| 197 | |||
| 198 | return | ||
| 199 | if $self->_excluded_import_path( $file, $base_dir, \@exclusions ); | ||
| 200 | |||
| 201 | return | ||
| 202 | if !$self->_matches_import_extension( $file, $spec->{ext} ); | ||
| 203 | |||
| 204 | my $relative = File::Spec->abs2rel( $file, $source ); | ||
| 205 | |||
| 206 | $self->_add_import_file( | ||
| 207 | files => \@files, | ||
| 208 | source => $file, | ||
| 209 | destination => $destination, | ||
| 210 | base_dir => $base_dir, | ||
| 211 | filename => $relative | ||
| 212 | ); | ||
| 213 | |||
| 214 | return; | ||
| 215 | }, | ||
| 216 | }, | ||
| 217 | $source | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | croak sprintf 'ERROR: unsupported import source: %s', $path; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | return { | ||
| 228 | files => $self->_resolve_import_collisions( \@files ), | ||
| 229 | packages => {}, | ||
| 230 | }; | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | ######################################################################## | ||
| 235 | sub _matches_import_extension { | ||
| 236 | ######################################################################## | ||
| 237 | my ( $self, $file, $extensions ) = @_; | ||
| 238 | |||
| 239 | return $TRUE | ||
| 240 | if !defined $extensions || $extensions eq q{*}; | ||
| 241 | |||
| 242 | if ( ( reftype($extensions) // q{} ) ne 'ARRAY' ) { | ||
| 243 | $extensions = [$extensions]; | ||
| 244 | } | ||
| 245 | |||
| 246 | foreach my $extension ( @{$extensions} ) { | ||
| 247 | return $TRUE if $extension eq q{*}; | ||
| 248 | |||
| 249 | return $TRUE | ||
| 250 | if $file =~ /[.](?:$extension)$/xsm; | ||
| 251 | } | ||
| 252 | |||
| 253 | return $FALSE; | ||
| 254 | } | ||
| 255 | |||
| 256 | ######################################################################## | ||
| 257 | sub _resolve_import_collisions { | ||
| 258 | ######################################################################## | ||
| 259 | my ( $self, $files ) = @_; | ||
| 260 | |||
| 261 | my %seen; | ||
| 262 | my %destinations; | ||
| 263 | my @resolved; | ||
| 264 | |||
| 265 | foreach my $file ( @{$files} ) { | ||
| 266 | my $source = $file->{source}; | ||
| 267 | my $destination = $file->{destination}; | ||
| 268 | |||
| 269 | next if $seen{$destination}{$source}++; | ||
| 270 | |||
| 271 | push @resolved, $file; | ||
| 272 | push @{ $destinations{$destination} }, $file; | ||
| 273 | } | ||
| 274 | |||
| 275 | foreach my $destination ( keys %destinations ) { | ||
| 276 | my $conflicts = $destinations{$destination}; | ||
| 277 | |||
| 278 | next if @{$conflicts} == 1; | ||
| 279 | |||
| 280 | foreach my $file ( @{$conflicts} ) { | ||
| 281 | $file->{original_destination} = $destination; | ||
| 282 | $file->{destination} = $self->_import_error_destination($file); | ||
| 283 | $file->{class} = 'import-error'; | ||
| 284 | $file->{reason} = sprintf q{multiple imported files map to '%s'}, $destination; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | my %final_destinations; | ||
| 289 | |||
| 290 | foreach my $file (@resolved) { | ||
| 291 | my $destination = $file->{destination}; | ||
| 292 | |||
| 293 | if ( exists $final_destinations{$destination} ) { | ||
| 294 | croak sprintf "ERROR: multiple import sources resolve to %s\n", $destination; | ||
| 295 | } | ||
| 296 | |||
| 297 | $final_destinations{$destination} = $file->{source}; | ||
| 298 | } | ||
| 299 | |||
| 300 | return \@resolved; | ||
| 301 | } | ||
| 302 | |||
| 303 | ######################################################################## | ||
| 304 | sub _add_import_file { | ||
| 305 | ######################################################################## | ||
| 306 | my ( $self, %args ) = @_; | ||
| 307 | |||
| 308 | my ( $files, $source, $destination, $base_dir, $filename ) = @args{qw(files source destination base_dir filename)}; | ||
| 309 | |||
| 310 | $filename //= basename($source); | ||
| 311 | |||
| 312 | $filename =~ s{[.](pm|pl)$}{.$1.in}xsm; | ||
| 313 | |||
| 314 | my $target = File::Spec->catfile( $destination, $filename ); | ||
| 315 | my $relative = File::Spec->abs2rel( $source, $base_dir ); | ||
| 316 | |||
| 317 | push @{$files}, | ||
| 318 | { | ||
| 319 | source => $source, | ||
| 320 | destination => $target, | ||
| 321 | import_root => dirname($relative), | ||
| 322 | relative => basename($relative), | ||
| 323 | class => $destination eq 'lib' ? 'module' : 'file', | ||
| 324 | }; | ||
| 325 | |||
| 326 | return; | ||
| 327 | } | ||
| 328 | |||
| 329 | ######################################################################## | ||
| 330 | sub _excluded_import_path { | ||
| 331 | ######################################################################## | ||
| 332 | my ( $self, $file, $base_dir, $exclusions ) = @_; | ||
| 333 | |||
| 334 | return $FALSE if !defined $exclusions; | ||
| 335 | |||
| 336 | if ( ( reftype($exclusions) // q{} ) ne 'ARRAY' ) { | ||
| 337 | $exclusions = [$exclusions]; | ||
| 338 | } | ||
| 339 | |||
| 340 | my $relative = File::Spec->abs2rel( $file, $base_dir ); | ||
| 341 | $relative =~ s{\\}{/}gxsm; | ||
| 342 | |||
| 343 | foreach my $exclude ( @{$exclusions} ) { | ||
| 344 | if ( index( $exclude, q{/} ) >= 0 ) { | ||
| 345 | my $directory = $exclude; | ||
| 346 | $directory =~ s{\A[.]/}{}xsm; | ||
| 347 | $directory =~ s{/+\z}{}xsm; | ||
| 348 | |||
| 349 | return $TRUE | ||
| 350 | if $relative eq $directory | ||
| 351 | || index( $relative, "$directory/" ) == 0; | ||
| 352 | |||
| 353 | next; | ||
| 354 | } | ||
| 355 | |||
| 356 | return $TRUE | ||
| 357 | if basename($file) =~ /(?:$exclude)$/xsm; | ||
| 358 | } | ||
| 359 | |||
| 360 | return $FALSE; | ||
| 361 | } | ||
| 362 | |||
| 363 | ######################################################################## | ||
| 364 | sub _test_type { | ||
| 365 | ######################################################################## | ||
| 366 | my ( $self, $name, $obj, $type ) = @_; | ||
| 367 | |||
| 368 | die "ERROR: $name is not a $type\n" | ||
| 369 | if ( reftype($obj) // q{} ) ne $type; | ||
| 370 | |||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | ######################################################################## | ||
| 375 | sub _load_import_manifest { | ||
| 376 | ######################################################################## | ||
| 377 | my ( $self, $import_manifest ) = @_; | ||
| 378 | |||
| 379 | require YAML::Tiny; | ||
| 380 | |||
| 381 | my $yaml = eval { YAML::Tiny->read($import_manifest); }; | ||
| 382 | |||
| 383 | croak "ERROR: error reading manifest file: $import_manifest\n$EVAL_ERROR" | ||
| 384 | if !$yaml || $EVAL_ERROR; | ||
| 385 | |||
| 386 | return $yaml->[0]; | ||
| 387 | } | ||
| 388 | |||
| 389 | ######################################################################## | ||
| 390 | sub _validate_import_regexp { | ||
| 391 | ######################################################################## | ||
| 392 | my ( $self, $expression, $field ) = @_; | ||
| 393 | |||
| 394 | my $regexp = eval {qr/(?:$expression)/xsm}; | ||
| 395 | |||
| 396 | if ($EVAL_ERROR) { | ||
| 397 | croak sprintf "ERROR: invalid regexp '%s' in import manifest %s:\n%s", $expression, $field, $EVAL_ERROR; | ||
| 398 | } | ||
| 399 | |||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | ######################################################################## | ||
| 404 | sub _importer_validate_import_destination { | ||
| 405 | ######################################################################## | ||
| 406 | my ( $self, $path, $field ) = @_; | ||
| 407 | |||
| 408 | die sprintf "ERROR: invalid import %s: empty path\n", $field | ||
| 409 | if !defined $path || ref $path || $path eq q{}; | ||
| 410 | |||
| 411 | die sprintf "ERROR: invalid import %s: absolute path '%s'\n", $field, $path | ||
| 412 | if File::Spec->file_name_is_absolute($path); | ||
| 413 | |||
| 414 | die sprintf "ERROR: invalid import %s: backslash in '%s'\n", $field, $path | ||
| 415 | if index( $path, q{\\} ) >= 0; | ||
| 416 | |||
| 417 | my @components = File::Spec->splitdir($path); | ||
| 418 | |||
| 419 | die sprintf "ERROR: invalid import %s: parent traversal in '%s'\n", $field, $path | ||
| 420 | if grep { $_ eq q{..} } @components; | ||
| 421 | |||
| 422 | return; | ||
| 423 | } | ||
| 424 | |||
| 425 | 1; |