| 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.5.0'; | ||
| 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 | my $base_dir = $manifest->{'base-dir'}; | ||
| 85 | my @files; | ||
| 86 | |||
| 87 | foreach my $entry ( @{ $manifest->{import} } ) { | ||
| 88 | $self->_test_type( import => $entry, 'HASH' ); | ||
| 89 | |||
| 90 | die "ERROR: each import entry must contain exactly one destination\n" | ||
| 91 | if keys %{$entry} != 1; | ||
| 92 | |||
| 93 | foreach my $destination ( sort keys %{$entry} ) { | ||
| 94 | my $spec = $entry->{$destination}; | ||
| 95 | |||
| 96 | $self->_test_type( $destination => $spec, 'HASH' ); | ||
| 97 | |||
| 98 | $self->_importer_validate_import_destination( $destination, 'destination' ); | ||
| 99 | |||
| 100 | die sprintf "ERROR: import destination '%s' requires a path\n", $destination | ||
| 101 | if !defined $spec->{path}; | ||
| 102 | |||
| 103 | if ( defined $spec->{dest} ) { | ||
| 104 | $self->_importer_validate_import_destination( $spec->{dest}, 'dest' ); | ||
| 105 | |||
| 106 | $destination = File::Spec->catdir( $destination, $spec->{dest} ); | ||
| 107 | } | ||
| 108 | |||
| 109 | my $extensions = $spec->{ext}; | ||
| 110 | |||
| 111 | if ( defined $extensions ) { | ||
| 112 | my @extensions | ||
| 113 | = ( reftype($extensions) // q{} ) eq 'ARRAY' | ||
| 114 | ? @{$extensions} | ||
| 115 | : ($extensions); | ||
| 116 | |||
| 117 | foreach my $extension (@extensions) { | ||
| 118 | next if $extension eq q{*}; | ||
| 119 | |||
| 120 | $self->_validate_import_regexp( $extension, 'ext' ); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | my $paths = $spec->{path}; | ||
| 125 | |||
| 126 | if ( ( reftype($paths) // q{} ) ne 'ARRAY' ) { | ||
| 127 | $paths = [$paths]; | ||
| 128 | } | ||
| 129 | |||
| 130 | foreach my $path ( @{$paths} ) { | ||
| 131 | my $source = File::Spec->file_name_is_absolute($path) ? $path : File::Spec->catfile( $base_dir, $path ); | ||
| 132 | |||
| 133 | $source = abs_path($source); | ||
| 134 | |||
| 135 | croak sprintf 'ERROR: import source does not exist: %s', $path | ||
| 136 | if !defined $source; | ||
| 137 | |||
| 138 | my $relative_source = File::Spec->abs2rel( $source, $base_dir ); | ||
| 139 | $relative_source =~ s{\\}{/}gxsm; | ||
| 140 | |||
| 141 | croak sprintf 'ERROR: import source is outside base-dir: %s', $path | ||
| 142 | if $relative_source eq q{..} | ||
| 143 | || $relative_source =~ m{\A[.][.]/}xsm; | ||
| 144 | |||
| 145 | if ( -f $source ) { | ||
| 146 | $self->_add_import_file( | ||
| 147 | files => \@files, | ||
| 148 | source => $source, | ||
| 149 | destination => $destination, | ||
| 150 | base_dir => $base_dir | ||
| 151 | ); | ||
| 152 | |||
| 153 | next; | ||
| 154 | } | ||
| 155 | elsif ( -d $source ) { | ||
| 156 | my $recurse = $spec->{recurse} // $FALSE; | ||
| 157 | |||
| 158 | my @exclusions; | ||
| 159 | |||
| 160 | foreach my $exclude_set ( $manifest->{exclude}, $spec->{exclude} ) { | ||
| 161 | next if !defined $exclude_set; | ||
| 162 | |||
| 163 | if ( ( reftype($exclude_set) // q{} ) eq 'ARRAY' ) { | ||
| 164 | push @exclusions, @{$exclude_set}; | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | push @exclusions, $exclude_set; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | foreach my $exclude (@exclusions) { | ||
| 172 | next if index( $exclude, q{/} ) >= 0; | ||
| 173 | |||
| 174 | $self->_validate_import_regexp( $exclude, 'exclude' ); | ||
| 175 | } | ||
| 176 | |||
| 177 | File::Find::find( | ||
| 178 | { no_chdir => $TRUE, | ||
| 179 | wanted => sub { | ||
| 180 | my $file = $File::Find::name; | ||
| 181 | |||
| 182 | if ( -d $file ) { | ||
| 183 | if ( | ||
| 184 | $file ne $source | ||
| 185 | && (!$recurse | ||
| 186 | || $self->_excluded_import_path( $file, $base_dir, \@exclusions ) ) | ||
| 187 | ) { | ||
| 188 | $File::Find::prune = $TRUE; | ||
| 189 | } | ||
| 190 | |||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | return if !-f $file; | ||
| 195 | |||
| 196 | return | ||
| 197 | if $self->_excluded_import_path( $file, $base_dir, \@exclusions ); | ||
| 198 | |||
| 199 | return | ||
| 200 | if !$self->_matches_import_extension( $file, $spec->{ext} ); | ||
| 201 | |||
| 202 | my $relative = File::Spec->abs2rel( $file, $source ); | ||
| 203 | |||
| 204 | $self->_add_import_file( | ||
| 205 | files => \@files, | ||
| 206 | source => $file, | ||
| 207 | destination => $destination, | ||
| 208 | base_dir => $base_dir, | ||
| 209 | filename => $relative | ||
| 210 | ); | ||
| 211 | |||
| 212 | return; | ||
| 213 | }, | ||
| 214 | }, | ||
| 215 | $source | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | croak sprintf 'ERROR: unsupported import source: %s', $path; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | return { | ||
| 226 | files => $self->_resolve_import_collisions( \@files ), | ||
| 227 | packages => {}, | ||
| 228 | }; | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | ######################################################################## | ||
| 233 | sub _matches_import_extension { | ||
| 234 | ######################################################################## | ||
| 235 | my ( $self, $file, $extensions ) = @_; | ||
| 236 | |||
| 237 | return $TRUE | ||
| 238 | if !defined $extensions || $extensions eq q{*}; | ||
| 239 | |||
| 240 | if ( ( reftype($extensions) // q{} ) ne 'ARRAY' ) { | ||
| 241 | $extensions = [$extensions]; | ||
| 242 | } | ||
| 243 | |||
| 244 | foreach my $extension ( @{$extensions} ) { | ||
| 245 | return $TRUE if $extension eq q{*}; | ||
| 246 | |||
| 247 | return $TRUE | ||
| 248 | if $file =~ /[.](?:$extension)$/xsm; | ||
| 249 | } | ||
| 250 | |||
| 251 | return $FALSE; | ||
| 252 | } | ||
| 253 | |||
| 254 | ######################################################################## | ||
| 255 | sub _resolve_import_collisions { | ||
| 256 | ######################################################################## | ||
| 257 | my ( $self, $files ) = @_; | ||
| 258 | |||
| 259 | my %seen; | ||
| 260 | my %destinations; | ||
| 261 | my @resolved; | ||
| 262 | |||
| 263 | foreach my $file ( @{$files} ) { | ||
| 264 | my $source = $file->{source}; | ||
| 265 | my $destination = $file->{destination}; | ||
| 266 | |||
| 267 | next if $seen{$destination}{$source}++; | ||
| 268 | |||
| 269 | push @resolved, $file; | ||
| 270 | push @{ $destinations{$destination} }, $file; | ||
| 271 | } | ||
| 272 | |||
| 273 | foreach my $destination ( keys %destinations ) { | ||
| 274 | my $conflicts = $destinations{$destination}; | ||
| 275 | |||
| 276 | next if @{$conflicts} == 1; | ||
| 277 | |||
| 278 | foreach my $file ( @{$conflicts} ) { | ||
| 279 | $file->{original_destination} = $destination; | ||
| 280 | $file->{destination} = $self->_import_error_destination($file); | ||
| 281 | $file->{class} = 'import-error'; | ||
| 282 | $file->{reason} = sprintf q{multiple imported files map to '%s'}, $destination; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | my %final_destinations; | ||
| 287 | |||
| 288 | foreach my $file (@resolved) { | ||
| 289 | my $destination = $file->{destination}; | ||
| 290 | |||
| 291 | if ( exists $final_destinations{$destination} ) { | ||
| 292 | croak sprintf "ERROR: multiple import sources resolve to %s\n", $destination; | ||
| 293 | } | ||
| 294 | |||
| 295 | $final_destinations{$destination} = $file->{source}; | ||
| 296 | } | ||
| 297 | |||
| 298 | return \@resolved; | ||
| 299 | } | ||
| 300 | |||
| 301 | ######################################################################## | ||
| 302 | sub _add_import_file { | ||
| 303 | ######################################################################## | ||
| 304 | my ( $self, %args ) = @_; | ||
| 305 | |||
| 306 | my ( $files, $source, $destination, $base_dir, $filename ) = @args{qw(files source destination base_dir filename)}; | ||
| 307 | |||
| 308 | $filename //= basename($source); | ||
| 309 | |||
| 310 | $filename =~ s{[.](pm|pl)$}{.$1.in}xsm; | ||
| 311 | |||
| 312 | my $target = File::Spec->catfile( $destination, $filename ); | ||
| 313 | my $relative = File::Spec->abs2rel( $source, $base_dir ); | ||
| 314 | |||
| 315 | push @{$files}, | ||
| 316 | { | ||
| 317 | source => $source, | ||
| 318 | destination => $target, | ||
| 319 | import_root => dirname($relative), | ||
| 320 | relative => basename($relative), | ||
| 321 | class => $destination eq 'lib' ? 'module' : 'file', | ||
| 322 | }; | ||
| 323 | |||
| 324 | return; | ||
| 325 | } | ||
| 326 | |||
| 327 | ######################################################################## | ||
| 328 | sub _excluded_import_path { | ||
| 329 | ######################################################################## | ||
| 330 | my ( $self, $file, $base_dir, $exclusions ) = @_; | ||
| 331 | |||
| 332 | return $FALSE if !defined $exclusions; | ||
| 333 | |||
| 334 | if ( ( reftype($exclusions) // q{} ) ne 'ARRAY' ) { | ||
| 335 | $exclusions = [$exclusions]; | ||
| 336 | } | ||
| 337 | |||
| 338 | my $relative = File::Spec->abs2rel( $file, $base_dir ); | ||
| 339 | $relative =~ s{\\}{/}gxsm; | ||
| 340 | |||
| 341 | foreach my $exclude ( @{$exclusions} ) { | ||
| 342 | if ( index( $exclude, q{/} ) >= 0 ) { | ||
| 343 | my $directory = $exclude; | ||
| 344 | $directory =~ s{\A[.]/}{}xsm; | ||
| 345 | $directory =~ s{/+\z}{}xsm; | ||
| 346 | |||
| 347 | return $TRUE | ||
| 348 | if $relative eq $directory | ||
| 349 | || index( $relative, "$directory/" ) == 0; | ||
| 350 | |||
| 351 | next; | ||
| 352 | } | ||
| 353 | |||
| 354 | return $TRUE | ||
| 355 | if basename($file) =~ /(?:$exclude)$/xsm; | ||
| 356 | } | ||
| 357 | |||
| 358 | return $FALSE; | ||
| 359 | } | ||
| 360 | |||
| 361 | ######################################################################## | ||
| 362 | sub _test_type { | ||
| 363 | ######################################################################## | ||
| 364 | my ( $self, $name, $obj, $type ) = @_; | ||
| 365 | |||
| 366 | die "ERROR: $name is not a $type\n" | ||
| 367 | if ( reftype($obj) // q{} ) ne $type; | ||
| 368 | |||
| 369 | return; | ||
| 370 | } | ||
| 371 | |||
| 372 | ######################################################################## | ||
| 373 | sub _load_import_manifest { | ||
| 374 | ######################################################################## | ||
| 375 | my ( $self, $import_manifest ) = @_; | ||
| 376 | |||
| 377 | require YAML::Tiny; | ||
| 378 | |||
| 379 | my $yaml = eval { YAML::Tiny->read($import_manifest); }; | ||
| 380 | |||
| 381 | croak "ERROR: error reading manifest file: $import_manifest\n$EVAL_ERROR" | ||
| 382 | if !$yaml || $EVAL_ERROR; | ||
| 383 | |||
| 384 | return $yaml->[0]; | ||
| 385 | } | ||
| 386 | |||
| 387 | ######################################################################## | ||
| 388 | sub _validate_import_regexp { | ||
| 389 | ######################################################################## | ||
| 390 | my ( $self, $expression, $field ) = @_; | ||
| 391 | |||
| 392 | my $regexp = eval {qr/(?:$expression)/xsm}; | ||
| 393 | |||
| 394 | if ($EVAL_ERROR) { | ||
| 395 | croak sprintf "ERROR: invalid regexp '%s' in import manifest %s:\n%s", $expression, $field, $EVAL_ERROR; | ||
| 396 | } | ||
| 397 | |||
| 398 | return; | ||
| 399 | } | ||
| 400 | |||
| 401 | ######################################################################## | ||
| 402 | sub _importer_validate_import_destination { | ||
| 403 | ######################################################################## | ||
| 404 | my ( $self, $path, $field ) = @_; | ||
| 405 | |||
| 406 | die sprintf "ERROR: invalid import %s: empty path\n", $field | ||
| 407 | if !defined $path || ref $path || $path eq q{}; | ||
| 408 | |||
| 409 | die sprintf "ERROR: invalid import %s: absolute path '%s'\n", $field, $path | ||
| 410 | if File::Spec->file_name_is_absolute($path); | ||
| 411 | |||
| 412 | die sprintf "ERROR: invalid import %s: backslash in '%s'\n", $field, $path | ||
| 413 | if index( $path, q{\\} ) >= 0; | ||
| 414 | |||
| 415 | my @components = File::Spec->splitdir($path); | ||
| 416 | |||
| 417 | die sprintf "ERROR: invalid import %s: parent traversal in '%s'\n", $field, $path | ||
| 418 | if grep { $_ eq q{..} } @components; | ||
| 419 | |||
| 420 | return; | ||
| 421 | } | ||
| 422 | |||
| 423 | 1; |