-
Notifications
You must be signed in to change notification settings - Fork 38
/
Build.pm
1165 lines (968 loc) · 32.8 KB
/
Build.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2004-2012 C-Dot Consultants - All rights reserved
# Copyright (C) 2008-2012 Foswiki Contributors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
package Foswiki::Contrib::Build;
use Foswiki::Contrib::BuildContrib::BaseBuild;
use Error qw(:try);
use CGI qw(:any);
=begin TML
---++ Package Foswiki::Contrib::Build
This is a base class used for making build scripts for Foswiki packages.
---+++ Methods
=cut
use strict;
use Digest::MD5 ();
use File::Copy ();
use File::Spec ();
use FindBin ();
use File::Find ();
use File::Path ();
use File::Temp ();
use POSIX ();
use Data::Dumper ();
use warnings;
use Foswiki::Time;
use Foswiki::Contrib::BuildContrib;
our $basedir; # Calculated root i.e one above 'lib'
our $buildpldir; # Calculated location of build.pl
our $libpath; # $basedir/lib/
our $VERSION; # Version of BuildContrib
our $RELEASE; # Release of BuildContrib
my $UPLOADSITEPUB = 'http://foswiki.org/pub';
my $UPLOADSITESCRIPT = 'http://foswiki.org/bin';
my $UPLOADSITESUFFIX = '';
my $UPLOADSITEBUGS = 'http://foswiki.org/Tasks';
my $UPLOADSITEEXTENSIONSWEB = "Extensions";
my $DEFAULTCUSTOMERDB = "$ENV{HOME}/customerDB";
my $FOSWIKIAUTHORSFILE = 'core/AUTHORS';
my $targetProject; # Foswiki or TWiki
# use diagnostics;
# use Carp ();
# $SIG{__DIE__} = sub { Carp::confess $_[0] };
$ENV{'LC_ALL'} = 'C';
# Find a file relative to a directory passed (function call) or
# the basedir (method call)
sub findRelative {
my ( $this, $name ) = @_;
my $startdir = ref($this) ? $this->{basedir} : $this;
my @path = split( /[\/\\]+/, $startdir );
while ( scalar(@path) > 0 ) {
my $found = join( '/', @path ) . '/' . $name;
return $found if -e $found;
pop(@path);
}
#try legacy TWiki Contrib
$startdir =~ s/\/Foswiki\//\/TWiki\//g;
@path = split( /\/+/, $startdir );
#try a legacy TWiki contrib path.
while ( scalar(@path) > 0 ) {
my $found = join( '/', @path ) . '/' . $name;
return $found if -e $found;
pop(@path);
}
return undef;
}
BEGIN {
# Get the absolute dir we are executing in (where build.pl is)
$buildpldir = $FindBin::RealBin;
$buildpldir = File::Spec->rel2abs($buildpldir);
$VERSION = $Foswiki::Contrib::BuildContrib::VERSION;
$RELEASE = $Foswiki::Contrib::BuildContrib::RELEASE;
print "Building with BuildContrib $VERSION - $RELEASE \n";
# Let's see if we are sitting in a conventional checkout structure,
# and it has a defined LocalLib.cfg. If so then that's the basis of
# our libs.
my $expect_core = "$buildpldir/../../../../../core";
if ( -e "$expect_core/bin/LocalLib.cfg"
&& -e "$expect_core/lib/LocalSite.cfg" )
{
print "Using Foswiki libraries found on relative paths\n";
do "$expect_core/bin/setlib.cfg";
}
else {
# Otherwise we need FOSWIKI_LIBS
my $env = $ENV{'FOSWIKI_LIBS'};
die <<ARGH unless $env;
We don't seem to be building in a configured subversion checkout, and
FOSWIKI_LIBS is not defined. I cannot determine how to find the Foswiki
libraries required to support the build system.
BuildContrib must either be run within a full subversion checkout
that has both LocalLib.cfg and LocalSite.cfg, or the environment variable
FOSWIKI_LIBS must point to a configured Foswiki.
In either case, the BuildContrib extension must be installed.
ARGH
print "Using Foswiki libraries from $env\n";
# normally this will be a nop, as build.pl will have already
# added FOSWIKI_LIBS to @INC. But just in case some other
# route was used, add the missing libs.
my %known;
map { $known{$_} = 1 } split( /:/, @INC );
foreach my $pc ( reverse split( /:/, $env ) ) {
unless ( $known{$pc} ) {
unshift( @INC, $pc );
}
}
}
print 'Build libraries: ' . join( ' ', @INC ) . "\n";
# Make sure we have a LocalSite.cfg
my $haveLSC = 0;
foreach my $dir (@INC) {
if ( -e "$dir/LocalSite.cfg" ) {
$haveLSC = 1;
last;
}
}
warn 'Could not find LocalSite.cfg anywhere in @INC - build aborted'
unless $haveLSC;
# Find the project lib root
if ( -e "$buildpldir/../../../Foswiki" || -e "$buildpldir/../lib/Foswiki" )
{
$libpath = findRelative( $buildpldir, 'lib/Foswiki' );
$targetProject = 'Foswiki';
}
else {
print STDERR "Assuming this is a TWiki project\n";
$libpath = findRelative( $buildpldir, 'lib/TWiki' );
$targetProject = 'TWiki';
}
die 'Could not find lib/Foswiki or lib/TWiki' unless $libpath;
$libpath =~ s#/[^/]*$##;
$basedir = $libpath;
$basedir =~ s#/[^/]*$##;
unless ( grep( /$basedir\/lib/, @INC ) ) {
unshift( @INC, $basedir . '/lib' );
}
}
=begin TML
---++++ new($project)
| $project | Name of plugin, addon, contrib or skin |
| $rootModule | Optional, if defined gives the name of the root .pm module that carries the VERSION and dependencies. Defaults to $project |
Construct a new build object. Define the basic directory
paths to places in the build/release. Read the manifest topic
and build file and dependency lists. Parse command line to get
target and options.
=cut
sub new {
my ( $class, $project, $rootModule ) = @_;
my $this = bless(
{
# Constants with internet paths
BUGSURL => $UPLOADSITEBUGS,
project => $project,
target => 'test',
basedir => $basedir,
},
$class
);
my $n = 0;
my $done = 0;
while ( $n <= $#ARGV ) {
if ( $ARGV[$n] =~ /^-/o ) {
$this->{ $ARGV[$n] } = 1;
}
else {
$this->{target} = $ARGV[$n];
}
$n++;
}
if ( $this->{-v} ) {
print 'Building in ', $buildpldir, "\n";
print 'Root module is ', $rootModule, "\n" if $rootModule;
print 'Basedir is ', $basedir, "\n";
print 'Component dir is ', $libpath, "\n";
print 'Using path ' . join( ':', @INC ) . "\n";
}
# The following paths are all relative to the root of the
# installation
#SMELL: Hardcoded project classification
# where the sub-modules live
$this->{libdir} = $libpath;
if ( $this->{project} =~ /Plugin$/ ) {
$this->{libdir} .= "/$targetProject/Plugins";
}
elsif ( $this->{project} =~ /(Contrib|Skin|AddOn)$/ ) {
$this->{libdir} .= "/$targetProject/Contrib";
}
# the .pm module
$this->{ROOTMODULE} = $rootModule || $project;
$this->{pm} = $this->{libdir} . '/' . $this->{ROOTMODULE} . '.pm';
my $stubpath = $this->{pm};
$stubpath =~ s/.*[\\\/]($targetProject[\\\/].*)\.pm/$1/;
$stubpath =~ s/[\\\/]/::/g;
my $badVersion;
# Get $VERSION, $RELEASE and $SHORTDESCRIPTION
if ( -e $this->{pm} ) {
my $fh;
if ( open( $fh, "<", $this->{pm} ) ) {
local $/;
my $text = <$fh>;
close $fh;
my $VERSION;
my $RELEASE;
my $SHORTDESCRIPTION;
my ($version) = $text =~
m/^\s*(?:use\ version.*?;)?\s*(?:our)?\s*(\$VERSION\s*=.*?);/sm;
my ($release) = $text =~ m/^\s*(?:our)?\s*(\$RELEASE\s*=.*?);/sm;
my ($description) =
$text =~ m/^\s*(?:our)?\s*(\$SHORTDESCRIPTION\s*=.*?);/sm;
$badVersion = 1 if ( $version =~ m/\$Date|\$Rev/ );
substr( $version, 0, 0, 'use version 0.77; ' )
if ( $version =~ /version/ );
eval $version if ($version);
eval $release if ($release);
eval $description if ($description);
$this->{files}[0]->{name} = $this->{pm};
$this->{files}[0]->{name} =~ s/^$basedir\/(.*)/$1/;
$this->{VERSION} = $VERSION;
$this->{RELEASE} = $RELEASE;
$this->{SHORTDESCRIPTION} = $SHORTDESCRIPTION || '';
}
}
# where data files live
$this->{data_systemdir} =
'data/' . ( ( $targetProject eq 'TWiki' ) ? 'TWiki' : 'System' );
# the root of the name of data files
$this->{topic_root} = $this->{data_systemdir} . '/' . $this->{project};
##############################################################
# Read the manifest
my $manifest = findRelative( $buildpldir, 'MANIFEST' );
if ( !defined($manifest) ) {
#the core MANIFEST is in the lib dir, not the tools dir
$manifest = findRelative( $libpath, 'MANIFEST' );
}
( $this->{files}, $this->{other_modules}, $this->{options} ) =
Foswiki::Contrib::BuildContrib::BaseBuild::readManifest( $this->{basedir},
'', $manifest, sub { exit(1) } );
# Generate a table representing the manifest contents
# and a hash table representing the files
my $mantable = '';
my $rawman = '';
my $hashtable = '';
foreach my $file ( @{ $this->{files} } ) {
$rawman .= join( ',',
map { $file->{$_} || '' }
qw{quotedName permissions md5 description} )
. "\n";
$mantable .=
" | =="
. $file->{name} . '== | '
. $file->{description} . ' |' . "\n";
$hashtable .= "'$file->{name}'=>$file->{permissions},";
}
$this->{RAW_MANIFEST} = $rawman;
$this->{MANIFEST} = $mantable;
$this->{FILES} = $hashtable;
##############################################################
# Work out the dependencies
my $dependencies = findRelative( $buildpldir, 'DEPENDENCIES' );
if ( !defined($dependencies) ) {
#the core DEPENDENCIES is in the lib dir, not the tools dir
$dependencies = findRelative( $libpath, 'DEPENDENCIES' );
}
$this->_loadDependenciesFrom($dependencies);
# Pull in dependencies from other modules
if ( $this->{other_modules} ) {
foreach my $module ( @{ $this->{other_modules} } ) {
try {
my $depsfile =
findRelative( "$basedir/$module", 'DEPENDENCIES' );
die 'Failed to find DEPENDENCIES for ' . $module
unless $depsfile && -f $depsfile;
$this->_loadDependenciesFrom($depsfile);
}
catch Error::Simple with {
warn "WARNING: no dependencies in $basedir/$module " . shift;
};
}
}
my $deptable = '';
my $rawdeps = '';
my $a = ' align="left"';
foreach my $dep ( @{ $this->{dependencies} } ) {
$rawdeps .=
"$dep->{name},$dep->{version},$dep->{trigger},$dep->{type},$dep->{description}\n";
my $v = $dep->{version};
$v =~ s/&/&/go;
$v =~ s/>/>/go;
$v =~ s/</</go;
my $cells =
CGI::td( { align => 'left' }, $dep->{name} )
. CGI::td( { align => 'left' }, $v )
. CGI::td( { align => 'left' }, $dep->{description} );
$deptable .= CGI::Tr( {}, $cells );
}
$this->{RAW_DEPENDENCIES} = $rawdeps;
$this->{DEPENDENCIES} = 'None';
if ($deptable) {
my $cells =
CGI::th( {}, 'Name' )
. CGI::th( {}, 'Version' )
. CGI::th( {}, 'Description' );
$this->{DEPENDENCIES} = CGI::table(
{ border => 1, class => 'foswikiTable' },
CGI::Tr( {}, $cells ) . $deptable
);
}
# Get repo information. If not in repo, returns today's date.
# $this->{DATE} is set to the full timestamp
my $latestDate = $this->_get_repo_information();
if ($badVersion) {
my $proposed;
if ( $this->{SVNREV} ) {
use version 0.77;
my $rev = sprintf( "%06d", $this->{SVNREV} );
$proposed = version->parse("1.$rev")->normal()
if $this->{SVNREV};
}
print STDERR <<ERROR;
\$VERSION string containing \$Date or \$Rev detected.
Keyword-based \$VERSION strings are no longer supported.
Please update to a real Perl version string.
ERROR
print STDERR
"\n Suggested version: '$proposed' built from SVN Rev:$this->{SVNREV}\n"
if $proposed;
die "Build stopped: SVN Rev's are not supported\n\n";
}
# If there is no RELEASE defined in the extension .pm
# set the RELEASE = the date of last modification.
if ( !$this->{RELEASE} ) {
$this->{RELEASE} = $latestDate;
}
local $/ = undef;
my $stage;
foreach $stage ( 'PREINSTALL', 'POSTINSTALL', 'PREUNINSTALL',
'POSTUNINSTALL' )
{
$this->{$stage} = '# No ' . $stage . ' script';
my $file = findRelative( $buildpldir, $stage );
if ( $file && open( PF, '<', $file ) ) {
$this->{$stage} = "\n" . <PF>;
}
}
$this->{MODULE} = $this->{project};
local $/;
$this->{INSTALL_INSTRUCTIONS} = <DATA>;
# Item9416: Implements %$FOSWIKIAUTHORS%. Depends on $/ = undef
$FOSWIKIAUTHORSFILE = $this->findRelative($FOSWIKIAUTHORSFILE);
if ($FOSWIKIAUTHORSFILE) {
open my $authorsfile, '<', $FOSWIKIAUTHORSFILE
or die "Couldn't open $FOSWIKIAUTHORSFILE";
$this->{FOSWIKIAUTHORS} = <$authorsfile>;
close $authorsfile;
}
my $config = $this->_loadConfig();
my $rep = $config->{repositories}->{ $this->{project} };
if ($rep) {
$this->{UPLOADTARGETPUB} = $rep->{pub};
$this->{UPLOADTARGETSCRIPT} = $rep->{script};
$this->{UPLOADTARGETSUFFIX} = $rep->{suffix};
$this->{UPLOADTARGETWEB} = $rep->{web};
$this->{DOWNTARGETSCRIPT} = $rep->{downscript} || $rep->{script};
$this->{DOWNTARGETSUFFIX} = $rep->{downsuffix} || $rep->{suffix};
$this->{DOWNTARGETWEB} = $rep->{downweb} || $rep->{web};
}
else {
$this->{UPLOADTARGETPUB} = $UPLOADSITEPUB
unless defined $this->{UPLOADTARGETPUB};
$this->{UPLOADTARGETSCRIPT} = $UPLOADSITESCRIPT
unless defined $this->{UPLOADTARGETSCRIPT};
$this->{UPLOADTARGETSUFFIX} = $UPLOADSITESUFFIX
unless defined $this->{UPLOADTARGETSUFFIX};
$this->{UPLOADTARGETWEB} = $UPLOADSITEEXTENSIONSWEB
unless defined $this->{UPLOADTARGETWEB};
$this->{DOWNTARGETSCRIPT} = $UPLOADSITESCRIPT
unless defined $this->{DOWNTARGETSCRIPT};
$this->{DOWNTARGETSUFFIX} = $UPLOADSITESUFFIX
unless defined $this->{DOWNTARGETSUFFIX};
$this->{DOWNTARGETWEB} = $UPLOADSITEEXTENSIONSWEB
unless defined $this->{DOWNTARGETWEB};
}
return $this;
}
sub DESTROY {
my $self = shift;
File::Path::rmtree( $self->{tmpDir} )
if $self->{tmpDir} && -d $self->{tmpDir};
}
# Load the config memory (passwords, repository locations etc)
sub _loadConfig {
my $this = shift;
use vars qw($VAR1);
if ( !defined $this->{config} ) {
#TODO: this really should be abstracted
my $configLocation = $this
->{libdir}; #default to leave one in each contrib - used for windows atm
$configLocation = $ENV{HOME} if ( defined( $ENV{HOME} ) );
if ( -r "$configLocation/.buildcontrib" ) {
do "$configLocation/.buildcontrib";
$this->{config} = $VAR1;
print "Loaded config from $this->{config}->{file}\n";
}
else {
}
unless ( $this->{config} ) {
$this->{config} = {
file => "$configLocation/.buildcontrib",
passwords => {},
repositories => {},
};
}
}
return $this->{config};
}
# Save the config
sub saveConfig {
my $this = shift;
if ( open( F, '>', $this->{config}->{file} ) ) {
print F Data::Dumper->Dump( [ $this->{config} ] );
close(F);
print "Config saved in $this->{config}->{file}\n";
}
else {
warn "Could not write $this->{config}->{file}: $!";
}
}
sub _addDependency {
my $this = shift;
my %dep = @_;
my @existing = grep { $_->{name} eq $dep{name} } @{ $this->{dependencies} };
if ( scalar @existing ) {
# SMELL: this is a crude merge of conditions, and probably not
# correct in some cases, but it will have to do
my $a = $existing[0]->{version};
my $b = $dep{version};
$a =~ s/[<>=]//g;
$b =~ s/[<>=]//g;
if ( $a =~ /^[0-9.]+$/ && $b =~ /^[0-9.]+$/ ) {
if ( $a < $b ) {
$existing[0]->{version} = $dep{version};
}
return;
}
}
# New dependency
push( @{ $this->{dependencies} }, \%dep );
}
sub _loadDependenciesFrom {
my ( $this, $depsFile ) = @_;
my $condition = 1;
if ( -f $depsFile ) {
open( PF, '<', $depsFile ) || die 'Failed to open ' . $depsFile;
while ( my $line = <PF> ) {
if ( $line =~ /^\s*$/ || $line =~ /^\s*#/ ) {
}
elsif ( $line =~ /^ONLYIF\s*(\(.*\))\s*$/ ) {
$condition = $1;
}
elsif ( $line =~ m/^(\w+)\s+(\w*)\s*(.*)$/o ) {
$this->_addDependency(
name => $1,
type => $2,
version => '',
description => $3,
trigger => $condition
);
$condition = 1;
}
elsif ( $line =~ m/^([^,]+),([^,]*),\s*(\w*)\s*,\s*(.+)$/o ) {
$this->_addDependency(
name => $1,
version => $2,
type => $3,
description => $4,
trigger => $condition
);
$condition = 1;
}
else {
warn 'WARNING: LINE ' . $line . ' IN ' . $depsFile . ' IGNORED';
}
}
}
else {
warn 'WARNING: no '
. $depsFile
. '; dependencies will only be extracted from code';
}
close(PF);
}
# Search the current working directory and its parents
# for a directory called git
# Also checks if this directory contains a svn subdir
# which indicates the use of git-svn
sub _findPathToDotGitDir {
my $this = shift;
require File::Spec;
require Cwd;
my @dirlist = File::Spec->splitdir( Cwd::getcwd() );
do {
my $gitdir = File::Spec->catdir( @dirlist, ".git", "svn" );
return wantarray ? ( $gitdir, 1 ) : $gitdir if -d $gitdir;
$gitdir = File::Spec->catdir( @dirlist, ".git" );
return $gitdir if -d $gitdir;
} while ( pop @dirlist );
return;
}
# SMELL: Would be good to change this to use SVN::Client, but Sven warns us
# that SVN::Client doesn't work in most places :-(. Maybe some day.
sub _get_repo_information {
my $this = shift;
my $max = 0; # max SVN rev no
my $maxd = 0; # max date
#Shelling out with a large number of files dies, killing the build.
my $idx = 0;
while ( $idx < scalar( @{ $this->{files} } ) ) {
my @files;
my $limit = $idx + 1000;
$limit = scalar( @{ $this->{files} } )
if $limit > scalar( @{ $this->{files} } );
while ( $idx < $limit ) {
if ( ${ $this->{files} }[$idx]->{name} ) {
my $file =
$this->{basedir} . '/' . ${ $this->{files} }[$idx]->{name};
if ( -f $file ) {
push @files, $file;
}
elsif ( $file =~ /\/$/ )
{ # Directory, create if it does not exist
File::Path::mkpath($file);
}
elsif ( !-d $file ) { # Ignore directories
warn
"WARNING: $file is in MANIFEST, but it doesn't exist\n";
}
}
$idx++;
}
# Get revision info all the files in the manifest
# To find the latest one
unless (
eval {
local $SIG{__DIE__};
my @command;
if ( -d ".svn" ) {
@command = qw(svn info);
my $log = $this->sys_action( @command, @files );
my $getDate = 0;
foreach my $line ( split( "\n", $log ) ) {
if ( $line =~ /^Last Changed Rev: (\d+)/ ) {
$getDate = 0;
if ( $1 > $max ) {
$max = $1;
$getDate = 1;
}
}
elsif ($getDate
&& $line =~
/(?:^Text Last Updated|Last Changed Date): ([\d-]+) ([\d:]+) ([-+\d]+)?/m
)
{
$maxd = Foswiki::Time::parseTime(
"$1T$2" . ( $3 || '' ) );
$getDate = 0;
}
}
}
elsif ( my ( $gitdir, $gitsvn ) =
$this->_findPathToDotGitDir() )
{
@command = qw(git log -1 --pretty=medium --date=iso --);
my $log = $this->sys_action( @command, @files );
if ( $log =~ /^\s+git-svn-id: \S+\@(\d+)\s/m ) {
$max = $1 if $1 > $max;
}
else {
die 'You have un-published changes.'
. ' Please "git svn dcommit"';
}
if ( $log =~ /^Date:\s+([\d-]+) ([\d:]+) ([-+\d]+)?/m ) {
$maxd = Foswiki::Time::parseTime("$1T$2$3");
}
}
else {
die "Cannot find a proper command to search history.";
}
1;
}
)
{
# This is commented out because it's annoying
# when auto-porting extensions
# warn "WARNING: $@";
$maxd = time() unless $maxd;
$this->{SVNREV} = $max if defined $max;
# People shouldn't test $@ for that reason, but they do...
$@ = undef;
}
}
$this->{DATE} = Foswiki::Time::formatTime( $maxd, '$iso', 'gmtime' );
# If not checked in, or we can't get to SVN, use the current time.
$this->{DATE} ||= Foswiki::Time::formatTime( time(), '$iso', 'gmtime' );
my $day = $this->{DATE};
$day =~ s/T.*//;
return $day;
}
# Filter a file from source to dest, calling $this->$sub on the text
sub filter_file {
my ( $this, $from, $to, $sub ) = @_;
my $fh;
open( $fh, '<', $from ) || die 'No source topic ' . $from . ' for filter';
local $/ = undef;
my $text = <$fh>;
$text = $this->$sub($text) unless $from =~ /Dependency.pm$/;
close($fh);
unless ( $this->{-n} ) {
my ( $v, $d, $f ) = File::Spec->splitpath($to);
$this->makepath( File::Spec->catpath( $v, $d, '' ) );
open( $fh, '>', $to )
|| die 'Bad dest topic ' . $to . ' for filter:' . $!;
print $fh $text;
close($fh);
}
}
sub ask {
my ( $q, $default ) = @_;
my $reply;
local $/ = "\n";
$q .= '?' unless $q =~ /\?\s*$/;
my $yorn = 'y/n';
if ( defined $default ) {
if ( $default =~ /y/i ) {
$default = 'yes';
$yorn = 'Y/n';
}
elsif ( $default =~ /n/i ) {
$default = 'no';
$yorn = 'y/N';
}
else {
$default = undef;
}
}
print $q. ' [' . $yorn . '] ';
while ( ( $reply = <STDIN> ) !~ /^[yn]/i ) {
if ( $reply =~ /^\s*$/ && defined($default) ) {
$reply = $default;
last;
}
print "Please answer yes or no\n";
}
return ( $reply =~ /^y/i ) ? 1 : 0;
}
sub prompt {
my ( $q, $default ) = @_;
local $/ = "\n";
my $reply = '';
while ( !$reply ) {
print $q;
print " ($default)" if defined $default;
print ': ';
$reply = <STDIN>;
chomp($reply);
$reply ||= $default;
}
return $reply;
}
=begin TML
---++++ pushd($dir)
Change to the given directory
=cut
sub pushd {
my ( $this, $file ) = @_;
if ( $this->{-v} || $this->{-n} ) {
print 'pushd ' . $file . "\n";
}
if ( !$this->{-n} ) {
push( @{ $this->{dirStack} }, Cwd::cwd() );
chdir($file) || die 'Failed to pushd to ' . $file;
}
}
=begin TML
---++++ popd()
Pop a dir level, previously pushed by pushd
=cut
sub popd {
my $this = shift;
die unless scalar( @{ $this->{dirStack} } );
my $dir = pop( @{ $this->{dirStack} } );
if ( $this->{-v} || $this->{-n} ) {
print 'popd ' . $dir . "\n";
}
if ( !$this->{-n} ) {
chdir($dir) || die 'Failed to popd to ' . $dir;
}
}
=begin TML
---++++ rm($file)
Remove the given file (or directory)
=cut
sub rm {
my ( $this, $file ) = @_;
if ( $this->{-v} || $this->{-n} ) {
print 'rm ' . $file . "\n";
}
if ( -e $file && !$this->{-n} ) {
if ( -d $file ) {
File::Path::rmtree($file);
}
else {
unlink($file) || warn 'WARNING: Failed to delete ' . $file;
}
}
}
=begin TML
---++++ makepath($to)
Make a directory and all directories leading to it.
=cut
sub makepath {
my ( $this, $to ) = @_;
File::Path::mkpath( $to, { verbose => $this->{-v} } );
}
=begin TML
---++++ cp($from, $to)
Copy a single file from - to. Will automatically make intervening
directories in the target. Also works for target directories.
=cut
sub cp {
my ( $this, $from, $to ) = @_;
die 'Source file ' . $from . ' does not exist '
unless ( $this->{-n} || -e $from );
my $mum = $to;
my ( $v, $d, $f ) = File::Spec->splitpath($to);
$this->makepath( File::Spec->catpath( $v, $d, '' ) );
if ( $this->{-v} || $this->{-n} ) {
print 'cp ' . $from . ' ' . $to . "\n";
}
unless ( $this->{-n} ) {
if ( -d $from ) {
unless ( -e $to ) {
mkdir($to) || warn 'Warning: Failed to make ' . $to . ': ' . $!;
}
}
else {
File::Copy::copy( $from, $to )
|| warn 'Warning: Failed to copy '
. $from . ' to '
. $to . ': '
. $!;
}
}
}
=begin TML
---++++ prot($perms, $file)
Set permissions on a file. Permissions should be expressed using POSIX
chmod notation.
=cut
sub prot {
my ( $this, $perms, $file ) = @_;
if ( !-d $file ) { #skip directories
$this->perl_action("chmod($perms,'$file')");
}
}
=begin TML
---++++ sys_action(@params)
Perform a "system" command.
=cut
sub sys_action {
my $this = shift;
# use double-quotes to protect string with spaces, because they
# work in both bash and DOS. Other shell metacharacters are fed
# to the shell.
my $cmd = join( ' ', map { /\s/ ? "\"$_\"" : $_ } @_ );
if ( $this->{-v} || $this->{-n} ) {
print "Running: $cmd\n";
}
return '' if ( $this->{-n} );
my $output = `$cmd`;
die 'Failed to ' . $cmd . ': ' . ( $? >> 8 ) if $? >> 8;
return $output;
}
=begin TML
---++++ perl_action($cmd)
Perform a "perl" command.
=cut
sub perl_action {
my ( $this, $cmd ) = @_;
if ( $this->{-v} || $this->{-n} ) {
print $cmd. "\n";
}
unless ( $this->{-n} ) {
eval $cmd;
die 'Failed to ' . $cmd . ': ' . $@ if ($@);
}
}
=begin TML
---++++ filter_txt
Expands tokens.
The filter is used in the generation of documentation topics and the installer
=cut
sub filter_txt {
my ( $this, $from, $to ) = @_;
$this->filter_file(
$from, $to,
sub {
my ( $this, $text ) = @_;
# Replace the version (SVN Rev or wrongly saved number) with rev 1.
# Item10629: Must preserve version for CompareRevisionAddOnDemoTopic, or nothing to demo
$text =~ s/^(%META:TOPICINFO{.*version=").*?(".*}%)$/${1}1$2/m
unless $from =~ m/CompareRevisionsAddOnDemoTopic.txt$/;
$text =~ s/%\$(\w+)%/&_expand($this,$1)/geo;
return $text;
}
);
}
sub _expand {
my ( $this, $tok ) = @_;
if ( !$this->{$tok} && $tok eq 'POD' ) {
$this->build('POD');
}
if ( defined( $this->{$tok} ) ) {
if ( $this->{-v} || $this->{-n} ) {
print 'expand %$' . $tok . '% to ' . $this->{$tok} . "\n";
}
return $this->{$tok};
}
else {
return '%$' . $tok . '%';
}
}