File Coverage

blib/lib/Games/Lacuna/Task/Setup.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Games::Lacuna::Task::Setup;
2              
3 1     1   1565 use 5.010;
  1         5  
  1         61  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   518 use Moose;
  0            
  0            
7             with qw(Games::Lacuna::Task::Role::Actions);
8              
9             use Games::Lacuna::Task::Utils qw(class_to_name name_to_class);
10             use Term::ANSIColor qw(color);
11             use Term::ReadLine;
12             use Try::Tiny;
13             use YAML::Any qw(DumpFile);
14              
15             has 'configfile' => (
16             is => 'rw',
17             isa => 'Path::Class::File',
18             required => 1,
19             );
20              
21             sub run {
22             my ($self) = @_;
23            
24             $self->sayline("=");
25            
26             $self->saycolor("bold cyan","Enter your empire name");
27             my $empire_name = $self->readline("Empire name:",qr/.+/);
28             $self->sayline();
29            
30             $self->saycolor("bold cyan","Enter your empire password (preferably your sitter passwort)");
31             my $empire_password = $self->readline("Password:",qr/.+/);
32             $self->sayline();
33            
34             $self->saycolor("bold cyan","Enter your e-mail address (required for e-mail notifications)");
35             my $email = $self->readline("E-Mail:",qr/.+\@.+/);
36             $self->sayline();
37            
38             $self->saycolor("bold cyan","Which tasks do you want to run regularly (e.g. every hour)");
39             say "Do not select task that you want to run less frequently (e.g. only once a day)\n";
40            
41             my %selected_tasks;
42             foreach my $task_class (sort $self->all_actions) {
43             my ($ok,$error) = $self->load_action($task_class);
44            
45             next
46             unless $ok;
47             next
48             if $task_class->meta->can('no_automatic')
49             && $task_class->meta->no_automatic;
50            
51             my $task_name = class_to_name($task_class);
52            
53             $self->saycolor("magenta bold",$task_name);
54             say $task_class->description;
55             if ($self->readline("Select task (y/n):",qr/^[yn]$/i) =~ /^[yY]$/) {
56             $selected_tasks{$task_name} = $task_class;
57             }
58             }
59            
60             $self->sayline();
61             $self->saycolor("bold cyan","The following task parameters require some kind of manual setup");
62             say "Please refer to the task documentation for details\n";
63            
64             while (my ($task_name,$task_class) = each %selected_tasks) {
65             foreach my $attribute ($task_class->meta->get_all_attributes) {
66             next
67             if $attribute->does('NoGetopt');
68             next
69             unless $attribute->is_required;
70             next
71             if $attribute->is_lazy_build || $attribute->has_default;
72             next
73             if $attribute->name eq 'email';
74             say color("magenta bold").$task_name.color("reset")." : Option '".$attribute->name."' needs manual setup.";
75             }
76             }
77            
78             $self->sayline();
79             $self->saycolor("bold cyan","Config written to ".$self->configfile->stringify);
80             say "You might want to customize the initial config file";
81            
82             my $config = {
83             connect => {
84             name => $empire_name,
85             password=> $empire_password,
86             },
87             global => {
88             task => [ keys %selected_tasks ],
89             email => $email,
90             },
91             };
92            
93             $self->sayline();
94             $self->saycolor("bold cyan","Please add the following line to your crontab:");
95             say "0 * * * * lacuna_task\n";
96             $self->saycolor("bold cyan","Optionally you can add other task that should be run less frequently e.g.");
97             say "5 8 * * * lacuna_run empire_report";
98            
99             DumpFile($self->configfile,$config);
100            
101             $self->sayline("=");
102            
103             return $config;
104             }
105              
106             sub sayline {
107             my ($self,$line) = @_;
108             $line ||= '-';
109             say $line x $Games::Lacuna::Task::Constants::SCREEN_WIDTH;
110             }
111              
112             sub saycolor {
113             my ($self,$color,$string) = @_;
114             say color($color).$string.color("reset");
115             }
116              
117             sub readline {
118             my ($self,$prompt,$expect) = @_;
119            
120             state $term ||= Term::ReadLine->new();
121             while (defined (my $response = $term->readline($prompt.' '))) {
122             if (defined $expect) {
123             return $response
124             if $response =~ $expect;
125             } else {
126             return $response
127             }
128             }
129             }
130              
131             __PACKAGE__->meta->make_immutable;
132             no Moose;
133             1;