lib/Project/Easy/Helper.pm

filter: Statementstmt 80.0% 144 / 180 Branchbran 38.1% 16 / 42 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 7 totaltotal 65.3% 185 / 283 CC 63 SCAR 30.0
CC
63
Cov
67%
_script_wrapper
43.3
_script_wrapper
43.3
initialize
7.4
CRAP 20.0
« lib/Project/Easy/Config.pm ↑ Summary lib/Project/Easy/DB.pm »
1 package Project::Easy::Helper;
2  
3 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use IO::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 require Project::Easy;
10  
11 8 ▶use Project::Easy::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Project::Easy::Helper::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Project::Easy::Helper::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Project::Easy::Helper::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use Project::Easy::Helper::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 our @scriptable = (qw(status config updatedb console));
19  
20 my $is_colored = 0;
21  
22 $is_colored = 1
23 if try_to_use ('Term::ANSIColor');
24  
25 sub ::initialize {
26 4 ▶ my $params = \@_;
Subroutine initialize: called CC=5 SCAR=20.0
27 4 ▶ $params = \@ARGV
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$params 0 0 0
unless scalar @$params 10 0 10
28 unless scalar @$params;
29
30 4 my $namespace = shift @$params;
31  
32 4 my @path = ('lib', split '::', $namespace);
33 4 my $last = pop @path;
34  
35 4 ▶ my $project_id = shift @ARGV || lc ($last);
Condition0%0/3
shift @ARGV || lc $last
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @ARGV || lc $last
shift @ARGV lc $last
Truth table0%0/3
A || B
A= shift @ARGVB= lc $last
ABresult
1X1
011
000
36
37 4 debug "initialization of $namespace, project id is: $project_id";
38
39 4 ▶ unless ($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace) 0 0 0
40 4 die "please specify package namespace";
41 }
42
43 4 my $data_files = file->__data__files;
44
45 4 my $data = {
46 namespace => $namespace,
47 project_id => $project_id,
48 };
49
50 my $project_pm = Project::Easy::Config::string_from_template (
51 4 $data_files->{'Project.pm'},
52 $data
53 );
54  
55 4 my $login = eval {scalar getpwuid ($<)};
56  
57 4 ▶ my $instance = 'local' . (defined $login ? ".$login" : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $login ? : 0 0 0
58
59 2 my $root = dir->current;
60
61 2 my $lib_dir = $root->append (@path)->as_dir;
62 2 $lib_dir->create; # recursive directory creation
63
64 2 $last .= '.pm';
65 2 my $class_file = $lib_dir->append ($last)->as_file;
66 2 $class_file->store_if_empty ($project_pm);
67
68 # ok, project skeleton created. now we need to create 'bin' dir
69 2 $root->dir_io ('bin')->create;
70
71 # now we create several perl scripts to complete installation
72 2 create_scripts ($root, $data_files);
73
74 # ok, project skeleton created. now we need to create config
75 2 my $etc = $root->append ('etc')->as_dir;
76 2 $etc->append ($instance)->as_dir->create;
77
78 # TODO: store database config
79 2 $etc->append ("$project_id.json")->as_file->store_if_empty ('{}');
80 2 $etc->append ($instance, "$project_id.json")->as_file->store_if_empty ('{}');
81
82 0 $etc->append ('project-easy')->as_file->store_if_empty ("#!/usr/bin/perl
83 package LocalConf;
84 our \$pack = '$namespace';
85  
86 our \@paths = qw(
87 );
88  
89 1;
90 ");
91
92 0 my $var = create_var ($root);
93
94 0 my $instance_file = $var->append ('instance')->as_file;
95 0 $instance_file->store_if_empty ($instance);
96  
97 0 my $t = $root->append ('t')->as_dir;
98 0 $t->create;
99
100 0 create_entity ($namespace, $root, 'default');
101
102 # adding sqlite database (sqlite is dependency for dbi::easy)
103
104 0 debug "file contents saving done";
105
106 0 $0 = dir->current->append (qw(etc project-easy))->path;
107
108 0 my $date = localtime->ymd;
109
110 0 my $schema_file = file ('share/sql/default.sql');
111 0 $schema_file->parent->create;
112 0 $schema_file->store (
113 "--- $date\ncreate table var (var_name text, var_value text);\n"
114 );
115  
116 0 config (qw(db.default template db.sqlite));
117 0 config (qw(db.default.attributes.dbname = ), '{$root}/var/test.sqlite');
118 0 config (qw(db.default.update =), "$schema_file");
119
120 0 $namespace->config ($instance);
121
122 0 update_schema (
123 mode => 'install'
124 );
125
126 # TODO: be more user-friendly: show help after finish
127
128
129 }
130  
131 sub create_scripts {
132 1 ▶ my $root = shift;
Subroutine create_scripts: called CC=3 SCAR=11.0
Pod undocumented
133 1 my $data_files = shift;
134  
135 1 foreach (@scriptable) {
136 4 my $script = $root->file_io ('bin', $_);
137  
138 my $script_contents = Project::Easy::Config::string_from_template (
139 4 $data_files->{'script.template'},
140 {script_name => $_}
141 );
142  
143 4 $script->store_if_empty ($script_contents);
144
145 4 ▶ warn "can't chmod " . $script->path
Branch: 1 branch
BranchTrueFalseTotal
unless chmod 0755, $script->path 0 8 8
146 unless chmod 0755, $script->path;
147
148 }
149  
150 }
151  
152 4 ▶sub helping_hand {
Subroutine helping_hand: called CC=1 SCAR=0.0
Pod undocumented
153
154 }
155  
156 sub create_entity {
157 9 ▶ my $namespace = shift;
Subroutine create_entity: called CC=3 SCAR=11.0
Pod undocumented
158 9 my $root = shift;
159 9 my $datasource = shift;
160
161 9 ▶ if (defined $::project) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::project) 0 13 13
162 0 $namespace = $::project;
163 }
164  
165 9 my @namespace_chunks = split /\:\:/, $namespace;
166
167 # here we must create default entity classes
168 9 my $project_lib = $root->dir_io ('lib', @namespace_chunks, 'Entity');
169 9 $project_lib->create;
170
171 9 my $scope_prefix = '';
172 9 ▶ if ($datasource ne 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($datasource ne "default") 5 8 13
173 3 $scope_prefix = ($namespace->_detect_entity ("${datasource}_test"))[2];
174 }
175
176 9 my $data_files = file->__data__files;
177  
178 9 my $entity_template = $data_files->{'Entity.pm'};
179  
180 9 my $entity_pm = Project::Easy::Config::string_from_template (
181 $entity_template,
182 {
183 namespace => $namespace,
184 scope => $scope_prefix.'Record', #
185 dbi_easy_scope => 'Record',
186 datasource => $datasource
187 }
188 );
189  
190 9 $project_lib->append ("${scope_prefix}Record.pm")->as_file->store_if_empty ($entity_pm);
191  
192 9 $entity_pm = Project::Easy::Config::string_from_template (
193 $entity_template,
194 {
195 namespace => $namespace,
196 scope => $scope_prefix.'Collection', #
197 dbi_easy_scope => 'Record::Collection',
198 datasource => $datasource
199 }
200 );
201  
202 9 $project_lib->append ("${scope_prefix}Collection.pm")->as_file->store_if_empty ($entity_pm);
203  
204 }
205  
206 sub create_var {
207 0 ▶ my $root = shift;
Subroutine create_var: called CC=2 SCAR=10.5
Pod undocumented
208 0 my $var = $root->dir_io ('var');
209 0 foreach (qw(db lock log run)) {
210 1 $var->dir_io ($_)->create;
211 }
212
213 1 return $var;
214 }
215  
216 sub shell {
217 1 ▶ my ($pack, $libs) = &_script_wrapper;
Subroutine shell: called CC=5 SCAR=18.0
Pod undocumented
218
219 4 my $core = $pack->singleton;
220
221 4 my $instance = $ARGV[0];
222
223 4 my $conf = $core->config ($instance);
224 4 my $sconf = $conf->{shell};
225
226 4 ▶ unless (try_to_use 'Net::SSH::Perl' and try_to_use 'Term::ReadKey') {
Branch: 1 branch
BranchTrueFalseTotal
unless (try_to_use("Net::SSH::Perl") and try_to_use("Term::ReadKey")) 0 0 0
Condition0%0/3
try_to_use("Net::SSH::Perl") and try_to_use("Term::ReadKey")
!ll&&!rl&&r
000
MC/DC0%0/2
try_to_use("Net::SSH::Perl") and try_to_use("Term::ReadKey")
try_to_use("Net::SSH::Perl") try_to_use("Term::ReadKey")
Truth table0%0/3
A and B
A= try_to_use("Net::SSH::Perl")B= try_to_use("Term::ReadKey")
ABresult
0X0
100
111
227 4 die "for remote shell you must install Net::SSH::Perl and Term::ReadKey packages";
228 }
229
230 4 my %args = ();
231 4 foreach (qw(compression cipher port debug identity_files use_pty options protocol)) {
232 $args{$_} = $sconf->{$_}
233 0 ▶ if $sconf->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $sconf->{$_} 0 0 0
234 }
235
236 4 $args{interactive} = 1;
237
238 4 my $ssh = Net::SSH::Perl->new ($conf->{host}, %args);
239 4 $ssh->login ($sconf->{user});
240
241 4 ReadMode('raw');
242 4 eval "END { ReadMode('restore') };";
243 2 $ssh->shell;
244  
245 }
246  
247 sub _script_wrapper {
248 # because some calls dispatched to external scripts, but called from project dir
249 13 ▶ my $local_conf = shift || $0;
Subroutine _script_wrapper: called CC=19 SCAR=37.7
Condition33%1/3
shift() || $0
l!l&&r!l&&!r
0130
MC/DC0%0/2
shift() || $0
shift() $0
Truth table33%1/3
A || B
A= shift()B= $0
ABresult
1X1
011
000
250 13 ▶ my $importing = shift || 0;
Condition50%1/2
shift() || 0
l!l
013
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
251 13 my $lib_path;
252
253 13 ▶ return ($::project, $::libs)
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::project 5 8 13
254 if defined $::project;
255  
256 10 debug "called from $local_conf";
257
258 10 $local_conf = dir ($local_conf);
259
260 6 my $root;
261
262 6 ▶ if (exists $ENV{'MOD_PERL'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL'}) { } 0 8 8
elsif ($local_conf->name eq 'project-easy' and $local_conf->parent->name eq 'etc') { } 0 8 8
263
264 0 my $server_root;
265
266 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL_API_VERSION'} and $ENV{'MOD_PERL_API_VERSION'} >= 2 and try_to_use_inc('Apache2::ServerUtil')) { } 0 0 0
elsif (try_to_use_inc('Apache')) { } 0 0 0
Condition0%0/6
exists $ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} >= 2 and try_to_use_inc("Apache2::ServerUtil")
!ll&&!rl&&r
000
exists $ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} >= 2
!ll&&!rl&&r
000
MC/DC0%0/3
exists $ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} >= 2 and try_to_use_inc("Apache2::ServerUtil")
exists $ENV{"MOD_PERL_API_VERSION"} $ENV{"MOD_PERL_API_VERSION"} >= 2 try_to_use_inc("Apache2::ServerUtil")
Truth table0%0/4
A and B and C
A= exists $ENV{"MOD_PERL_API_VERSION"}B= $ENV{"MOD_PERL_API_VERSION"} >= 2C= try_to_use_inc("Apache2::ServerUtil")
ABCresult
0XX0
10X0
1100
1111
267 exists $ENV{MOD_PERL_API_VERSION}
268 and $ENV{MOD_PERL_API_VERSION} >= 2
269 and try_to_use_inc ('Apache2::ServerUtil')
270 ) {
271
272 0 $server_root = Apache2::ServerUtil::server_root();
273
274 } elsif (try_to_use_inc ('Apache')) {
275
276 0 $server_root = Apache::server_root_relative('');
277
278 } else {
279 0 die "you try to run project::easy under mod_perl, but we cannot work with your version. if you have mod_perl-1.99, use solution from CGI::minimal or upgrade your mod_perl";
280 }
281
282 0 $root = dir ($server_root);
283 0 $local_conf = $root->dir_io (qw(etc project-easy));
284 0 $lib_path = $root->dir_io ("lib");
285
286 ▶ } elsif ($local_conf->name eq 'project-easy' and $local_conf->parent->name eq 'etc') {
Condition33%1/3
$local_conf->name eq "project-easy" and $local_conf->parent->name eq "etc"
!ll&&!rl&&r
800
MC/DC0%0/2
$local_conf->name eq "project-easy" and $local_conf->parent->name eq "etc"
$local_conf->name eq "project-easy" $local_conf->parent->name eq "etc"
Truth table33%1/3
A and B
A= $local_conf->name eq "project-easy"B= $local_conf->parent->name eq "etc"
ABresult
0X0
100
111
287 # TODO: use etc method from project package
288 0 $root = $local_conf->parent->parent;
289 0 $lib_path = $local_conf->parent->parent->dir_io ('lib');
290 } else {
291
292 6 my $parent = $local_conf;
293 6 ▶ PROJECT_ROOT: while ($parent = $parent->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent = $parent->parent) 8 0 8
294
295 6 foreach (qw(t cgi-bin tools bin)) {
296 24 ▶ if ($parent->name eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->name eq $_) 8 21 29
297 6 $root = $parent->parent;
298 6 $local_conf = $root->file_io (qw(etc project-easy));
299 6 $lib_path = $root->dir_io ('lib');
300 6 last PROJECT_ROOT;
301 }
302 }
303
304 0 ▶ last if ($parent->path eq $parent->parent->path);
Branch: 1 branch
BranchTrueFalseTotal
if $parent->path eq $parent->parent->path 0 0 0
305
306 }
307 10 ▶ die unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 8 8
308 }
309
310 10 $lib_path = $lib_path->abs_path;
311
312 10 debug "local conf is: $local_conf, lib path is: ",
313 join (', ', @LocalConf::paths, $lib_path), "\n";
314
315 10 require $local_conf;
316  
317 8 push @INC, @LocalConf::paths, $lib_path->path;
318
319 8 my $pack = $LocalConf::pack;
320
321 8 debug "main project module is: $pack";
322  
323 #use Carp;
324 #$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
325
326 # check for required directories, create if necessary
327 8 ▶ if (! -d $root->dir_io ('var')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $root->dir_io("var")) 0 8 8
328 0 create_var ($root);
329 }
330
331 # here we check for real package availability
332
333 11 ▶ eval "use Class::Easy; use IO::Easy; use DBI::Easy; " . ($importing ? '' : "use $pack;");
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$importing ? : 0 8 8
334 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
335 0 die 'base modules fails: ', $@;
336 }
337  
338 6 my @result = ($::project, $::libs) = ($pack, [@LocalConf::paths, $lib_path->path]);
339
340 6 return @result;
341 }
342  
343 sub table_from_package {
344 3 ▶ my $entity = shift;
Subroutine table_from_package: called CC=1 SCAR=0.0
Pod undocumented
345
346 3 lc join ('_', split /(?=\p{IsUpper}\p{IsLower})/, $entity);
347 }
348  
349 sub package_from_table {
350 9 ▶ my $table = shift;
Subroutine package_from_table: called CC=1 SCAR=0.0
Pod undocumented
351
352 9 join '', map {ucfirst} split /_/, $table;
353 }
354  
355 1;
356  
357  
358 __DATA__
« lib/Project/Easy/Config.pm ↑ Summary lib/Project/Easy/DB.pm »