Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
perlpilot committed Sep 29, 2011
commit 4a12de18e31eb3894e88aba5e327e0f12195fe98
33 changes: 32 additions & 1 deletion script/dancer
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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($);
Expand All @@ -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 );
Expand All @@ -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 <<NOYAML;
Expand All @@ -73,6 +82,28 @@ NOYAML

# subs

sub create_app_from_template {
my ($template_name, $appdir, $appname) = @_;
my $template_base = catfile($TEMPLATES_DIR, $template_name);
unless (-e $template_base) {
die "No template named $template_name found in $TEMPLATES_DIR\n";
}
find({ no_chdir => 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,}/) {
Expand Down