GX/Script.pm

filter: Statementstmt 25.1% 35 / 139 Branchbran 0.0% 0 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.0% 9 / 30 Podpod 0.0% 0 / 12 totaltotal 18.8% 44 / 233 CC 27 SCAR 28.2
CC
27
Cov
18%
create_file
56.0
_process_argv
42.0
create_directory
20.0
CRAP 16.8
« GX/View/Template/TT.pm ↑ Summary GX/File/Watcher.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Script.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Script;
7  
8 3 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use GX::Meta::Constants qw( REGEX_CLASS_NAME );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 # ----------------------------------------------------------------------------------------------------------------------
19 # Class setup
20 # ----------------------------------------------------------------------------------------------------------------------
21  
22 3 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has 'application_class' => (
25 isa => 'String',
26 0 ▶ constraint => sub { ! ref && $_ =~ REGEX_CLASS_NAME }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
not ref $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/ 0 0 0
27 3 );
28  
29 3 has 'application_path' => (
30 isa => 'Scalar',
31 initializer => '_initialize_application_path'
32 );
33  
34 has 'cwd' => (
35 isa => 'String',
36 initialize => 1,
37 0 ▶ initializer => sub { Cwd::cwd() }
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 3 );
39  
40 3 has 'options' => (
41 isa => 'Hash',
42 initialize => 1,
43 initializer => '_initialize_options',
44 accessor => { type => 'get_reference' }
45 );
46  
47 has 'script_name' => (
48 isa => 'String',
49 initialize => 1,
50 0 ▶ initializer => sub { File::Basename::basename( $_[0]->script_path ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 3 );
52  
53 has 'script_path' => (
54 isa => 'String',
55 initialize => 1,
56 0 ▶ initializer => sub { File::Spec->file_name_is_absolute( $0 ) ? $0 : File::Spec->rel2abs( $0 ) }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($0) ? : 0 0 0
57 3 );
58  
59 3 build;
60  
61  
62 # ----------------------------------------------------------------------------------------------------------------------
63 # Public methods
64 # ----------------------------------------------------------------------------------------------------------------------
65  
66 0 ▶sub chmod_file {
Subroutine chmod_file: not called CC=3 SCAR=24.8
Pod undocumented
67  
68 0 my $self = shift;
69 0 my $file = shift;
70 0 my $mode = shift;
71  
72 0 ▶ if ( ! File::Spec->file_name_is_absolute( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 0 0 0
73 0 $file = $self->construct_path( $file );
74 }
75  
76 0 ▶ chmod( $mode, $file ) or die "Cannot chmod() file \"$file\": $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, $file or die qq[Cannot chmod() file "$file": $!] 0 0 0
77  
78 0 $self->print_message( sprintf( " [+] $file -> chmod %o", $mode ) );
79  
80 0 return;
81  
82 }
83  
84 0 ▶sub construct_path {
Subroutine construct_path: not called CC=2 SCAR=17.9
Pod undocumented
85  
86 0 my $self = shift;
87 0 my @path = @_;
88  
89 0 ▶ my $base_path = $self->application_path or die "Cannot construct path";
Branch: 1 branch
BranchTrueFalseTotal
my $base_path = $self->application_path or die "Cannot construct path" 0 0 0
90  
91 0 return File::Spec->rel2abs( File::Spec->catfile( @path ), $base_path );
92  
93 }
94  
95 0 ▶sub create_directory {
Subroutine create_directory: not called CC=4 SCAR=30.0
Pod undocumented
96  
97 0 my $self = shift;
98 0 my $directory = shift;
99  
100 0 ▶ if ( ! File::Spec->file_name_is_absolute( $directory ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($directory)) 0 0 0
101 0 $directory = $self->construct_path( $directory );
102 }
103  
104 0 ▶ if ( -e $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $directory) 0 0 0
105 0 $self->print_message( " [-] $directory" );
106 0 return;
107 }
108  
109 0 ▶ File::Path::mkpath( $directory ) or die "Cannot create directory \"$directory\": $!";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::mkpath($directory) or die qq[Cannot create directory "$directory": $!] 0 0 0
110  
111 0 $self->print_message( " [+] $directory" );
112  
113 0 return 1;
114  
115 }
116  
117 0 ▶sub create_file {
Subroutine create_file: not called CC=7 SCAR=40.3
Pod undocumented
118  
119 0 my $self = shift;
120 0 my $file = shift;
121 0 my $content = shift;
122  
123 0 ▶ if ( ! File::Spec->file_name_is_absolute( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 0 0 0
124 0 $file = $self->construct_path( $file );
125 }
126  
127 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
128 0 $self->print_message( " [-] $file" );
129 0 return;
130 }
131  
132 0 my @path = File::Spec->splitpath( $file );
133 0 my $directory = File::Spec->catpath( $path[0], File::Spec->canonpath( $path[1] ), '' );
134  
135 0 ▶ if ( ! -e $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $directory) 0 0 0
136 0 ▶ File::Path::mkpath( $directory ) or die "Cannot create directory \"$directory\": $!";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::mkpath($directory) or die qq[Cannot create directory "$directory": $!] 0 0 0
137 }
138  
139 0 ▶ my $fh = IO::File->new( "> $file" ) or die "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new("> $file") or die "$!\n" 0 0 0
140 0 ▶ $fh->print( $content ) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
141 0 $fh->close;
142  
143 0 $self->print_message( " [+] $file" );
144  
145 0 return 1;
146  
147 }
148  
149 0 ▶sub exit {
Subroutine exit: not called CC=2 SCAR=17.9
Pod undocumented
150  
151 0 my $self = shift;
152 0 my $status = shift;
153  
154 0 ▶ CORE::exit( defined $status ? $status : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 0 0 0
155  
156 }
157  
158 0 ▶sub load_application {
Subroutine load_application: not called CC=3 SCAR=24.8
Pod undocumented
159  
160 0 my $self = shift;
161  
162 0 ▶ my $application_class = $self->application_class or die "No application specified";
Branch: 1 branch
BranchTrueFalseTotal
my $application_class = $self->application_class or die "No application specified" 0 0 0
163  
164 0 ▶ eval "require $application_class" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $application_class" or die $@ 0 0 0
165  
166 0 return;
167  
168 }
169  
170 0 ▶sub print_error {
Subroutine print_error: not called CC=1 SCAR=6.9
Pod undocumented
171  
172 0 my $self = shift;
173  
174 0 print join( "\n", @_ ) . "\n";
175  
176 0 return;
177  
178 }
179  
180 0 ▶sub print_message {
Subroutine print_message: not called CC=1 SCAR=6.9
Pod undocumented
181  
182 0 my $self = shift;
183  
184 0 print join( "\n", @_ ) . "\n";
185  
186 0 return;
187  
188 }
189  
190 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=0.0
Pod undocumented
191  
192 # Abstract method
193  
194 }
195  
196 0 ▶sub show_copyright {
Subroutine show_copyright: not called CC=1 SCAR=6.9
Pod undocumented
197  
198 0 my $self = shift;
199  
200 0 $self->print_message( <<EOT );
201 Copyright (c) 2009-2010 Jörg A. Uzarek.
202  
203 This program is free software: you can redistribute it and/or modify it under
204 the terms of the GNU General Public License Version 3 as published by the Free
205 Software Foundation.
206  
207 See the GNU General Public License for more details.
208 EOT
209  
210 0 return;
211  
212 }
213  
214 0 ▶sub show_help {
Subroutine show_help: not called CC=1 SCAR=6.9
Pod undocumented
215  
216 0 my $self = shift;
217  
218 0 $self->print_message( "Sorry, no help available." );
219  
220 0 return;
221  
222 }
223  
224 0 ▶sub show_version {
Subroutine show_version: not called CC=1 SCAR=2.9
Pod undocumented
225  
226 0 my $self = shift;
227  
228 0 my $class = ref $self;
229 0 ▶ my $version = do { no strict 'refs'; ${"${class}::VERSION"} };
Subroutine BEGIN: called CC=1 SCAR=1.2
230  
231 0 $self->print_message( "$class version $version" );
232  
233 0 return;
234  
235 }
236  
237  
238 # ----------------------------------------------------------------------------------------------------------------------
239 # Private methods
240 # ----------------------------------------------------------------------------------------------------------------------
241  
242 0 ▶sub _argv_options {
Subroutine _argv_options: not called CC=1 SCAR=6.9
243  
244 return (
245 0 'copyright',
246 'help|?',
247 'version'
248 );
249  
250 }
251  
252 0 ▶sub _initialize_application_path {
Subroutine _initialize_application_path: not called CC=3 SCAR=24.8
253  
254 0 my $self = shift;
255  
256 0 my ( $volume, $dirs, $file ) = File::Spec->splitpath( $self->script_path );
257  
258 0 my @dirs = File::Spec->splitdir( File::Spec->canonpath( $dirs ) );
259  
260 0 ▶ while ( @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
261 0 my $path = File::Spec->catpath( $volume, File::Spec->catdir( @dirs ), '' );
262 0 ▶ return $path if -d File::Spec->rel2abs( 'script', $path );
Branch: 1 branch
BranchTrueFalseTotal
if -d "File::Spec"->rel2abs("script", $path) 0 0 0
263 0 pop @dirs;
264 }
265  
266 0 return undef;
267  
268 }
269  
270 0 ▶sub _initialize_options {
Subroutine _initialize_options: not called CC=1 SCAR=6.9
271  
272 0 return {};
273  
274 }
275  
276 0 ▶sub _process_argv {
Subroutine _process_argv: not called CC=6 SCAR=37.4
277  
278 0 my $self = shift;
279 0 ▶ my @argv = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
280  
281 0 my %argv;
282  
283 0 ▶ if ( ! Getopt::Long::GetOptionsFromArray( \@argv, \%argv, $self->_argv_options ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Getopt::Long::GetOptionsFromArray(\@argv, \%argv, $self->_argv_options)) 0 0 0
284 0 $self->show_help;
285 0 $self->exit( 1 );
286 }
287  
288 0 ▶ if ( $argv{'help'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv{"help"}) 0 0 0
289 0 $self->show_help;
290 0 $self->exit;
291 }
292  
293 0 ▶ if ( $argv{'version'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv{"version"}) 0 0 0
294 0 $self->show_version;
295 0 $self->exit;
296 }
297  
298 0 ▶ if ( $argv{'copyright'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv{"copyright"}) 0 0 0
299 0 $self->show_copyright;
300 0 $self->exit;
301 }
302  
303 0 %{$self->options} = ( %{$self->options}, %argv );
304  
305 0 return @argv;
306  
307 }
308  
309 0 ▶sub _set_inc {
Subroutine _set_inc: not called CC=3 SCAR=24.8
310  
311 0 my $self = shift;
312  
313 0 ▶ if ( $self->application_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->application_path) 0 0 0
314  
315 0 my $lib_path = $self->construct_path( 'lib' );
316  
317 0 ▶ if ( -d $lib_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $lib_path) 0 0 0
318 0 unshift @INC, $lib_path;
319 }
320  
321 }
322  
323 0 return;
324  
325 }
326  
327  
328 3 1;
329  
330 __END__
« GX/View/Template/TT.pm ↑ Summary GX/File/Watcher.pm »