← Index
NYTProf Performance Profile   « line view »
For script/ponapi
  Run on Wed Feb 10 15:51:26 2016
Reported on Thu Feb 11 09:43:09 2016

Filename/home/mickey/git_tree/PONAPI/Server/lib/PONAPI/CLI/Command/gen.pm
StatementsExecuted 11 statements in 1.05ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111644µs32.9msPONAPI::CLI::Command::gen::::BEGIN@9PONAPI::CLI::Command::gen::BEGIN@9
11113µs148µsPONAPI::CLI::Command::gen::::BEGIN@4PONAPI::CLI::Command::gen::BEGIN@4
1116µs15µsPONAPI::CLI::Command::gen::::BEGIN@6PONAPI::CLI::Command::gen::BEGIN@6
1115µs9µsPONAPI::CLI::Command::gen::::BEGIN@7PONAPI::CLI::Command::gen::BEGIN@7
1113µs3µsPONAPI::CLI::Command::gen::::BEGIN@10PONAPI::CLI::Command::gen::BEGIN@10
0000s0sPONAPI::CLI::Command::gen::::abstractPONAPI::CLI::Command::gen::abstract
0000s0sPONAPI::CLI::Command::gen::::create_conf_filePONAPI::CLI::Command::gen::create_conf_file
0000s0sPONAPI::CLI::Command::gen::::create_dirPONAPI::CLI::Command::gen::create_dir
0000s0sPONAPI::CLI::Command::gen::::create_psgi_filePONAPI::CLI::Command::gen::create_psgi_file
0000s0sPONAPI::CLI::Command::gen::::create_repo_modulePONAPI::CLI::Command::gen::create_repo_module
0000s0sPONAPI::CLI::Command::gen::::descriptionPONAPI::CLI::Command::gen::description
0000s0sPONAPI::CLI::Command::gen::::executePONAPI::CLI::Command::gen::execute
0000s0sPONAPI::CLI::Command::gen::::opt_specPONAPI::CLI::Command::gen::opt_spec
0000s0sPONAPI::CLI::Command::gen::::validate_argsPONAPI::CLI::Command::gen::validate_args
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# ABSTRACT: ponapi project generation command line utility
2package PONAPI::CLI::Command::gen;
3
4224µs2283µs
# spent 148µs (13+135) within PONAPI::CLI::Command::gen::BEGIN@4 which was called: # once (13µs+135µs) by Module::Runtime::require_module at line 4
use PONAPI::CLI -command;
# spent 148µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@4 # spent 135µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337]
5
6218µs225µs
# spent 15µs (6+10) within PONAPI::CLI::Command::gen::BEGIN@6 which was called: # once (6µs+10µs) by Module::Runtime::require_module at line 6
use strict;
# spent 15µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@6 # spent 10µs making 1 call to strict::import
7220µs213µs
# spent 9µs (5+4) within PONAPI::CLI::Command::gen::BEGIN@7 which was called: # once (5µs+4µs) by Module::Runtime::require_module at line 7
use warnings;
# spent 9µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@7 # spent 4µs making 1 call to warnings::import
8
92442µs232.9ms
# spent 32.9ms (644µs+32.3) within PONAPI::CLI::Command::gen::BEGIN@9 which was called: # once (644µs+32.3ms) by Module::Runtime::require_module at line 9
use Path::Class;
# spent 32.9ms making 1 call to PONAPI::CLI::Command::gen::BEGIN@9 # spent 31µs making 1 call to Exporter::import
102539µs13µs
# spent 3µs within PONAPI::CLI::Command::gen::BEGIN@10 which was called: # once (3µs+0s) by Module::Runtime::require_module at line 10
use Module::Runtime ();
# spent 3µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@10
11
12sub abstract { "Initialize a PONAPI server working environment" }
13sub description { "This tool will assist you in setting up a PONAPI server" }
14
15sub opt_spec {
16 return (
17 [ "d|dir=s", "Server directory to be created" ],
18 [ "r|repo=s", "EXSISTING repository module to POINT to" ],
19 [ "n|new_repo=s", "NEW repository module NAME to CREATE" ],
20 [ "c|conf=s", "Copy server config file", { default => "" } ],
21 [ "p|psgi=s", "Copy server startup script", { default => "" } ],
22 );
23}
24
25sub validate_args {
26 my ( $self, $opt, $args ) = @_;
27
28 # check directory name
29 $self->usage_error("'directory name' is required.\n")
30 unless $self->{_dir} = $opt->{dir} || $opt->{d};
31
32 # check repo / new_repo
33 my $repo = $self->{_repo} = $opt->{repo} || $opt->{r};
34 my $n_repo = $self->{_new_repo} = $opt->{new_repo} || $opt->{n};
35
36 $self->{_conf_repo} = $repo || $n_repo;
37
38 $self->usage_error("one of new (--new_repo STR) or existing (--repo STR) is required.\n")
39 unless $repo xor $n_repo;
40
41 $self->usage_error("$repo is an invalid module name\n")
42 if $repo and ! Module::Runtime::use_module($repo);
43
44 # check conf
45 $self->{_conf_content} = file( $opt->{conf} )->slurp()
46 if $opt->{conf};
47
48 # check psgi
49 $self->{_startup_content} = file( $opt->{psgi} )->slurp()
50 if $opt->{psgi};
51}
52
53sub execute {
54 my ( $self, $opt, $args ) = @_;
55
56 $self->create_dir( $self->{_dir} ); # must pass as arg.
57 $self->create_repo_module();
58 $self->create_conf_file();
59 $self->create_psgi_file();
60}
61
62sub create_dir {
63 my ( $self, $name ) = @_;
64 return unless $name;
65
66 my $dir = dir( split '/' => $name );
67 unless ( -d $name or $name eq '.' or $name eq '..' ) {
68 $dir->mkpath() or $self->usage_error("Failed to create directory $name\n");
69 }
70
71 return $dir;
72}
73
74sub create_repo_module {
75 my $self = shift;
76
77 my $name = $self->{_new_repo};
78 return unless $name;
79
80 $name =~ s/\.pm$//;
81 $name =~ s|::|/|g;
82 $name =~ s|^(.*)/||;
83
84 my $repo_dir_name = ( $1 ? '/' . $1 : '' );
85
86 my $repo_dir = $self->create_dir( $self->{_dir} . '/lib' . $repo_dir_name );
87 my $repo_file = $repo_dir->file( $name . '.pm' );
88
89 $self->usage_error("Failed to create new module file\n")
90 unless $repo_file->openw();
91
92 $repo_file->spew(<<"MODULE");
93package @{[ $self->{_new_repo} ]};
94
95use Moose;
96
97use PONAPI::Constants;
98use PONAPI::Exception;
99with 'PONAPI::Repository';
100
101__PACKAGE__->meta->make_immutable;
102no Moose; 1;
103
104__END__
105MODULE
106}
107
108sub create_conf_file {
109 my $self = shift;
110 my $dir = $self->{_dir};
111
112 my $conf_dir = $self->create_dir( $dir . '/conf' );
113
114 my $file = file( $dir . '/conf/server.yml' );
115
116 $file->spew( $self->{_conf_content} || <<"DEFAULT_CONF" );
117# PONAPI server & repository configuration file
118
119# switch options take the positive values: "yes", 1 & "true"
120# and negative values: "no", 0 & "false"
121
122server:
123 spec_version: "1.0" # {json:api} version
124 sort_allowed: "false" # server-side sorting support
125 send_version_header: "true" # server will send 'X-PONAPI-Server-Version' header responses
126 send_document_self_link: "true" # server will add a 'self' link to documents without errors
127 links_type: "relative" # all links are either "relative" or "full" (inc. request base)
128 respond_to_updates_with_200: "false" # successful updates will return 200's instead of 202's
129
130repository:
131 class: "@{[ $self->{_conf_repo} ]}"
132 args: []
133DEFAULT_CONF
134}
135
136sub create_psgi_file {
137 my $self = shift;
138 my $dir = $self->{_dir};
139
140 my $psgi_dir = $self->create_dir( $dir . '/psgi' );
141
142 my $file = file( $dir . '/psgi/ponapi.psgi' );
143
144 $file->spew( $self->{_startup_content} || <<"DEFAULT_PSGI" );
145use strict;
146use warnings;
147use Plack::Middleware::MethodOverride;
148use PONAPI::Server;
149
150Plack::Middleware::MethodOverride->wrap(
151 PONAPI::Server->new()->to_app()
152);
153DEFAULT_PSGI
154}
155
15612µs1;