11 getopts( 'r:p:e:vud', $Opts );
15 my $ExcludeRe = $Opts->{e} ? qr/$Opts->{e}/ : undef;
16 my $Debug = $Opts->{v} || 0;
17 my $RunDiff = $Opts->{d} || 0;
18 my $PkgDir = $Opts->{p} || cwd();
19 my $MasterRepo = $Opts->{r} or die "Need repository!\n". usage();
21 ### strip trailing slashes;
22 $MasterRepo =~ s|/$||;
24 my $CPV = $Debug ? '-v' : '';
25 my $TestBin = 'ptardiff';
26 my $PkgDirRe = quotemeta( $PkgDir .'/' );
27 my $Repo = $MasterRepo . '-' . basename( $PkgDir ) . '.' . $$;
30 chdir $PkgDir or die "Could not chdir to $PkgDir: $!";
32 ### set up the repo dir from the master repo
33 { print "Setting up working repo under '$Repo'..." if $Verbose;
35 system( "mkdir -p $Repo" )
36 and die "Could not create working repo '$Repo': $?";
39 system( "cp -Rf $MasterRepo/* $Repo" )
40 and die "Copying master repo to $Repo failed: $?";
42 print "done\n" if $Verbose;
45 ### copy over all files under lib/
46 { print "Copying libdir..." if $Verbose;
47 die "No lib/ directory found\n" unless -d 'lib';
48 system( "cp -fR $CPV lib $Repo" ) and die "Copy of lib/ failed: $?";
49 print "done\n" if $Verbose;
52 ### find the directory to put the t/ and bin/ files under
53 my $RelTopDir; # topdir from the repo root
54 my $TopDir; # full path to the top dir
55 my $ModName; # name of the module
56 my @ModFiles; # the .PMs in this package
57 { print "Creating top level dir..." if $Verbose;
59 ### make sure we get the shortest file, so we dont accidentally get
61 @ModFiles = sort { length($a) <=> length($b) }
63 grep { $ExcludeRe ? $_ !~ $ExcludeRe : 1 }
65 `find $PkgDir/lib -type f`
66 or die "No TopDir detected\n";
68 $RelTopDir = $ModFiles[0];
69 $RelTopDir =~ s/^$PkgDirRe//;
70 $RelTopDir =~ s/\.p(m|od)$//;
71 $TopDir = "$Repo/$RelTopDir";
73 ### create the dir if it's not there yet
74 unless( -d $TopDir ) {
75 system( "mkdir $TopDir" ) and die "Creating dir $TopDir failed: $?";
78 ### the module name, like Foo::Bar
79 ### slice syntax not elegant, but we need to remove the
80 ### leading 'lib/' entry
81 ### stupid temp vars! stupid perl! it doesn't do @{..}[0..-1] :(
82 { my @list = @{[split '/', $RelTopDir]};
83 $ModName = join '::', @list[1 .. $#list];
86 ### the .pm files in this package
87 @ModFiles = map { s|^$PkgDirRe||; $_ } @ModFiles
88 or die "Could not detect modfiles\n";
90 print "done\n" if $Verbose;
93 my $TopDirRe = quotemeta( $TopDir . '/' );
95 ### copy over t/ and bin/ directories to the $TopDir
97 { print "Copying t/* files to $TopDir..." if $Verbose;
100 ? system( "cp -fR $CPV t $TopDir" ) && die "Copy of t/ failed: $?"
101 : warn "No t/ directory found\n";
103 @TestFiles = map { chomp; s|^$TopDirRe||; $_ }
104 ### should we get rid of this file?
105 grep { $ExcludeRe && $_ =~ $ExcludeRe
106 ? do { warn "Removing $_\n";
107 system("rm $_") and die "rm '$_' failed: $?";
111 } `find $TopDir/t -type f`
112 or die "Could not detect testfiles\n";
114 print "done\n" if $Verbose;
120 print "No bin/ directory found\n" if $Verbose;
123 print "Copying bin/* files to $TopDir..." if $Verbose;
125 system("cp -fR $CPV bin/* $TopDir/bin/") && die "Copy of bin/ failed: $?";
127 @BinFiles = map { chomp; s|^$TopDirRe||; $_ }
128 ### should we get rid of this file?
129 grep { $ExcludeRe && $_ =~ $ExcludeRe
130 ? do { warn "Removing $_\n";
131 system("rm $_") and die "rm '$_' failed: $?";
135 } `find $TopDir/bin -type f`
136 or die "Could not detect binfiles\n";
138 print "done\n" if $Verbose;
141 ### add files where they are required
143 { for my $bin ( map { basename( $_ ) } @BinFiles ) {
144 print "Registering $bin with system files...\n";
146 ### fix installperl, so these files get installed by other utils
147 ### ./installperl: return if $name =~
148 ### /^(?:cpan|instmodsh|prove|corelist|ptar|ptardiff|config_data)\z/;
149 { my $file = 'installperl';
151 ### not there already?
152 unless( `grep $TestBin $Repo/$file| grep $bin` ) {
153 print " Adding $bin to $file..." if $Verbose;
155 ### double \\| required --> once for in this script, once
157 system("$^X -pi -e 's/($TestBin\\|)/$bin|\$1/' $Repo/$file")
158 and die "Could not add $bin to $file: $?";
159 print "done\n" if $Verbose;
161 print " $bin already mentioned in $file\n" if $Verbose;
165 ### fix utils.lst, so the new tools are mentioned
166 { my $file = 'utils.lst';
168 ### not there already?
169 unless( `grep $bin $Repo/$file` ) {
170 print " Adding $bin to $file..." if $Verbose;
172 ### double \\| required --> once for in this script, once
174 system("$^X -pi -e 's!($TestBin)!\$1\nutils/$bin!' $Repo/$file")
175 and die "Could not add $bin to $file: $?";
176 print "done\n" if $Verbose;
178 print " $bin already mentioned in $file\n" if $Verbose;
182 ### make a $bin.PL file and fix it up
183 { my $src = "utils/${TestBin}.PL";
184 my $file = "utils/${bin}.PL";
186 ### not there already?
187 unless( -e "$Repo/$file" ) {
188 print " Creating $file..." if $Verbose;
190 ### important part of the template looks like this
191 ### (we'll need to change it):
192 # my $script = File::Spec->catfile(
193 # File::Spec->catdir(
194 # File::Spec->updir, qw[lib Archive Tar bin]
195 # ), "module-load.pl");
197 ### copy another template file
198 system( "cp -f $Repo/$src $Repo/$file" )
199 and die "Could not create $file from $src: $?";
201 ### change the 'updir' path
202 ### make sure to escape the \[ character classes
203 my $updir = join ' ', (split('/', $RelTopDir), 'bin');
204 system( "$^X -pi -e'".
205 's/^(.*?File::Spec->updir, qw\[).+?(\].*)$/'.
206 "\$1 $updir \$2/' $Repo/$file"
207 ) and die "Could not fix updir for $bin in $file: $?";
210 ### change the name of the file from $TestBin to $bin
211 system( "$^X -pi -e's/$TestBin/$bin/' $Repo/$file" )
212 and die "Could not update $file with '$bin' as name: $?";
214 print "done\n" if $Verbose;
217 print " $file already exists\n" if $Verbose;
220 ### we've may just have created a new file, it will have to
221 ### go into the manifest
222 push @NewFiles, $file;
225 ### add an entry to utils/Makefile for $bin
226 { my $file = "utils/Makefile";
228 ### not there already?
229 unless( `grep $bin $Repo/$file` ) {
230 print " Adding $bin entries to $file..." if $Verbose;
232 ### $bin appears on 4 lines in this file, so replace all 4
234 system( "$^X -pi -e'/^pl\\s+=/ && s/(${TestBin}.PL)/".
235 "\$1 ${bin}.PL/' $Repo/$file"
236 ) and die "Could not add $bin to the pl = entry: $?";
238 ### next, plextract =
239 system( "$^X -pi -e'/^plextract\\s+=/ " .
240 "&& s/(${TestBin})/\$1 $bin/' $Repo/$file"
241 ) and die "Could not add $bin to the plextract = entry: $?";
243 ### third, plextractexe =
244 system( "$^X -pi -e'/^plextractexe\\s+=/ " .
245 "&& s!(\./${TestBin})!\$1 ./$bin!' $Repo/$file"
246 ) and die "Could not add $bin to the plextractexe = entry: $?";
248 ### last, the make directive $bin:
249 system( "$^X -pi -e'/^(${TestBin}:.+)/; \$x=\$1 or next;" .
250 "\$x =~ s/$TestBin/$bin/g;" . '$_.=$/.$x.$/;' .
252 ) and die "Could not add $bin as a make directive: $?";
254 print "done\n" if $Verbose;
256 print " $bin already added to $file\n" if $Verbose;
260 ### add entries to win32/Makefile and win32/makefile.mk
261 ### they contain the following lines:
262 # ./win32/makefile.mk: ..\utils\ptardiff \
263 # ./win32/makefile.mk: xsubpp instmodsh prove ptar ptardiff
264 for my $file ( qw[win32/Makefile win32/makefile.mk] ) {
265 unless ( `grep $bin $Repo/$file` ) {
266 print " Adding $bin entries to $file..." if $Verbose;
268 system( "$^X -pi -e'/^(.+?utils.${TestBin}.+)/;".
270 "\$x =~ s/$TestBin/$bin/g;" . '$_.=$x.$/;' .
272 ) and die "Could not add $bin to UTILS section in $file: $?\n";
274 system( "$^X -pi -e's/( $TestBin)/\$1 $bin/' $Repo/$file" )
275 and die "Could not add $bin to $file: $?\n";
277 print "done\n" if $Verbose;
279 print " $bin already added to $file\n" if $Verbose;
283 ### we need some entries in a vms specific file as well..
284 ### except, i dont understand how it works or what it does, and it
285 ### looks all a bit odd... so lets just print a warning...
286 ### the entries look something like this:
287 # ./vms/descrip_mms.template:utils4 = [.utils]enc2xs.com
288 # [.utils]piconv.com [.utils]cpan.com [.utils]prove.com
289 # [.utils]ptar.com [.utils]ptardiff.com [.utils]shasum.com
290 # ./vms/descrip_mms.template:[.utils]ptardiff.com : [.utils]ptardiff.PL
291 # $(ARCHDIR)Config.pm
292 { my $file = 'vms/descrip_mms.template';
294 unless( `grep $bin $Repo/$file` ) {
296 print " WARNING! You should add entries like the following\n"
297 . " to $file (Using $TestBin as an example)\n"
298 . " Unfortunately I dont understand what these entries\n"
299 . " do, so I wont change them automatically:\n\n";
301 print `grep -nC1 $TestBin $Repo/$file`;
305 print " $bin already added to $file\n" if $Verbose;
311 ### binary files must be encoded!
312 ### XXX use the new 'uupacktool.pl'
313 { my $pack = "$Repo/uupacktool.pl";
315 ### pack.pl encodes binary files for us
316 -e $pack or die "Need $pack to encode binary files!";
318 ### chdir, so uupacktool writes relative files properly
319 ### into it's header...
321 chdir($Repo) or die "Could not chdir to '$Repo': $!";
323 for my $aref ( \@ModFiles, \@TestFiles, \@BinFiles ) {
324 for my $file ( @$aref ) {
325 my $full = -e $file ? $file :
326 -e "$RelTopDir/$file" ? "$RelTopDir/$file" :
327 die "Can not find $file in $Repo or $TopDir\n";
329 if( -f $full && -s _ && -B _ ) {
330 print "Binary file $file needs encoding\n" if $Verbose;
332 my $out = $full . '.packed';
334 ### does the file exist already?
335 ### and doesn't have +w
336 if( -e $out && not -w _ ) {
337 system("chmod +w $out")
338 and die "Could not set chmod +w to '$out': $!";
341 ### -D to remove the original
342 system("$^X $pack -D -p $full $out")
343 and die "Could not encode $full to $out";
351 chdir($curdir) or die "Could not chdir back to '$curdir': $!";
354 ### update the manifest
355 { my $file = $Repo . '/MANIFEST';
357 { open my $fh, "<$file" or die "Could not open $file: $!";
362 ### fill it with files from our package
365 $pkg_files{$_} = "$_\t$ModName\n";
369 $pkg_files{"$RelTopDir/$_"} = "$RelTopDir/$_\t$ModName tests\n"
373 $pkg_files{"$RelTopDir/$_"} = "$RelTopDir/$_\tthe ".
374 basename($_) ." utility\n";
378 $pkg_files{$_} = "$_\tthe ".
379 do { m/(.+?)\.PL$/; basename($1) } .
383 ### remove all the files that are already in the manifest;
384 delete $pkg_files{ [split]->[0] } for @manifest;
386 print "Adding the following entries to the MANIFEST:\n" if $Verbose;
387 print "\t$_" for sort values %pkg_files;
390 push @manifest, values %pkg_files;
393 open my $fh, ">$file" or die "Could not open $file for writing: $!";
394 #print $fh sort { lc $a cmp lc $b } @manifest;
395 ### XXX stolen from pod/buildtoc:sub do_manifest
398 sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
399 map { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] }
406 ### would you like us to show you a diff?
408 my $diff = $Repo; $diff =~ s/$$/patch/;
411 my $master = basename( $MasterRepo );
412 my $repo = basename( $Repo );
413 my $chdir = dirname( $MasterRepo );
415 ### the .patch file is added by an rsync from the APC
416 ### but isn't actually in the p4 repo, so exclude it
417 my $cmd = "cd $chdir; diff -ruN --exclude=.patch $master $repo > $diff";
419 print "Running: '$cmd'\n";
421 print "Generating diff..." if $Verbose;
424 #and die "Could not write diff to '$diff': $?";
425 die "Could not write diff to '$diff'" unless -e $diff && -s _;
427 print "done\n" if $Verbose;
428 print "\nDiff can be applied with patch -p1 in $MasterRepo\n\n";
429 print " Diff written to: $diff\n\n" if $Verbose;
433 my $me = basename($0);
436 Usage: $me -r PERL_REPO_DIR [-p PACKAGE_DIR] [-v] [-d] [-e REGEX]
439 -r Path to perl-core repository
441 -e Perl regex matching files that shouldn't be included
442 -d Create a diff as patch file
443 -p Path to the package to add. Defaults to cwd()