PONAPI/CLI/Command/gen.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 143 CC 11 SCAR 27.9
CC
11
Cov
0%
validate_args
42.0
create_repo_module
20.0
create_dir
12.0
CRAP 16.2
1 # ABSTRACT: ponapi project generation command line utility
2 package PONAPI::CLI::Command::gen;
3  
4 use PONAPI::CLI -command;
5  
6 use strict;
7 use warnings;
8  
9 use Path::Class;
10 use Module::Runtime ();
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;
157  
158 __END__
159  
160 =pod
161  
162 =encoding UTF-8
163  
164 =head1 NAME
165  
166 PONAPI::CLI::Command::gen - ponapi project generation command line utility
167  
168 =head1 VERSION
169  
170 version 0.003002
171  
172 =head1 AUTHORS
173  
174 =over 4
175  
176 =item *
177  
178 Mickey Nasriachi <mickey@cpan.org>
179  
180 =item *
181  
182 Stevan Little <stevan@cpan.org>
183  
184 =item *
185  
186 Brian Fraser <hugmeir@cpan.org>
187  
188 =back
189  
190 =head1 COPYRIGHT AND LICENSE
191  
192 This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser.
193  
194 This is free software; you can redistribute it and/or modify it under
195 the same terms as the Perl 5 programming language system itself.
196  
197 =cut