From 33e04aab6332f5db17fce1ebbbf2f3462ff41e1c Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 29 Sep 2011 10:10:25 -0500 Subject: [PATCH 1/6] Make $name less global The appname is passed to several subs, but others rely on the globally scoped $name declared near the top of the program. So, make those others take $name as a parameter. --- script/dancer | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/script/dancer b/script/dancer index b26affb10..b4f604182 100755 --- a/script/dancer +++ b/script/dancer @@ -20,7 +20,7 @@ my $path = '.'; sub templates($); sub app_tree($); -sub create_node($;$); +sub create_node($;$$); GetOptions( "h|help" => \$help, @@ -52,7 +52,7 @@ my $DANCER_VERSION = $Dancer::VERSION; version_check() if $do_check_dancer_version; safe_mkdir($DANCER_APP_DIR); -create_node( app_tree($name), $DANCER_APP_DIR ); +create_node( app_tree($name), $DANCER_APP_DIR, $name ); unless (eval "require YAML") { print < 'MANIFEST'); @@ -123,12 +123,12 @@ sub create_node($;$) { }; $add_to_manifest->($manifest_name); - _create_node($add_to_manifest, $node, $root); + _create_node($add_to_manifest, $node, $root, $name); close $manifest; } sub _create_node { - my ($add_to_manifest, $node, $root) = @_; + my ($add_to_manifest, $node, $root, $name) = @_; my $templates = templates($name); @@ -137,7 +137,7 @@ sub _create_node { if (ref($content) eq 'HASH') { safe_mkdir($path); - _create_node($add_to_manifest, $content, $path); + _create_node($add_to_manifest, $content, $path, $name); } elsif (ref($content) eq 'CODE') { # The content is a coderef, which, given the path to the file it # should create, will do the appropriate thing: From 997688f4f83b6b675fee5f7e9130a6226a528de2 Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 29 Sep 2011 10:19:42 -0500 Subject: [PATCH 2/6] Make "meta-template" indicators more obnoxious --- script/dancer | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/script/dancer b/script/dancer index b4f604182..4f033a41c 100755 --- a/script/dancer +++ b/script/dancer @@ -241,8 +241,8 @@ sub write_file { sub process_template { my ($template, $tokens) = @_; my $engine = Dancer::Template::Simple->new; - $engine->{start_tag} = '[%'; - $engine->{stop_tag} = '%]'; + $engine->{start_tag} = '[[%%'; + $engine->{stop_tag} = '%%]]'; return $engine->render(\$template, $tokens); } @@ -335,7 +335,7 @@ WriteMakefile( PREREQ_PM => { 'Test::More' => 0, 'YAML' => 0, - 'Dancer' => [% dancer_version %], + 'Dancer' => [[%% dancer_version %%]], }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => '$cleanfiles-*' }, @@ -385,7 +385,7 @@ WriteMakefile(

Your application\'s environment

    -
  • Location: [% appdir %]
  • +
  • Location: [[%% appdir %%]]
  • Template engine: <% settings.template %>
  • Logger: <% settings.logger %>
  • Environment: <% settings.environment %>
  • @@ -425,7 +425,7 @@ WriteMakefile( Appdir - [% appdir %] + [[%% appdir %%]] Template engine From e9f740433f0bc38aefaa439496492e4198a64ed7 Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 29 Sep 2011 10:20:05 -0500 Subject: [PATCH 3/6] Use local $appname rather than global $name --- script/dancer | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/dancer b/script/dancer index 4f033a41c..d500ec385 100755 --- a/script/dancer +++ b/script/dancer @@ -894,7 +894,7 @@ EOH # all the settings in this file will be loaded at Dancer's startup. # Your application's name -appname: \"$name\" +appname: \"$appname\" # The default layout to use for your application (located in # views/layouts/main.tt) From 4a12de18e31eb3894e88aba5e327e0f12195fe98 Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 29 Sep 2011 10:36:19 -0500 Subject: [PATCH 4/6] Add simple templating ability for creating new apps Allow for a template to be specified on the command line with the -t or --template option. These templates are subdirectories within the $HOME/.dancer_templates directory. The files in the template dir are copied to the application dir with some simple substitutions performed: filenames have APPNAME replaced by the actual application name, within files [[%% appname %%]] and [[%% appdir %%]] are replaced with the appropriate values for the Actual application. (The delimiters there are intentionally "big" so That they don't conflict with whatever template delimiters may be used by the templated files themselves (within views for instance)) Also, the file mode of the template files determines the file mode used for the corresponding file in the application directory. --- script/dancer | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/script/dancer b/script/dancer index d500ec385..17bdd73bd 100755 --- a/script/dancer +++ b/script/dancer @@ -4,6 +4,7 @@ use strict; use warnings; use Dancer::Template::Simple; use File::Basename 'basename', 'dirname'; +use File::Find; use File::Path 'mkpath'; use File::Spec::Functions; use Getopt::Long; @@ -12,11 +13,14 @@ use Dancer::Renderer; use LWP::UserAgent; use constant FILE => 1; +my $TEMPLATES_DIR = "$ENV{HOME}/.dancer_templates"; + # options my $help = 0; my $do_check_dancer_version = 1; my $name = undef; my $path = '.'; +my $template_name; sub templates($); sub app_tree($); @@ -26,6 +30,7 @@ GetOptions( "h|help" => \$help, "a|application=s" => \$name, "p|path=s" => \$path, + "t|template=s" => \$template_name, "x|no-check" => sub { $do_check_dancer_version = 0 }, "v|version" => \&version, ) or pod2usage( -verbose => 1 ); @@ -52,7 +57,11 @@ my $DANCER_VERSION = $Dancer::VERSION; version_check() if $do_check_dancer_version; safe_mkdir($DANCER_APP_DIR); -create_node( app_tree($name), $DANCER_APP_DIR, $name ); +if (defined $template_name) { + create_app_from_template( $template_name, $DANCER_APP_DIR, $name ); +} else { + create_node( app_tree($name), $DANCER_APP_DIR, $name ); +} unless (eval "require YAML") { print < 1, wanted => sub { + return if $File::Find::name eq $template_base; + (my $path = $File::Find::name) =~ s{^$template_base/}{}; + $path =~ s/APPNAME/$appname/g; + my $appfile = catfile($appdir, $path); + if (-d $File::Find::name) { safe_mkdir($appfile); return } + my $template = do { local (@ARGV,$/) = $File::Find::name; <> }; + write_file($appfile, $template, { + appdir => File::Spec->rel2abs($appdir), + appname => $appname, + }); + # Match the mode of the template file + chmod +(stat($File::Find::name))[2], $appfile; + }}, $template_base); +} + sub validate_app_name { my $name = shift; if ($name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) { From 2ae161a2c2d4a51ea7492b43effa6ca383076991 Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 29 Sep 2011 11:33:00 -0500 Subject: [PATCH 5/6] Application templates update * Change the template dir to be ~/.dancer/templates * If the argument to -t starts with a /, it's assumed to be the full path to a template directory --- script/dancer | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/script/dancer b/script/dancer index 17bdd73bd..dcb72a1d5 100755 --- a/script/dancer +++ b/script/dancer @@ -13,7 +13,7 @@ use Dancer::Renderer; use LWP::UserAgent; use constant FILE => 1; -my $TEMPLATES_DIR = "$ENV{HOME}/.dancer_templates"; +my $TEMPLATES_DIR = "$ENV{HOME}/.dancer/templates"; # options my $help = 0; @@ -84,7 +84,9 @@ NOYAML sub create_app_from_template { my ($template_name, $appdir, $appname) = @_; - my $template_base = catfile($TEMPLATES_DIR, $template_name); + my $template_base = $template_name =~ m!^/! + ? $template_name + : catfile($TEMPLATES_DIR, $template_name); unless (-e $template_base) { die "No template named $template_name found in $TEMPLATES_DIR\n"; } From d9ebf538b28929ed6197e140cc6183e8ae178f55 Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Thu, 8 Dec 2011 08:06:48 -0600 Subject: [PATCH 6/6] Allow refs to pass through view unscathed Useful for inline templates so that you don't necessarily need a directory structure or external files. Also, the underlying template implementation supports passing a scalar ref, but without the high-level implementation also supporting this, there's no way to do inline templates with hooks. --- lib/Dancer/Template/Abstract.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Dancer/Template/Abstract.pm b/lib/Dancer/Template/Abstract.pm index fdfb300d2..88214dbcf 100644 --- a/lib/Dancer/Template/Abstract.pm +++ b/lib/Dancer/Template/Abstract.pm @@ -23,6 +23,7 @@ sub default_tmpl_ext { "tt" } sub _template_name { my ( $self, $view ) = @_; + return $view if ref($view); my $def_tmpl_ext = $self->config->{extension} || $self->default_tmpl_ext(); $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/; return $view; @@ -33,7 +34,7 @@ sub view { $view = $self->_template_name($view); - return path(Dancer::App->current->setting('views'), $view); + return ref($view) ? $view : path(Dancer::App->current->setting('views'), $view); } sub layout {