WSST/Generator.pm

filter: Statementstmt 24.5% 39 / 159 Branchbran 11.3% 5 / 44 Conditioncond 26.6% 4 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 76.9% 10 / 13 Podpod 100% 3 / 3 totaltotal 25.9% 63 / 243 CC 37 SCAR 59.9
CC
37
Cov
22%
generate
716.7
__ANON__
20.0
_find_tmpl_dir
12.0
CRAP 398.1
↑ Summary WSST/Schema.pm »
1 package WSST::Generator;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(WSST::Generator);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use WSST::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use WSST::CodeTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.1.1';
12  
13 1 my $TMPL_PACKAGE_ID = 0;
14  
15 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
16 1 my $class = shift;
17 1 my $self = {@_};
18 1 ▶ $self->{tmpl_dir} ||= _find_tmpl_dir();
Condition33%1/3
$self->{"tmpl_dir"} ||= _find_tmpl_dir()
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"tmpl_dir"} ||= _find_tmpl_dir()
$self->{"tmpl_dir"} _find_tmpl_dir()
Truth table33%1/3
A ||= B
A= $self->{"tmpl_dir"}B= _find_tmpl_dir()
ABresult
1X1
011
000
19 1 return bless($self, $class);
20 }
21  
22 1 ▶sub generator_names {
Subroutine generator_names: called CC=5 SCAR=16.1
Pod documented
23 1 my $self = shift;
24
25 1 my $names = [];
26
27 opendir(DIR, $self->{tmpl_dir})
28 1 ▶ || die "failed opendir('$self->{tmpl_dir}'): $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $self->{"tmpl_dir"} or die qq[failed opendir('$self->{"tmpl_dir"}'): $!] 1 0 1
29 1 ▶ while (my $ent = readdir(DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = readdir DIR)) 5 1 6
30 5 ▶ next if $ent =~ /^\./ || ! -d "$self->{tmpl_dir}/$ent";
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /^\./ or not -d qq[$self->{"tmpl_dir"}/$ent] 3 2 5
Condition100%3/3
$ent =~ /^\./ or not -d qq[$self->{"tmpl_dir"}/$ent]
l!l&&r!l&&!r
212
MC/DC100%2/2
$ent =~ /^\./ or not -d qq[$self->{"tmpl_dir"}/$ent]
$ent =~ /^\./ not -d qq[$self->{"tmpl_dir"}/$ent]
Truth table100%3/3
A or not(B)
A= $ent =~ /^\./B= not -d qq[$self->{"tmpl_dir"}/$ent]
ABresult
1X1
011
000
31 2 push(@$names, $ent);
32 }
33 1 closedir(DIR);
34
35 1 return [sort @$names];
36 }
37  
38 0 ▶sub generate {
Subroutine generate: not called CC=27 SCAR=65.7
Pod documented
39 0 my $self = shift;
40 0 my $name = shift;
41 0 my $schema = shift;
42 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
43  
44 0 my $result = [];
45
46 0 my $vars = $schema->clone_data;
47 0 foreach my $opt_var (@{$opts->{var}}) {
48 0 my ($key, $val) = split(/=/, $opt_var, 2);
49 0 $vars->{$key} = $val;
50 }
51
52 0 my $tmpl_dir = "$self->{tmpl_dir}/$name";
53 0 my $tmpl = new WSST::CodeTemplate(tmpl_dirs => [$tmpl_dir],
54 vars => $vars);
55
56 0 ▶ my $odir = ($opts->{outdir} || "output") . "/$name";
Condition0%0/2
$opts->{'outdir'} || 'output'
l!l
00
MC/DC0%0/1
$opts->{'outdir'} || 'output'
$opts->{'outdir'}
Truth table0%0/2
A
A= $opts->{'outdir'}
Aresult
11
00
57 0 ▶ unless (-d $odir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $odir) 0 0 0
58 0 ▶ mkdir($odir) || die "failed mkdir('$odir'): $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $odir or die "failed mkdir('${odir}'): $!" 0 0 0
59 }
60  
61 0 my $files = [];
62 0 my $libs = [];
63 0 ▶ my $wanted = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
64 0 ▶ push(@$files, $File::Find::name) if /\.tmpl$/ && !/^inc_/;
Branch: 1 branch
BranchTrueFalseTotal
if /\.tmpl$/ and not /^inc_/ 0 0 0
Condition0%0/3
/\.tmpl$/ and not /^inc_/
!ll&&!rl&&r
000
MC/DC0%0/2
/\.tmpl$/ and not /^inc_/
/\.tmpl$/ not /^inc_/
Truth table0%0/3
A and not(B)
A= /\.tmpl$/B= not /^inc_/
ABresult
0X0
100
111
65 0 ▶ push(@$libs, $File::Find::name) if /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
if /\.pm$/ 0 0 0
66 0 };
67 0 find($wanted, $tmpl_dir);
68
69 0 ▶ return [] unless @$files;
Branch: 1 branch
BranchTrueFalseTotal
unless @$files 0 0 0
70
71 0 foreach my $method (@{$tmpl->get('methods')}) {
72 0 $method->{class_name} = ucfirst($method->{name});
73 0 $method->{class_name} =~ s/_(.)/uc($1)/eg;
74 0 $method->{interface_name} = $method->{name};
75 }
76
77 0 my $listeners = {};
78  
79 0 my $tmpl_pkg = undef;
80 0 ▶ if (@$libs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$libs) 0 0 0
81 0 my $tmpl_pkg_name = "TMPL" . $TMPL_PACKAGE_ID++;
82 0 $tmpl_pkg = "WSST::Generator::$tmpl_pkg_name";
83 {
84 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
85 0 *{"${tmpl_pkg}::schema"} = \$schema;
86 0 *{"${tmpl_pkg}::opts"} = \$opts;
87 0 *{"${tmpl_pkg}::tmpl"} = \$tmpl;
88 0 *{"${tmpl_pkg}::files"} = \$files;
89 0 *{"${tmpl_pkg}::libs"} = \$libs;
90 0 *{"${tmpl_pkg}::result"} = \$result;
91 0 *{"${tmpl_pkg}::tmpl_dir"} = \$tmpl_dir;
92 0 *{"${tmpl_pkg}::odir"} = \$odir;
93 0 *{"${tmpl_pkg}::listeners"} = \$listeners;
94 }
95 0 foreach my $lib (@$libs) {
96 0 eval qq{
97 package ${tmpl_pkg};
98 require '$lib';
99 };
100 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101 0 warn __PACKAGE__ . ": library load error: $@\n";
102 }
103 }
104 0 my $hash = \%{$WSST::Generator::{"${tmpl_pkg_name}::"}};
105 0 foreach my $key (keys %$hash) {
106 0 ▶ next if $tmpl->get($key);
Branch: 1 branch
BranchTrueFalseTotal
if $tmpl->get($key) 0 0 0
107 #$tmpl->set($key => $hash->{$key});
108 0 $tmpl->set($key => \&{$hash->{$key}});
109 }
110 }
111
112 0 foreach my $file (@$files) {
113 0 my $ofile = $file;
114 0 $ofile =~ s/^\Q$tmpl_dir\E//;
115 0 $ofile =~ s/\.tmpl$//;
116
117 0 my $fdir = dirname($file);
118 0 unshift(@{$tmpl->{tmpl_dirs}}, $fdir);
119 0 ▶ if ($file =~ /{method\.([a-zA-Z_][a-zA-Z0-9_]*)}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /{method\.([a-zA-Z_][a-zA-Z0-9_]*)}/) { } 0 0 0
120 0 foreach my $method (@{$tmpl->get('methods')}) {
121 0 my $ofile2 = $ofile;
122 0 ▶ $ofile2 =~ s/{method\.([a-zA-Z_][a-zA-Z0-9_]*)}/$method->{$1}||"{$1}"/eg;
Condition0%0/2
$method->{$1} || "{$1}"
l!l
00
MC/DC0%0/1
$method->{$1} || "{$1}"
$method->{$1}
Truth table0%0/2
A
A= $method->{$1}
Aresult
11
00
123 0 $ofile2 =~ s/{([a-zA-Z_][a-zA-Z0-9_]*)}/
124 0 my $val = $tmpl->get($1);
125 0 ▶ $val = &{$val}() if (ref $val eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "CODE" 0 0 0
126 0 $val;
127 /egx;
128 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
129 0 $ofile2 = $odir . $ofile2;
130
131 0 $tmpl->set('method' => $method);
132
133 0 my $tmpl_name = $file;
134 0 $tmpl_name =~ s#^\Q$tmpl_dir/\E##;
135 0 my $odata = $tmpl->expand($tmpl_name);
136
137 0 my $osubdir = dirname($ofile2);
138 0 ▶ unless (-d $osubdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $osubdir) 0 0 0
139 0 ▶ mkpath($osubdir)
Branch: 1 branch
BranchTrueFalseTotal
mkpath($osubdir) or die "failed mkpath('${osubdir}'): $!" 0 0 0
140 || die "failed mkpath('$osubdir'): $!";
141 }
142
143 0 ▶ open(OUT, ">$ofile2")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$ofile2" or die "failed open('>${ofile2}'): $!" 0 0 0
144 || die "failed open('>$ofile2'): $!";
145 0 print OUT $odata;
146 0 close(OUT);
147  
148 0 push(@$result, $ofile2);
149 }
150 } else {
151 0 $ofile =~ s/{([a-zA-Z_][a-zA-Z0-9_]*)}/
152 0 my $val = $tmpl->get($1);
153 0 ▶ $val = &{$val}() if (ref $val eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "CODE" 0 0 0
154 0 $val;
155 /egx;
156 0 $ofile = $odir . $ofile;
157  
158 0 my $tmpl_name = $file;
159 0 $tmpl_name =~ s#^\Q$tmpl_dir/\E##;
160 0 my $odata = $tmpl->expand($tmpl_name);
161
162 0 my $osubdir = dirname($ofile);
163 0 ▶ unless (-d $osubdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $osubdir) 0 0 0
164 0 ▶ mkpath($osubdir)
Branch: 1 branch
BranchTrueFalseTotal
mkpath($osubdir) or die "failed mkpath('${osubdir}'): $!" 0 0 0
165 || die "failed mkpath('$osubdir'): $!";
166 }
167
168 0 ▶ open(OUT, ">$ofile")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$ofile" or die "failed open('>${ofile}'): $!" 0 0 0
169 || die "failed open('>$ofile'): $!";
170 0 print OUT $odata;
171 0 close(OUT);
172
173 0 push(@$result, $ofile);
174 }
175 0 shift(@{$tmpl->{tmpl_dirs}});
176 }
177
178 0 foreach my $func (@{$listeners->{post_generate}}) {
179 0 &{$func}();
180 }
181
182 0 return $result;
183 }
184  
185 0 ▶sub _find_tmpl_dir {
Subroutine _find_tmpl_dir: not called CC=3 SCAR=24.8
186 0 foreach my $base_dir (@INC) {
187 0 my $dir = "$base_dir/WSST/Templates";
188 0 ▶ return $dir if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 0 0 0
189 }
190 0 return undef;
191 }
192  
193 =head1 NAME
194  
195 WSST::Generator - Generator class of WSST
196  
197 =head1 DESCRIPTION
198  
199 Generator is a template base generator.
200 The generate method of this class looks for
201 the template file from the template directory,
202 and processes those files.
203  
204 =head1 METHODS
205  
206 =head2 new
207  
208 Constructor.
209  
210 =head2 generator_names
211  
212 Returns generator name and alias.
213 This method returns subdirectory name of template directory.
214  
215 If the structure of template directory is as follows,
216 ["perl", "php"] is returned.
217  
218 templates/
219 |-- perl/
220 | `-- ...
221 `-- php/
222 `-- ...
223  
224 =head2 generate
225  
226 Generate library and return generated file names
227 by processing template files in template directory.
228  
229 =head1 SEE ALSO
230  
231 http://code.google.com/p/wsst/
232  
233 =head1 AUTHORS
234  
235 Mitsuhisa Oshikawa <mitsuhisa [at] gmail.com>
236 Yusuke Kawasaki <u-suke [at] kawa.net>
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 Copyright 2008 WSS Project Team
241  
242 =cut
243 1 1;
↑ Summary WSST/Schema.pm »