home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / ttree.bat < prev    next >
Encoding:
DOS Batch File  |  2004-03-20  |  35.4 KB  |  1,086 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!/usr/bin/perl -w
  14. #line 15
  15. #========================================================================
  16. #
  17. # ttree
  18. #
  19. # DESCRIPTION
  20. #   Script for processing all directory trees containing templates.
  21. #   Template files are processed and the output directed to the 
  22. #   relvant file in an output tree.  The timestamps of the source and
  23. #   destination files can then be examined for future invocations 
  24. #   to process only those files that have changed.  In other words,
  25. #   it's a lot like 'make' for templates.
  26. #
  27. # AUTHOR
  28. #   Andy Wardley   <abw@wardley.org>
  29. #
  30. # COPYRIGHT
  31. #   Copyright (C) 1996-2003 Andy Wardley.  All Rights Reserved.
  32. #   Copyright (C) 1998-2003 Canon Research Centre Europe Ltd.
  33. #
  34. #   This module is free software; you can redistribute it and/or
  35. #   modify it under the same terms as Perl itself.
  36. #
  37. #------------------------------------------------------------------------
  38. #
  39. # $Id: ttree.bat,v 1.3 2004/03/20 23:00:29 joker Exp $
  40. #
  41. #========================================================================
  42.  
  43. use strict;
  44. use Template;
  45. use AppConfig qw( :expand );
  46. use File::Copy;
  47. use File::Path;
  48. use File::Spec;
  49. use File::Basename;
  50. use Text::ParseWords qw(quotewords);
  51.  
  52. my $NAME     = "ttree";
  53. my $VERSION  = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
  54. my $HOME     = $ENV{ HOME } || '';
  55. my $RCFILE   = $ENV{"\U${NAME}rc"} || "$HOME/.${NAME}rc";
  56. my $TTMODULE = 'Template';
  57.  
  58. #------------------------------------------------------------------------
  59. # configuration options
  60. #------------------------------------------------------------------------
  61.  
  62. # offer create a sample config file if it doesn't exist, unless a '-f'
  63. # has been specified on the command line
  64. unless (-f $RCFILE or grep(/^-f$/, @ARGV) ) {
  65.     print("Do you want me to create a sample '.ttreerc' file for you?\n",
  66.       "(file: $RCFILE)   [y/n]: ");
  67.     my $y = <STDIN>;
  68.     if ($y =~ /^y(es)?/i) {
  69.         write_config($RCFILE);
  70.         exit(0);
  71.     }
  72. }
  73.  
  74. # read configuration file and command line arguments - I need to remember 
  75. # to fix varlist() and varhash() in AppConfig to make this nicer...
  76. my $config   = read_config($RCFILE);
  77. my $dryrun   = $config->nothing;
  78. my $verbose  = $config->verbose || $dryrun;
  79. my $recurse  = $config->recurse;
  80. my $preserve = $config->preserve;
  81. my $all      = $config->all;
  82. my $libdir   = $config->lib;
  83. my $ignore   = $config->ignore;
  84. my $copy     = $config->copy;
  85. my $accept   = $config->accept;
  86. my $absolute = $config->absolute;
  87. my $relative = $config->relative;
  88. my $suffix   = $config->suffix;
  89. my $depends  = $config->depend;
  90. my $depsfile = $config->depend_file;
  91. my $srcdir   = $config->src
  92.     || die "Source directory not set (-s)\n";
  93. my $destdir  = $config->dest
  94.     || die "Destination directory not set (-d)\n";
  95. die "Source and destination directories may not be the same:\n  $srcdir\n"
  96.     if $srcdir eq $destdir;
  97.  
  98. # unshift any perl5lib directories onto front of INC
  99. unshift(@INC, @{ $config->perl5lib });
  100.  
  101. # get all template_* options from the config and fold keys to UPPER CASE
  102. my %ttopts   = $config->varlist('^template_', 1);
  103. my $ttmodule = delete($ttopts{ module });
  104. my $ucttopts = {
  105.     map { my $v = $ttopts{ $_ }; defined $v ? (uc $_, $v) : () }
  106.     keys %ttopts,
  107. };
  108.  
  109. # get all template variable definitions
  110. my $replace = $config->get('define');
  111.  
  112. # now create complete parameter hash for creating template processor
  113. my $ttopts   = {
  114.     %$ucttopts,
  115.     RELATIVE     => $relative,
  116.     ABSOLUTE     => $absolute,
  117.     INCLUDE_PATH => [ $srcdir, @$libdir ],
  118.     OUTPUT_PATH  => $destdir,
  119. };
  120.  
  121. # load custom template module 
  122. if ($ttmodule) {
  123.     my $ttpkg = $ttmodule;
  124.     $ttpkg =~ s[::][/]g;
  125.     $ttpkg .= '.pm';
  126.     require $ttpkg;
  127. }
  128. else {
  129.     $ttmodule = $TTMODULE;
  130. }
  131.  
  132.  
  133. #------------------------------------------------------------------------
  134. # inter-file dependencies
  135. #------------------------------------------------------------------------
  136.  
  137. if ($depsfile or $depends) {
  138.     $depends = dependencies($depsfile, $depends);
  139. else {
  140.     $depends = { };
  141. }
  142.  
  143. my $global_deps = $depends->{'*'} || [ ];
  144.  
  145. # add any PRE_PROCESS, etc., templates as global dependencies
  146. foreach my $ttopt (qw( PRE_PROCESS POST_PROCESS PROCESS WRAPPER )) {
  147.     my $deps = $ucttopts->{ $ttopt } || next;
  148.     my @deps = ref $deps eq 'ARRAY' ? (@$deps) : ($deps);
  149.     next unless @deps;
  150.     push(@$global_deps, @deps);
  151. }
  152.  
  153. # remove any duplicates
  154. $global_deps = { map { ($_ => 1) } @$global_deps };
  155. $global_deps = [ keys %$global_deps ];
  156.  
  157. # update $depends hash or delete it if there are no dependencies
  158. if (@$global_deps) {
  159.     $depends->{'*'} = $global_deps;
  160. }
  161. else {
  162.     delete $depends->{'*'};
  163.     $global_deps = undef;
  164. }
  165. $depends = undef
  166.     unless keys %$depends;
  167.  
  168. my $DEP_DEBUG = $config->depend_debug();
  169.  
  170.  
  171. #------------------------------------------------------------------------
  172. # pre-amble
  173. #------------------------------------------------------------------------
  174.  
  175. if ($verbose) {
  176.     local $" = ', ';
  177.  
  178.  
  179.     print "$NAME $VERSION (Template Toolkit version $Template::VERSION)\n\n";
  180.  
  181.     my $sfx = join(', ', map { "$_ => $suffix->{$_}" } keys %$suffix);
  182.  
  183.     print("      Source: $srcdir\n",
  184.           " Destination: $destdir\n",
  185.           "Include Path: [ @$libdir ]\n",
  186.           "      Ignore: [ @$ignore ]\n",
  187.           "        Copy: [ @$copy ]\n",
  188.           "      Accept: [ @$accept ]\n",
  189.           "      Suffix: [ $sfx ]\n");
  190.     print("      Module: $ttmodule ", $ttmodule->module_version(), "\n")
  191.         unless $ttmodule eq $TTMODULE;
  192.  
  193.     if ($depends && $DEP_DEBUG) {
  194.         print "Dependencies:\n";
  195.         foreach my $key ('*', grep { !/\*/ } keys %$depends) {
  196.             printf("    %-16s %s\n", $key, 
  197.                    join(', ', @{ $depends->{ $key } }));
  198.         }
  199.     }
  200.     print "\n";
  201.     print "NOTE: dry run, doing nothing...\n"
  202.         if $dryrun;
  203. }
  204.  
  205. #------------------------------------------------------------------------
  206. # main processing loop
  207. #------------------------------------------------------------------------
  208.  
  209. my $template = $ttmodule->new($ttopts)
  210.     || die $ttmodule->error();
  211.  
  212. if (@ARGV) {
  213.     # explicitly process files specified on command lines 
  214.     foreach my $file (@ARGV) {
  215.         process_file($file, $srcdir ? "$srcdir/$file" : $file, force => 1);
  216.     }
  217. }
  218. else {
  219.     # implicitly process all file in source directory
  220.     process_tree();
  221. }
  222.  
  223. exit(0);
  224.  
  225.  
  226. #========================================================================
  227. # END 
  228. #========================================================================
  229.  
  230.  
  231. #------------------------------------------------------------------------
  232. # process_tree($dir)
  233. #
  234. # Walks the directory tree starting at $dir or the current directory
  235. # if unspecified, processing files as found.
  236. #------------------------------------------------------------------------
  237.  
  238. sub process_tree {
  239.     my $dir = shift;
  240.     my ($file, $path, $abspath, $check);
  241.     my $target;
  242.     local *DIR;
  243.  
  244.     my $absdir = join('/', $srcdir ? $srcdir : (), $dir ? $dir : ());
  245.     $absdir ||= '.';
  246.  
  247.     opendir(DIR, $absdir) || do { warn "$absdir: $!\n"; return undef; };
  248.  
  249.     FILE: while (defined ($file = readdir(DIR))) {
  250.         next if $file eq '.' || $file eq '..';
  251.         $path = $dir ? "$dir/$file" : $file;
  252.         $abspath = "$absdir/$file";
  253.         
  254.         next unless -e $abspath;
  255.  
  256.         # check against ignore list
  257.         foreach $check (@$ignore) {
  258.             if ($path =~ /$check/) {
  259.                 printf "  - %-32s (ignored, matches /$check/)\n", $path
  260.                     if $verbose;
  261.                 next FILE;
  262.             }
  263.         }
  264.  
  265.         if (-d $abspath) {
  266.             if ($recurse) {
  267.                 my ($uid, $gid, $mode);
  268.                 
  269.                 (undef, undef, $mode, undef, $uid, $gid, undef, undef,
  270.                  undef, undef, undef, undef, undef)  = stat($abspath);
  271.                 
  272.                 # create target directory if required
  273.                 $target = "$destdir/$path";
  274.                 unless (-d $target || $dryrun) {
  275.                     mkdir $target, $mode || do { 
  276.                         warn "mkdir  ($target): $!\n";
  277.                         next;
  278.                     };
  279. # commented out by abw on 2000/12/04 - seems to raise a warning?   
  280. #           chown($uid, $gid, $target) || warn "chown($target): $!\n";
  281.                     printf "  + %-32s (created target directory)\n", $path
  282.                         if $verbose;
  283.                 }
  284.                 # recurse into directory
  285.                 process_tree($path);
  286.             }
  287.             else {
  288.                 printf "  - %-32s (directory, not recursing)\n", $path
  289.                     if $verbose;
  290.             }
  291.         }
  292.         else {
  293.             process_file($path, $abspath);
  294.         }
  295.     }
  296.     closedir(DIR);
  297. }
  298.     
  299.  
  300. #------------------------------------------------------------------------
  301. # process_file()
  302. #
  303. # File filtering and processing sub-routine called by process_tree()
  304. #------------------------------------------------------------------------
  305.  
  306. sub process_file {
  307.     my ($file, $absfile, %options) = @_;
  308.     my ($dest, $destfile, $filename, $check, 
  309.         $srctime, $desttime, $mode, $uid, $gid);
  310.     my ($old_suffix, $new_suffix);
  311.     my $is_dep = 0;
  312.     my $copy_file = 0;
  313.  
  314.     $absfile ||= $file;
  315.     $filename = basename($file);
  316.     $destfile = $file;
  317.     
  318.     # look for any relevant suffix mapping
  319.     if (%$suffix) {
  320.         if ($filename =~ m/\.(.+)$/) {
  321.             $old_suffix = $1;
  322.             if ($new_suffix = $suffix->{ $old_suffix }) {
  323.                 $destfile =~ s/$old_suffix$/$new_suffix/;
  324.             }
  325.         }
  326.     }
  327.     $dest = $destdir ? "$destdir/$destfile" : $destfile;
  328.                    
  329. #    print "proc $file => $dest\n";
  330.  
  331.  
  332.     
  333.     # check against copy list
  334.     foreach $check (@$copy) {
  335.         if ($filename =~ /$check/) {
  336.             $copy_file = 1;
  337.         }
  338.     }
  339.  
  340.     # stat the source file unconditionally, so we can preserve
  341.     # mode and ownership
  342.     (undef, undef, $mode, undef, $uid, $gid, undef, undef, undef, $srctime,
  343.      undef, undef, undef)  = stat($absfile);
  344.     
  345.     # test modification time of existing destination file
  346.     if (! $all && ! $options{ force } && -f $dest) {
  347.         $desttime = ( stat($dest) )[9];
  348.  
  349.         if (defined $depends and not $copy_file) {
  350.             my $deptime  = depend_time($file, $depends);
  351.             if (defined $deptime && ($srctime < $deptime)) {
  352.                 $srctime = $deptime;
  353.                 $is_dep = 1;
  354.             }
  355.         }
  356.     
  357.         if ($desttime >= $srctime) {
  358.             printf "  - %-32s (not modified)\n", $file
  359.                 if $verbose;
  360.             return;
  361.         }
  362.     }
  363.     
  364.     # check against copy list
  365.     if ($copy_file) {
  366.         printf "  > %-32s (copied, matches /$check/)\n", $file
  367.             if $verbose;
  368.  
  369.         unless ($dryrun) {
  370.             copy($absfile, $dest);
  371.  
  372.             if ($preserve) {
  373.                 chown($uid, $gid, $dest) || warn "chown($dest): $!\n";
  374.                 chmod($mode, $dest) || warn "chmod($dest): $!\n";
  375.             }
  376.         }
  377.         return;
  378.     }
  379.  
  380.     # check against acceptance list
  381.     if (@$accept) {
  382.         unless (grep { $filename =~ /$_/ } @$accept) {
  383.             printf "  - %-32s (not accepted)\n", $file
  384.                 if $verbose;
  385.             return;
  386.         }
  387.     }
  388.  
  389.     if ($verbose) {
  390.         printf("  + %-32s", $file);
  391.         print " (changed suffix to $new_suffix)" if $new_suffix;
  392.         print "\n";
  393.     }
  394.  
  395.     # process file
  396.     unless ($dryrun) {
  397.         $template->process($file, $replace, $destfile)
  398.             || print("  ! ", $template->error(), "\n");
  399.  
  400.         if ($preserve) {
  401.             chown($uid, $gid, $dest) || warn "chown($dest): $!\n";
  402.             chmod($mode, $dest) || warn "chmod($dest): $!\n";
  403.         }
  404.     }
  405. }
  406.  
  407.  
  408. #------------------------------------------------------------------------
  409. # dependencies($file, $depends)
  410. # Read the dependencies from $file, if defined, and merge in with 
  411. # those passed in as the hash array $depends, if defined.
  412. #------------------------------------------------------------------------
  413.  
  414. sub dependencies {
  415.     my ($file, $depend) = @_;
  416.     my %depends = ();
  417.  
  418.     if (defined $file) {
  419.         my ($fh, $text, $line);
  420.         open $fh, $file or die "Can't open $file, $!";
  421.         local $/ = undef;
  422.         $text = <$fh>;
  423.         close($fh);
  424.         $text =~ s[\\\n][]mg;
  425.         
  426.         foreach $line (split("\n", $text)) {
  427.             next if $line =~ /^\s*(#|$)/;
  428.             chomp $line;
  429.             my ($file, @files) = quotewords('\s*:\s*', 0, $line);
  430.             $file =~ s/^\s+//;
  431.             @files = grep(defined, quotewords('(,|\s)\s*', 0, @files));
  432.             $depends{$file} = \@files;
  433.         }
  434.     }
  435.  
  436.     if (defined $depend) {
  437.         foreach my $key (keys %$depend) {
  438.             $depends{$key} = [ quotewords(',', 0, $depend->{$key}) ];
  439.         }
  440.     }
  441.  
  442.     return \%depends;
  443. }
  444.  
  445.  
  446.  
  447. #------------------------------------------------------------------------
  448. # depend_time($file, \%depends)
  449. #
  450. # Returns the mtime of the most recent in @files.
  451. #------------------------------------------------------------------------
  452.  
  453. sub depend_time {
  454.     my ($file, $depends) = @_;
  455.     my ($deps, $absfile, $modtime);
  456.     my $maxtime = 0;
  457.     my @pending = ($file);
  458.     my @files;
  459.     my %seen;
  460.  
  461.     # push any global dependencies onto the pending list
  462.     if ($deps = $depends->{'*'}) {
  463.         push(@pending, @$deps);
  464.     }
  465.  
  466.     print "    # checking dependencies for $file...\n"
  467.         if $DEP_DEBUG;
  468.  
  469.     # iterate through the list of pending files
  470.     while (@pending) {
  471.         $file = shift @pending;
  472.         next if $seen{ $file }++;
  473.  
  474.         if (File::Spec->file_name_is_absolute($file) && -f $file) {
  475.             $modtime = (stat($file))[9];
  476.             print "    #   $file [$modtime]\n"
  477.                 if $DEP_DEBUG;
  478.         }
  479.         else {
  480.             $modtime = 0;
  481.             foreach my $dir ($srcdir, @$libdir) {
  482.                 $absfile = File::Spec->catfile($dir, $file);
  483.                 if (-f $absfile) {
  484.                     $modtime = (stat($absfile))[9];
  485.                     print "    #   $absfile [$modtime]\n"
  486.                         if $DEP_DEBUG;
  487.                     last;
  488.                 }
  489.             }
  490.         }
  491.         $maxtime = $modtime
  492.             if $modtime > $maxtime;
  493.  
  494.         if ($deps = $depends->{ $file }) {
  495.             push(@pending, @$deps);
  496.             print "    #     depends on ", join(', ', @$deps), "\n"
  497.                 if $DEP_DEBUG;
  498.         }
  499.     }
  500.  
  501.     return $maxtime;
  502. }
  503.  
  504.  
  505. #------------------------------------------------------------------------
  506. # read_config($file)
  507. #
  508. # Handles reading of config file and/or command line arguments.
  509. #------------------------------------------------------------------------
  510.  
  511. sub read_config {
  512.     my $file = shift;
  513.  
  514.     my $config = AppConfig->new(
  515.         { 
  516.             ERROR  => sub { die(@_, "\ntry `$NAME --help'\n") }
  517.         }, 
  518.         'help|h'      => { ACTION => \&help },
  519.         'src|s=s'     => { EXPAND => EXPAND_ALL },
  520.         'dest|d=s'    => { EXPAND => EXPAND_ALL },
  521.         'lib|l=s@'    => { EXPAND => EXPAND_ALL },
  522.         'cfg|c=s'     => { EXPAND => EXPAND_ALL, DEFAULT => '.' },
  523.         'verbose|v'   => { DEFAULT => 0 },
  524.         'recurse|r'   => { DEFAULT => 0 },
  525.         'nothing|n'   => { DEFAULT => 0 },
  526.         'preserve|p'  => { DEFAULT => 0 },
  527.         'absolute'    => { DEFAULT => 0 },
  528.         'relative'    => { DEFAULT => 0 },
  529.         'all|a'       => { DEFAULT => 0 },
  530.         'define=s%',
  531.         'suffix=s%',
  532.         'ignore=s@',
  533.         'copy=s@',
  534.         'accept=s@',
  535.         'depend=s%',
  536.         'depend_file|depfile=s',
  537.         'depend_debug|depdbg',
  538.         'template_module|module=s',
  539.         'template_anycase|anycase',
  540.         'template_eval_perl|eval_perl',
  541.         'template_load_perl|load_perl',
  542.         'template_interpolate|interpolate',
  543.         'template_pre_chomp|pre_chomp|prechomp',
  544.         'template_post_chomp|post_chomp|postchomp',
  545.         'template_trim|trim',
  546.         'template_pre_process|pre_process|preprocess=s@',
  547.         'template_post_process|post_process|postprocess=s@',
  548.         'template_process|process=s',
  549.         'template_wrapper|wrapper=s',
  550.         'template_recursion|recursion',
  551.         'template_expose_blocks|expose_blocks',
  552.         'template_default|default=s',
  553.         'template_error|error=s',
  554.         'template_debug|debug=s',
  555.         'template_start_tag|start_tag|starttag=s',
  556.         'template_end_tag|end_tag|endtag=s',
  557.         'template_tag_style|tag_style|tagstyle=s',
  558.         'template_compile_ext|compile_ext=s',
  559.         'template_compile_dir|compile_dir=s',
  560.         'template_plugin_base|plugin_base|pluginbase=s@',
  561.         'perl5lib|perllib=s@'
  562.     );
  563.  
  564.     # add the 'file' option now that we have a $config object that we 
  565.     # can reference in a closure
  566.     $config->define(
  567.         'file|f=s@' => { 
  568.             EXPAND => EXPAND_ALL, 
  569.             ACTION => sub { 
  570.                 my ($state, $item, $file) = @_;
  571.                 $file = $state->cfg . "/$file" 
  572.                     unless $file =~ /^[\.\/]|(?:\w:)/;
  573.                 $config->file($file) }  
  574.         }
  575.     );
  576.  
  577.     # process main config file, then command line args
  578.     $config->file($file) if -f $file;
  579.     $config->args();
  580.  
  581.     $config;
  582. }
  583.  
  584.  
  585. #------------------------------------------------------------------------
  586. # write_config($file)
  587. #
  588. # Writes a sample configuration file to the filename specified.
  589. #------------------------------------------------------------------------
  590.  
  591. sub write_config {
  592.     my $file = shift;
  593.  
  594.     open(CONFIG, ">$file") || die "failed to create $file: $!\n";
  595.     print(CONFIG <<END_OF_CONFIG);
  596. #------------------------------------------------------------------------
  597. # sample .ttreerc file created automatically by $NAME version $VERSION
  598. #
  599. # This file originally written to $file
  600. #
  601. # For more information on the contents of this configuration file, see
  602. #     perldoc ttree
  603. #     ttree -h
  604. #
  605. #------------------------------------------------------------------------
  606.  
  607. # The most flexible way to use ttree is to create a separate directory 
  608. # for configuration files and simply use the .ttreerc to tell ttree where
  609. # it is.  
  610. #
  611. #     cfg = /path/to/ttree/config/directory
  612.  
  613. # print summary of what's going on 
  614. verbose 
  615.  
  616. # recurse into any sub-directories and process files
  617. recurse
  618.  
  619. # regexen of things that aren't templates and should be ignored
  620. ignore = \\b(CVS|RCS)\\b
  621. ignore = ^#
  622.  
  623. # ditto for things that should be copied rather than processed.
  624. copy = \\.png\$ 
  625. copy = \\.gif\$ 
  626.  
  627. # by default, everything not ignored or copied is accepted; add 'accept'
  628. # lines if you want to filter further. e.g.
  629. #
  630. #    accept = \\.html\$
  631. #    accept = \\.tt2\$
  632.  
  633. # options to rewrite files suffixes (htm => html, tt2 => html)
  634. #
  635. #    suffix htm=html
  636. #    suffix tt2=html
  637.  
  638. # options to define dependencies between templates
  639. #
  640. #    depend *=header,footer,menu
  641. #    depend index.html=mainpage,sidebar
  642. #    depend menu=menuitem,menubar
  643.  
  644. #------------------------------------------------------------------------
  645. # The following options usually relate to a particular project so 
  646. # you'll probably want to put them in a separate configuration file 
  647. # in the directory specified by the 'cfg' option and then invoke tree 
  648. # using '-f' to tell it which configuration you want to use.
  649. # However, there's nothing to stop you from adding default 'src',
  650. # 'dest' or 'lib' options in the .ttreerc.  The 'src' and 'dest' options
  651. # can be re-defined in another configuration file, but be aware that 'lib' 
  652. # options accumulate so any 'lib' options defined in the .ttreerc will
  653. # be applied every time you run ttree.
  654. #------------------------------------------------------------------------
  655. # # directory containing source page templates
  656. # src = /path/to/your/source/page/templates
  657. #
  658. # # directory where output files should be written
  659. # dest = /path/to/your/html/output/directory
  660. # # additional directories of library templates
  661. # lib = /first/path/to/your/library/templates
  662. # lib = /second/path/to/your/library/templates
  663.  
  664. END_OF_CONFIG
  665.  
  666.     close(CONFIG);
  667.     print "$file created.  Please edit accordingly and re-run $NAME\n"; 
  668. }
  669.  
  670.  
  671. #------------------------------------------------------------------------
  672. # help()
  673. #
  674. # Prints help message and exits.
  675. #------------------------------------------------------------------------
  676.  
  677. sub help {
  678.     print<<END_OF_HELP;
  679. $NAME $VERSION (Template Toolkit version $Template::VERSION)
  680.  
  681. usage: $NAME [options] [files]
  682.  
  683. Options:
  684.    -a      (--all)          Process all files, regardless of modification
  685.    -r      (--recurse)      Recurse into sub-directories
  686.    -p      (--preserve)     Preserve file ownership and permission
  687.    -n      (--nothing)      Do nothing, just print summary (enables -v)
  688.    -v      (--verbose)      Verbose mode
  689.    -h      (--help)         This help
  690.    -s DIR  (--src=DIR)      Source directory
  691.    -d DIR  (--dest=DIR)     Destination directory
  692.    -c DIR  (--cfg=DIR)      Location of configuration files
  693.    -l DIR  (--lib=DIR)      Library directory (INCLUDE_PATH)  (multiple)
  694.    -f FILE (--file=FILE)    Read named configuration file     (multiple)
  695.  
  696. File search specifications (all may appear multiple times):
  697.    --ignore=REGEX           Ignore files matching REGEX
  698.    --copy=REGEX             Copy files matching REGEX
  699.    --accept=REGEX           Process only files matching REGEX 
  700.  
  701. File Dependencies Options:
  702.    --depend foo=bar,baz     Specify that 'foo' depends on 'bar' and 'baz'.
  703.    --depend_file FILE       Read file dependancies from FILE.
  704.    --depend_debug           Enable debugging for dependencies
  705.  
  706.  
  707. File suffix rewriting (may appear multiple times)
  708.    --suffix old=new         Change any '.old' suffix to '.new'
  709.  
  710. Additional options to set Template Toolkit configuration items:
  711.    --define var=value       Define template variable
  712.    --interpolate            Interpolate '\$var' references in text
  713.    --anycase                Accept directive keywords in any case.
  714.    --pre_chomp              Chomp leading whitespace 
  715.    --post_chomp             Chomp trailing whitespace
  716.    --trim                   Trim blank lines around template blocks
  717.    --eval_perl              Evaluate [% PERL %] ... [% END %] code blocks
  718.    --load_perl              Load regular Perl modules via USE directive
  719.    --absolute               Enable the ABSOLUTE option
  720.    --relative               Enable the RELATIVE option
  721.    --pre_process=TEMPLATE   Process TEMPLATE before each main template
  722.    --post_process=TEMPLATE  Process TEMPLATE after each main template
  723.    --process=TEMPLATE       Process TEMPLATE instead of main template
  724.    --wrapper=TEMPLATE       Process TEMPLATE wrapper around main template
  725.    --default=TEMPLATE       Use TEMPLATE as default
  726.    --error=TEMPLATE         Use TEMPLATE to handle errors
  727.    --debug=STRING           Set TT DEBUG option to STRING
  728.    --start_tag=STRING       STRING defines start of directive tag
  729.    --end_tag=STRING         STRING defined end of directive tag
  730.    --tag_style=STYLE        Use pre-defined tag STYLE    
  731.    --plugin_base=PACKAGE    Base PACKAGE for plugins            
  732.    --compile_ext=STRING     File extension for compiled template files
  733.    --compile_dir=DIR        Directory for compiled template files
  734.    --perl5lib=DIR           Specify additional Perl library directories
  735.    --template_module=MODULE Specify alternate Template module
  736.  
  737. See 'perldoc ttree' for further information.  
  738.  
  739. END_OF_HELP
  740.  
  741.     exit(0);
  742. }
  743.  
  744. __END__
  745.  
  746.  
  747. #------------------------------------------------------------------------
  748. # IMPORTANT NOTE
  749. #   This documentation is generated automatically from source
  750. #   templates.  Any changes you make here may be lost.
  751. #   The 'docsrc' documentation source bundle is available for download
  752. #   from http://www.template-toolkit.org/docs.html and contains all
  753. #   the source templates, XML files, scripts, etc., from which the
  754. #   documentation for the Template Toolkit is built.
  755. #------------------------------------------------------------------------
  756.  
  757. =head1 NAME
  758.  
  759. Template::Tools::ttree - Process entire directory trees of templates
  760.  
  761. =head1 SYNOPSIS
  762.  
  763.     ttree [options] [files]
  764.  
  765. =head1 DESCRIPTION
  766.  
  767. The F<ttree> script is used to process entire directory trees containing
  768. template files.  The resulting output from processing each file is then 
  769. written to a corresponding file in a destination directory.  The script
  770. compares the modification times of source and destination files (where
  771. they already exist) and processes only those files that have been modified.
  772. In other words, it is the equivalent of 'make' for the Template Toolkit.
  773.  
  774. It supports a number of options which can be used to configure
  775. behaviour, define locations and set Template Toolkit options.  The
  776. script first reads the F<.ttreerc> configuration file in the HOME
  777. directory, or an alternative file specified in the TTREERC environment
  778. variable.  Then, it processes any command line arguments, including
  779. any additional configuration files specified via the C<-f> (file)
  780. option.
  781.  
  782. =head2 The F<.ttreerc> Configuration File
  783.  
  784. When you run F<ttree> for the first time it will ask you if you want
  785. it to create a F<.ttreerc> file for you.  This will be created in your
  786. home directory.
  787.  
  788.     $ ttree
  789.     Do you want me to create a sample '.ttreerc' file for you?
  790.     (file: /home/abw/.ttreerc)   [y/n]: y
  791.     /home/abw/.ttreerc created.  Please edit accordingly and re-run ttree
  792.  
  793. The purpose of this file is to set any I<global> configuration options
  794. that you want applied I<every> time F<ttree> is run.  For example, you
  795. can use the C<ignore> and C<copy> option to provide regular expressions
  796. that specify which files should be ignored and which should be copied 
  797. rather than being processed as templates.  You may also want to set 
  798. flags like C<verbose> and C<recurse> according to your preference.
  799.  
  800. A minimal F<.ttreerc>:
  801.  
  802.     # ignore these files
  803.     ignore = \b(CVS|RCS)\b
  804.     ignore = ^#
  805.     ignore = ~$
  806.  
  807.     # copy these files
  808.     copy   = \.(gif|png|jpg|pdf)$ 
  809.  
  810.     # recurse into directories
  811.     recurse
  812.  
  813.     # provide info about what's going on
  814.     verbose
  815.  
  816. In most cases, you'll want to create a different F<ttree> configuration 
  817. file for each project you're working on.  The C<cfg> option allows you
  818. to specify a directory where F<ttree> can find further configuration 
  819. files.
  820.  
  821.     cfg = /home/abw/.ttree
  822.  
  823. The C<-f> command line option can be used to specify which configuration
  824. file should be used.  You can specify a filename using an absolute or 
  825. relative path:
  826.  
  827.     $ ttree -f /home/abw/web/example/etc/ttree.cfg
  828.     $ ttree -f ./etc/ttree.cfg
  829.     $ ttree -f ../etc/ttree.cfg
  830.  
  831. If the configuration file does not begin with C</> or C<.> or something
  832. that looks like a MS-DOS absolute path (e.g. C<C:\\etc\\ttree.cfg>) then
  833. F<ttree> will look for it in the directory specified by the C<cfg> option.
  834.  
  835.     $ ttree -f test1          # /home/abw/.ttree/test1
  836.  
  837. The C<cfg> option can only be used in the F<.ttreerc> file.  All the
  838. other options can be used in the F<.ttreerc> or any other F<ttree>
  839. configuration file.  They can all also be specified as command line
  840. options.
  841.  
  842. Remember that F<.ttreerc> is always processed I<before> any
  843. configuration file specified with the C<-f> option.  Certain options
  844. like C<lib> can be used any number of times and accumulate their values.
  845.  
  846. For example, consider the following configuration files:
  847.  
  848. F</home/abw/.ttreerc>:
  849.  
  850.     cfg = /home/abw/.ttree
  851.     lib = /usr/local/tt2/templates
  852.  
  853. F</home/abw/.ttree/myconfig>:
  854.  
  855.     lib = /home/abw/web/example/templates/lib
  856.  
  857. When F<ttree> is invoked as follows:
  858.  
  859.     $ ttree -f myconfig
  860.  
  861. the C<lib> option will be set to the following directories:
  862.  
  863.     /usr/local/tt2/templates
  864.     /home/abw/web/example/templates/lib
  865.  
  866. Any templates located under F</usr/local/tt2/templates> will be used
  867. in preference to those located under
  868. F</home/abw/web/example/templates/lib>.  This may be what you want,
  869. but then again, it might not.  For this reason, it is good practice to
  870. keep the F<.ttreerc> as simple as possible and use different
  871. configuration files for each F<ttree> project.
  872.  
  873. =head2 Directory Options
  874.  
  875. The C<src> option is used to define the directory containing the
  876. source templates to be processed.  It can be provided as a command
  877. line option or in a configuration file as shown here:
  878.  
  879.     src = /home/abw/web/example/templates/src
  880.  
  881. Each template in this directory typically corresponds to a single
  882. web page or other document. 
  883.  
  884. The C<dest> option is used to specify the destination directory for the
  885. generated output.
  886.  
  887.     dest = /home/abw/web/example/html
  888.  
  889. The C<lib> option is used to define one or more directories containing
  890. additional library templates.  These templates are not documents in
  891. their own right and typically comprise of smaller, modular components
  892. like headers, footers and menus that are incorporated into pages templates.
  893.  
  894.     lib = /home/abw/web/example/templates/lib
  895.     lib = /usr/local/tt2/templates
  896.  
  897. The C<lib> option can be used repeatedly to add further directories to
  898. the search path.
  899.  
  900. A list of templates can be passed to F<ttree> as command line arguments.
  901.  
  902.     $ ttree foo.html bar.html
  903.  
  904. It looks for these templates in the C<src> directory and processes them
  905. through the Template Toolkit, using any additional template components
  906. from the C<lib> directories.  The generated output is then written to 
  907. the corresponding file in the C<dest> directory.
  908.  
  909. If F<ttree> is invoked without explicitly specifying any templates
  910. to be processed then it will process every file in the C<src> directory.
  911. If the C<-r> (recurse) option is set then it will additionally iterate
  912. down through sub-directories and process and other template files it finds
  913. therein.
  914.  
  915.     $ ttree -r
  916.  
  917. If a template has been processed previously, F<ttree> will compare the
  918. modification times of the source and destination files.  If the source
  919. template (or one it is dependant on) has not been modified more
  920. recently than the generated output file then F<ttree> will not process
  921. it.  The F<-a> (all) option can be used to force F<ttree> to process
  922. all files regardless of modification time.
  923.  
  924.     $ tree -a
  925.  
  926. Any templates explicitly named as command line argument are always
  927. processed and the modification time checking is bypassed.
  928.  
  929. =head2 File Options
  930.  
  931. The C<ignore>, C<copy> and C<accept> options are used to specify Perl
  932. regexen to filter file names.  Files that match any of the C<ignore>
  933. options will not be processed.  Remaining files that match any of the
  934. C<copy> regexen will be copied to the destination directory.  Remaining
  935. files that then match any of the C<accept> criteria are then processed
  936. via the Template Toolkit.  If no C<accept> parameter is specified then 
  937. all files will be accepted for processing if not already copied or
  938. ignored.
  939.  
  940.     # ignore these files
  941.     ignore = \b(CVS|RCS)\b
  942.     ignore = ^#
  943.     ignore = ~$
  944.  
  945.     # copy these files
  946.     copy   = \.(gif|png|jpg|pdf)$ 
  947.  
  948.     # accept only .tt2 templates
  949.     accept = \.tt2$
  950.  
  951. The C<suffix> option is used to define mappings between the file
  952. extensions for source templates and the generated output files.  The
  953. following example specifies that source templates with a C<.tt2>
  954. suffix should be output as C<.html> files:
  955.  
  956.     suffix tt2=html
  957.  
  958. Or on the command line, 
  959.  
  960.     --suffix tt2=html
  961.  
  962. You can provide any number of different suffix mappings by repeating 
  963. this option.
  964.  
  965. =head2 Template Dependencies
  966.  
  967. The C<depend> and C<depend_file> options allow you to specify
  968. how any given template file depends on another file or group of files. 
  969. The C<depend> option is used to express a single dependency.
  970.  
  971.   $ ttree --depend foo=bar,baz
  972.  
  973. This command line example shows the C<--depend> option being used to
  974. specify that the F<foo> file is dependant on the F<bar> and F<baz>
  975. templates.  This option can be used many time on the command line:
  976.  
  977.   $ ttree --depend foo=bar,baz --depend crash=bang,wallop
  978.  
  979. or in a configuration file:
  980.  
  981.   depend foo=bar,baz
  982.   depend crash=bang,wallop
  983.  
  984. The file appearing on the left of the C<=> is specified relative to
  985. the C<src> or C<lib> directories.  The file(s) appearing on the right
  986. can be specified relative to any of these directories or as absolute
  987. file paths.
  988.  
  989. For example:
  990.  
  991.   $ ttree --depend foo=bar,/tmp/baz
  992.  
  993. To define a dependency that applies to all files, use C<*> on the 
  994. left of the C<=>.
  995.  
  996.   $ ttree --depend *=header,footer
  997.  
  998. or in a configuration file:
  999.  
  1000.   depend *=header,footer
  1001.  
  1002. Any templates that are defined in the C<pre_process>, C<post_process>,
  1003. C<process> or C<wrapper> options will automatically be added to the
  1004. list of global dependencies that apply to all templates.
  1005.  
  1006. The C<depend_file> option can be used to specify a file that contains
  1007. dependency information.  
  1008.  
  1009.     $ ttree --depend_file=/home/abw/web/example/etc/ttree.dep
  1010.  
  1011. Here is an example of a dependency file:
  1012.  
  1013.    # This is a comment. It is ignored.
  1014.   
  1015.    index.html: header footer menubar 
  1016.   
  1017.    header: titlebar hotlinks
  1018.   
  1019.    menubar: menuitem
  1020.   
  1021.    # spanning multiple lines with the backslash
  1022.    another.html: header footer menubar \
  1023.    sidebar searchform
  1024.  
  1025. Lines beginning with the C<#> character are comments and are ignored.
  1026. Blank lines are also ignored.  All other lines should provide a
  1027. filename followed by a colon and then a list of dependant files
  1028. separated by whitespace, commas or both.  Whitespace around the colon
  1029. is also optional.  Lines ending in the C<\> character are continued
  1030. onto the following line.
  1031.  
  1032. Files that contain spaces can be quoted. That is only necessary
  1033. for files after the colon (':'). The file before the colon may be
  1034. quoted if it contains a colon. 
  1035.  
  1036. As with the command line options, the C<*> character can be used
  1037. as a wildcard to specify a dependency for all templates.
  1038.  
  1039.     * : config,header
  1040.  
  1041. =head2 Template Toolkit Options
  1042.  
  1043. F<ttree> also provides access to the usual range of Template Toolkit
  1044. options.  For example, the C<--pre_chomp> and C<--post_chomp> F<ttree>
  1045. options correspond to the C<PRE_CHOMP> and C<POST_CHOMP> options.
  1046.  
  1047. Run C<ttree -h> for a summary of the options available.
  1048.  
  1049. =head1 AUTHORS
  1050.  
  1051. Andy Wardley E<lt>abw@andywardley.comE<gt>
  1052.  
  1053. L<http://www.andywardley.com/|http://www.andywardley.com/>
  1054.  
  1055. With contributions from Dylan William Hardison (support for
  1056. dependencies), Bryce Harrington (C<absolute> and C<relative> options),
  1057. Mark Anderson (C<suffix> and C<debug> options), Harald Joerg and Leon
  1058. Brocard who gets everywhere, it seems.
  1059.  
  1060. =head1 VERSION
  1061.  
  1062. 2.69, distributed as part of the
  1063. Template Toolkit version 2.13, released on 30 January 2004.
  1064.  
  1065. =head1 COPYRIGHT
  1066.  
  1067.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  1068.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  1069.  
  1070. This module is free software; you can redistribute it and/or
  1071. modify it under the same terms as Perl itself.
  1072.  
  1073. =head1 SEE ALSO
  1074.  
  1075. L<tpage|Template::Tools::tpage>
  1076.  
  1077.  
  1078. __END__
  1079. :endofperl
  1080.