| Filename | /home/mickey/git_tree/PONAPI/Server/lib/PONAPI/CLI/Command/gen.pm |
| Statements | Executed 11 statements in 1.05ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 644µs | 32.9ms | PONAPI::CLI::Command::gen::BEGIN@9 |
| 1 | 1 | 1 | 13µs | 148µs | PONAPI::CLI::Command::gen::BEGIN@4 |
| 1 | 1 | 1 | 6µs | 15µs | PONAPI::CLI::Command::gen::BEGIN@6 |
| 1 | 1 | 1 | 5µs | 9µs | PONAPI::CLI::Command::gen::BEGIN@7 |
| 1 | 1 | 1 | 3µs | 3µs | PONAPI::CLI::Command::gen::BEGIN@10 |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::abstract |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::create_conf_file |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::create_dir |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::create_psgi_file |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::create_repo_module |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::description |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::execute |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::opt_spec |
| 0 | 0 | 0 | 0s | 0s | PONAPI::CLI::Command::gen::validate_args |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | # ABSTRACT: ponapi project generation command line utility | ||||
| 2 | package PONAPI::CLI::Command::gen; | ||||
| 3 | |||||
| 4 | 2 | 24µs | 2 | 283µ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 # 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 | |||||
| 6 | 2 | 18µs | 2 | 25µ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 # spent 15µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@6
# spent 10µs making 1 call to strict::import |
| 7 | 2 | 20µs | 2 | 13µ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 # spent 9µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@7
# spent 4µs making 1 call to warnings::import |
| 8 | |||||
| 9 | 2 | 442µs | 2 | 32.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 # spent 32.9ms making 1 call to PONAPI::CLI::Command::gen::BEGIN@9
# spent 31µs making 1 call to Exporter::import |
| 10 | 2 | 539µs | 1 | 3µ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 # spent 3µs making 1 call to PONAPI::CLI::Command::gen::BEGIN@10 |
| 11 | |||||
| 12 | sub abstract { "Initialize a PONAPI server working environment" } | ||||
| 13 | sub description { "This tool will assist you in setting up a PONAPI server" } | ||||
| 14 | |||||
| 15 | sub 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 | |||||
| 25 | sub 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 | |||||
| 53 | sub 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 | |||||
| 62 | sub 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 | |||||
| 74 | sub 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"); | ||||
| 93 | package @{[ $self->{_new_repo} ]}; | ||||
| 94 | |||||
| 95 | use Moose; | ||||
| 96 | |||||
| 97 | use PONAPI::Constants; | ||||
| 98 | use PONAPI::Exception; | ||||
| 99 | with 'PONAPI::Repository'; | ||||
| 100 | |||||
| 101 | __PACKAGE__->meta->make_immutable; | ||||
| 102 | no Moose; 1; | ||||
| 103 | |||||
| 104 | __END__ | ||||
| 105 | MODULE | ||||
| 106 | } | ||||
| 107 | |||||
| 108 | sub 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 | |||||
| 122 | server: | ||||
| 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 | |||||
| 130 | repository: | ||||
| 131 | class: "@{[ $self->{_conf_repo} ]}" | ||||
| 132 | args: [] | ||||
| 133 | DEFAULT_CONF | ||||
| 134 | } | ||||
| 135 | |||||
| 136 | sub 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" ); | ||||
| 145 | use strict; | ||||
| 146 | use warnings; | ||||
| 147 | use Plack::Middleware::MethodOverride; | ||||
| 148 | use PONAPI::Server; | ||||
| 149 | |||||
| 150 | Plack::Middleware::MethodOverride->wrap( | ||||
| 151 | PONAPI::Server->new()->to_app() | ||||
| 152 | ); | ||||
| 153 | DEFAULT_PSGI | ||||
| 154 | } | ||||
| 155 | |||||
| 156 | 1 | 2µs | 1; |