lib/MKDoc/Setup.pm

filter: Statementstmt 16.8% 21 / 125 Branchbran 18.7% 6 / 32 Conditioncond 9.5% 2 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 10.5% 2 / 19 Podpod 100% 16 / 16 totaltotal 21.3% 48 / 225 CC 20 SCAR 31.0
CC
20
Cov
16%
display_menu
72.0
label
20.0
display_value_delete
20.0
CRAP 22.1
« lib/MKDoc/Setup/Site.pm ↑ Summary lib/MKDoc/Setup/Core.pm »
1 =head1 NAME
2  
3 MKDoc::Setup - Generic user setup for MKDoc products.
4  
5 =head1 SUMMARY
6  
7 L<MKDoc::Setup> provides a generic base class to make user-friendly installation
8 procedures for applications which are meant to integrate in the L<MKDoc::Core>
9 framework.
10  
11 Although this class stands on its two feet, it is a mockup class. It is meant to be
12 subclassed for different installations.
13  
14 MKDoc::Setup doesn't do anything useful on it's own, however you can run it 'as is'
15 for testing purposes, e.g:
16  
17 perl -MMKDoc::Setup -e mockup
18  
19 If you want to see actual subclasses, look at L<MKDoc::Setup::MKDoc> and
20 L<MKDoc::Setup::Site>.
21  
22 =head1 Methods which you should know about
23  
24 =cut
25 package MKDoc::Setup;
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 # import MKDoc::Setup::<*>
31 1 foreach my $include_dir (@INC)
32 {
33 9 my $dir = "$include_dir/MKDoc/Setup";
34 9 ▶ if (-e $dir and -d $dir)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir and -d $dir) 3 6 9
Condition66%2/3
-e $dir and -d $dir
!ll&&!rl&&r
603
MC/DC50%1/2
-e $dir and -d $dir
-e $dir -d $dir
Truth table66%2/3
A and B
A= -e $dirB= -d $dir
ABresult
0X0
100
111
35 {
36 3 ▶ opendir DD, $dir or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir DD, $dir or do { warn "Cannot open directory $dir. Reason: $!"; next } 3 0 3
37 0 warn "Cannot open directory $dir. Reason: $!";
38 0 next;
39 };
40  
41 3 my @modules = map { s/\.pm$//; $_ }
42 grep /\.pm$/,
43 grep !/^\./,
44 readdir (DD);
45  
46 3 closedir DD;
47  
48 3 foreach my $module (@modules)
49 {
50 6 $module =~ /^(\w+)$/;
51 6 $module = $1;
52  
53 6 ▶ $INC{"MKDoc/Setup/$module.pm"} && next;
Branch: 1 branch
BranchTrueFalseTotal
$INC{"MKDoc/Setup/$module.pm"} and next 4 2 6
54 2 require "MKDoc/Setup/$module.pm";
55 2 ▶ $@ and warn "Cannot require module $module. Reason: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and warn "Cannot require module $module. Reason: $@" 0 2 2
56 }
57 }
58 }
59  
60  
61  
62 =head1 Other methods / functions
63  
64 =head2 main::mockup()
65  
66 Just a bit of syntaxic sugar to ease command line usage, i.e.
67  
68 perl -MMKDoc::Setup -e mockup
69  
70 re-write main::mockup() to main::somethingelse() in your
71 sub-classes.
72  
73 sub main::another { __PACKAGE__->new()->process() }
74  
75 You can then do:
76  
77 perl -MMKDoc::Setup -e another
78  
79 =cut
80 0 ▶sub main::mockup { __PACKAGE__->new()->process() }
Subroutine mockup: not called CC=1 SCAR=6.9
81  
82  
83  
84 =head2 $self->keys();
85  
86 You MUST subclass this method.
87  
88 In this method you define a list of keys which you want configuration values for
89 and in the order in which you want them displayed. For example:
90  
91 sub keys
92 {
93 my $self = shift;
94 return qw /foo bar baz/;
95 }
96  
97  
98 Might display something like this once run:
99  
100 1. Foo: Carrot
101 2. Bar: Tomato
102 3. Baz: Banana
103  
104 =cut
105 sub keys
106 0 ▶{
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
107 0 my $self = shift;
108 0 return qw /foo bar baz/;
109 }
110  
111  
112  
113 =head2 $self->label ($key);
114  
115 You MUST subclass this method.
116  
117 Returns a human readable label for a given key. In the example above, the
118 menu displays 'Foo' (notice the capital letter) for a key 'foo'. This is
119 done as follows:
120  
121 sub label
122 {
123 my $self = shift;
124  
125 $_ = shift;
126 /foo/ and return 'Foo';
127 /bar/ and return 'Bar';
128 /baz/ and return 'Baz';
129 return;
130 }
131  
132 =cut
133 sub label
134 0 ▶{
Subroutine label: not called CC=4 SCAR=30.0
Pod documented
135 0 my $self = shift;
136  
137 0 $_ = shift;
138 0 ▶ /foo/ and return 'Foo';
Branch: 1 branch
BranchTrueFalseTotal
/foo/ and return "Foo" 0 0 0
139 0 ▶ /bar/ and return 'Bar';
Branch: 1 branch
BranchTrueFalseTotal
/bar/ and return "Bar" 0 0 0
140 0 ▶ /baz/ and return 'Baz';
Branch: 1 branch
BranchTrueFalseTotal
/baz/ and return "Baz" 0 0 0
141 0 return;
142 }
143  
144  
145  
146 =head2 $self->install();
147  
148 You MUST subclass this method - assuming you want your installer to actually
149 install things :-)
150  
151 sub install
152 {
153 # some install code here...
154 print "\n\nDone.\n";
155 exit (0);
156 }
157  
158 =cut
159 sub install
160 0 ▶{
Subroutine install: not called CC=1 SCAR=6.9
Pod documented
161 0 print "\n\nDone.\n";
162 0 exit (0);
163 }
164  
165  
166  
167 =head2 $self->initialize();
168  
169 You MAY subclass this method.
170  
171 Initializes $self, the setup object. $self is a hash which contains key / value
172 pairs for setup config. This method gives you a chance to initialize your object
173 with sensible, default values before those values are presented to the user.
174  
175 =cut
176 sub initialize
177 0 ▶{
Subroutine initialize: not called CC=1 SCAR=6.9
Pod documented
178 0 my $self = shift;
179 0 $self->{foo} = 'Carrot';
180 0 $self->{bar} = 'Tomato';
181 0 $self->{baz} = 'Banana';
182 }
183  
184  
185  
186 =head2 $self->validate();
187  
188 You MAY subclass this method.
189  
190 If you must do any tests on the data to make sure it complies witch whichever
191 validation rules you choose to implement, you can do it by subclassing validate().
192  
193 Returns TRUE if the object validates, FALSE otherwise.
194  
195 I suggest your validate method looks like this:
196  
197 sub validate
198 {
199 my $self = shift;
200 $self->validate_xxx() &
201 $self->validate_yyy() &
202 $self->validate_zzz();
203 }
204  
205 sub validate_xxx
206 {
207 my $self = shift;
208 $self->some_failure_condition() and do {
209 print "Meaningful error message;
210 return 0;
211 };
212  
213 return 1;
214 }
215  
216 =cut
217 sub validate
218 0 ▶{
Subroutine validate: not called CC=3 SCAR=24.8
Pod documented
219 0 my $self = shift;
220  
221 # For subclasses which don't redefine the validate() method,
222 # this block of code does not exist. It's here for testing
223 # purposes only.
224 0 ▶ ref $self eq 'MKDoc::Setup' and do {
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq "MKDoc::Setup" and do { $self->{".seen"} or do { print "I will fail just once to test the error message\n"; $self->{".seen"} = 1; return } } 0 0 0
225 0 ▶ $self->{'.seen'} || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{".seen"} or do { print "I will fail just once to test the error message\n"; $self->{".seen"} = 1; return } 0 0 0
226 0 print "I will fail just once to test the error message\n";
227 0 $self->{'.seen'} = 1;
228 0 return;
229 };
230 };
231  
232 0 return 1;
233 }
234  
235  
236  
237 =head2 $self->display_value ($key);
238  
239 Returns the same as $self->{$key}, except when $self->{$key} is undef.
240  
241 When $self->{$key} is undef, the string "(undef") is sent back rather
242 than the value itself.
243  
244 This is used by the menu as a way to display the information 'undef'
245 to the user.
246  
247 =cut
248 sub display_value
249 0 ▶{
Subroutine display_value: not called CC=2 SCAR=17.9
Pod documented
250 0 my $self = shift;
251 0 my $key = shift;
252 0 my $val = $self->{$key};
253 0 ▶ return defined $val ? $val : '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
254 }
255  
256  
257  
258 =head2 $self->display_error();
259  
260 When the object does not validate, this method displays a message error
261 and waits for a keystroke before re-displaying the main menu.
262  
263 =cut
264 sub display_error
265 0 ▶{
Subroutine display_error: not called CC=1 SCAR=6.9
Pod documented
266 0 my $self = shift;
267 0 $self->message_prompt (
268 qq |It seems that this configuration is not correct.
269 Press enter to continue.| );
270  
271 0 $self->{'.state'} = 'display_menu';
272 }
273  
274  
275  
276 =head2 $class->new (%args);
277  
278 Constructor - Creates a new MKDoc::Setup object.
279  
280 =cut
281 sub new
282 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
283 0 my $class = shift;
284 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
285  
286 0 my $self = bless { @_ }, $class;
287 0 $self->initialize();
288 0 return $self;
289 }
290  
291  
292  
293 =head2 $self->process();
294  
295 Method which initializes the state of the object to 'display_menu' and starts
296 the FSM mechanism.
297  
298 =cut
299 sub process
300 0 ▶{
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
301 0 my $self = shift;
302 0 $self->initialize();
303  
304 0 my $state = 'display_menu';
305 0 $self->{'.state'} = $state;
306 0 while (1)
307 {
308 0 my $state = $self->{'.state'};
309 0 $self->$state();
310 }
311 }
312  
313  
314  
315 =head2 $self->display_menu();
316  
317 Displays the main user menu with a list of options to choose from.
318  
319 =cut
320 sub display_menu
321 0 ▶{
Subroutine display_menu: not called CC=8 SCAR=42.8
Pod documented
322 0 my $self = shift;
323  
324 0 system ('clear');
325  
326 0 my $count = 0;
327 0 my @keys = $self->keys();
328  
329 0 for (@keys)
330 {
331 0 $count++;
332 format STDOUT =
333 @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
334 $count . '. ' . $self->label ($_), $self->display_value ($_)
335 .
336  
337 0 write();
338 }
339
340 # print join "\n", map { ++$count . '. ' . $self->label ($_) . ': ' . $self->display_value ($_) } @keys;
341 0 print "\nD. Delete an option\n\n";
342
343 0 print "I. Install with the options above\n";
344
345 0 print "C. Cancel installation\n\n";
346
347 0 print "Input your choice: ";
348 0 my $line = $self->get_stdin_line();
349
350 0 ▶ $line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys and do { $self->{".selected"} = $line; $self->{".state"} = "display_value_set" } 0 0 0
Condition0%0/6
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys
!ll&&!rl&&r
000
$line =~ /^\d+$/ and $line > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys
$line =~ /^\d+$/ $line > 0 $line <= scalar @keys
Truth table0%0/4
A and B and C
A= $line =~ /^\d+$/B= $line > 0C= $line <= scalar @keys
ABCresult
0XX0
10X0
1100
1111
351 0 $self->{'.selected'} = $line;
352 0 $self->{'.state'} = 'display_value_set';
353 };
354  
355 0 ▶ $line =~ /^d$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^d$/i and do { delete $self->{".selected"}; $self->{".state"} = "display_value_delete" } 0 0 0
356 0 delete $self->{'.selected'};
357 0 $self->{'.state'} = 'display_value_delete';
358 };
359
360 0 ▶ $line =~ /^c$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^c$/i and do { delete $self->{".selected"}; $self->{".state"} = "cancel" } 0 0 0
361 0 delete $self->{'.selected'};
362 0 $self->{'.state'} = 'cancel';
363 };
364
365 0 ▶ $line =~ /^i$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^i$/i and do { delete $self->{".selected"}; $self->{".state"} = "proceed" } 0 0 0
366 0 delete $self->{'.selected'};
367 0 $self->{'.state'} = 'proceed';
368 };
369
370 }
371  
372  
373  
374 =head2 $self->display_value_set();
375  
376 When the user chooses to set a given value to another value, this
377 method is invoked. It basically prompts the user for the new value.
378  
379 =cut
380 sub display_value_set
381 0 ▶{
Subroutine display_value_set: not called CC=1 SCAR=6.9
Pod documented
382 0 my $self = shift;
383 0 my $index = $self->{'.selected'} - 1;
384 0 my @keys = $self->keys();
385 0 my $key = $keys[$index];
386 0 my $label = $self->label ($key);
387 0 my $disp = $self->display_value ($key);
388 0 my $value = $self->{$key};
389  
390 0 $value = $self->message_prompt ("$label [$disp]: ", $value);
391 0 $self->{$key} = $value;
392 0 $self->{'.state'} = 'display_menu';
393 }
394  
395  
396  
397 =head2 $self->display_value_delete();
398  
399 When the user chooses to delete some value, this method is invoked.
400 It basically prompts the user for which value to delete / undefine.
401  
402 =cut
403 sub display_value_delete
404 0 ▶{
Subroutine display_value_delete: not called CC=4 SCAR=30.0
Pod documented
405 0 my $self = shift;
406 0 my $line = $self->message_prompt ("Input value to delete: ", 'none');
407
408 0 my @keys = $self->keys();
409 0 ▶ $line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys and do { my $key = $keys[$line - 1]; delete $self->{$key} } 0 0 0
Condition0%0/6
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys
!ll&&!rl&&r
000
$line =~ /^\d+$/ and $line > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$line =~ /^\d+$/ and $line > 0 and $line <= scalar @keys
$line =~ /^\d+$/ $line > 0 $line <= scalar @keys
Truth table0%0/4
A and B and C
A= $line =~ /^\d+$/B= $line > 0C= $line <= scalar @keys
ABCresult
0XX0
10X0
1100
1111
410 0 my $key = $keys[$line - 1];
411 0 delete $self->{$key};
412 };
413
414 0 $self->{'.state'} = 'display_menu';
415 }
416  
417  
418  
419 =head2 $self->message_prompt ($message, $default_value);
420  
421 Prompts the user for a value by displaying $message. If the user just
422 hits 'enter', $default_value is returned instead.
423  
424 =cut
425 sub message_prompt
426 0 ▶{
Subroutine message_prompt: not called CC=2 SCAR=17.9
Pod documented
427 0 my $self = shift;
428 0 my $message = shift;
429 0 my $default = shift;
430 0 chomp ($message);
431 0 print "\n";
432 0 print $message;
433 0 my $line = $self->get_stdin_line();
434 0 ▶ return $line || $default;
Condition0%0/3
$line || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$line || $default
$line $default
Truth table0%0/3
A || B
A= $lineB= $default
ABresult
1X1
011
000
435 }
436  
437  
438  
439 =head2 $self->cancel();
440  
441 Cancels the installation process, exits to system.
442  
443 =cut
444 sub cancel
445 0 ▶{
Subroutine cancel: not called CC=1 SCAR=6.9
Pod documented
446 0 my $self = shift;
447 0 print "\nBye.\n";
448 0 exit(0);
449 }
450  
451  
452  
453 =head2 $self->proceed();
454  
455 Invokes $self->validate() first. If there are errors, invokes $self->display_error().
456  
457 Otherwise if everything's OK invokes $self->install() which performs the installation.
458  
459 =cut
460 sub proceed
461 0 ▶{
Subroutine proceed: not called CC=2 SCAR=17.9
Pod documented
462 0 my $self = shift;
463 0 my $check = $self->validate();
464 0 ▶ if ($check) { $self->{'.state'} = 'install' }
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 0 0 0
465 0 else { $self->{'.state'} = 'display_error' }
466 }
467  
468  
469  
470 =head2 $self->get_stdin_line();
471  
472 Reads one line from <STDIN>, chomps it, and returns it.
473  
474 =cut
475 sub get_stdin_line
476 0 ▶{
Subroutine get_stdin_line: not called CC=1 SCAR=6.9
Pod documented
477 0 my $self = shift;
478 0 my $line = <STDIN>;
479 0 chomp ($line);
480 0 return $line;
481 }
482  
483  
484 1 1;
485  
486  
487 __END__
« lib/MKDoc/Setup/Site.pm ↑ Summary lib/MKDoc/Setup/Core.pm »