| 1 | package VIM::Packager::Manifest; | ||
| 2 | require Exporter; | ||
| 3 | use Config; | ||
| 4 | use File::Basename; | ||
| 5 | use File::Copy 'copy'; | ||
| 6 | use File::Find; | ||
| 7 | use File::Spec; | ||
| 8 | use Carp; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | use vars qw($VERSION @ISA @EXPORT_OK | ||
| 12 | $Is_MacOS $Is_VMS $Is_VMS_mode $Is_VMS_lc $Is_VMS_nodot | ||
| 13 | $Debug $Verbose $Quiet $MANIFEST $DEFAULT_MSKIP); | ||
| 14 | |||
| 15 | $VERSION = '0.1'; | ||
| 16 | @ISA=('Exporter'); | ||
| 17 | @EXPORT_OK = qw(mkmanifest | ||
| 18 | manicheck filecheck fullcheck skipcheck | ||
| 19 | manifind maniread manicopy maniadd | ||
| 20 | maniskip | ||
| 21 | ); | ||
| 22 | |||
| 23 | $Is_MacOS = $^O eq 'MacOS'; | ||
| 24 | $Is_VMS = $^O eq 'VMS'; | ||
| 25 | $Is_VMS_mode = 0; | ||
| 26 | $Is_VMS_lc = 0; | ||
| 27 | $Is_VMS_nodot = 0; # No dots in dir names or double dots in files | ||
| 28 | |||
| 29 | if ($Is_VMS) { | ||
| 30 | require VMS::Filespec if $Is_VMS; | ||
| 31 | my $vms_unix_rpt; | ||
| 32 | my $vms_efs; | ||
| 33 | my $vms_case; | ||
| 34 | |||
| 35 | $Is_VMS_mode = 1; | ||
| 36 | $Is_VMS_lc = 1; | ||
| 37 | $Is_VMS_nodot = 1; | ||
| 38 | if (eval { local $SIG{__DIE__}; require VMS::Feature; }) { | ||
| 39 | $vms_unix_rpt = VMS::Feature::current("filename_unix_report"); | ||
| 40 | $vms_efs = VMS::Feature::current("efs_charset"); | ||
| 41 | $vms_case = VMS::Feature::current("efs_case_preserve"); | ||
| 42 | } else { | ||
| 43 | my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || ''; | ||
| 44 | my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || ''; | ||
| 45 | my $efs_case = $ENV{'DECC$EFS_CASE_PRESERVE'} || ''; | ||
| 46 | $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i; | ||
| 47 | $vms_efs = $efs_charset =~ /^[ET1]/i; | ||
| 48 | $vms_case = $efs_case =~ /^[ET1]/i; | ||
| 49 | } | ||
| 50 | $Is_VMS_lc = 0 if ($vms_case); | ||
| 51 | $Is_VMS_mode = 0 if ($vms_unix_rpt); | ||
| 52 | $Is_VMS_nodot = 0 if ($vms_efs); | ||
| 53 | } | ||
| 54 | |||
| 55 | $Debug = 0; | ||
| 56 | $Verbose = 1; | ||
| 57 | $Quiet = 0; | ||
| 58 | $MANIFEST = 'MANIFEST'; | ||
| 59 | |||
| 60 | $DEFAULT_MSKIP = File::Spec->catfile( dirname(__FILE__), "$MANIFEST.SKIP" ); | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | =head1 SYNOPSIS | ||
| 65 | |||
| 66 | use VIM::Packager::Manifest qw(...funcs to import...); | ||
| 67 | |||
| 68 | mkmanifest(); | ||
| 69 | |||
| 70 | my @missing_files = manicheck; | ||
| 71 | my @skipped = skipcheck; | ||
| 72 | my @extra_files = filecheck; | ||
| 73 | my($missing, $extra) = fullcheck; | ||
| 74 | |||
| 75 | my $found = manifind(); | ||
| 76 | |||
| 77 | my $manifest = maniread(); | ||
| 78 | |||
| 79 | manicopy($read,$target); | ||
| 80 | |||
| 81 | maniadd({$file => $comment, ...}); | ||
| 82 | |||
| 83 | |||
| 84 | =head1 DESCRIPTION | ||
| 85 | |||
| 86 | this is borrowed from ExtUtils::Manifest | ||
| 87 | |||
| 88 | =head2 Functions | ||
| 89 | |||
| 90 | VIM::Packager::Manifest exports no functions by default. The following are | ||
| 91 | exported on request | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =item mkmanifest | ||
| 96 | |||
| 97 | mkmanifest(); | ||
| 98 | |||
| 99 | Writes all files in and below the current directory to your F<MANIFEST>. | ||
| 100 | It works similar to the result of the Unix command | ||
| 101 | |||
| 102 | find . > MANIFEST | ||
| 103 | |||
| 104 | All files that match any regular expression in a file F<MANIFEST.SKIP> | ||
| 105 | (if it exists) are ignored. | ||
| 106 | |||
| 107 | Any existing F<MANIFEST> file will be saved as F<MANIFEST.bak>. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub _sort { | ||
| 112 | return sort { lc $a cmp lc $b } @_; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub mkmanifest { | ||
| 116 | my $manimiss = 0; | ||
| 117 | my $read = (-r 'MANIFEST' && maniread()) or $manimiss++; | ||
| 118 | $read = {} if $manimiss; | ||
| 119 | local *M; | ||
| 120 | my $bakbase = $MANIFEST; | ||
| 121 | $bakbase =~ s/\./_/g if $Is_VMS_nodot; # avoid double dots | ||
| 122 | rename $MANIFEST, "$bakbase.bak" unless $manimiss; | ||
| 123 | open M, "> $MANIFEST" or die "Could not open $MANIFEST: $!"; | ||
| 124 | my $skip = maniskip(); | ||
| 125 | my $found = manifind(); | ||
| 126 | my($key,$val,$file,%all); | ||
| 127 | %all = (%$found, %$read); | ||
| 128 | |||
| 129 | $all{$MANIFEST} = ($Is_VMS_mode ? "$MANIFEST\t\t" : '') | ||
| 130 | if $manimiss; # add new MANIFEST to known file list | ||
| 131 | foreach $file (_sort keys %all) { | ||
| 132 | if ($skip->($file)) { | ||
| 133 | # Policy: only remove files if they're listed in MANIFEST.SKIP. | ||
| 134 | # Don't remove files just because they don't exist. | ||
| 135 | warn "Removed from $MANIFEST: $file\n" if $Verbose and exists $read->{$file}; | ||
| 136 | next; | ||
| 137 | } | ||
| 138 | if ($Verbose){ | ||
| 139 | warn "Added to $MANIFEST: $file\n" unless exists $read->{$file}; | ||
| 140 | } | ||
| 141 | my $text = $all{$file}; | ||
| 142 | $file = _unmacify($file); | ||
| 143 | my $tabs = (5 - (length($file)+1)/8); | ||
| 144 | $tabs = 1 if $tabs < 1; | ||
| 145 | $tabs = 0 unless $text; | ||
| 146 | if ($file =~ /\s/) { | ||
| 147 | $file =~ s/([\\'])/\\$1/g; | ||
| 148 | $file = "'$file'"; | ||
| 149 | } | ||
| 150 | print M $file, "\t" x $tabs, $text, "\n"; | ||
| 151 | } | ||
| 152 | close M; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Geez, shouldn't this use File::Spec or File::Basename or something? | ||
| 156 | # Why so careful about dependencies? | ||
| 157 | sub clean_up_filename { | ||
| 158 | my $filename = shift; | ||
| 159 | $filename =~ s|^\./||; | ||
| 160 | $filename =~ s/^:([^:]+)$/$1/ if $Is_MacOS; | ||
| 161 | return $filename; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | =item manifind | ||
| 166 | |||
| 167 | my $found = manifind(); | ||
| 168 | |||
| 169 | returns a hash reference. The keys of the hash are the files found | ||
| 170 | below the current directory. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub manifind { | ||
| 175 | my $p = shift || {}; | ||
| 176 | my $found = {}; | ||
| 177 | |||
| 178 | my $wanted = sub { | ||
| 179 | my $name = clean_up_filename($File::Find::name); | ||
| 180 | warn "Debug: diskfile $name\n" if $Debug; | ||
| 181 | return if -d $_; | ||
| 182 | |||
| 183 | if( $Is_VMS_lc ) { | ||
| 184 | $name =~ s#(.*)\.$#\L$1#; | ||
| 185 | $name = uc($name) if $name =~ /^MANIFEST(\.SKIP)?$/i; | ||
| 186 | } | ||
| 187 | $found->{$name} = ""; | ||
| 188 | }; | ||
| 189 | |||
| 190 | # We have to use "$File::Find::dir/$_" in preprocess, because | ||
| 191 | # $File::Find::name is unavailable. | ||
| 192 | # Also, it's okay to use / here, because MANIFEST files use Unix-style | ||
| 193 | # paths. | ||
| 194 | find({wanted => $wanted}, | ||
| 195 | $Is_MacOS ? ":" : "."); | ||
| 196 | |||
| 197 | return $found; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | =item manicheck | ||
| 202 | |||
| 203 | my @missing_files = manicheck(); | ||
| 204 | |||
| 205 | checks if all the files within a C<MANIFEST> in the current directory | ||
| 206 | really do exist. If C<MANIFEST> and the tree below the current | ||
| 207 | directory are in sync it silently returns an empty list. | ||
| 208 | Otherwise it returns a list of files which are listed in the | ||
| 209 | C<MANIFEST> but missing from the directory, and by default also | ||
| 210 | outputs these names to STDERR. | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub manicheck { | ||
| 215 | return _check_files(); | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | =item filecheck | ||
| 220 | |||
| 221 | my @extra_files = filecheck(); | ||
| 222 | |||
| 223 | finds files below the current directory that are not mentioned in the | ||
| 224 | C<MANIFEST> file. An optional file C<MANIFEST.SKIP> will be | ||
| 225 | consulted. Any file matching a regular expression in such a file will | ||
| 226 | not be reported as missing in the C<MANIFEST> file. The list of any | ||
| 227 | extraneous files found is returned, and by default also reported to | ||
| 228 | STDERR. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub filecheck { | ||
| 233 | return _check_manifest(); | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | =item fullcheck | ||
| 238 | |||
| 239 | my($missing, $extra) = fullcheck(); | ||
| 240 | |||
| 241 | does both a manicheck() and a filecheck(), returning then as two array | ||
| 242 | refs. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub fullcheck { | ||
| 247 | return [_check_files()], [_check_manifest()]; | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | =item skipcheck | ||
| 252 | |||
| 253 | my @skipped = skipcheck(); | ||
| 254 | |||
| 255 | lists all the files that are skipped due to your C<MANIFEST.SKIP> | ||
| 256 | file. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub skipcheck { | ||
| 261 | my($p) = @_; | ||
| 262 | my $found = manifind(); | ||
| 263 | my $matches = maniskip(); | ||
| 264 | |||
| 265 | my @skipped = (); | ||
| 266 | foreach my $file (_sort keys %$found){ | ||
| 267 | if (&$matches($file)){ | ||
| 268 | warn "Skipping $file\n"; | ||
| 269 | push @skipped, $file; | ||
| 270 | next; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | return @skipped; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | sub _check_files { | ||
| 279 | my $p = shift; | ||
| 280 | my $dosnames=(defined(&Dos::UseLFN) && Dos::UseLFN()==0); | ||
| 281 | my $read = maniread() || {}; | ||
| 282 | my $found = manifind($p); | ||
| 283 | |||
| 284 | my(@missfile) = (); | ||
| 285 | foreach my $file (_sort keys %$read){ | ||
| 286 | warn "Debug: manicheck checking from $MANIFEST $file\n" if $Debug; | ||
| 287 | if ($dosnames){ | ||
| 288 | $file = lc $file; | ||
| 289 | $file =~ s=(\.(\w|-)+)=substr ($1,0,4)=ge; | ||
| 290 | $file =~ s=((\w|-)+)=substr ($1,0,8)=ge; | ||
| 291 | } | ||
| 292 | unless ( exists $found->{$file} ) { | ||
| 293 | warn "No such file: $file\n" unless $Quiet; | ||
| 294 | push @missfile, $file; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | return @missfile; | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | sub _check_manifest { | ||
| 303 | my($p) = @_; | ||
| 304 | my $read = maniread() || {}; | ||
| 305 | my $found = manifind($p); | ||
| 306 | my $skip = maniskip(); | ||
| 307 | |||
| 308 | my @missentry = (); | ||
| 309 | foreach my $file (_sort keys %$found){ | ||
| 310 | next if $skip->($file); | ||
| 311 | warn "Debug: manicheck checking from disk $file\n" if $Debug; | ||
| 312 | unless ( exists $read->{$file} ) { | ||
| 313 | my $canon = $Is_MacOS ? "\t" . _unmacify($file) : ''; | ||
| 314 | warn "Not in $MANIFEST: $file$canon\n" unless $Quiet; | ||
| 315 | push @missentry, $file; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | return @missentry; | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | =item maniread | ||
| 324 | |||
| 325 | my $manifest = maniread(); | ||
| 326 | my $manifest = maniread($manifest_file); | ||
| 327 | |||
| 328 | reads a named C<MANIFEST> file (defaults to C<MANIFEST> in the current | ||
| 329 | directory) and returns a HASH reference with files being the keys and | ||
| 330 | comments being the values of the HASH. Blank lines and lines which | ||
| 331 | start with C<#> in the C<MANIFEST> file are discarded. | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | sub maniread { | ||
| 336 | my ($mfile) = @_; | ||
| 337 | $mfile ||= $MANIFEST; | ||
| 338 | my $read = {}; | ||
| 339 | local *M; | ||
| 340 | unless (open M, "< $mfile"){ | ||
| 341 | warn "Problem opening $mfile: $!"; | ||
| 342 | return $read; | ||
| 343 | } | ||
| 344 | local $_; | ||
| 345 | while (<M>){ | ||
| 346 | chomp; | ||
| 347 | next if /^\s*#/; | ||
| 348 | |||
| 349 | my($file, $comment); | ||
| 350 | |||
| 351 | # filename may contain spaces if enclosed in '' | ||
| 352 | # (in which case, \\ and \' are escapes) | ||
| 353 | if (($file, $comment) = /^'(\\[\\']|.+)+'\s*(.*)/) { | ||
| 354 | $file =~ s/\\([\\'])/$1/g; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | ($file, $comment) = /^(\S+)\s*(.*)/; | ||
| 358 | } | ||
| 359 | next unless $file; | ||
| 360 | |||
| 361 | if ($Is_MacOS) { | ||
| 362 | $file = _macify($file); | ||
| 363 | $file =~ s/\\([0-3][0-7][0-7])/sprintf("%c", oct($1))/ge; | ||
| 364 | } | ||
| 365 | elsif ($Is_VMS_mode) { | ||
| 366 | require File::Basename; | ||
| 367 | my($base,$dir) = File::Basename::fileparse($file); | ||
| 368 | # Resolve illegal file specifications in the same way as tar | ||
| 369 | if ($Is_VMS_nodot) { | ||
| 370 | $dir =~ tr/./_/; | ||
| 371 | my(@pieces) = split(/\./,$base); | ||
| 372 | if (@pieces > 2) | ||
| 373 | { $base = shift(@pieces) . '.' . join('_',@pieces); } | ||
| 374 | my $okfile = "$dir$base"; | ||
| 375 | warn "Debug: Illegal name $file changed to $okfile\n" if $Debug; | ||
| 376 | $file = $okfile; | ||
| 377 | } | ||
| 378 | $file = lc($file) | ||
| 379 | unless $Is_VMS_lc &&($file =~ /^MANIFEST(\.SKIP)?$/); | ||
| 380 | } | ||
| 381 | |||
| 382 | $read->{$file} = $comment; | ||
| 383 | } | ||
| 384 | close M; | ||
| 385 | $read; | ||
| 386 | } | ||
| 387 | |||
| 388 | =item maniskip | ||
| 389 | |||
| 390 | my $skipchk = maniskip(); | ||
| 391 | my $skipchk = maniskip($manifest_skip_file); | ||
| 392 | |||
| 393 | if ($skipchk->($file)) { .. } | ||
| 394 | |||
| 395 | reads a named C<MANIFEST.SKIP> file (defaults to C<MANIFEST.SKIP> in | ||
| 396 | the current directory) and returns a CODE reference that tests whether | ||
| 397 | a given filename should be skipped. | ||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | # returns an anonymous sub that decides if an argument matches | ||
| 402 | sub maniskip { | ||
| 403 | my @skip ; | ||
| 404 | my $mfile = shift || "$MANIFEST.SKIP"; | ||
| 405 | _check_mskip_directives($mfile) if -f $mfile; | ||
| 406 | local(*M, $_); | ||
| 407 | open M, "< $mfile" or open M, "< $DEFAULT_MSKIP" or return sub {0}; | ||
| 408 | while (<M>){ | ||
| 409 | chomp; | ||
| 410 | s/\r//; | ||
| 411 | next if /^#/; | ||
| 412 | next if /^\s*$/; | ||
| 413 | s/^'//; | ||
| 414 | s/'$//; | ||
| 415 | push @skip, _macify($_); | ||
| 416 | } | ||
| 417 | close M; | ||
| 418 | return sub {0} unless (scalar @skip > 0); | ||
| 419 | |||
| 420 | my $opts = $Is_VMS_mode ? '(?i)' : ''; | ||
| 421 | |||
| 422 | # Make sure each entry is isolated in its own parentheses, in case | ||
| 423 | # any of them contain alternations | ||
| 424 | my $regex = join '|', map "(?:$_)", @skip; | ||
| 425 | |||
| 426 | return sub { $_[0] =~ qr{$opts$regex} }; | ||
| 427 | } | ||
| 428 | |||
| 429 | # checks for the special directives | ||
| 430 | # #!include_default | ||
| 431 | # #!include /path/to/some/manifest.skip | ||
| 432 | # in a custom MANIFEST.SKIP for, for including | ||
| 433 | # the content of, respectively, the default MANIFEST.SKIP | ||
| 434 | # and an external manifest.skip file | ||
| 435 | sub _check_mskip_directives { | ||
| 436 | my $mfile = shift; | ||
| 437 | local (*M, $_); | ||
| 438 | my @lines = (); | ||
| 439 | my $flag = 0; | ||
| 440 | unless (open M, "< $mfile") { | ||
| 441 | warn "Problem opening $mfile: $!"; | ||
| 442 | return; | ||
| 443 | } | ||
| 444 | while (<M>) { | ||
| 445 | if (/^#!include_default\s*$/) { | ||
| 446 | if (my @default = _include_mskip_file()) { | ||
| 447 | push @lines, @default; | ||
| 448 | warn "Debug: Including default MANIFEST.SKIP\n" if $Debug; | ||
| 449 | $flag++; | ||
| 450 | } | ||
| 451 | next; | ||
| 452 | } | ||
| 453 | if (/^#!include\s+(.*)\s*$/) { | ||
| 454 | my $external_file = $1; | ||
| 455 | if (my @external = _include_mskip_file($external_file)) { | ||
| 456 | push @lines, @external; | ||
| 457 | warn "Debug: Including external $external_file\n" if $Debug; | ||
| 458 | $flag++; | ||
| 459 | } | ||
| 460 | next; | ||
| 461 | } | ||
| 462 | push @lines, $_; | ||
| 463 | } | ||
| 464 | close M; | ||
| 465 | return unless $flag; | ||
| 466 | my $bakbase = $mfile; | ||
| 467 | $bakbase =~ s/\./_/g if $Is_VMS_nodot; # avoid double dots | ||
| 468 | rename $mfile, "$bakbase.bak"; | ||
| 469 | warn "Debug: Saving original $mfile as $bakbase.bak\n" if $Debug; | ||
| 470 | unless (open M, "> $mfile") { | ||
| 471 | warn "Problem opening $mfile: $!"; | ||
| 472 | return; | ||
| 473 | } | ||
| 474 | print M $_ for (@lines); | ||
| 475 | close M; | ||
| 476 | return; | ||
| 477 | } | ||
| 478 | |||
| 479 | # returns an array containing the lines of an external | ||
| 480 | # manifest.skip file, if given, or $DEFAULT_MSKIP | ||
| 481 | sub _include_mskip_file { | ||
| 482 | my $mskip = shift || $DEFAULT_MSKIP; | ||
| 483 | unless (-f $mskip) { | ||
| 484 | warn qq{Included file "$mskip" not found - skipping}; | ||
| 485 | return; | ||
| 486 | } | ||
| 487 | local (*M, $_); | ||
| 488 | unless (open M, "< $mskip") { | ||
| 489 | warn "Problem opening $mskip: $!"; | ||
| 490 | return; | ||
| 491 | } | ||
| 492 | my @lines = (); | ||
| 493 | push @lines, "\n#!start included $mskip\n"; | ||
| 494 | push @lines, $_ while <M>; | ||
| 495 | close M; | ||
| 496 | push @lines, "#!end included $mskip\n\n"; | ||
| 497 | return @lines; | ||
| 498 | } | ||
| 499 | |||
| 500 | =item manicopy | ||
| 501 | |||
| 502 | manicopy(\%src, $dest_dir); | ||
| 503 | manicopy(\%src, $dest_dir, $how); | ||
| 504 | |||
| 505 | Copies the files that are the keys in %src to the $dest_dir. %src is | ||
| 506 | typically returned by the maniread() function. | ||
| 507 | |||
| 508 | manicopy( maniread(), $dest_dir ); | ||
| 509 | |||
| 510 | This function is useful for producing a directory tree identical to the | ||
| 511 | intended distribution tree. | ||
| 512 | |||
| 513 | $how can be used to specify a different methods of "copying". Valid | ||
| 514 | values are C<cp>, which actually copies the files, C<ln> which creates | ||
| 515 | hard links, and C<best> which mostly links the files but copies any | ||
| 516 | symbolic link to make a tree without any symbolic link. C<cp> is the | ||
| 517 | default. | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub manicopy { | ||
| 522 | my($read,$target,$how)=@_; | ||
| 523 | croak "manicopy() called without target argument" unless defined $target; | ||
| 524 | $how ||= 'cp'; | ||
| 525 | require File::Path; | ||
| 526 | require File::Basename; | ||
| 527 | |||
| 528 | $target = VMS::Filespec::unixify($target) if $Is_VMS_mode; | ||
| 529 | File::Path::mkpath([ $target ],! $Quiet,$Is_VMS ? undef : 0755); | ||
| 530 | foreach my $file (keys %$read){ | ||
| 531 | if ($Is_MacOS) { | ||
| 532 | if ($file =~ m!:!) { | ||
| 533 | my $dir = _maccat($target, $file); | ||
| 534 | $dir =~ s/[^:]+$//; | ||
| 535 | File::Path::mkpath($dir,1,0755); | ||
| 536 | } | ||
| 537 | cp_if_diff($file, _maccat($target, $file), $how); | ||
| 538 | } else { | ||
| 539 | $file = VMS::Filespec::unixify($file) if $Is_VMS_mode; | ||
| 540 | if ($file =~ m!/!) { # Ilya, that hurts, I fear, or maybe not? | ||
| 541 | my $dir = File::Basename::dirname($file); | ||
| 542 | $dir = VMS::Filespec::unixify($dir) if $Is_VMS_mode; | ||
| 543 | File::Path::mkpath(["$target/$dir"],! $Quiet,$Is_VMS ? undef : 0755); | ||
| 544 | } | ||
| 545 | cp_if_diff($file, "$target/$file", $how); | ||
| 546 | } | ||
| 547 | } | ||
| 548 | } | ||
| 549 | |||
| 550 | sub cp_if_diff { | ||
| 551 | my($from, $to, $how)=@_; | ||
| 552 | if (! -f $from) { | ||
| 553 | carp "$from not found"; | ||
| 554 | return; | ||
| 555 | } | ||
| 556 | my($diff) = 0; | ||
| 557 | local(*F,*T); | ||
| 558 | open(F,"< $from\0") or die "Can't read $from: $!\n"; | ||
| 559 | if (open(T,"< $to\0")) { | ||
| 560 | local $_; | ||
| 561 | while (<F>) { $diff++,last if $_ ne <T>; } | ||
| 562 | $diff++ unless eof(T); | ||
| 563 | close T; | ||
| 564 | } | ||
| 565 | else { $diff++; } | ||
| 566 | close F; | ||
| 567 | if ($diff) { | ||
| 568 | if (-e $to) { | ||
| 569 | unlink($to) or confess "unlink $to: $!"; | ||
| 570 | } | ||
| 571 | STRICT_SWITCH: { | ||
| 572 | best($from,$to), last STRICT_SWITCH if $how eq 'best'; | ||
| 573 | cp($from,$to), last STRICT_SWITCH if $how eq 'cp'; | ||
| 574 | ln($from,$to), last STRICT_SWITCH if $how eq 'ln'; | ||
| 575 | croak("VIM::Packager::Manifest::cp_if_diff " . | ||
| 576 | "called with illegal how argument [$how]. " . | ||
| 577 | "Legal values are 'best', 'cp', and 'ln'."); | ||
| 578 | } | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | sub cp { | ||
| 583 | my ($srcFile, $dstFile) = @_; | ||
| 584 | my ($access,$mod) = (stat $srcFile)[8,9]; | ||
| 585 | |||
| 586 | copy($srcFile,$dstFile); | ||
| 587 | utime $access, $mod + ($Is_VMS ? 1 : 0), $dstFile; | ||
| 588 | _manicopy_chmod($srcFile, $dstFile); | ||
| 589 | } | ||
| 590 | |||
| 591 | |||
| 592 | sub ln { | ||
| 593 | my ($srcFile, $dstFile) = @_; | ||
| 594 | # Fix-me - VMS can support links. | ||
| 595 | return &cp if $Is_VMS or ($^O eq 'MSWin32' and Win32::IsWin95()); | ||
| 596 | link($srcFile, $dstFile); | ||
| 597 | |||
| 598 | unless( _manicopy_chmod($srcFile, $dstFile) ) { | ||
| 599 | unlink $dstFile; | ||
| 600 | return; | ||
| 601 | } | ||
| 602 | 1; | ||
| 603 | } | ||
| 604 | |||
| 605 | # 1) Strip off all group and world permissions. | ||
| 606 | # 2) Let everyone read it. | ||
| 607 | # 3) If the owner can execute it, everyone can. | ||
| 608 | sub _manicopy_chmod { | ||
| 609 | my($srcFile, $dstFile) = @_; | ||
| 610 | |||
| 611 | my $perm = 0444 | (stat $srcFile)[2] & 0700; | ||
| 612 | chmod( $perm | ( $perm & 0100 ? 0111 : 0 ), $dstFile ); | ||
| 613 | } | ||
| 614 | |||
| 615 | # Files that are often modified in the distdir. Don't hard link them. | ||
| 616 | my @Exceptions = qw(MANIFEST META.yml SIGNATURE); | ||
| 617 | sub best { | ||
| 618 | my ($srcFile, $dstFile) = @_; | ||
| 619 | |||
| 620 | my $is_exception = grep $srcFile =~ /$_/, @Exceptions; | ||
| 621 | if ($is_exception or !$Config{d_link} or -l $srcFile) { | ||
| 622 | cp($srcFile, $dstFile); | ||
| 623 | } else { | ||
| 624 | ln($srcFile, $dstFile) or cp($srcFile, $dstFile); | ||
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | sub _macify { | ||
| 629 | my($file) = @_; | ||
| 630 | |||
| 631 | return $file unless $Is_MacOS; | ||
| 632 | |||
| 633 | $file =~ s|^\./||; | ||
| 634 | if ($file =~ m|/|) { | ||
| 635 | $file =~ s|/+|:|g; | ||
| 636 | $file = ":$file"; | ||
| 637 | } | ||
| 638 | |||
| 639 | $file; | ||
| 640 | } | ||
| 641 | |||
| 642 | sub _maccat { | ||
| 643 | my($f1, $f2) = @_; | ||
| 644 | |||
| 645 | return "$f1/$f2" unless $Is_MacOS; | ||
| 646 | |||
| 647 | $f1 .= ":$f2"; | ||
| 648 | $f1 =~ s/([^:]:):/$1/g; | ||
| 649 | return $f1; | ||
| 650 | } | ||
| 651 | |||
| 652 | sub _unmacify { | ||
| 653 | my($file) = @_; | ||
| 654 | |||
| 655 | return $file unless $Is_MacOS; | ||
| 656 | |||
| 657 | $file =~ s|^:||; | ||
| 658 | $file =~ s|([/ \n])|sprintf("\\%03o", unpack("c", $1))|ge; | ||
| 659 | $file =~ y|:|/|; | ||
| 660 | |||
| 661 | $file; | ||
| 662 | } | ||
| 663 | |||
| 664 | |||
| 665 | =item maniadd | ||
| 666 | |||
| 667 | maniadd({ $file => $comment, ...}); | ||
| 668 | |||
| 669 | Adds an entry to an existing F<MANIFEST> unless its already there. | ||
| 670 | |||
| 671 | $file will be normalized (ie. Unixified). B<UNIMPLEMENTED> | ||
| 672 | |||
| 673 | =cut | ||
| 674 | |||
| 675 | sub maniadd { | ||
| 676 | my($additions) = shift; | ||
| 677 | |||
| 678 | _normalize($additions); | ||
| 679 | _fix_manifest($MANIFEST); | ||
| 680 | |||
| 681 | my $manifest = maniread(); | ||
| 682 | my @needed = grep { !exists $manifest->{$_} } keys %$additions; | ||
| 683 | return 1 unless @needed; | ||
| 684 | |||
| 685 | open(MANIFEST, ">>$MANIFEST") or | ||
| 686 | die "maniadd() could not open $MANIFEST: $!"; | ||
| 687 | |||
| 688 | foreach my $file (_sort @needed) { | ||
| 689 | my $comment = $additions->{$file} || ''; | ||
| 690 | if ($file =~ /\s/) { | ||
| 691 | $file =~ s/([\\'])/\\$1/g; | ||
| 692 | $file = "'$file'"; | ||
| 693 | } | ||
| 694 | printf MANIFEST "%-40s %s\n", $file, $comment; | ||
| 695 | } | ||
| 696 | close MANIFEST or die "Error closing $MANIFEST: $!"; | ||
| 697 | |||
| 698 | return 1; | ||
| 699 | } | ||
| 700 | |||
| 701 | |||
| 702 | # Sometimes MANIFESTs are missing a trailing newline. Fix this. | ||
| 703 | sub _fix_manifest { | ||
| 704 | my $manifest_file = shift; | ||
| 705 | |||
| 706 | open MANIFEST, $MANIFEST or die "Could not open $MANIFEST: $!"; | ||
| 707 | |||
| 708 | # Yes, we should be using seek(), but I'd like to avoid loading POSIX | ||
| 709 | # to get SEEK_* | ||
| 710 | my @manifest = <MANIFEST>; | ||
| 711 | close MANIFEST; | ||
| 712 | |||
| 713 | unless( $manifest[-1] =~ /\n\z/ ) { | ||
| 714 | open MANIFEST, ">>$MANIFEST" or die "Could not open $MANIFEST: $!"; | ||
| 715 | print MANIFEST "\n"; | ||
| 716 | close MANIFEST; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | |||
| 720 | |||
| 721 | # UNIMPLEMENTED | ||
| 722 | sub _normalize { | ||
| 723 | return; | ||
| 724 | } | ||
| 725 | |||
| 726 | |||
| 727 | =back | ||
| 728 | |||
| 729 | =head2 MANIFEST | ||
| 730 | |||
| 731 | A list of files in the distribution, one file per line. The MANIFEST | ||
| 732 | always uses Unix filepath conventions even if you're not on Unix. This | ||
| 733 | means F<foo/bar> style not F<foo\bar>. | ||
| 734 | |||
| 735 | Anything between white space and an end of line within a C<MANIFEST> | ||
| 736 | file is considered to be a comment. Any line beginning with # is also | ||
| 737 | a comment. Beginning with VIM::Packager::Manifest 1.52, a filename may | ||
| 738 | contain whitespace characters if it is enclosed in single quotes; single | ||
| 739 | quotes or backslashes in that filename must be backslash-escaped. | ||
| 740 | |||
| 741 | # this a comment | ||
| 742 | some/file | ||
| 743 | some/other/file comment about some/file | ||
| 744 | 'some/third file' comment | ||
| 745 | |||
| 746 | |||
| 747 | =head2 MANIFEST.SKIP | ||
| 748 | |||
| 749 | The file MANIFEST.SKIP may contain regular expressions of files that | ||
| 750 | should be ignored by mkmanifest() and filecheck(). The regular | ||
| 751 | expressions should appear one on each line. Blank lines and lines | ||
| 752 | which start with C<#> are skipped. Use C<\#> if you need a regular | ||
| 753 | expression to start with a C<#>. | ||
| 754 | |||
| 755 | For example: | ||
| 756 | |||
| 757 | # Version control files and dirs. | ||
| 758 | \bRCS\b | ||
| 759 | \bCVS\b | ||
| 760 | ,v$ | ||
| 761 | \B\.svn\b | ||
| 762 | |||
| 763 | # Makemaker generated files and dirs. | ||
| 764 | ^MANIFEST\. | ||
| 765 | ^Makefile$ | ||
| 766 | ^blib/ | ||
| 767 | ^MakeMaker-\d | ||
| 768 | |||
| 769 | # Temp, old and emacs backup files. | ||
| 770 | ~$ | ||
| 771 | \.old$ | ||
| 772 | ^#.*#$ | ||
| 773 | ^\.# | ||
| 774 | |||
| 775 | If no MANIFEST.SKIP file is found, a default set of skips will be | ||
| 776 | used, similar to the example above. If you want nothing skipped, | ||
| 777 | simply make an empty MANIFEST.SKIP file. | ||
| 778 | |||
| 779 | In one's own MANIFEST.SKIP file, certain directives | ||
| 780 | can be used to include the contents of other MANIFEST.SKIP | ||
| 781 | files. At present two such directives are recognized. | ||
| 782 | |||
| 783 | =over 4 | ||
| 784 | |||
| 785 | =item #!include_default | ||
| 786 | |||
| 787 | This inserts the contents of the default MANIFEST.SKIP file | ||
| 788 | |||
| 789 | =item #!include /Path/to/another/manifest.skip | ||
| 790 | |||
| 791 | This inserts the contents of the specified external file | ||
| 792 | |||
| 793 | =back | ||
| 794 | |||
| 795 | The included contents will be inserted into the MANIFEST.SKIP | ||
| 796 | file in between I<#!start included /path/to/manifest.skip> | ||
| 797 | and I<#!end included /path/to/manifest.skip> markers. | ||
| 798 | The original MANIFEST.SKIP is saved as MANIFEST.SKIP.bak. | ||
| 799 | |||
| 800 | =head2 EXPORT_OK | ||
| 801 | |||
| 802 | C<&mkmanifest>, C<&manicheck>, C<&filecheck>, C<&fullcheck>, | ||
| 803 | C<&maniread>, and C<&manicopy> are exportable. | ||
| 804 | |||
| 805 | =head2 GLOBAL VARIABLES | ||
| 806 | |||
| 807 | C<$VIM::Packager::Manifest::MANIFEST> defaults to C<MANIFEST>. Changing it | ||
| 808 | results in both a different C<MANIFEST> and a different | ||
| 809 | C<MANIFEST.SKIP> file. This is useful if you want to maintain | ||
| 810 | different distributions for different audiences (say a user version | ||
| 811 | and a developer version including RCS). | ||
| 812 | |||
| 813 | =head1 DIAGNOSTICS | ||
| 814 | |||
| 815 | All diagnostic output is sent to C<STDERR>. | ||
| 816 | |||
| 817 | =over 4 | ||
| 818 | |||
| 819 | =item C<Not in MANIFEST:> I<file> | ||
| 820 | |||
| 821 | is reported if a file is found which is not in C<MANIFEST>. | ||
| 822 | |||
| 823 | =item C<Skipping> I<file> | ||
| 824 | |||
| 825 | is reported if a file is skipped due to an entry in C<MANIFEST.SKIP>. | ||
| 826 | |||
| 827 | =item C<No such file:> I<file> | ||
| 828 | |||
| 829 | is reported if a file mentioned in a C<MANIFEST> file does not | ||
| 830 | exist. | ||
| 831 | |||
| 832 | =item C<MANIFEST:> I<$!> | ||
| 833 | |||
| 834 | is reported if C<MANIFEST> could not be opened. | ||
| 835 | |||
| 836 | =item C<Added to MANIFEST:> I<file> | ||
| 837 | |||
| 838 | is reported by mkmanifest() if $Verbose is set and a file is added | ||
| 839 | to MANIFEST. $Verbose is set to 1 by default. | ||
| 840 | |||
| 841 | =back | ||
| 842 | |||
| 843 | =head1 ENVIRONMENT | ||
| 844 | |||
| 845 | =over 4 | ||
| 846 | |||
| 847 | =back | ||
| 848 | |||
| 849 | =head1 AUTHOR | ||
| 850 | |||
| 851 | Andreas Koenig C<andreas.koenig@anima.de> | ||
| 852 | Michael G Schwern C<schwern@pobox.com> | ||
| 853 | Randy Kobes C<r.kobes@uwinnipeg.ca>. | ||
| 854 | |||
| 855 | =cut | ||
| 856 | |||
| 857 | 1; |