Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TTSchema.pm
index 89f77ca..60cbf34 100644 (file)
@@ -1,9 +1,9 @@
 package SQL::Translator::Producer::TTSchema;
 
 # -------------------------------------------------------------------
-# $Id: TTSchema.pm,v 1.8 2004-11-16 21:06:35 grommit Exp $
+# $Id$
 # -------------------------------------------------------------------
-# Copyright (C) 2002-4 SQLFairy Authors
+# Copyright (C) 2002-2009 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -41,6 +41,11 @@ SQL::Translator::Producer::TTSchema -
           ttargs     => {
               author => "Mr Foo",
           },
+
+          # Template config options
+          ttargs     => {
+              INCLUDE_PATH => '/foo/templates',
+          },
       },
   );
   print $translator->translate;
@@ -70,19 +75,24 @@ Here's a brief example of what the template could look like:
 See F<t/data/template/basic.tt> for a more complete example.
 
 The template will also get the set of extra variables given as a hashref via the
-C<ttvars> producer arg.
+C<tt_vars> producer arg.
 
 You can set any of the options used to initiallize the Template object by
-adding them to your producer_args. See Template Toolkit docs for details of
+adding a tt_conf producer_arg. See Template Toolkit docs for details of
 the options.
+(Note that the old style of passing this config directly in the producer args
+has been deprecated).
+
 
   $translator          = SQL::Translator->new(
       to               => 'TT',
       producer_args    => {
           ttfile       => 'foo_template.tt',
           ttargs       => {},
-          INCLUDE_PATH => '/foo/templates/tt',
-          INTERPOLATE  => 1,
+          tt_conf      = {
+            INCLUDE_PATH => '/foo/templates/tt',
+            INTERPOLATE  => 1,
+          }
       },
   );
 
@@ -101,10 +111,15 @@ limitless!
 
 The template file to generate the output with.
 
-=item ttvars
+=item tt_vars
 
 A hash ref of extra variables you want to add to the template.
 
+=item tt_conf
+
+A hash ref of configuration options to pass to the L<Template> object's
+constructor.
+
 =back
 
 =cut
@@ -114,7 +129,7 @@ A hash ref of extra variables you want to add to the template.
 use strict;
 
 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.99';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Template;
@@ -131,8 +146,23 @@ sub produce {
     my $scma       = $translator->schema;
     my $args       = $translator->producer_args;
     my $file       = delete $args->{'ttfile'} or die "No template file!";
-    my $ttvars     = delete $args->{'ttargs'} || {};
-    # Any args left here get given to the Template object.
+
+    my $tt_vars  = delete $args->{'tt_vars'} || {};
+    if ( exists $args->{ttargs} ) {
+        warn "Use of 'ttargs' producer arg is deprecated."
+            ." Please use 'tt_vars' instead.\n";
+        %$tt_vars = { %{$args->{ttargs}}, %$tt_vars };
+    }
+
+    my %tt_conf = exists $args->{tt_conf} ? %{$args->{tt_conf}} : ();
+    # sqlt passes the producer args for _all_ producers in, so we use this
+    # grep hack to test for the old usage.
+    debug(Dumper(\%tt_conf));
+    if ( grep /^[A-Z_]+$/, keys %$args ) {
+        warn "Template config directly in the producer args is deprecated."
+            ." Please use 'tt_conf' instead.\n";
+        %tt_conf = ( %tt_conf, %$args );
+    }
 
     debug "Processing template $file\n";
     my $out;
@@ -140,14 +170,18 @@ sub produce {
         DEBUG    => $DEBUG,
         ABSOLUTE => 1, # Set so we can use from the command line sensibly
         RELATIVE => 1, # Maybe the cmd line code should set it! Security!
-        %$args,        # Allow any TT opts to be passed in the producer_args
-    ) || die "Failed to initialize Template object: ".Template->error;
+        %tt_conf,
+    );
+    debug("Template ERROR: " . Template->error. "\n") if(!$tt);
+    $tt || die "Failed to initialize Template object: ".Template->error;
 
-    $tt->process(
+    my $ttproc = $tt->process(
         $file,
-        { schema => $scma , %$ttvars },
+        { schema => $scma , %$tt_vars },
         \$out
-    ) or die "Error processing template '$file': ".$tt->error;
+    );
+    debug("ERROR: ". $tt->error. "\n") if(!$ttproc);
+    $ttproc or die "Error processing template '$file': ".$tt->error;
 
     return $out;
 };