Skip to content

Instantly share code, notes, and snippets.

@mix3
Created February 8, 2011 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mix3/816679 to your computer and use it in GitHub Desktop.
Save mix3/816679 to your computer and use it in GitHub Desktop.
Ark skeletone project generater
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd;
use Data::Section::Simple qw/get_data_section/;
if($#ARGV < 0){
print "Usage: perl generate_app.pl filename\n";
exit 1;
}
my $Bin = Cwd::getcwd();
# __APP__
my $app_name = ucfirst($ARGV[0]) . "App";
# project_root/app_name
my $project_root = $Bin . "/" . $ARGV[0];
# project_root/app_name/lib
my $lib = $project_root . "/lib";
# project_root/app_name/root
my $root = $project_root . "/root";
# project_root/app_name/root/tmpl
my $tmpl = $root . "/tmpl";
# project_root/app_name/eg
my $eg = $project_root . "/eg";
# project_root/app_name/lib/__APP__
my $app = $lib . "/" . $app_name;
# project_root/app_name/lib/__APP__/View
my $view = $app . "/View";
# project_root/app_name/lib/__APP__/Controller
my $controller = $app . "/Controller";
# project_root/app_name/lib/__APP__/Ark
my $ark = $lib . "/Ark";
# project_root/app_name/lib/__APP__/Ark/View
my $ark_view = $ark . "/View";
mkdir($project_root);
mkdir($lib);
mkdir($root);
mkdir($tmpl);
mkdir($eg);
mkdir($app);
mkdir($view);
mkdir($controller);
mkdir($ark);
mkdir($ark_view);
my $list = get_data_section;
foreach(keys %$list){
&output($_, $project_root);
}
sub output {
my $section = shift;
my $project_root = shift;
(my $path = $section) =~ s/__APP__/$app_name/;
$path = $project_root . $path;
open(OUT, "> $path") || die $path;
foreach(split(/\n/, get_data_section($section))){
$_ =~ s/__APP__/$app_name/;
print OUT $_ . "\n";
}
close(OUT);
}
__DATA__
@@ /eg/start.psgi
use Plack::Builder;
use Plack::Middleware::Static;
use lib 'lib';
use __APP__;
my $app = __APP__->new;
$app->setup;
builder {
enable 'Plack::Middleware::Static',
path => qr{^/(js/|css/|swf/|images?/|imgs?/|static/|[^/]+\.[^/]+$)},
root => $app->path_to('root')->stringify;
$app->handler;
};
@@ /root/tmpl/index.mt
Hello World!
@@ /lib/__APP__.pm
package __APP__;
use Ark;
our $VERSION = '0.01';
use_model '__APP__::Models';
my $home = __APP__::Models->get('home');
# MT
config 'View::Tiffany' => {
view => 'Text::MicroTemplate::Extended',
options => {
include_path => [ $home->subdir('root', 'tmpl') ],
template_args => {
c => sub{ __PACKAGE__->context },
s => sub{ __PACKAGE__->context->stash },
},
},
};
1;
@@ /lib/__APP__/View/Tiffany.pm
package __APP__::View::Tiffany;
use Ark 'View::Tiffany';
1;
@@ /lib/__APP__/Controller/Root.pm
package __APP__::Controller::Root;
use Ark 'Controller';
has '+namespace' => default => '';
# default 404 handler
sub default :Path :Args {
my ($self, $c) = @_;
$c->res->status(404);
$c->res->body('404 Not Found');
}
sub index :Path {
my ($self, $c) = @_;
}
sub end :Private {
my ($self, $c) = @_;
unless ($c->res->body or $c->res->status =~ /^3\d\d/) {
$c->forward( $c->view('Tiffany') );
}
}
1;
@@ /lib/__APP__/Models.pm
package __APP__::Models;
use Ark::Models -base;
1;
@@ /lib/Ark/View/Tiffany.pm
package Ark::View::Tiffany;
use Ark 'View';
use Tiffany;
has view => (
is => 'rw',
isa => 'Str',
lazy => 1,
default => 'Text::MicroTemplate::Extended',
);
has extension => (
is => 'rw',
isa => 'Str',
lazy => 1,
default => '',
);
has options => (
is => 'rw',
isa => 'HashRef',
lazy => 1,
default => sub { +{ } },
);
has tiffany => (
is => 'rw',
isa => 'Object',
lazy => 1,
default => sub { Tiffany->load($_[0]->view, $_[0]->options) },
);
sub template
{
my ($self, $template) = @_;
$self->context->stash->{__view_tiffany_template} = $template;
return $self;
}
sub render
{
my $self = shift;
my $template = shift;
my $context = $self->context;
$template ||= $self->context->stash->{__view_tiffany_template}
|| $self->context->request->action->reverse
|| return;
return $self->tiffany->render(
$template.$self->extension,
{
%{ $context->stash },
c => $self->context,
@_,
}
);
}
sub process
{
# my ($self, $c) = @_;
# $c->response->body( $self->render );
$_[1]->response->body( $_[0]->render );
}
__PACKAGE__->meta->make_immutable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment