blib/lib/Template/App/ttree.pm untested

filter: Statementstmt 0.0% 0 / 373 Branchbran 0.0% 0 / 180 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 743 CC 91 SCAR 62.4
CC
91
Cov
0%
process_file
1122.0
run
812.0
process_tree
182.0
CRAP 514.8
1 package Template::App::ttree;
2  
3 #========================================================================
4 #
5 # Template::App::ttre
6 #
7 # DESCRIPTION
8 # Script for processing all directory trees containing templates.
9 # Template files are processed and the output directed to the
10 # relevant file in an output tree. The timestamps of the source and
11 # destination files can then be examined for future invocations
12 # to process only those files that have changed. In other words,
13 # it's a lot like 'make' for templates.
14 #
15 # AUTHOR
16 # Andy Wardley <abw@wardley.org>
17 #
18 # COPYRIGHT
19 # Copyright (C) 1996-2013 Andy Wardley. All Rights Reserved.
20 # Copyright (C) 1998-2003 Canon Research Centre Europe Ltd.
21 #
22 # This module is free software; you can redistribute it and/or
23 # modify it under the same terms as Perl itself.
24 #
25 #========================================================================
26  
27 use strict;
28 use warnings;
29 use base 'Template::Base';
30  
31 our $VERSION = '3.106';
32  
33 use Template;
34 use AppConfig qw( :expand );
35 use File::Copy;
36 use File::Path;
37 use File::Spec;
38 use File::Basename;
39 use Text::ParseWords qw(quotewords);
40  
41 use constant DEFAULT_TTMODULE => 'Template';
42 use constant DEFAULT_HOME => $ENV{ HOME } || '';
43  
44 sub emit_warn {
45 my $self = shift;
46 my $msg = shift;
47 warn $msg;
48 }
49  
50 sub emit_log {
51 my $self = shift;
52 print @_
53 }
54  
55 sub _get_myname {
56 my $self = shift;
57 (split /[:]{2}/, __PACKAGE__)[-1];
58 }
59  
60 sub _get_rc_file {
61 my $self = shift;
62 my $NAME = $self->_get_myname();
63 return $ENV{"\U${NAME}rc"} || DEFAULT_HOME . "/.${NAME}rc";
64 }
65  
66 sub offer_create_a_sample_config_file {
67 my $self = shift;
68 my $RCFILE = $self->_get_rc_file();
69 # offer create a sample config file if it doesn't exist, unless a '-f'
70 # has been specified on the command line
71 unless (-f $RCFILE or grep(/^(-f|-h|--help)$/, @ARGV) ) {
72 $self->emit_log("Do you want me to create a sample '.ttreerc' file for you?\n",
73 "(file: $RCFILE) [y/n]: ");
74 my $y = <STDIN>;
75 if ($y =~ /^y(es)?/i) {
76 $self->write_config($RCFILE);
77 exit(0);
78 }
79 }
80 }
81  
82 sub run {
83 my $self = shift;
84 my $NAME = $self->_get_myname();
85  
86 #------------------------------------------------------------------------
87 # configuration options
88 #------------------------------------------------------------------------
89  
90 # read configuration file and command line arguments - I need to remember
91 # to fix varlist() and varhash() in AppConfig to make this nicer...
92 my $config = $self->read_config( $self->_get_rc_file() );
93 my $dryrun = $config->nothing;
94 my $verbose = $config->verbose || $dryrun;
95 my $colour = $config->colour;
96 my $summary = $config->summary;
97 my $recurse = $config->recurse;
98 my $preserve = $config->preserve;
99 my $all = $config->all;
100 my $libdir = $config->lib;
101 my $ignore = $config->ignore;
102 my $copy = $config->copy;
103 my $link = $config->link;
104 my $accept = $config->accept;
105 my $absolute = $config->absolute;
106 my $relative = $config->relative;
107 my $suffix = $config->suffix;
108 my $binmode = $config->binmode;
109 my $depends = $config->depend;
110 my $depsfile = $config->depend_file;
111 my $copy_dir = $config->copy_dir;
112 my ($n_proc, $n_unmod, $n_skip, $n_copy, $n_link, $n_mkdir) = (0) x 6;
113  
114 my $srcdir = $config->src
115 || die "Source directory not set (-s)\n";
116 my $destdir = $config->dest
117 || die "Destination directory not set (-d)\n";
118 die "Source and destination directories may not be the same:\n $srcdir\n"
119 if $srcdir eq $destdir;
120  
121 # unshift any perl5lib directories onto front of INC
122 unshift(@INC, @{ $config->perl5lib });
123  
124 # get all template_* options from the config and fold keys to UPPER CASE
125 my %ttopts = $config->varlist('^template_', 1);
126 my $ttmodule = delete($ttopts{ module });
127 my $ucttopts = {
128 map { my $v = $ttopts{ $_ }; defined $v ? (uc $_, $v) : () }
129 keys %ttopts,
130 };
131  
132 # get all template variable definitions
133 my $replace = $config->get('define');
134  
135 # now create complete parameter hash for creating template processor
136 my $ttopts = {
137 %$ucttopts,
138 RELATIVE => $relative,
139 ABSOLUTE => $absolute,
140 INCLUDE_PATH => [ $srcdir, @$libdir ],
141 OUTPUT_PATH => $destdir,
142 };
143  
144 # load custom template module
145 if ($ttmodule) {
146 my $ttpkg = $ttmodule;
147 $ttpkg =~ s[::][/]g;
148 $ttpkg .= '.pm';
149 require $ttpkg;
150 }
151 else {
152 $ttmodule = DEFAULT_TTMODULE;
153 }
154  
155  
156 #------------------------------------------------------------------------
157 # inter-file dependencies
158 #------------------------------------------------------------------------
159  
160 if ($depsfile or $depends) {
161 $depends = $self->dependencies($depsfile, $depends);
162 }
163 else {
164 $depends = { };
165 }
166  
167 my $global_deps = $depends->{'*'} || [ ];
168  
169 # add any PRE_PROCESS, etc., templates as global dependencies
170 foreach my $ttopt (qw( PRE_PROCESS POST_PROCESS PROCESS WRAPPER )) {
171 my $deps = $ucttopts->{ $ttopt } || next;
172 my @deps = ref $deps eq 'ARRAY' ? (@$deps) : ($deps);
173 next unless @deps;
174 push(@$global_deps, @deps);
175 }
176  
177 # remove any duplicates
178 $global_deps = { map { ($_ => 1) } @$global_deps };
179 $global_deps = [ keys %$global_deps ];
180  
181 # update $depends hash or delete it if there are no dependencies
182 if (@$global_deps) {
183 $depends->{'*'} = $global_deps;
184 }
185 else {
186 delete $depends->{'*'};
187 $global_deps = undef;
188 }
189 $depends = undef
190 unless keys %$depends;
191  
192 my $DEP_DEBUG = $config->depend_debug();
193  
194  
195 #------------------------------------------------------------------------
196 # pre-amble
197 #------------------------------------------------------------------------
198  
199 if ($colour) {
200 no strict 'refs';
201 *red = \&_red;
202 *green = \&_green;
203 *yellow = \&_yellow;
204 *blue = \&_blue;
205 }
206 else {
207 no strict 'refs';
208 *red = \&_white;
209 *green = \&_white;
210 *yellow = \&_white;
211 *blue = \&_white;
212 }
213  
214 if ($verbose) {
215 local $" = ', ';
216  
217  
218 $self->emit_log( "$NAME $VERSION (Template Toolkit version $Template::VERSION)\n\n" );
219  
220 my $sfx = join(', ', map { "$_ => $suffix->{$_}" } keys %$suffix);
221  
222 $self->emit_log(" Source: $srcdir\n",
223 " Destination: $destdir\n",
224 "Include Path: [ @$libdir ]\n",
225 " Ignore: [ @$ignore ]\n",
226 " Copy: [ @$copy ]\n",
227 " Link: [ @$link ]\n",
228 " Copy_Dir: [ @$copy_dir ]\n",
229 " Accept: [ @$accept ]\n",
230 " Suffix: [ $sfx ]\n");
231 $self->emit_log(" Module: $ttmodule ", $ttmodule->module_version(), "\n")
232 unless $ttmodule eq DEFAULT_TTMODULE;
233  
234 if ($depends && $DEP_DEBUG) {
235 $self->emit_log("Dependencies:\n");
236 foreach my $key ('*', grep { !/\*/ } keys %$depends) {
237 $self->emit_log( sprintf( " %-16s %s\n", $key,
238 join(', ', @{ $depends->{ $key } }) ) )
239 if defined $depends->{ $key };
240  
241 }
242 }
243 $self->emit_log( "\n" ) if $verbose > 1;
244 $self->emit_log( red("NOTE: dry run, doing nothing...\n") )
245 if $dryrun;
246 }
247  
248 #------------------------------------------------------------------------
249 # main processing loop
250 #------------------------------------------------------------------------
251  
252 my $template = $ttmodule->new($ttopts)
253 || die $ttmodule->error();
254  
255 my $running_conf = {
256 accept => $accept,
257 all => $all,
258 binmode => $binmode,
259 config => $config,
260 copy => $copy,
261 copy_dir => $copy_dir,
262 depends => $depends,
263 destdir => $destdir,
264 dryrun => $dryrun,
265 ignore => $ignore,
266 libdir => $libdir,
267 link => $link,
268 n_copy => \$n_copy,
269 n_link => \$n_link,
270 n_mkdir => \$n_mkdir,
271 n_proc => \$n_proc,
272 n_skip => \$n_skip,
273 n_unmod => \$n_unmod,
274 preserve => $preserve,
275 recurse => $recurse,
276 replace => $replace,
277 srcdir => $srcdir,
278 suffix => $suffix,
279 template => $template,
280 verbose => $verbose,
281 };
282  
283 if (@ARGV) {
284 # explicitly process files specified on command lines
285 foreach my $file (@ARGV) {
286 my $path = $srcdir ? File::Spec->catfile($srcdir, $file) : $file;
287 if ( -d $path ) {
288 $self->process_tree($file, $running_conf);
289 }
290 else {
291 $self->process_file($file, $path, $running_conf, force => 1);
292 }
293 }
294 }
295 else {
296 # implicitly process all file in source directory
297 $self->process_tree(undef, $running_conf);
298 }
299  
300 if ($summary || $verbose) {
301 my $format = "%13d %s %s\n";
302 $self->emit_log( "\n" ) if $verbose > 1;
303 $self->emit_log(
304 " Summary: ",
305 $dryrun ? red("This was a dry run. Nothing was actually done\n") : "\n",
306 green(sprintf($format, $n_proc, $n_proc == 1 ? 'file' : 'files', 'processed')),
307 green(sprintf($format, $n_copy, $n_copy == 1 ? 'file' : 'files', 'copied')),
308 green(sprintf($format, $n_link, $n_link == 1 ? 'file' : 'files', 'linked')),
309 green(sprintf($format, $n_mkdir, $n_mkdir == 1 ? 'directory' : 'directories', 'created')),
310 yellow(sprintf($format, $n_unmod, $n_unmod == 1 ? 'file' : 'files', 'skipped (not modified)')),
311 yellow(sprintf($format, $n_skip, $n_skip == 1 ? 'file' : 'files', 'skipped (ignored)'))
312 );
313 }
314  
315 }
316  
317  
318  
319 #========================================================================
320 # END
321 #========================================================================
322  
323  
324 #------------------------------------------------------------------------
325 # $self->process_tree($dir)
326 #
327 # Walks the directory tree starting at $dir or the current directory
328 # if unspecified, processing files as found.
329 #------------------------------------------------------------------------
330  
331 sub process_tree {
332 my $self = shift;
333 my $dir = shift;
334 my $running_conf = shift;
335  
336 my(
337 $destdir,
338 $dryrun,
339 $ignore,
340 $n_mkdir,
341 $n_skip,
342 $recurse,
343 $srcdir,
344 $verbose,
345 ) = @{ $running_conf }{ qw(
346 destdir
347 dryrun
348 ignore
349 n_mkdir
350 n_skip
351 recurse
352 srcdir
353 verbose
354 )};
355  
356 my ($file, $path, $abspath, $check);
357 my $target;
358 local *DIR;
359  
360 my $absdir = join('/', $srcdir ? $srcdir : (), defined $dir ? $dir : ());
361 $absdir ||= '.';
362  
363 opendir(DIR, $absdir) || do { $self->emit_warn("$absdir: $!\n"); return undef; };
364  
365 FILE: while (defined ($file = readdir(DIR))) {
366 next if $file eq '.' || $file eq '..';
367 $path = defined $dir ? "$dir/$file" : $file;
368 $abspath = "$absdir/$file";
369  
370 next unless -e $abspath;
371  
372 # check against ignore list
373 foreach $check (@$ignore) {
374 if ($path =~ /$check/) {
375 $self->emit_log( yellow(sprintf " - %-32s (ignored, matches /$check/)\n", $path ) )
376 if $verbose > 1;
377 $$n_skip++;
378 next FILE;
379 }
380 }
381  
382 if (-d $abspath) {
383 if ($recurse) {
384 my ($uid, $gid, $mode);
385  
386 (undef, undef, $mode, undef, $uid, $gid, undef, undef,
387 undef, undef, undef, undef, undef) = stat($abspath);
388  
389 # create target directory if required
390 $target = "$destdir/$path";
391 unless (-d $target || $dryrun) {
392 mkpath($target, $verbose, $mode) or
393 die red("Could not mkpath ($target): $!\n");
394  
395 # commented out by abw on 2000/12/04 - seems to raise a warning?
396 # chown($uid, $gid, $target) || warn "chown($target): $!\n";
397  
398 $$n_mkdir++;
399 $self->emit_log( green( sprintf " + %-32s (created target directory)\n", $path ) )
400 if $verbose;
401 }
402 # recurse into directory
403 $self->process_tree($path, $running_conf);
404 }
405 else {
406 $$n_skip++;
407 $self->emit_log( yellow(sprintf " - %-32s (directory, not recursing)\n", $path ) )
408 if $verbose > 1;
409 }
410 }
411 else {
412 $self->process_file($path, $abspath, $running_conf);
413 }
414 }
415 closedir(DIR);
416 }
417  
418  
419 #------------------------------------------------------------------------
420 # $self->process_file()
421 #
422 # File filtering and processing sub-routine called by $self->process_tree()
423 #------------------------------------------------------------------------
424  
425 sub process_file {
426 my $self = shift;
427 my ($file, $absfile, $running_conf, %options) = @_;
428  
429 my(
430 $accept,
431 $all,
432 $binmode,
433 $config,
434 $copy,
435 $copy_dir,
436 $depends,
437 $destdir,
438 $dryrun,
439 $libdir,
440 $link,
441 $n_copy,
442 $n_link,
443 $n_proc,
444 $n_skip,
445 $n_unmod,
446 $preserve,
447 $replace,
448 $srcdir,
449 $suffix,
450 $template,
451 $verbose,
452 ) = @{ $running_conf }{ qw(
453 accept
454 all
455 binmode
456 config
457 copy
458 copy_dir
459 depends
460 destdir
461 dryrun
462 libdir
463 link
464 n_copy
465 n_link
466 n_proc
467 n_skip
468 n_unmod
469 preserve
470 replace
471 srcdir
472 suffix
473 template
474 verbose
475 )};
476  
477 my ($dest, $destfile, $filename, $check,
478 $srctime, $desttime, $mode, $uid, $gid);
479 my ($old_suffix, $new_suffix);
480 my $is_dep = 0;
481 my $copy_file = 0;
482 my $link_file = 0;
483  
484 $absfile ||= $file;
485 $filename = basename($file);
486 $destfile = $file;
487  
488 # look for any relevant suffix mapping
489 if (%$suffix) {
490 if ($filename =~ m/\.(.+)$/) {
491 $old_suffix = $1;
492 if ($new_suffix = $suffix->{ $old_suffix }) {
493 $destfile =~ s/$old_suffix$/$new_suffix/;
494 }
495 }
496 }
497 $dest = $destdir ? "$destdir/$destfile" : $destfile;
498  
499 # $self->emit_log( "proc $file => $dest\n" );
500  
501 unless ($link_file) {
502 # check against link list
503 foreach my $link_pattern (@$link) {
504 if ($filename =~ /$link_pattern/) {
505 $link_file = $copy_file = 1;
506 $check = "/$link_pattern/";
507 last;
508 }
509 }
510 }
511  
512 unless ($link_file) {
513 foreach my $prefix (@$copy_dir) {
514 if ( index($file, "$prefix/") == 0 ) {
515 $copy_file = 1;
516 $check = "copy_dir: $prefix";
517 last;
518 }
519 }
520 }
521  
522 unless ($copy_file) {
523 # check against copy list
524 foreach my $copy_pattern (@$copy) {
525 if ($filename =~ /$copy_pattern/) {
526 $copy_file = 1;
527 $check = "/$copy_pattern/";
528 last;
529 }
530 }
531 }
532  
533 # check against acceptance list
534 if (not $copy_file and @$accept) {
535 unless (grep { $filename =~ /$_/ } @$accept) {
536 $self->emit_log( yellow( sprintf " - %-32s (not accepted)\n", $file ) )
537 if $verbose > 1;
538 $$n_skip++;
539 return;
540 }
541 }
542  
543 # stat the source file unconditionally, so we can preserve
544 # mode and ownership
545 ( undef, undef, $mode, undef, $uid, $gid, undef,
546 undef, undef, $srctime, undef, undef, undef ) = stat($absfile);
547  
548 # test modification time of existing destination file
549 if (! $all && ! $options{ force } && -f $dest) {
550 $desttime = ( stat($dest) )[9];
551  
552 if (defined $depends and not $copy_file) {
553 my $deptime = $self->depend_time($file, $depends, $config, $libdir, $srcdir);
554 if (defined $deptime && ($srctime < $deptime)) {
555 $srctime = $deptime;
556 $is_dep = 1;
557 }
558 }
559  
560 if ($desttime >= $srctime) {
561 $self->emit_log( yellow( sprintf " - %-32s (not modified)\n", $file ) )
562 if $verbose > 1;
563 $$n_unmod++;
564 return;
565 }
566 }
567  
568 # check against link list
569 if ($link_file) {
570 unless ($dryrun) {
571 if (link($absfile, $dest) == 1) {
572 $copy_file = 0;
573 }
574 else {
575 $self->emit_warn( red("Could not link ($absfile to $dest) : $!\n") );
576 }
577 }
578  
579 unless ($copy_file) {
580 $$n_link++;
581 $self->emit_log( green( sprintf " > %-32s (linked, matches $check)\n", $file ) )
582 if $verbose;
583 return;
584 }
585 }
586  
587 # check against copy list
588 if ($copy_file) {
589 $$n_copy++;
590 unless ($dryrun) {
591 copy($absfile, $dest) or die red("Could not copy ($absfile to $dest) : $!\n");
592  
593 if ($preserve) {
594 chown($uid, $gid, $dest) || $self->emit_warn( red("chown($dest): $!\n") );
595 chmod($mode, $dest) || $self->emit_warn( red("chmod($dest): $!\n") );
596 }
597 }
598  
599 $self->emit_log( green( sprintf " > %-32s (copied, matches $check)\n", $file ) )
600 if $verbose;
601  
602 return;
603 }
604  
605 $$n_proc++;
606  
607 if ($verbose) {
608 $self->emit_log( green( sprintf " + %-32s", $file) );
609 $self->emit_log( green( sprintf " (changed suffix to $new_suffix)") ) if $new_suffix;
610 $self->emit_log( "\n" );
611 }
612  
613 # process file
614 unless ($dryrun) {
615 $template->process($file, $replace, $destfile,
616 $binmode ? {binmode => $binmode} : {})
617 || $self->emit_log(red(" ! "), $template->error(), "\n");
618  
619 if ($preserve) {
620 chown($uid, $gid, $dest) || $self->emit_warn( red("chown($dest): $!\n") );
621 chmod($mode, $dest) || $self->emit_warn( red("chmod($dest): $!\n") );
622 }
623 }
624 }
625  
626  
627 #------------------------------------------------------------------------
628 # $self->dependencies($file, $depends)
629 #
630 # Read the dependencies from $file, if defined, and merge in with
631 # those passed in as the hash array $depends, if defined.
632 #------------------------------------------------------------------------
633  
634 sub dependencies {
635 my $self = shift;
636 my ($file, $depend) = @_;
637 my %depends = ();
638  
639 if (defined $file) {
640 my ($fh, $text, $line);
641 open $fh, $file or die "Can't open $file, $!";
642 local $/ = undef;
643 $text = <$fh>;
644 close($fh);
645 $text =~ s[\\\n][]mg;
646  
647 foreach $line (split("\n", $text)) {
648 next if $line =~ /^\s*(#|$)/;
649 chomp $line;
650 my ($file, @files) = quotewords('\s*:\s*', 0, $line);
651 $file =~ s/^\s+//;
652 @files = grep(defined, quotewords('(,|\s)\s*', 0, @files));
653 $depends{$file} = \@files;
654 }
655 }
656  
657 if (defined $depend) {
658 foreach my $key (keys %$depend) {
659 $depends{$key} = [ quotewords(',', 0, $depend->{$key}) ];
660 }
661 }
662  
663 return \%depends;
664 }
665  
666  
667  
668 #------------------------------------------------------------------------
669 # $self->depend_time($file, \%depends)
670 #
671 # Returns the mtime of the most recent in @files.
672 #------------------------------------------------------------------------
673  
674 sub depend_time {
675 my $self = shift;
676 my ($file, $depends, $config, $libdir, $srcdir) = @_;
677 my ($deps, $absfile, $modtime);
678 my $maxtime = 0;
679 my @pending = ($file);
680 my @files;
681 my %seen;
682  
683 my $DEP_DEBUG = $config->depend_debug();
684  
685 # push any global dependencies onto the pending list
686 if ($deps = $depends->{'*'}) {
687 push(@pending, @$deps);
688 }
689  
690 $self->emit_log( " # checking dependencies for $file...\n" )
691 if $DEP_DEBUG;
692  
693 # iterate through the list of pending files
694 while (@pending) {
695 $file = shift @pending;
696 next if $seen{ $file }++;
697  
698 if (File::Spec->file_name_is_absolute($file) && -f $file) {
699 $modtime = (stat($file))[9];
700 $self->emit_log( " # $file [$modtime]\n" )
701 if $DEP_DEBUG;
702 }
703 else {
704 $modtime = 0;
705 foreach my $dir ($srcdir, @$libdir) {
706 $absfile = File::Spec->catfile($dir, $file);
707 if (-f $absfile) {
708 $modtime = (stat($absfile))[9];
709 $self->emit_log( " # $absfile [$modtime]\n" )
710 if $DEP_DEBUG;
711 last;
712 }
713 }
714 }
715 $maxtime = $modtime
716 if $modtime > $maxtime;
717  
718 if ($deps = $depends->{ $file }) {
719 push(@pending, @$deps);
720 $self->emit_log( " # depends on ", join(', ', @$deps), "\n" )
721 if $DEP_DEBUG;
722 }
723 }
724  
725 return $maxtime;
726 }
727  
728  
729 #------------------------------------------------------------------------
730 # read_config($file)
731 #
732 # Handles reading of config file and/or command line arguments.
733 #------------------------------------------------------------------------
734  
735 sub read_config {
736 my $self = shift;
737 my $file = shift;
738  
739 my $NAME = $self->_get_myname();
740 my $verbose = 0;
741 my $verbinc = sub {
742 my ($state, $var, $value) = @_;
743 $state->{ VARIABLE }->{ verbose } = $value ? ++$verbose : --$verbose;
744 };
745 my $config = AppConfig->new(
746 {
747 ERROR => sub { die(@_, "\ntry `$NAME --help'\n") }
748 },
749 'help|h' => { ACTION => sub { $self->help } },
750 'src|s=s' => { EXPAND => EXPAND_ALL },
751 'dest|d=s' => { EXPAND => EXPAND_ALL },
752 'lib|l=s@' => { EXPAND => EXPAND_ALL },
753 'cfg|c=s' => { EXPAND => EXPAND_ALL, DEFAULT => '.' },
754 'verbose|v' => { DEFAULT => 0, ACTION => $verbinc },
755 'recurse|r' => { DEFAULT => 0 },
756 'nothing|n' => { DEFAULT => 0 },
757 'preserve|p' => { DEFAULT => 0 },
758 'absolute' => { DEFAULT => 0 },
759 'relative' => { DEFAULT => 0 },
760 'colour|color'=> { DEFAULT => 0 },
761 'summary' => { DEFAULT => 0 },
762 'all|a' => { DEFAULT => 0 },
763 'define=s%',
764 'suffix=s%',
765 'binmode=s',
766 'ignore=s@',
767 'copy=s@',
768 'link=s@',
769 'accept=s@',
770 'depend=s%',
771 'depend_debug|depdbg',
772 'depend_file|depfile=s' => { EXPAND => EXPAND_ALL },
773 'copy_dir=s@',
774 'template_module|module=s',
775 'template_anycase|anycase',
776 'template_encoding|encoding=s',
777 'template_eval_perl|eval_perl',
778 'template_load_perl|load_perl',
779 'template_interpolate|interpolate',
780 'template_pre_chomp|pre_chomp|prechomp',
781 'template_post_chomp|post_chomp|postchomp',
782 'template_trim|trim',
783 'template_pre_process|pre_process|preprocess=s@',
784 'template_post_process|post_process|postprocess=s@',
785 'template_process|process=s',
786 'template_wrapper|wrapper=s',
787 'template_recursion|recursion',
788 'template_expose_blocks|expose_blocks',
789 'template_default|default=s',
790 'template_error|error=s',
791 'template_debug|debug=s',
792 'template_strict|strict',
793 'template_start_tag|start_tag|starttag=s',
794 'template_end_tag|end_tag|endtag=s',
795 'template_tag_style|tag_style|tagstyle=s',
796 'template_compile_ext|compile_ext=s',
797 'template_compile_dir|compile_dir=s' => { EXPAND => EXPAND_ALL },
798 'template_plugin_base|plugin_base|pluginbase=s@' => { EXPAND => EXPAND_ALL },
799 'perl5lib|perllib=s@' => { EXPAND => EXPAND_ALL },
800 );
801  
802 # add the 'file' option now that we have a $config object that we
803 # can reference in a closure
804 $config->define(
805 'file|f=s@' => {
806 EXPAND => EXPAND_ALL,
807 ACTION => sub {
808 my ($state, $item, $file) = @_;
809 $file = $state->cfg . "/$file"
810 unless $file =~ /^[\.\/]|(?:\w:)/;
811 $config->file($file) }
812 }
813 );
814  
815 # process main config file, then command line args
816 $config->file($file) if -f $file;
817 $config->args();
818  
819 $config;
820 }
821  
822  
823 sub ANSI_escape {
824 my $attr = shift;
825 my $text = join('', @_);
826 return join("\n",
827 map {
828 # look for an existing escape start sequence and add new
829 # attribute to it, otherwise add escape start/end sequences
830 s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx
831 ? $_
832 : "\e[${attr}m" . $_ . "\e[0m";
833 }
834 split(/\n/, $text, -1) # -1 prevents it from ignoring trailing fields
835 );
836 }
837  
838 sub _red(@) { ANSI_escape(31, @_) }
839 sub _green(@) { ANSI_escape(32, @_) }
840 sub _yellow(@) { ANSI_escape(33, @_) }
841 sub _blue(@) { ANSI_escape(34, @_) }
842 sub _white(@) { @_ } # nullop
843  
844  
845 #------------------------------------------------------------------------
846 # $self->write_config($file)
847 #
848 # Writes a sample configuration file to the filename specified.
849 #------------------------------------------------------------------------
850  
851 sub write_config {
852 my $self = shift;
853 my $file = shift;
854  
855 my $NAME = $self->_get_myname();
856  
857 open(CONFIG, ">", $file) || die "failed to create $file: $!\n";
858 print(CONFIG <<END_OF_CONFIG);
859 #------------------------------------------------------------------------
860 # sample .ttreerc file created automatically by $NAME version $VERSION
861 #
862 # This file originally written to $file
863 #
864 # For more information on the contents of this configuration file, see
865 #
866 # perldoc ttree
867 # ttree -h
868 #
869 #------------------------------------------------------------------------
870  
871 # The most flexible way to use ttree is to create a separate directory
872 # for configuration files and simply use the .ttreerc to tell ttree where
873 # it is.
874 #
875 # cfg = /path/to/ttree/config/directory
876  
877 # print summary of what's going on
878 verbose
879  
880 # recurse into any sub-directories and process files
881 recurse
882  
883 # regexen of things that aren't templates and should be ignored
884 ignore = \\b(CVS|RCS)\\b
885 ignore = ^#
886  
887 # ditto for things that should be copied rather than processed.
888 copy = \\.png\$
889 copy = \\.gif\$
890  
891 # ditto for things that should be linked rather than copied / processed.
892 # link = \\.flv\$
893  
894 # by default, everything not ignored or copied is accepted; add 'accept'
895 # lines if you want to filter further. e.g.
896 #
897 # accept = \\.html\$
898 # accept = \\.tt2\$
899  
900 # options to rewrite files suffixes (htm => html, tt2 => html)
901 #
902 # suffix htm=html
903 # suffix tt2=html
904  
905 # options to define dependencies between templates
906 #
907 # depend *=header,footer,menu
908 # depend index.html=mainpage,sidebar
909 # depend menu=menuitem,menubar
910 #
911  
912 #------------------------------------------------------------------------
913 # The following options usually relate to a particular project so
914 # you'll probably want to put them in a separate configuration file
915 # in the directory specified by the 'cfg' option and then invoke tree
916 # using '-f' to tell it which configuration you want to use.
917 # However, there's nothing to stop you from adding default 'src',
918 # 'dest' or 'lib' options in the .ttreerc. The 'src' and 'dest' options
919 # can be re-defined in another configuration file, but be aware that 'lib'
920 # options accumulate so any 'lib' options defined in the .ttreerc will
921 # be applied every time you run ttree.
922 #------------------------------------------------------------------------
923 # # directory containing source page templates
924 # src = /path/to/your/source/page/templates
925 #
926 # # directory where output files should be written
927 # dest = /path/to/your/html/output/directory
928 #
929 # # additional directories of library templates
930 # lib = /first/path/to/your/library/templates
931 # lib = /second/path/to/your/library/templates
932  
933 END_OF_CONFIG
934  
935 close(CONFIG);
936 $self->emit_log( "$file created. Please edit accordingly and re-run $NAME\n" );
937 }
938  
939  
940 #------------------------------------------------------------------------
941 # help()
942 #
943 # Prints help message and exits.
944 #------------------------------------------------------------------------
945  
946 sub help {
947 my $self = shift;
948 my $NAME = $self->_get_myname();
949 print<<END_OF_HELP;
950 $NAME $VERSION (Template Toolkit version $Template::VERSION)
951  
952 usage: $NAME [options] [files]
953  
954 Options:
955 -a (--all) Process all files, regardless of modification
956 -r (--recurse) Recurse into sub-directories
957 -p (--preserve) Preserve file ownership and permission
958 -n (--nothing) Do nothing, just print summary (enables -v)
959 -v (--verbose) Verbose mode. Use twice for more verbosity: -v -v
960 -h (--help) This help
961 -s DIR (--src=DIR) Source directory
962 -d DIR (--dest=DIR) Destination directory
963 -c DIR (--cfg=DIR) Location of configuration files
964 -l DIR (--lib=DIR) Library directory (INCLUDE_PATH) (multiple)
965 -f FILE (--file=FILE) Read named configuration file (multiple)
966  
967 Display options:
968 --colour / --color Enable colo(u)rful verbose output.
969 --summary Show processing summary.
970  
971 File search specifications (all may appear multiple times):
972 --ignore=REGEX Ignore files matching REGEX
973 --copy=REGEX Copy files matching REGEX
974 --link=REGEX Link files matching REGEX
975 --copy_dir=DIR Copy files in dir DIR (recursive)
976 --accept=REGEX Process only files matching REGEX
977  
978 File Dependencies Options:
979 --depend foo=bar,baz Specify that 'foo' depends on 'bar' and 'baz'.
980 --depend_file FILE Read file dependancies from FILE.
981 --depend_debug Enable debugging for dependencies
982  
983 File suffix rewriting (may appear multiple times)
984 --suffix old=new Change any '.old' suffix to '.new'
985  
986 File encoding options
987 --binmode=value Set binary mode of output files
988 --encoding=value Set encoding of input files
989  
990 Additional options to set Template Toolkit configuration items:
991 --define var=value Define template variable
992 --interpolate Interpolate '\$var' references in text
993 --anycase Accept directive keywords in any case.
994 --pre_chomp Chomp leading whitespace
995 --post_chomp Chomp trailing whitespace
996 --trim Trim blank lines around template blocks
997 --eval_perl Evaluate [% PERL %] ... [% END %] code blocks
998 --load_perl Load regular Perl modules via USE directive
999 --absolute Enable the ABSOLUTE option
1000 --relative Enable the RELATIVE option
1001 --pre_process=TEMPLATE Process TEMPLATE before each main template
1002 --post_process=TEMPLATE Process TEMPLATE after each main template
1003 --process=TEMPLATE Process TEMPLATE instead of main template
1004 --wrapper=TEMPLATE Process TEMPLATE wrapper around main template
1005 --default=TEMPLATE Use TEMPLATE as default
1006 --error=TEMPLATE Use TEMPLATE to handle errors
1007 --debug=STRING Set TT DEBUG option to STRING
1008 --start_tag=STRING STRING defines start of directive tag
1009 --end_tag=STRING STRING defined end of directive tag
1010 --tag_style=STYLE Use pre-defined tag STYLE
1011 --plugin_base=PACKAGE Base PACKAGE for plugins
1012 --compile_ext=STRING File extension for compiled template files
1013 --compile_dir=DIR Directory for compiled template files
1014 --perl5lib=DIR Specify additional Perl library directories
1015 --template_module=MODULE Specify alternate Template module
1016 --expose_blocks Make blocks visible as pseudo-files
1017  
1018  
1019  
1020 See 'perldoc ttree' for further information.
1021  
1022 END_OF_HELP
1023  
1024 exit(0);
1025 }
1026  
1027 1;
1028  
1029 __END__