Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring/script #706

Open
wants to merge 1 commit into
base: refactoring/script
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 52 additions & 12 deletions lib/Dancer/Script.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Dancer::ModuleLoader;
use Dancer::Template::Simple;
use Dancer::Renderer;
use File::Basename 'basename';
use File::Find;
use File::Path 'mkpath';
use File::Spec::Functions qw/catdir catfile/;
use Getopt::Long;
Expand All @@ -18,9 +19,13 @@ use constant FILE => 1;
set logger => 'console';
set logger_format => '%L> %m';

my $TEMPLATES_DIR = $ENV{DANCER_APPLICATION_TEMPLATES}
|| "$ENV{HOME}/.dancer/templates";

Dancer::Script->attributes(
qw(appname root_path check_version dancer_app_dir dancer_script
dancer_version do_check_dancer_version do_overwrite_all lib_file lib_path)
dancer_version do_check_dancer_version do_overwrite_all lib_file lib_path
dancer_app_template)
);

# Here goes the code to use File::ShareDir
Expand All @@ -40,13 +45,14 @@ sub init {
$self->appname($args{appname});
$self->root_path($args{path});
$self->check_version($args{check_version});

$self->dancer_app_template($args{template});
}
else {
my ($appname, $path, $check_version) = $self->_parse_opts;
my ($appname, $path, $check_version, $template) = $self->_parse_opts;
$self->appname($appname);
$self->root_path($path);
$self->check_version($check_version);
$self->dancer_app_template($template);
}

$self->do_overwrite_all(0);
Expand All @@ -67,12 +73,13 @@ sub _parse_opts {
my $do_check_dancer_version = 1;
my $name = undef;
my $path = '.';

my $template = undef;

GetOptions(
"h|help" => \$help,
"a|application=s" => \$name,
"p|path=s" => \$path,
"t|template=s" => \$template,
"x|no-check" => sub { $do_check_dancer_version = 0 },
"v|version" => \&version,
) or pod2usage(-verbose => 1);
Expand Down Expand Up @@ -103,18 +110,51 @@ following commands:
NOYAML
}

return ($name, $path, $do_check_dancer_version);
return ($name, $path, $do_check_dancer_version, $template);
}

sub run {
my $self = shift;
$self->_version_check if $self->do_check_dancer_version;
$self->_safe_mkdir($self->dancer_app_dir);

# TODO private method for _create_node
$self->create_node($self->_app_tree, $self->dancer_app_dir);
if (defined $self->dancer_app_template) {
$self->create_app_from_template;
} else {
# TODO private method for _create_node
$self->create_node($self->_app_tree, $self->dancer_app_dir);
}
}

sub create_app_from_template {
my $self = shift;

my $appdir = $self->dancer_app_dir;
my $template_name = $self->dancer_app_template;
my $appname = $self->appname;
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";
}
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) { $self->_safe_mkdir($appfile); return }
my $template = do { local (@ARGV,$/) = $File::Find::name; <> };
$self->_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 run_scaffold {
my $class = shift;
my $type = shift;
Expand Down Expand Up @@ -337,8 +377,8 @@ sub _process_template {
my $template = shift;
my $tokens = shift;
my $engine = Dancer::Template::Simple->new;
$engine->{start_tag} = '[%';
$engine->{stop_tag} = '%]';
$engine->{start_tag} = '[[%%';
$engine->{stop_tag} = '%%]]';
return $engine->render(\$template, $tokens);
}

Expand Down Expand Up @@ -442,7 +482,7 @@ WriteMakefile(
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer' => [% dancer_version %],
'Dancer' => [[%% dancer_version %%]],
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => '$cleanfiles-*' },
Expand Down Expand Up @@ -491,7 +531,7 @@ WriteMakefile(
<h3>Your application\'s environment</h3>

<ul>
<li>Location: <code>[% appdir %]</code></li>
<li>Location: <code>[[%% appdir %%]]</code></li>
<li>Template engine: <code><% settings.template %></code></li>
<li>Logger: <code><% settings.logger %></code></li>
<li>Environment: <code><% settings.environment %></code></li>
Expand Down Expand Up @@ -531,7 +571,7 @@ WriteMakefile(
</tr>
<tr>
<td>Appdir</td>
<td><tt>[% appdir %]</tt></td>
<td><tt>[[%% appdir %%]]</tt></td>
</tr>
<tr>
<td>Template engine</td>
Expand Down
45 changes: 45 additions & 0 deletions script/dancer
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dancer [options] -a <appname>
-a, --application : the name of your application
-p, --path : the path where to create your application
(current directory if not specified)
-t, --template : specify an application template
-x, --no-check : don't check for the latest version of Dancer
(checking version implies internet connection)
-v, --version : print the version of dancer being used
Expand Down Expand Up @@ -74,12 +75,56 @@ The application is ready to serve:
>> Listening on 127.0.0.1:3000
== Entering the development dance floor ...

=head1 Application Templates

Dancer applications may be created from an existing directory structure
by passing an argument to -t (--template).

If the first character of the string passed to C<-t> is a slash (C</>),
then it is taken to be an absolute path to a directory structure that
contains a Dancer application which you would like to base your new
application upon. If the first character is I<not> a slash, Dancer looks
for a directory of the same name in C<~/.dancer/templates> and will copy
that directory structure into your new application directory.

In either case there are a few substitutions that are performed on files
within the template directory. Within each filename the string C<APPNAME>
is replaced with the actual name of the Dancer application. For
instance, if you were creating an app called C<MyApp> and there was a
file named C<fred_APPNAME_jones.pm> within your template directory, the
resultant application would obtain a file named C<fred_MyApp_jones.pm>.

Also, within files, instances of C<[[%% appname %%]]> are replaced with
the actual name of the application, and instances of C<[[%% appdir %%]]>
are replaced with the actual directory where the application is created.
The markers around these substitution strings (C<[[%%> and C<%%]]>) are
intentionally fat and ugly so as to not conflict with the markers that
may be used within views or other C<.tt> files within the directory.

Examples:

# creates app based on ~/.dancer/templates/foo
$ dancer -t foo -a myfooapp

# create app based on an existing app
$ dancer -t /home/user/apps/simple_login -a other_login

B<Note>, in the second example, dancer will not rename
C<lib/simple_login.pm> when it creates the new application.

Where dancer looks for application templates (C<~/.dancer/templates>)
may be overridden by setting the C<DANCER_APPLICATION_TEMPLATES>
environment variable.

=head1 AUTHOR

This script has been written by Sebastien Deseille
<[email protected]> and Alexis Sukrieh
<[email protected]>.

Application templates added by Jonathan Scott Duff
<[email protected]>

=head1 SOURCE CODE

See L<Dancer> for more information.
Expand Down