Line # Revision Author
1 146 abw #!/usr/bin/perl -w # -*- perl -*-
2
3 use strict;
4 1002 abw use warnings;
5 146 abw
6 1156 abw use 5.006;
7 146 abw use lib qw( ./lib );
8 405 abw use Config;
9 use File::Spec::Functions qw( catfile );
10 146 abw use Template;
11 use ExtUtils::MakeMaker;
12 98 abw use Cwd;
13 2 abw
14 22 abw select STDERR;
15 $| = 1;
16 select STDOUT;
17
18 1170 abw use vars qw( $TT_VERSION $TT_PREFIX
19 898 abw $TT_XS_ENABLE $TT_XS_DEFAULT
20 1170 abw $TT_QUIET $TT_ACCEPT $TT_YES );
21 124 abw
22 146 abw # check O/S to set sensible defaults
23 156 abw
24 my ($WIN32, $FLAVOUR, $PREFIX, $IMAGES, $MAKE);
25 176 abw if ($^O eq 'MSWin32') { # any others also?
26 146 abw $WIN32 = 1;
27 $FLAVOUR = 'Win32';
28 1148 abw $PREFIX = 'C:/Program Files/Template Toolkit 2';
29 $IMAGES = '/tt2/images';
30 146 abw }
31 else {
32 $WIN32 = 0;
33 $FLAVOUR = 'Unix';
34 1148 abw $PREFIX = '/usr/local/tt2';
35 $IMAGES = '/tt2/images';
36 146 abw }
37 1136 abw $MAKE=$Config{'make'};
38 146 abw
39 156 abw
40 # read command line args putting TT_* into $ttconfig and
41 # everything else (regular Makefile.PL args, e.g. PREFIX)
42 # goes into $config
43
44 146 abw my (%config, %ttconfig);
45 while ($_ = shift) {
46 my ($k, $v) = split(/=/);
47 if ($k =~ /^TT/) {
48 731 abw $ttconfig{ $k } = $v || 0;
49 146 abw }
50 else {
51 731 abw $config{ $k } = $v || 0;
52 146 abw }
53 };
54
55 156 abw
56 # print help if they asked for it
57
58 146 abw if (exists $ttconfig{ TT_HELP }) {
59 print <<EOF;
60 The following options can be specified as command line
61 arguments to 'perl Makefile.PL'. e.g.
62
63 1148 abw perl Makefile.PL TT_XS_DEFAULT=y TT_ACCEPT=y
64 146 abw
65 156 abw TT_XS_ENABLE Enable XS Stash (y)
66 TT_XS_DEFAULT Use XS Stash by default (y)
67 1170 abw TT_QUIET no messages (n)
68 156 abw TT_ACCEPT accept defaults (n)
69
70 146 abw By default, the Makefile.PL runs in interactive mode,
71 prompting for confirmation of the various configuration
72 options. Setting the TT_ACCEPT option causes the default
73 value (possibly modified by other command line options)
74 to be accepted. The TT_QUIET option can also be set to
75 suppress the prompt messages.
76
77 EOF
78 exit(0);
79 }
80
81 156 abw # these global package variables are the main flags used
82 # in this script, here defaulted to sensible values
83
84 146 abw $TT_VERSION = $Template::VERSION;
85 156 abw $TT_XS_ENABLE = 'y';
86 $TT_XS_DEFAULT = 'y';
87 146 abw $TT_QUIET = 'n';
88 $TT_ACCEPT = 'n';
89 131 abw
90 124 abw my $DEFAULTS_FILE = '.defaults.cfg';
91 my $DEFAULTS = '';
92 121 abw
93 124 abw if (-f $DEFAULTS_FILE) {
94 require $DEFAULTS_FILE;
95 $DEFAULTS = " read from '$DEFAULTS_FILE'";
96 }
97
98 156 abw $TT_XS_ENABLE = $ttconfig{ TT_XS_ENABLE } if defined $ttconfig{ TT_XS_ENABLE };
99 $TT_XS_DEFAULT = $ttconfig{ TT_XS_DEFAULT } if defined $ttconfig{ TT_XS_DEFAULT };
100 $TT_QUIET = $ttconfig{ TT_QUIET } if defined $ttconfig{ TT_QUIET };
101 146 abw
102 898 abw if (defined $ttconfig{ TT_ACCEPT }) {
103 $TT_ACCEPT = $ttconfig{ TT_ACCEPT };
104 }
105 else {
106 # standard behaviour for MakeMaker to indicate accept all defaults
107 $TT_ACCEPT = $ENV{PERL_MM_USE_DEFAULT} ? 'y' : 'n';
108 }
109
110 1170 abw foreach ($TT_XS_ENABLE, $TT_XS_DEFAULT ) {
111 146 abw $_ = 'n' if ! $_;
112 }
113 $TT_ACCEPT = 0 if $TT_ACCEPT eq 'n';
114 $TT_QUIET = 0 if $TT_QUIET eq 'n';
115 $TT_QUIET = 0 unless $TT_ACCEPT;
116
117 741 abw # define version numbers of required modules
118 944 abw my $TT_APPCONFIG_VERSION = '1.56';
119 741 abw my $TT_FILE_SPEC_VERSION = '0.8';
120 my $TT_FILE_TEMP_VERSION = '0.12';
121
122
123 146 abw #========================================================================
124
125 welcome_message();
126 version_check();
127 303 abw mandatory_modules();
128 156 abw optional_stash_xs();
129 146 abw write_defaults();
130
131 print "\n";
132
133
134 #------------------------------------------------------------------------
135 # build options and write Makefile
136 #------------------------------------------------------------------------
137
138 package main;
139
140 my %opts = (
141 %config,
142 1170 abw 'NAME' => 'Template',
143 146 abw 'DISTNAME' => 'Template-Toolkit',
144 'VERSION_FROM' => 'lib/Template.pm',
145 'EXE_FILES' => [ 'bin/tpage', 'bin/ttree' ],
146 'PMLIBDIRS' => [ 'lib' ],
147 156 abw 'DIR' => [ ],
148 292 abw 'PREREQ_PM' => {
149 741 abw 'AppConfig' => $TT_APPCONFIG_VERSION,
150 'File::Spec' => $TT_FILE_SPEC_VERSION,
151 'File::Temp' => $TT_FILE_TEMP_VERSION,
152 292 abw },
153 146 abw 'dist' => {
154 731 abw 'COMPRESS' => 'gzip',
155 'SUFFIX' => 'gz',
156 146 abw },
157 978 abw 'test' => {
158 1136 abw 'TESTS' => join(' ', map { glob } qw( t/*.t t/vmethods/*.t )),
159 978 abw },
160 303 abw 'clean' => {
161 731 abw 'FILES' => join(' ', qw( docs/ttree.cfg
162 examples/ttree.cfg
163 t/dbi_test.cfg
164 t/test/src/baz.ttc
165 t/test/src/complex.org
166 t/test/src/complex.ttc
167 t/test/src/evalperl.ttc
168 t/test/src/foo.ttc )),
169 303 abw },
170 146 abw );
171
172 156 abw push @{ $opts{'DIR'} }, 'xs' if $TT_XS_ENABLE;
173
174 1028 abw # Handle dev versions in our check
175 my $mmv = $ExtUtils::MakeMaker::VERSION;
176 $mmv =~ s/\_.+//;
177
178 if ($mmv >= 5.43) {
179 731 abw $opts{ AUTHOR } = 'Andy Wardley <abw@wardley.org>';
180 $opts{ ABSTRACT } = 'comprehensive template processing system',
181 146 abw }
182
183 1136 abw if ($ExtUtils::MakeMaker::VERSION ge '6.30_00') {
184 $opts{'LICENSE' } = 'perl';
185 }
186 146 abw
187 WriteMakefile( %opts );
188
189 print <<EOF;
190
191 Configuration complete. You should now run '$MAKE', '$MAKE test' and
192 then '$MAKE install'. See the README file for further information.
193 EOF
194
195
196 #========================================================================
197
198
199
200 #------------------------------------------------------------------------
201 # welcome_message()
202 #
203 # Print opening banner.
204 #------------------------------------------------------------------------
205
206 sub welcome_message {
207 print(<<EOF);
208
209 Template Toolkit Version $TT_VERSION
210 65 abw =============================
211 121 abw
212 124 abw Using $FLAVOUR defaults$DEFAULTS.
213 156 abw Run 'perl Makefile.PL TT_HELP' for a summary of options.
214 54 abw EOF
215 146 abw print "Messages suppressed (TT_QUIET). " if $TT_QUIET;
216 print "Accepting defaults automatically (TT_ACCEPT)." if $TT_ACCEPT;
217 }
218 22 abw
219 146 abw
220
221 54 abw #------------------------------------------------------------------------
222 146 abw # version_check()
223 #
224 # Check for pre-version 2.00 installation and issue warning
225 54 abw #------------------------------------------------------------------------
226 38 abw
227 146 abw sub version_check {
228 eval "use Template";
229 unless ($@ or $Template::VERSION =~ /^2/) {
230 1170 abw warn(<<EOF) unless $TT_QUIET;
231 65 abw
232 54 abw IMPORTANT NOTE:
233 146 abw
234 54 abw You have version $Template::VERSION of the Template Toolkit installed.
235
236 66 abw There are some minor incompatabilities between version 1 and 2
237 54 abw of the Template Toolkit which you should be aware of. Installing
238 this version will overwrite your version $Template::VERSION files
239 unless you take measures to install one or the other version in a
240 66 abw different location (i.e. perl Makefile.PL PREFIX=/other/path).
241 54 abw
242 Please consult the README and Changes file for further details.
243 Most of the changes are in the more obscure features and
244 directives so hopefully you will find the upgrade process fairly
245 156 abw painless. If you're feeling brave, then answer 'y', otherwise 'n'.
246 54 abw
247 38 abw EOF
248 1170 abw exit unless ttprompt("Do you want to continue?", 'y') =~ /y/i;
249 146 abw }
250 54 abw }
251 38 abw
252 146 abw
253 54 abw #------------------------------------------------------------------------
254 146 abw # mandatory_modules()
255 #
256 # Detect mandatory module
257 98 abw #------------------------------------------------------------------------
258
259 146 abw sub mandatory_modules {
260 eval "use AppConfig";
261 741 abw if ($@ or $AppConfig::VERSION < $TT_APPCONFIG_VERSION) {
262 1170 abw warn(<<EOF);
263 98 abw
264 741 abw The Template Toolkit requires that the AppConfig module (version $TT_APPCONFIG_VERSION
265 or later) first be installed. This is used by
266 the 'ttree' program for reading command line options and configuration
267 files. It is available from CPAN:
268 98 abw
269 http://www.cpan.org/authors/Andy_Wardley/
270
271 EOF
272 146 abw }
273 98 abw
274 146 abw eval "use File::Spec";
275 741 abw if ($@ or $File::Spec::VERSION < $TT_FILE_SPEC_VERSION) {
276 1170 abw warn(<<EOF);
277 121 abw
278 741 abw The Template Toolkit requires that the File::Spec module (version $TT_FILE_SPEC_VERSION
279 121 abw or later) first be installed. This is used by the File plugin. It is
280 available from CPAN:
281
282 http://search.cpan.org/search?dist=File-Spec
283
284 EOF
285 146 abw }
286 650 abw
287 eval "use File::Temp";
288 741 abw if ($@ or $File::Temp::VERSION < $TT_FILE_TEMP_VERSION) {
289 650 abw warn(<<EOF);
290
291 741 abw The Template Toolkit requires that the File::Temp module (version $TT_FILE_TEMP_VERSION
292 650 abw or later) first be installed. This is used by the Template::Document
293 class for storing compiled templates. It is available from CPAN:
294
295 http://search.cpan.org/search?dist=File-Temp
296
297 EOF
298 }
299 121 abw }
300
301
302 98 abw #------------------------------------------------------------------------
303 156 abw # optional_stash_xs()
304 #
305 # Prompt for installation and default use of XS Stash.
306 #------------------------------------------------------------------------
307 146 abw
308 156 abw sub optional_stash_xs {
309 # return if $TT_ACCEPT && (! $TT_XS_ENABLE || $TT_XS_ENABLE eq 'n');
310
311 message(<<EOF);
312
313
314 898 abw Template::Stash::XS
315 -------------------
316 156 abw
317 898 abw The Template::Stash module is a core part of the Template Toolkit,
318 implementing the magic for accessing data using the dot notation.
319 156 abw
320 898 abw There is a high speed version, Template::Stash::XS, written in C.
321 This makes the Template Toolkit run about twice as fast as when using
322 the regular Template::Stash written in Perl. If you've got a C
323 compiler on your system then you can elect to have the XS Stash built.
324 You can also specify that you want to use the XS Stash by default.
325 156 abw
326 944 abw Note that as of version 2.15 the XS Stash now supports access to tied
327 hashes and arrays.
328 898 abw
329 See 'perldoc Template::Config' for further details.
330
331 156 abw EOF
332
333 $TT_XS_ENABLE = (ttprompt('Do you want to build the XS Stash module?',
334 1170 abw $TT_XS_ENABLE) =~ /^y/i);
335 156 abw
336 if ($TT_XS_ENABLE) {
337 686 abw $TT_XS_DEFAULT =
338 898 abw (ttprompt('Do you want to use the XS Stash by default?',
339 $TT_XS_DEFAULT) =~ /^y/i);
340 686 abw }
341 else {
342 # If the XS stash is disabled, we cannot use it as the default stash.
343 $TT_XS_DEFAULT = 0;
344 }
345 156 abw
346 686 abw # Actually, we would have to fix 'Config.pm' only if the XS stash is
347 # disabled. But this way, we are sure the correct module is used.
348 1170 abw fix_file(catfile('lib','Template','Config.pm'),
349 '$STASH',
350 $TT_XS_DEFAULT ? 'Template::Stash::XS' : 'Template::Stash');
351 898 abw }
352 156 abw
353
354 131 abw
355
356 98 abw
357 146 abw #--------------------------------------------------------------------
358 # write_defaults()
359 #
360 # write configuration defaults to file
361 #--------------------------------------------------------------------
362
363 sub write_defaults {
364 124 abw open(FP, "> $DEFAULTS_FILE") || die "$DEFAULTS_FILE: $!\n";
365 1170 abw my ( $ttxs_enable, $ttxs_default )
366 = map { $_ ? 'y' : 'n' }
367 ( $TT_XS_ENABLE, $TT_XS_DEFAULT );
368 124 abw print FP <<EOF;
369 156 abw \$TT_XS_ENABLE = '$ttxs_enable';
370 \$TT_XS_DEFAULT = '$ttxs_default';
371 146 abw \$TT_ACCEPT = '$TT_ACCEPT';
372 \$TT_QUIET = '$TT_QUIET';
373 137 abw 1;
374 124 abw EOF
375 close(FP);
376 98 abw }
377
378
379 38 abw
380 2 abw
381 66 abw #------------------------------------------------------------------------
382 98 abw # fix_file($file, $find, $fix)
383 #
384 # Fixes a variable definition in a file. e.g.
385 157 abw # fix_file('templates/splash/config', 'images', '/tt2/splash')
386 98 abw #------------------------------------------------------------------------
387
388 sub fix_file {
389 my ($file, $find, $fix) = @_;
390 local *FP;
391 local $/ = undef;
392
393 $find = quotemeta($find);
394
395 open(FP, "< $file") || die "$file: $!\n";
396 my $text = <FP>;
397 close(FP);
398
399 ($text =~ s/^(\s*${find}\s*=\s*)'.*?'/$1'$fix'/m)
400 1170 abw || die "$find not found in $file\n";
401 98 abw
402 open(FP, "> $file") || die "$file: $!\n";
403 print FP $text;
404 close(FP);
405 }
406
407 146 abw
408 #------------------------------------------------------------------------
409 156 abw # find_program($path, $prog)
410 131 abw #
411 # Find a program, $prog, by traversing the given directory path, $path.
412 # Returns full path if the program is found.
413 #
414 # Written by Craig Barratt, Richard Tietjen add fixes for Win32.
415 156 abw #
416 # abw changed name from studly caps findProgram() to find_program() :-)
417 146 abw #------------------------------------------------------------------------
418
419 156 abw sub find_program {
420 131 abw my($path, $prog) = @_;
421 405 abw # my $sep = $WIN32 ? qr/;/ : qr/:/;
422 # foreach my $dir ( split($sep, $path) ) {
423 foreach my $dir ( split($Config{path_sep}, $path) ) {
424 131 abw my $file = File::Spec->catfile($dir, $prog);
425 if ( !$WIN32 ) {
426 return $file if ( -x $file );
427 } else {
428 # Windows executables end in .xxx, exe precedes .bat and .cmd
429 foreach my $dx ( qw/exe bat cmd/ ) {
430 return "$file.$dx" if ( -x "$file.$dx" );
431 }
432 }
433 }
434 }
435 146 abw
436
437 #------------------------------------------------------------------------
438 # message($text)
439 #
440 # Print message unless quiet mode.
441 #------------------------------------------------------------------------
442
443 sub message {
444 return if $TT_QUIET;
445 print @_;
446 }
447
448
449 #------------------------------------------------------------------------
450 # ttprompt($message, $default)
451 #------------------------------------------------------------------------
452
453 sub ttprompt {
454 my ($msg, $def)=@_;
455 my $ISA_TTY = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; # Pipe?
456 my $dispdef = defined $def ? "[$def] " : " ";
457 $def = defined $def ? $def : "";
458 my $ans = '';
459 local $|=1;
460 print "$msg $dispdef" unless $TT_QUIET;
461 if ($TT_ACCEPT || ! $ISA_TTY) {
462 731 abw print "$def\n" unless $TT_QUIET;
463 146 abw }
464 else {
465 731 abw chomp($ans = <STDIN>);
466 146 abw }
467 return ($ans ne '') ? $ans : $def;
468 }
469
470
471 #------------------------------------------------------------------------
472 # yep($text)
473 #------------------------------------------------------------------------
474
475 sub yep {
476 return if $TT_QUIET;
477 print '[X] ', shift, "\n";
478 }
479
480
481 #------------------------------------------------------------------------
482 # nope($text)
483 #------------------------------------------------------------------------
484 sub nope {
485 return if $TT_QUIET;
486 print '[ ] ', shift, "\n";
487 }