File Coverage

blib/lib/Script/Ichigeki/Hissatsu.pm
Criterion Covered Total %
statement 36 62 58.0
branch 1 12 8.3
condition 2 8 25.0
subroutine 12 16 75.0
pod 1 2 50.0
total 52 100 52.0


line stmt bran cond sub pod time code
1             package Script::Ichigeki::Hissatsu;
2 2     2   32234 use Mouse;
  2         78141  
  2         13  
3 2     2   733 use Mouse::Util::TypeConstraints;
  2         5  
  2         8  
4              
5 2     2   2412 use Encode;
  2         27994  
  2         245  
6 2     2   54482 use Time::Piece;
  2         36184  
  2         14  
7 2     2   2133 use Path::Class qw/file/;
  2         128577  
  2         209  
8 2     2   2440 use IO::Prompt::Simple qw/prompt/;
  2         4977  
  2         143  
9 2     2   16 use IO::Handle;
  2         5  
  2         91  
10 2     2   2158 use File::Tee qw/tee/;
  2         20788  
  2         154  
11 2     2   2179 use Term::Encoding qw(term_encoding);
  2         1557  
  2         838  
12              
13             subtype 'Time::Piece' => as Object => where { $_->isa('Time::Piece') };
14             coerce 'Time::Piece'
15             => from 'Str',
16             => via {
17             my $t = Time::Piece->strptime($_, '%Y-%m-%d');
18             die "Invalie time format: [$_] .(format should be '%Y-%m-%d'.)" unless $t;
19             localtime($t);
20             };
21              
22             has exec_date => (
23             is => 'ro',
24             isa => 'Time::Piece',
25             coerce => 1,
26             default => sub {
27             localtime(Time::Piece->strptime(localtime->ymd, "%Y-%m-%d"));
28             }
29             );
30              
31             has confirm_dialog => (
32             is => 'ro',
33             default => 1,
34             );
35              
36             has log_file_postfix => (
37             is => 'ro',
38             default => '.log',
39             );
40              
41             has script => (
42             is => 'ro',
43             default => sub { file($0) },
44             );
45              
46             has is_running => (
47             is => 'rw',
48             );
49              
50             has in_compilation => (
51             is => 'ro'
52             );
53              
54             has dialog_message => (
55             is => 'ro',
56             default => sub {
57             'Do you really execute `%s` ?';
58             }
59             );
60              
61 2     2   34 no Mouse;
  2         4  
  2         35  
62              
63             sub execute {
64 0     0 0 0 my $self = shift;
65              
66 0         0 my $now = localtime;
67 0         0 my $today = localtime(Time::Piece->strptime($now->ymd, "%Y-%m-%d"));
68 0 0       0 $self->_exiting('exec_date: '. $self->exec_date->strftime('%Y-%m-%d') .' is not today!') unless $self->exec_date == $today;
69              
70 0 0       0 $self->_exiting(sprintf('Can\'t execute! Execution log file [%s] already exists!', $self->_log_file)) if -f $self->_log_file;
71              
72 0 0       0 if ($self->confirm_dialog) {
73 0   0     0 my $enc = term_encoding || 'utf-8';
74 0         0 my $answer = prompt(encode($enc, sprintf($self->dialog_message, $self->script->basename) . ' [y/n] [n]'));
75 0 0       0 $self->_exiting('canceled.') unless $answer =~ /^y(?:es)?$/i;
76             }
77              
78 0         0 STDOUT->autoflush;
79 0         0 STDERR->autoflush;
80              
81 0         0 $self->_log(join "\n",
82             '# This log file is generated dy Script::Icigeki.',
83 0         0 "start: @{[localtime->datetime]}",
84             '---', ''
85             );
86              
87 0         0 $self->is_running(1);
88 0         0 tee STDOUT, $self->_log_fh;
89 0         0 tee STDERR, $self->_log_fh;
90             }
91              
92             {
93             my $_log_file;
94             sub _log_file {
95 4     4   182300 my $self = shift;
96 4   66     40 $_log_file ||= do {
97 1         6 my $script = $self->script;
98 1         8 $script->dir->file('.' . $script->basename . $self->log_file_postfix);
99             };
100             }
101              
102             my $_log_fh;
103             sub _log_fh {
104 0   0 0   0 $_log_fh ||= shift->_log_file->open('>>');
105             }
106             }
107              
108             sub _log {
109 0     0   0 shift->_log_fh->print(@_);
110             }
111              
112              
113             sub _exiting {
114 0     0   0 my ($self, $msg) = @_;
115              
116 0         0 $msg .= "\n";
117 0 0       0 if ($self->in_compilation) {
118 0         0 warn $msg;
119 0         0 exit 1;
120             }
121             else {
122 0         0 die $msg;
123             }
124             }
125              
126             sub DEMOLISH {
127 1     1 1 182197 my $self = shift;
128 1 50       292 if ($self->is_running) {
129 0           my $now = localtime->datetime;
130 0           $self->_log(join "\n",
131             '','---',
132             "end: $now",'',
133             );
134             }
135             }
136              
137             __PACKAGE__->meta->make_immutable;