TaskPipe/Tool/Command_Setup.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 119 CC 5 SCAR 20.4
CC
5
Cov
0%
execute
20.0
change_home_filename
6.0
BEGIN
2.0
CRAP 7.7
1 package TaskPipe::Tool::Command_Setup;
2  
3 use Moose;
4 use TaskPipe::PodReader;
5 use TaskPipe::FileInstaller;
6 use TaskPipe::PathSettings::Global;
7 use File::Inplace;
8 use Try::Tiny;
9 use File::Save::Home;
10 use Module::Runtime 'require_module';
11 extends 'TaskPipe::Tool::Command';
12  
13 has option_specs => (is => 'ro', isa => 'ArrayRef', default => sub{[{
14 module => 'TaskPipe::PathSettings::Global',
15 items => [
16 'home_filename',
17 'root_dir',
18 'project_dir',
19 'global_dir',
20 'global_conf_dir',
21 'global_lib_dir',
22 'global_templates_dir',
23 'global_conf_filename',
24 'system_conf_filename'
25 ],
26 section => 'METHODS',
27 is_config => 0
28 }]});
29  
30 has job_manager => (is => 'ro', isa => 'TaskPipe::JobManager', lazy => 1, default => sub{
31 TaskPipe::JobManager->new(
32 name => $_[0]->name,
33 project => '*global',
34 shell => 'foreground'
35 );
36 });
37  
38 has pod_reader => (is => 'ro', isa => 'TaskPipe::PodReader', default => sub{
39 TaskPipe::PodReader->new;
40 });
41  
42 has template_names => (is => 'ro', isa => 'ArrayRef', default => sub{[
43 'Config_Global',
44 'Config_System'
45 ]});
46  
47 has file_installer => (is => 'rw', isa => 'TaskPipe::FileInstaller', lazy => 1, default => sub{
48 my ($self) = @_;
49  
50 TaskPipe::FileInstaller->new(
51 on_rollback => sub{
52 if ( $self->home_filename_changed ){
53 $self->change_home_filename( $self->existing_home_filename );
54 }
55 }
56 );
57 });
58  
59 has replacement_line => (is => 'ro', isa => 'Str', lazy => 1, default => sub{
60 return "has <param> => (is => 'ro', isa => 'Str', default => '<default>');";
61 });
62  
63 has home_filename_changed => (is => 'rw', isa => 'Bool', default => 0);
64  
65  
66 has ps => (is => 'ro', isa => 'TaskPipe::PathSettings::Global', default => sub{
67 TaskPipe::PathSettings::Global->new;
68 });
69  
70  
71 has existing_home_filename => (is => 'rw', isa => 'Str', lazy => 1, default => sub{
72 my ($self) = @_;
73 my $conf = MooseX::ConfigCascade::Util->conf;
74 MooseX::ConfigCascade::Util->conf({});
75 my $ps = TaskPipe::PathSettings::Global->new;
76 MooseX::ConfigCascade::Util->conf($conf);
77 return +$ps->home_filename;
78 });
79  
80  
81 sub execute{
82 my ($self) = @_;
83  
84 $self->run_info->scope('global');
85  
86 try {
87  
88 if ( $self->ps->home_filename ne $self->existing_home_filename ){
89 $self->change_home_filename( $self->ps->home_filename );
90 $self->home_filename_changed( 1 );
91 }
92  
93 my $home_fp = $self->path_settings->home_filepath;
94 if ( -f $home_fp ){
95  
96 $self->pod_reader->message( qq|=pod
97  
98 The file $home_fp already seems to exist on your system. Did you set up TaskPipe already? If you are sure TaskPipe has not already been set up, or you need a fresh install, you should delete this file.
99  
100 If this file exists because some other process created it, you will need to change the default filename which TaskPipe looks for in your home directory. You can do this by including the C<--home_filename> parameter on the command line:
101  
102 taskpipe --home_filename=.new_taskpipe_home_file
103  
104 Note that the C<--home_filename> parameter B<will change this filename system wide>. ie B<all users> will be affected by the change, so use with caution.
105  
106 |);
107 exit;
108  
109 }
110  
111 confess "root_dir parameter needs to be specified" unless $self->ps->root_dir;
112  
113 $self->file_installer->create_dir( $self->ps->root_dir );
114 $self->file_installer->create_file( $home_fp, $self->ps->root_dir );
115  
116 foreach my $dir ( qw(project_dir global_dir) ){
117 my $path = File::Spec->catdir( $self->ps->root_dir, $self->ps->$dir );
118 $self->file_installer->create_dir( $path );
119 }
120 foreach my $dir ( qw(lib conf log) ){
121 my $method = 'global_'.$dir.'_dir';
122  
123 my $path = File::Spec->catdir(
124 $self->ps->root_dir,
125 $self->ps->global_dir,
126 $self->ps->$method
127 );
128 $self->file_installer->create_dir( $path );
129 }
130  
131 foreach my $template_name ( @{$self->template_names} ){
132 my $module = 'TaskPipe::Template_'.$template_name;
133 require_module( $module );
134 my $template = $module->new;
135 $template->deploy;
136 unshift @{$self->file_installer->files_created}, +$template->target_path;
137 }
138
139 } catch {
140  
141 $self->file_installer->rollback;
142 confess "Rolled back changes: ".$_;
143  
144 }
145 }
146  
147  
148 sub change_home_filename{
149 my ($self,$fn) = @_;
150  
151 my $editor = new File::Inplace( file => +$self->ps->module_path );
152  
153 while ( my ($line) = $editor->next_line ){
154  
155 my $search_for = "has home_filename";
156 if ( $line =~ /^$search_for/ ){
157 my $replacement = $self->replacement_line;
158 $replacement =~ s/<param>/home_filename/;
159 $replacement =~ s/<default>/$fn/;
160 $editor->replace_line( $replacement );
161 }
162 }
163  
164 $editor->commit;
165 }
166  
167  
168  
169 =head1 NAME
170  
171 TaskPipe::Tool::Command_Setup - the TaskPipe setup command
172  
173 =head1 PURPOSE
174  
175 Setup is intended to be run once immediately after running package install (ie after installing the modules with C<cpan>, C<cpanm> or manually with C<make>). Setup creates the directories TaskPipe will use to store projects, and deploys the global configuration files.
176  
177  
178 =head1 DESCRIPTION
179  
180 You need to specify a value for C<root_dir> at a minimum, e.g.:
181  
182 taskpipe setup --root_dir=/home/myusername/taskpipe
183  
184 Subdirectories can also be specified on the command line, but will take defaults otherwise. (See options section below for default values)
185  
186 You can also use setup to regenerate global directories and/or default global config files if you want to change overall directory structure or reset the global config files to factory defaults. Use with caution. Setup will I<not> overwrite files or directories that already exist - it will ignore them. So you should delete the directories/files you want to regenerate prior to running setup.
187  
188 =head2 GENERAL TASKPIPE SETUP INSTRUCTIONS
189  
190 You may be looking at this help because you want to know how to setup TaskPipe in general. In fact the command C<taskpipe setup> does not completely setup TaskPipe on your system (sorry!) mainly because TaskPipe doesn't immediately know how to connect to your database.
191  
192 C<taskpipe setup> installs the skeleton directory structure and the global config file. You should run C<taskpipe setup> immediately after install, because C<taskpipe> needs to find the global config file to be able to do anything further.
193  
194 A suggested workflow to achieve a full setup and working projects is as follows:
195  
196 =over
197  
198 =item 1. Install TaskPipe
199  
200 Do this using the C<cpan> shell, C<cpanp>, C<cpanm> etc. or manually using make:
201  
202 perl Makefile.PL
203 make
204 make test
205 make install
206  
207 If you are reading this using the C<taskpipe help setup> command, you have already successfully completed this step.
208  
209 =item 2. Run C<setup>
210  
211 taskpipe setup --root_dir=~/taskpipe
212  
213 The C</taskpipe> subdirectory inside your B<home> directory is the suggested location to install TaskPipe and these docs will tend to assume this is your install location. However, any location where you have full read/write permissions is good.
214  
215 Note that TaskPipe installs a hidden file C<.taskpipe> in your home directory which will store the path to your TaskPipe install. You need to make sure C<TaskPipe> can create this file and it remains in place and readable.
216  
217 Once you have run setup, you should find the following directory structure is created
218  
219 /taskpipe
220 /global
221 /conf
222 global.conf
223 /lib
224 /projects
225  
226 =item 3. Set up the global database
227  
228 To do this
229  
230 =over
231  
232 =item *
233  
234 Create the database that you are going to use for TaskPipe global tables. E.g. in MySQL
235  
236 CREATE DATABASE taskpipe_global;
237  
238 =item *
239  
240 Edit the global config file to tell TaskPipe which database to use.
241  
242 nano ~/taskpipe/global/conf/global.conf
243  
244 Look for the section C<TaskPipe::SchemaManager::Settings_Global> and fill in C<host>, C<name> etc. for your database. Make sure the database user account you specified has full privileges to your TaskPipe global database.
245  
246 =item *
247  
248 Run C<deploy tables>:
249  
250 taskpipe deploy tables --scope=global
251  
252 More comprehensive information on this step can be found in the help for C<deploy tables>.
253  
254 =item *
255  
256 Generate global database schema files. TaskPipe uses the C<DBIx::Class> ORM, and schema files need creating over each database being used. To do this, you should just be able to type:
257  
258 taskpipe generate schema --scope=global
259  
260 Schema files will be generated into the global lib dir (normally C</global/lib>).
261  
262 More comprehensive information on this step can be found in the help for C<generate schema>
263  
264 =back
265  
266  
267 =item 4. Create your project
268  
269 To do this for a new project called C<myproject>:
270  
271 =over
272  
273 =item *
274  
275 Edit the global config file and change C<project> to C<myproject> in the section C<TaskPipe::PathSettings::Global>. This basically means you have set C<myproject> as the default project, so that you don't need to type C<--project=myproject> each time you execute a project-related TaskPipe command at the terminal.
276  
277 =item *
278  
279 Deploy project files:
280  
281 taskpipe deploy files
282  
283 See the help for C<deploy files> for more comprehensive information on this step.
284  
285 =item *
286  
287 Create a database to use for this project. E.g. in MySQL:
288  
289 CREATE DATABASE myproject;
290  
291 Make sure your database user account has full privileges to this database. E.g. in MySQL
292  
293 GRANT ALL PRIVILEGES ON myproject.* TO 'taskpipe_user'@'localhost';
294  
295 =item *
296  
297 Edit the project config file and change the database connection information (C<host>, C<name>, ...) to match the project database. (Look for the section C<TaskPipe::SchemaManager::Settings_Project> in your project config file (usually found in the C</projects/myproject/conf/project.yml> file.
298  
299 =item *
300  
301 Deploy the project cache tables:
302  
303 taskpipe deploy tables
304  
305 See the help for C<deploy tables> for more comprehensive information on this step.
306  
307 =item *
308  
309 Create the project itself. A project consists of several tasks, a plan of how to execute those tasks and some database tables to store gathered data. This is the meat and potatoes of TaskPipe, and here it is up to you to be creative. See the general instructions by reading the manpage for the L<TaskPipe> module or typing
310  
311 taskpipe help taskpipe
312  
313 on the command line for more information on how to create projects.
314  
315 =item *
316  
317 Run C<generate schema> over your project to create the project schema files
318  
319 taskpipe generate schema
320  
321 See the help for C<generate schema> for more information.
322  
323 =back
324  
325 =item 5. Run your plan
326  
327 Run your plan and gather your data:
328  
329 taskpipe run plan
330  
331 See the help for C<run plan> for more information on this step.
332  
333 =back
334  
335 =head1 OPTIONS
336  
337 =over
338  
339 =item root_dir
340  
341 The base directory where all TaskPipe files and subdirectories should be installed. B<This parameter is required>.
342  
343 =back
344  
345 =cut
346  
347 has root_dir => (is => 'ro', isa => 'Str');
348  
349 =head1 AUTHOR
350  
351 Tom Gracey <tomgracey@gmail.com>
352  
353 =head1 COPYRIGHT AND LICENSE
354  
355 Copyright (c) Tom Gracey 2018
356  
357 TaskPipe is free software, licensed under
358  
359 The GNU Public License Version 3
360  
361 =cut
362  
363 __PACKAGE__->meta->make_immutable;
364 1;