bin/clearpress

filter: Statementstmt 31.0% 50 / 161 Branchbran 13.4% 7 / 52 Conditioncond 26.9% 7 / 26 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 73.3% 11 / 15 Podpod n/a totaltotal 28.6% 77 / 269 CC 43 SCAR 51.4
CC
43
Cov
27%
main
383.5
process_template
72.0
create_application
30.0
CRAP 169.9
↑ Summary blib/lib/ClearPress/driver/Pg.pm »
1 #!/usr/bin/perl -T
2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
4 #########
5 # Author: rmp
6 # Created: 2007-06-21
7 #
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Lingua::EN::Inflect qw(PL);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use lib qw(blib/lib lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use ClearPress;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 our $VERSION = q[2026.07.25];
19 our $ASPECTS = [qw(read list add create edit update)];
20  
21 local $ENV{PATH} = join q(:), qw(/bin /usr/bin /usr/local/bin /opt/local/bin);
22  
23 main();
24 0;
25  
26  
27 1 ▶sub main { ## no critic (complexity)
Subroutine main: called CC=23 SCAR=59.5
28 1 my $opts = {};
29 1 my @argvcopy = @ARGV;
30  
31 1 GetOptions($opts,
32 'new=s',
33 'driver=s',
34 'yes',
35 'help',
36 'version',
37 );
38  
39 1 ▶ if($opts->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"help"}) 0 1 1
40 0 ▶ print <<"EOT" or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print "Usage Example:\n $main::PROGRAM_NAME --new GrandKids 'child->family child(name,birthday:date) family(name,address,city,state,zip)'\n" or croak("Error printing: $main::ERRNO") 0 0 0
41 Usage Example:
42 $PROGRAM_NAME --new GrandKids 'child->family child(name,birthday:date) family(name,address,city,state,zip)'
43 EOT
44 0 return 1;
45 }
46  
47 1 ▶ if($opts->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"version"}) 0 1 1
48 0 ▶ print q(ClearPress v).ClearPress->VERSION.qq(\n) or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print 'ClearPress v' . "ClearPress"->VERSION . "\n" or croak("Error printing: $main::ERRNO") 0 0 0
49 0 return 1;
50 }
51  
52 1 ▶ ($opts->{driver}) = ($opts->{driver} || 'mysql') =~ /(SQLite|mysql)/smx;
Condition50%1/2
$opts->{'driver'} || 'mysql'
l!l
01
MC/DC0%0/1
$opts->{'driver'} || 'mysql'
$opts->{'driver'}
Truth table50%1/2
A
A= $opts->{'driver'}
Aresult
11
00
53  
54 1 ▶ if(!$opts->{new}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"new"}) 1 0 1
55 1 croak q(Please specify --new <application-name>);
56 }
57  
58 0 my ($app) = $opts->{new} =~ /^([[:lower:]][[:lower:][:digit:]_]+)$/smxi;
59 0 ▶ $app ||= q();
Condition0%0/2
$app ||= ""
l!l
00
MC/DC0%0/1
$app ||= ""
$app
Truth table0%0/2
A
A= $app
Aresult
11
00
60  
61 0 ▶ if($app ne $opts->{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app ne $opts->{"new"}) 0 0 0
62 0 croak q(Invalid characters in application name, only /[a-z][a-z\\d_]+/i allowed.);
63 }
64  
65 0 my $all_structure = shift @ARGV;
66  
67 0 my $schema = {};
68 0 my $driver_pkg = "ClearPress::driver::$opts->{driver}";
69  
70 0 eval "require $driver_pkg"; ## no critic (ProhibitStringyEval RequireCheckingReturnValueOfEval)
71 0 my $driver = $driver_pkg->new();
72  
73 0 for my $structure (split /\s+/smx, $all_structure) {
74 0 ▶ if($structure =~ /\S+[(]\S+[)]/smx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($structure =~ /\S+[(]\S+[)]/msx) { } 0 0 0
elsif ($structure =~ /\S+\->\S+/msx) { } 0 0 0
75 #########
76 # table definition
77 #
78 0 my ($table, $columns) = $structure =~ /(\S+)[(](\S+)[)]/smx;
79  
80 0 for my $column (split /,/smx, $columns) {
81 0 my ($name, $type) = split /:/smx, $column;
82 0 ▶ $name ||= $column;
Condition0%0/3
$name ||= $column
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= $column
$name $column
Truth table0%0/3
A ||= B
A= $nameB= $column
ABresult
1X1
011
000
83 0 ▶ $type = $driver->type_map($type||'char(128)');
Condition0%0/2
$type || 'char(128)'
l!l
00
MC/DC0%0/1
$type || 'char(128)'
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
84 0 $schema->{$table}->{fields}->{$name} = $type;
85  
86 0 ▶ print {*STDERR} qq($table has $name of type $type\n) or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} "$table has $name of type $type\n" or croak("Error printing: $main::ERRNO") 0 0 0
87 }
88  
89 } elsif($structure =~ /\S+\->\S+/smx) {
90 #########
91 # relationship
92 #
93 0 my ($one, $many) = $structure =~ /(\S+)\->(\S+)/smx;
94 0 push @{$schema->{$one}->{has_a}}, $many;
95 0 push @{$schema->{$many}->{has_many}}, $one;
96 0 ▶ print {*STDERR} qq($one has a $many\n$many has many @{[PL($one)]}\n) or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} "$one has a $many\n$many has many @{[PL($one)];}\n" or croak("Error printing: $main::ERRNO") 0 0 0
97 }
98 }
99  
100 0 my $template_cache = {};
101 0 read_templates($template_cache);
102  
103 create_application({
104 'template_cache' => $template_cache,
105 'application' => $app,
106 0 'views' => [keys %{$schema}],
107 'yes' => exists $opts->{yes},
108 'driver' => $opts->{driver},
109 0 'schema' => $schema,
110 });
111  
112 0 my $precedence = [map { $_->{name} }
113 sort _sorter
114 0 values %{$schema}];
115  
116 0 my $cfg = qq($app/config.layout);
117 0 ▶ open my $fh, q(>), $cfg or croak qq[Error opening $cfg: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cfg or croak("Error opening ${cfg}: $main::ERRNO") 0 0 0
118 0 ▶ print {$fh} $PROGRAM_NAME, (map { qq( '$_') } @argvcopy), qq(\n) or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $main::PROGRAM_NAME, map({" '${_}'";} @argvcopy), "\n" or croak("Error printing: $main::ERRNO") 0 0 0
119 0 ▶ close $fh or croak qq[Error closing $cfg: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ${cfg}: $main::ERRNO") 0 0 0
120  
121 0 ▶ if($opts->{driver} eq 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'driver'} eq 'mysql') { } 0 0 0
122 #########
123 # mysql message
124 #
125 0 ▶ print <<"EOT" or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print qq[You now need to configure your database.\n1. Check and/or modify $app/data/config.ini\n2. If necessary create a database, something like this:\n mysqladmin -uroot create $app\n3. cat @{[map({"$app/data/schema/$_.mysql \\\n ";} @{$precedence;})];} | mysql -uroot $app\n\nNote you may need to create your new schema in order, depending on your foreign key constraints.\n] or croak("Error printing: $main::ERRNO") 0 0 0
126 You now need to configure your database.
127 1. Check and/or modify $app/data/config.ini
128 2. If necessary create a database, something like this:
129 mysqladmin -uroot create $app
130 0 3. cat @{[map { "$app/data/schema/$_.mysql \\\n " } @{$precedence}]} | mysql -uroot $app
131  
132 Note you may need to create your new schema in order, depending on your foreign key constraints.
133 EOT
134  
135 } else {
136 #########
137 # SQLite message
138 #
139 0 ▶ print <<"EOT" or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print "You now need to configure your database.\n1. Check and/or modify $app/data/config.ini\n2. If necessary create a database, something like this:\n cat $app/data/schema/*.SQLite | sqlite3 $app/$app\n" or croak("Error printing: $main::ERRNO") 0 0 0
140 You now need to configure your database.
141 1. Check and/or modify $app/data/config.ini
142 2. If necessary create a database, something like this:
143 cat $app/data/schema/*.SQLite | sqlite3 $app/$app
144 EOT
145 }
146  
147 0 return 1;
148 }
149  
150 0 ▶sub read_templates {
Subroutine read_templates: not called CC=4 SCAR=30.0
151 0 my $cache = shift;
152 0 local $RS = "\n-- \n";
153  
154 0 ▶ if(!scalar keys %{$cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$cache;}) 0 0 0
155 0 for my $field (qw(config schema_mysql schema_SQLite application_sa application_cgi util model view view_error),
156 0 (map { "aspect_$_" } @{$ASPECTS}),
157 qw(actions warnings stylesheet)) {
158 0 my $str = <DATA>;
159 0 $str =~ s/$RS//smx;
160 0 $cache->{$field} = \$str;
161 0 ▶ print qq(Read @{[length($str)]} bytes for $field\n) or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print "Read @{[length $str];} bytes for $field\n" or croak("Error printing: $main::ERRNO") 0 0 0
162 }
163 }
164 0 return 1;
165 }
166  
167 0 ▶sub create_application {
Subroutine create_application: not called CC=5 SCAR=34.0
168 0 my $opts = shift;
169 0 my $app = $opts->{application};
170 0 my $cache = $opts->{template_cache};
171 0 my $driver = $opts->{driver};
172 0 my $schema = $opts->{schema};
173 0 my $tt = Template->new({
174 EVAL_PERL => 1,
175 TAG_STYLE => 'asp',
176 });
177  
178 0 for my $view (@{$opts->{views}}) {
179 0 $schema->{$view}->{name} = $view;
180 0 $opts->{name} = $view;
181 $opts->{fields} = [map { {name => $_,
182 0 type => $schema->{$view}->{fields}->{$_}};
183 0 } sort keys %{$schema->{$view}->{fields} }];
184 0 ▶ $opts->{has_many} = $schema->{$view}->{has_many} || [];
Condition0%0/2
$schema->{$view}{'has_many'} || []
l!l
00
MC/DC0%0/1
$schema->{$view}{'has_many'} || []
$schema->{$view}{'has_many'}
Truth table0%0/2
A
A= $schema->{$view}{'has_many'}
Aresult
11
00
185 0 ▶ $opts->{has_a} = $schema->{$view}->{has_a} || [];
Condition0%0/2
$schema->{$view}{'has_a'} || []
l!l
00
MC/DC0%0/1
$schema->{$view}{'has_a'} || []
$schema->{$view}{'has_a'}
Truth table0%0/2
A
A= $schema->{$view}{'has_a'}
Aresult
11
00
186  
187 0 process_template($opts, $tt, $cache->{"schema_$driver"}, "$app/data/schema", "$view.$driver");
188  
189 0 process_template($opts, $tt, $cache->{model}, "$app/lib/$app/model", "$view.pm");
190 0 process_template($opts, $tt, $cache->{view}, "$app/lib/$app/view", "$view.pm");
191  
192 0 for my $aspect (@{$ASPECTS}) {
193 0 process_template($opts, $tt, $cache->{"aspect_$aspect"}, "$app/data/templates", "${view}_$aspect.tt2");
194 }
195 }
196  
197 0 process_template($opts, $tt, $cache->{util}, "$app/lib/$app", 'util.pm');
198  
199 0 process_template($opts, $tt, $cache->{view_error}, "$app/lib/$app/view", 'error.pm');
200  
201 0 process_template($opts, $tt, $cache->{config}, "$app/data", 'config.ini');
202 0 process_template($opts, $tt, $cache->{application_cgi}, "$app/cgi-bin", $app);
203 0 process_template($opts, $tt, $cache->{application_sa}, "$app/bin", $app);
204 0 process_template($opts, $tt, $cache->{stylesheet}, "$app/htdocs", "$app.css");
205 0 process_template($opts, $tt, $cache->{actions}, "$app/data/templates", 'actions.tt2');
206 0 process_template($opts, $tt, $cache->{warnings}, "$app/data/templates", 'warnings.tt2');
207  
208 0 return 1;
209 }
210  
211 0 ▶sub _yn {
Subroutine _yn: not called CC=2 SCAR=17.9
212 0 my ($default) = @_;
213 0 local $RS = "\n";
214 0 my $response = <>;
215 0 chomp $response;
216 0 ▶ $response ||= $default;
Condition0%0/3
$response ||= $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$response ||= $default
$response $default
Truth table0%0/3
A ||= B
A= $responseB= $default
ABresult
1X1
011
000
217 0 return (uc $response eq uc $default)
218 }
219  
220 0 ▶sub process_template {
Subroutine process_template: not called CC=8 SCAR=42.8
221 0 my ($opts, $tt, $tmpl, $path, $fn) = @_;
222  
223 0 $fn = "$path/$fn";
224  
225 0 ▶ if(!$opts->{yes} && -e $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"yes"} and -e $fn) 0 0 0
Condition0%0/3
not $opts->{"yes"} and -e $fn
!ll&&!rl&&r
000
MC/DC0%0/2
not $opts->{"yes"} and -e $fn
not $opts->{"yes"} -e $fn
Truth table0%0/3
not(A) and B
A= not $opts->{"yes"}B= -e $fn
ABresult
0X0
100
111
226 0 ▶ print "$fn exists. Overwrite? [y/N] " or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print "$fn exists. Overwrite? [y/N] " or croak("Error printing: $main::ERRNO") 0 0 0
227 0 ▶ _yn('N') and return 1;
Branch: 1 branch
BranchTrueFalseTotal
_yn("N") and return 1 0 0 0
228 }
229  
230 0 system qw(mkdir -p), $path;
231 0 ▶ open my $fh, q[>], $fn or croak "Opening $fn: $ERRNO";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fn or croak("Opening ${fn}: $main::ERRNO") 0 0 0
232 0 ▶ $tt->process($tmpl, $opts, $fh) or croak "Template error building $fn: ".$tt->error(). "\nTemplate was:\n".${$tmpl}."\n";
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($tmpl, $opts, $fh) or croak("Template error building ${fn}: " . $tt->error . "\nTemplate was:\n" . ${$tmpl;} . "\n") 0 0 0
233 0 ▶ close $fh or croak "Closing $fn: $ERRNO";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Closing ${fn}: $main::ERRNO") 0 0 0
234 0 return 1;
235 }
236  
237 16 ▶sub _sorter {
Subroutine _sorter: called CC=6 SCAR=17.9
238 16 ▶ my $a_deps = [@{$a->{has_a}||[]}];#, @{$a->{has_many}||[]}];
Condition100%2/2
$main::a->{'has_a'} || []
l!l
106
MC/DC100%1/1
$main::a->{'has_a'} || []
$main::a->{'has_a'}
Truth table100%2/2
A
A= $main::a->{'has_a'}
Aresult
11
00
239 16 ▶ my $b_deps = [@{$b->{has_a}||[]}];#, @{$b->{has_many}||[]}];
Condition100%2/2
$main::b->{'has_a'} || []
l!l
88
MC/DC100%1/1
$main::b->{'has_a'} || []
$main::b->{'has_a'}
Truth table100%2/2
A
A= $main::b->{'has_a'}
Aresult
11
00
240  
241 16 ▶ if(scalar grep { $_ eq $b->{name} } @{$a_deps}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq $main::b->{"name"};} @{$a_deps;})) 5 11 16
242 5 return 1;
243 }
244  
245 11 ▶ if(scalar grep { $_ eq $a->{name} } @{$b_deps}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq $main::a->{"name"};} @{$b_deps;})) 5 6 11
246 5 return -1; ## no critic (ProhibitMagicNumbers)
247 }
248  
249 6 ▶ return (scalar @{$a_deps} <=> scalar @{$b_deps} || $a->{name} cmp $b->{name});
Condition66%2/3
scalar @{$a_deps;} <=> scalar @{$b_deps;} || $main::a->{'name'} cmp $main::b->{'name'}
l!l&&r!l&&!r
420
MC/DC0%0/2
scalar @{$a_deps;} <=> scalar @{$b_deps;} || $main::a->{'name'} cmp $main::b->{'name'}
scalar @{$a_deps;} <=> scalar @{$b_deps;} $main::a->{'name'} cmp $main::b->{'name'}
Truth table66%2/3
A || B
A= scalar @{$a_deps;} <=> scalar @{$b_deps;}B= $main::a->{'name'} cmp $main::b->{'name'}
ABresult
1X1
011
000
250 }
251  
252 __END__
↑ Summary blib/lib/ClearPress/driver/Pg.pm »