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 < prev    next >
Encoding:
Text File  |  2004-03-20  |  35.0 KB  |  1,070 lines

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