new
=> 0,
rw
=> [
qw( appname approot pmpath templates meta_template )
],
);
sub
new {
my
(
$class
,
%opts
) =
@_
;
my
%plugin
=
map
{
'Nephia::Plugin::'
.
$_
=>
'0'
} @{
$opts
{plugin}};
my
$appname
=
$opts
{appname};
$appname
=~ s/::/-/g;
$opts
{approot} = File::Spec->catdir(
'.'
,
$appname
);
$opts
{pmpath} = File::Spec->catfile(
$opts
{approot},
'lib'
,
split
(/::/,
$opts
{appname}.
'.pm'
) );
$opts
{meta_template} = Nephia::MetaTemplate->new;
return
bless
{
%opts
,
required_modules
=> {
'Nephia'
=>
'0'
,
'Config::Micro'
=>
'0.02'
,
%plugin
,
},
},
$class
;
}
sub
_parse_template_data {
my
(
$self
,
@data
) =
@_
;
$self
->templates( +{
map
{
my
(
$key
,
$template
) =
split
(
"---"
,
$_
, 2);
$key
=~ s/(\s|\r|\n)//g;
$template
=~ s/^\n//;
(
$key
,
$template
);
}
grep
{
$_
=~ /---/ }
split
(
"==="
,
join
(
''
,
@data
) )
} );
}
sub
_set_required_modules {
my
(
$self
,
$required_modules
) =
@_
;
for
my
$module
(
keys
%{
$required_modules
}) {
my
$version
=
$required_modules
->{
$module
};
if
(
defined
$self
->{required_modules}->{
$module
}) {
my
$defined_version
=
$self
->{required_modules}->{
$module
};
$self
->{required_modules}->{
$module
} =
$defined_version
>
$version
?
$defined_version
:
$version
;
}
else
{
$self
->{required_modules}->{
$module
} =
$version
;
}
}
}
sub
create {
my
$self
=
shift
;
if
(-d
$self
->approot) {
croak
"Cannot mkdir '"
.
$self
->approot .
"': Directory exists"
;
}
$self
->mkpath(
$self
->approot);
map
{
my
@path
=
split
'/'
,
$_
;
$self
->mkpath(
$self
->approot,
@path
);
}
qw( lib etc etc/conf view root root/static t )
;
$self
->changes_file;
$self
->psgi_file;
$self
->app_class_file;
$self
->index_template_file;
$self
->css_file;
$self
->cpanfile;
$self
->basic_test_file;
$self
->config_file;
$self
->gitignore_file;
for
my
$method
(
keys
%{
$self
->{additional_methods}}) {
$self
->
$method
if
$self
->can(
$method
);
}
}
sub
spew {
my
(
$self
,
$file
,
$body
) =
@_
;
print
"spew into file $file\n"
;
open
my
$fh
,
'>'
,
$file
or croak
"could not spew into $file: $!"
;
print
$fh
$body
;
close
$fh
;
}
sub
spew_as_template {
my
(
$self
,
$file
,
$body
) =
@_
;
my
$template_body
=
$self
->meta_template->process(
$body
);
$self
->spew(
$file
,
$template_body
);
}
sub
mkpath {
my
(
$self
,
@part
) =
@_
;
my
$path
= File::Spec->catdir(
@part
);
unless
(-d
$path
) {
print
"create path $path\n"
;
mkdir
$path
, 0755 or croak
"could not create path $path: $!"
;
}
}
sub
dir {
my
(
$self
,
@part
) =
@_
;
my
$path
= File::Spec->catfile(
@part
);
dirname(
$path
);
}
sub
psgi_file {
my
$self
=
shift
;
my
$appname
=
$self
->appname;
my
$body
=
$self
->templates->{psgi_file};
$body
=~ s[\
$appname
][
$appname
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
'app.psgi'
);
$self
->spew(
$file
,
$body
);
}
sub
app_class_file {
my
$self
=
shift
;
my
@plugins
=
sort
{
$a
cmp
$b
}
map
{
(
my
$module
=
$_
) =~ s/^Nephia::Plugin:://;
$module
}
grep
{
$_
=~ /^Nephia::Plugin::/
}
keys
%{
$self
->{required_modules}};
my
$plugins
=
@plugins
== 0 ?
''
:
" plugins => [qw/\n\t"
. (
join
"\n\t"
,
@plugins
) .
"\n/]"
;
my
$approot
=
$self
->approot;
my
$appname
=
$self
->appname;
my
$body
=
$self
->templates->{app_class_file};
$body
=~ s[\
$approot
][
$approot
]g;
$body
=~ s[\
$appname
][
$appname
]g;
$body
=~ s[\
$plugins
][
$plugins
]g;
$body
=~ s[:::][=]g;
my
$dir
=
$self
->dir(
$self
->pmpath);
$self
->mkpath(
$dir
);
$self
->spew(
$self
->pmpath,
$body
);
}
sub
index_template_file {
my
$self
=
shift
;
my
$body
=
$self
->templates->{index_template_file};
my
$file
= File::Spec->catfile(
$self
->approot,
qw/view index.html/
);
$self
->spew_as_template(
$file
,
$body
);
}
sub
css_file {
my
$self
=
shift
;
my
$body
=
$self
->templates->{css_file};
my
$file
= File::Spec->catfile(
$self
->approot,
qw/root static style.css/
);
$self
->spew(
$file
,
$body
);
}
sub
changes_file {
my
$self
=
shift
;
my
$body
=
$self
->templates->{Changes};
my
$appname
=
$self
->appname;
$body
=~ s[\
$appname
][
$appname
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
qw/Changes/
);
$self
->spew(
$file
,
$body
);
}
sub
cpanfile {
my
$self
=
shift
;
my
$required_modules
=
join
"\n"
,
map
{
qq{requires '$_' => '$self->{required_modules}
->{
$_
}';}
}
sort
{
(
$b
=~ /Nephia/) <=> (
$a
=~ /Nephia/) ||
$a
cmp
$b
}
keys
%{
$self
->{required_modules}};
my
$appname
=
$self
->appname;
$appname
=~ s[::][-]g;
my
$pmpath
=
$self
->pmpath;
$pmpath
=~ s[
$appname
][.];
my
$body
=
$self
->templates->{cpanfile};
$body
=~ s[\
$required_modules
][
$required_modules
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
'cpanfile'
);
$self
->spew(
$file
,
$body
);
}
sub
basic_test_file {
my
$self
=
shift
;
my
$appname
=
$self
->appname;
my
$body
=
$self
->templates->{basic_test_file};
$body
=~ s[\
$appname
][
$appname
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
qw/t 001_basic.t/
);
$self
->spew(
$file
,
$body
);
}
sub
config_file {
my
$self
=
shift
;
my
$appname
=
$self
->appname;
$appname
=~ s[::][-]g;
my
$common
=
$self
->templates->{common_conf};
$common
=~ s[\
$appname
][
$appname
]g;
my
$common_conf_path
= File::Spec->catfile(
$self
->approot,
'etc'
,
'conf'
,
'common.pl'
);
$self
->spew(
$common_conf_path
,
$common
);
for
my
$envname
(
qw( development staging production )
) {
my
$body
=
$self
->templates->{conf_file};
$body
=~ s[\
$common_conf_path
][
$common_conf_path
]g;
$body
=~ s[\
$envname
][
$envname
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
'etc'
,
'conf'
,
$envname
.
'.pl'
);
$self
->spew(
$file
,
$body
);
}
}
sub
gitignore_file {
my
$self
=
shift
;
my
$appname
=
$self
->appname;
my
$body
=
$self
->templates->{gitignore_file};
$body
=~ s[\
$appname
][
$appname
]g;
my
$file
= File::Spec->catfile(
$self
->approot,
'.gitignore'
);
$self
->spew(
$file
,
$body
);
}
1;