blib/lib/FreePAN/Command.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 133 CC 10 SCAR 17.7
CC
10
Cov
0%
handle_reconfigure
12.0
prompt_info
12.0
process
6.0
CRAP 5.8
1 package FreePAN::Command;
2 use Spoon::Command -Base;
3 use FreePAN;
4  
5 field 'dist';
6 field author_name => 'Johnny XXX';
7 field author_email => '';
8  
9 sub all {
10 map { ($_, $self->$_) } qw(
11 author_name
12 author_email
13 );
14 }
15  
16 sub process {
17 return $self->first_time unless -d $self->hub->config->base;
18 $self->setup;
19 super;
20 }
21  
22 sub handle_reconfigure {
23 my $base = io->dir($self->hub->config->base);
24 if ($base->exists) {
25 unless (io->prompt('Do you really want to purge the existing configuration? ') =~ /^Y(ES)?$/) {
26 warn "Aborting reconfigure.\n";
27 return;
28 }
29 io->dir($self->hub->config->base)->rmtree;
30 }
31 $self->first_time;
32 }
33  
34 sub first_time {
35 $self->msg("\nFreePAN First Time User Setup:\n\n");
36 mkdir $self->hub->config->base or die "Can't create '$self->hub->config->base':\n $!";
37 my $base = io($self->hub->config->base)->chdir;
38 $self->install('config');
39 io('plugin')->mkdir;
40 my $config = $self->hub->config->config_class->new(
41 'config.yaml', -plugins => 'plugins',
42 );
43 $self->hub->config($config);
44 $self->create_registry;
45 $self->install_plugins;
46 $self->prompt_info;
47 $self->hub->template->path($self->hub->config->base);
48 $self->create_file('config.yaml');
49 $self->msg("\nFreePAN First Time User Setup is Complete.\n\n");
50 }
51  
52 sub prompt_info {
53 my $author_name = io->prompt('Enter your full name: ');
54 $self->author_name($author_name) if $author_name;
55 my $author_email = io->prompt('Enter your email address: ');
56 $self->author_email($author_email) if $author_email;
57 }
58  
59 sub create_file {
60 my $path = shift;
61 my $template_name = shift || io->file($path)->filename;
62 my $content = $self->hub->template->process($template_name,
63 $self->all,
64 );
65 my $output = io($path);
66 # XXX assert is broken without a directory path.
67 $output->assert if $path =~ /\//;
68 $output->print($content);
69 }
70  
71 sub setup {
72 $self->update_registry
73 if $self->registry_outdated;
74 $self->hub->registry->load;
75 }
76  
77 sub install {
78 my $class_id = shift;
79 my $object = $self->hub->$class_id
80 or return;
81 return unless $object->can('extract_files');
82 my $class_title = $self->hub->$class_id->class_title;
83 $self->msg("Extracting files for $class_title:\n");
84 $self->hub->$class_id->quiet($self->quiet);
85 $self->hub->$class_id->extract_files;
86 $self->msg("\n");
87 }
88  
89 sub install_plugins {
90 map {
91 $self->install($_->{id});
92 } @{$self->hub->registry->lookup->{plugins}};
93 }
94  
95 sub create_registry {
96 my $registry = $self->hub->registry;
97 my $registry_path = $registry->registry_path;
98 $self->msg("Generating FreePAN Registry '$registry_path'\n");
99 $registry->update;
100 if ($registry->validate) {
101 $registry->write;
102 }
103 $registry->load;
104 }
105  
106 sub registry_outdated {
107 my $base = io($self->hub->config->base)->chdir;
108 -M 'plugins' < -M 'registry.dd';
109 }
110  
111 sub update_registry {
112 $self->create_registry(@_);
113 }
114  
115 sub handle_all {
116 warn "-all not yet implemented\n";
117 }
118  
119 sub handle_update {
120 my $base = io($self->hub->config->base)->chdir;
121 $self->create_registry;
122 $self->install_plugins;
123 }
124  
125 sub usage {
126 warn <<END . $self->command_usage(" freepan -%-14s# %s\n");
127 usage:
128 freepan -update # Upate FreePAN configuration
129 END
130 }
131  
132 __DATA__