lib/Rapi/Blog/Util/Rabl/Create.pm untested

filter: Statementstmt 0.0% 0 / 153 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 282 CC 26 SCAR 39.6
CC
26
Cov
0%
call
90.0
_recursive_copy
90.0
prompt_select_scaffold
42.0
CRAP 52.4
1 package Rapi::Blog::Util::Rabl::Create;
2 use strict;
3 use warnings;
4  
5 # ABSTRACT: Rabl module for creating new Rapi::Blog sites
6  
7 use Moo;
8 extends 'Rapi::Blog::Util::Rabl';
9  
10 use RapidApp::Util ':all';
11 use Rapi::Blog;
12  
13 use Path::Class qw(file dir);
14 use Scalar::Util 'blessed';
15 use Rapi::Blog::Util::Term::ReadPassword;
16  
17 sub call {
18 my $class = shift;
19 my $path = shift || $ARGV[0];
20 my $scaffold_name = shift || $ARGV[1] || 'bootstrap-blog';
21
22 $class->usage unless ($path);
23
24 my $Dir = dir($path);
25 if(-d $Dir) {
26 my $count = $Dir->children;
27 die "Target directory '$Dir' already exists and is not empty - aborting.\n" if ($count > 0);
28 }
29 else {
30 my $parent = $Dir->parent;
31 die "Parent dir '$parent' not found; automatic creation of parent dirs is not supported.\n"
32 unless (-d $parent);
33 print "==> creating directory '$Dir'";
34 $Dir->mkpath;
35 print "\n";
36 }
37
38 die 'app.psgi already exists!' if (-e $Dir->file('app.psgi')); # redundant
39
40 my $ScafDir = $class->prompt_select_scaffold($scaffold_name)
41 or die "Unable to locate skeleton scaffold -- unknown error";
42  
43 my $TargScaf = $Dir->subdir('scaffold');
44
45 print "\nInitializing scaffold using the built-in skeleton '$scaffold_name':\n ";
46 $class->_recursive_copy($ScafDir,$TargScaf);
47
48 print "\nCreating app.psgi: ";
49 $Dir->file('app.psgi')->spew( $class->_app_psgi_content );
50 print "done.\n\n";
51
52 if(my $pw = $class->prompt_password) {
53
54 my $App = Rapi::Blog->new({ site_path => "$Dir", debug => 0 });
55
56 print "\nInitializing databases, please wait...\n";
57 $App->to_app;
58
59 print "\n\n";
60
61 my $AdminUser = $App
62 ->appname
63 ->model('RapidApp::CoreSchema::User')
64 ->search({ 'me.username' => 'admin' })
65 ->first;
66
67 $AdminUser->update({ set_pw => $pw });
68 }
69
70 print join("",
71 ' *** SUCCESS! ***',"\n\n",
72 "New Rapi::Blog site sucessfully created in '$Dir' ... ","\n\n",
73 "If you would like to test and start the site using plackup now, type in 'up' or \n",
74 "a custom TCP port number (e.g. 5005) -- ('up' and '5000' are equivelent)\n",
75 "or anything else to exit.\n",
76 " ==> "
77 );
78 my $port = <STDIN>;
79 chomp($port);
80 $port = '5000' if ($port && $port eq 'up');
81
82 if ($port && $port =~ /^\d+$/) {
83 print "\n\nStarting the PSGI test server... once it finishes loading, you can access ";
84 print "the public site at http://0:$port/ and the admin section at: http://0:$port/adm\n";
85
86 my @cmd = ('plackup','-p',$port,"$Dir/app.psgi");
87 print "\n\n exec --> " . join(' ',@cmd) . "\n\n";
88 exec(@cmd);
89 exit; #redundant
90 }
91
92
93 print join("\n",'',
94 ' *** FINISHED! ***',"\n",
95 "When you're ready you may 'plackup' the test server:",'',
96 " plackup $Dir/app.psgi",'',
97 "Login to the admin section at: http://0:5000/adm (or whatever host/port you deploy to)",'',
98 "Next steps: ",'',
99 " - edit/customize your scaffold.yml and html files/content in '$TargScaf'",
100 '',"Happy blogging!",''
101 );
102 }
103  
104  
105 sub prompt_select_scaffold {
106 my $self = shift;
107 my $default = shift || '';
108
109 my @scaffolds = $self->discover_scaffolds;
110 my $num = scalar(@scaffolds);
111
112 print "\n** Choose a skeleton scaffold to use to initialize your new site...\n";
113 print " There are currently $num built-in scaffolds available:\n\n";
114
115 my $defN;
116 my $n = 0;
117 for my $Dir (@scaffolds) {
118 $n++;
119 my $name = $Dir->basename;
120 print " [ $n ] $name ";
121 if($name eq $default) {
122 $defN = $n;
123 print ' (**default**)';
124 }
125 print "\n";
126 }
127
128 print "\nType the number of the scaffold you would like to use\n";
129 print " (or hit Enter to use the default '$default')\n" if ($default && $defN);
130
131 my $SelDir;
132 until ($SelDir) {
133
134 print " ==> ";
135 my $input = <STDIN>;
136 chomp($input);
137
138 $input = $defN if(!$input && $defN);
139
140 unless ($input && $input =~ /^\d+$/) {
141 print " *** invalid selection\n";
142 next;
143 }
144
145 my $i = $input - 1;
146
147 $SelDir = $scaffolds[$i];
148 unless ($SelDir) {
149 print " *** invalid selection\n";
150 next;
151 }
152 }
153  
154 return $SelDir
155 }
156  
157  
158 sub discover_scaffolds {
159 my $self = shift;
160  
161 my $share_dir = Rapi::Blog->_get_share_dir or die "Unable to identify ShareDir for Rapi::Blog.\n";
162 my $Scafs = dir($share_dir)->subdir('scaffolds')->resolve;
163
164 grep { $_->isa('Path::Class::Dir') } $Scafs->children
165 }
166  
167  
168 sub prompt_password {
169 my $self = shift;
170
171 print " Set password for the 'admin' user [or leave blank for RapidApp/AuthCore default]:\n\n";
172
173
174 my $pw = read_password(" (New Password) ==> ",0,1);
175 if(! defined $pw) { # they hit ctrl-c
176 print " aborting.\n";
177 exit;
178 }
179
180 if ($pw eq '') {
181 print " *** Using RapidApp/AuthCore default admin password\n\n\n";
182 return undef;
183 }
184
185 my $pw2 = read_password(" (Confirm Password) ==> ",0,1);
186 if(! defined $pw2) {
187 print " aborting.\n";
188 exit;
189 }
190
191 unless($pw eq $pw2) {
192 print " ** passwords don't match **\n";
193 return $self->prompt_password;
194 }
195
196 return $pw;
197 }
198  
199  
200 sub _app_psgi_content {
201 my $self = shift;
202
203 return "# created by 'rabl.pl create'\n\nuse Rapi::Blog $Rapi::Blog::VERSION;\n\n" .
204
205 <<'--END--',
206 use Path::Class qw/file dir/;
207 my $dir = file($0)->parent->stringify;
208  
209 my $app = Rapi::Blog->new({
210 site_path => $dir,
211 scaffold_path => "$dir/scaffold"
212 });
213  
214 # Plack/PSGI app:
215 $app->to_app
216 --END--
217 ;
218  
219 }
220  
221  
222 sub _recursive_copy {
223 my $class = shift;
224 my ($src, $dst, $depth) = @_;
225 $depth ||= 0;
226
227 die "bad src '$src' - not a Path::Class::Dir" unless (
228 blessed $src && $src->isa('Path::Class::Dir')
229 );
230
231 die "bad dst '$dst' - not a Path::Class::Dir" unless (
232 blessed $dst && $dst->isa('Path::Class::Dir')
233 );
234
235 unless (-d $dst) {
236 die "Error -- refuse to sync to a dir with non-existing parent dir" unless (-d $dst->parent);
237 print ' ' x $depth;
238 $dst->mkpath(1);
239 }
240
241 my (@files,@dirs);
242
243 for my $itm ($src->children) {
244 die "unexpected child item '$itm'" unless (blessed $itm);
245 if($itm->isa('Path::Class::Dir')) {
246 push @dirs,$itm;
247 }
248 elsif($itm->isa('Path::Class::File')) {
249 push @files, $itm
250 }
251 else {
252 die "unexpected child item '$itm'"
253 }
254 }
255
256 # files first"
257 for my $itm (@files) {
258 my $tfile = $dst->file($itm->basename);
259 die "target file '$tfile' already exists!" if (-e $tfile);
260 print ' ' x $depth;
261 print ' - ' . $itm->relative($src);
262 $tfile->spew( scalar $itm->slurp );
263 print "\n";
264 }
265
266 for my $itm (@dirs) {
267 $class->_recursive_copy($itm, $dst->subdir( $itm->basename ),$depth + 1);
268 }
269 }
270  
271  
272  
273 1;
274  
275  
276 __END__