Kusarigama/Hook.pm

filter: Statementstmt 45.9% 45 / 98 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 48.1% 13 / 27 Podpod 100% 5 / 5 totaltotal 47.0% 63 / 134 CC 8 SCAR 15.1
CC
8
Cov
44%
run_launch
20.0
__ANON__
6.0
run_exit
6.0
CRAP 4.5
« Kusarigama/Plugin/Command/AndAfter.pm ↑ Summary Kusarigama/App/Install.pm »
1 package Taskwarrior::Kusarigama::Hook;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Entry-point for Kusarigama's hook scripts
4 5 $Taskwarrior::Kusarigama::Hook::VERSION = '0.10.0';
5  
6 5 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use MooseX::MungeHas;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Hash::Merge qw/merge /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use List::AllUtils qw/ reduce pairmap pairmap /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use experimental 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 with 'Taskwarrior::Kusarigama::Core';
24  
25  
26 has raw_args => (
27 is => 'ro',
28 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 trigger => sub {
30 0 ▶ my( $self, $new ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
31  
32 0 ▶ pairmap { $self->$a($b) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 map { split ':', $_, 2 } @$new
34 },
35 5 );
36  
37 5 has exit_on_failure => (
38 is => 'ro',
39 default => 1,
40 );
41  
42  
43 has config => sub {
44 0 ▶ run3 [qw/ task rc.verbose=nothing rc.hooks=off show /], undef, \my $output;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _build_config: not called CC=1 SCAR=6.9
45 0 $output =~ s/^.*?---$//sm;
46 0 $output =~ s/^Some of your.*//mg;
47 0 $output =~ s/^\s+.*//mg;
48  
49 0 ▶ reduce { merge( $a, $b ) } map {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 ▶ reduce { +{ $b => $a } } $_->[1], reverse split '\.', $_->[0]
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 } map { [split ' ', $_, 2] } grep { /\w/ } split "\n", $output;
52 5 };
53  
54  
55 sub run_event {
56 0 ▶ my( $self, $event ) = @_;
Subroutine run_event: not called CC=1 SCAR=6.9
Pod documented
57  
58 0 my $method = join '_', 'run', $event;
59  
60 0 my @plugins = $self->plugins->@*;
61  
62 0 my @tasks = map { from_json($_) } <STDIN>;
63  
64 try {
65 0 ▶ $self->$method(\@plugins,@tasks);
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 }
67 catch {
68 0 ▶ say $_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 # TODO die instead of exit
70 0 ▶ exit 1 if $self->exit_on_failure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->exit_on_failure 0 0 0
71 0 };
72 }
73  
74 # TODO document how to abort the pipeline (by dying)
75 # TODO document 'feedback'
76  
77  
78 sub run_exit {
79 0 ▶ my( $self, $plugins, @tasks ) = @_;
Subroutine run_exit: not called CC=2 SCAR=17.9
Pod documented
80 0 $_->on_exit(@tasks) for grep { $_->DOES('Taskwarrior::Kusarigama::Hook::OnExit') } @$plugins;
81 }
82  
83  
84 sub run_launch {
85 0 ▶ my( $self, $plugins, @tasks ) = @_;
Subroutine run_launch: not called CC=4 SCAR=30.0
Pod documented
86  
87 0 for my $cmd ( grep { $_->DOES('Taskwarrior::Kusarigama::Hook::OnCommand') } @$plugins ) {
88 0 ▶ next unless $cmd->command_name eq $self->command;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd->command_name eq $self->command 0 0 0
89 0 $cmd->on_command(@tasks);
90 0 die sprintf "ran custom command '%s'\n", $cmd->command_name;
91 }
92  
93 0 $_->on_launch(@tasks) for grep { $_->DOES('Taskwarrior::Kusarigama::Hook::OnLaunch') } @$plugins;
94 }
95  
96  
97 sub run_add {
98 0 ▶ my( $self, $plugins, $task ) = @_;
Subroutine run_add: not called CC=2 SCAR=17.9
Pod documented
99 0 $_->on_add($task) for grep { $_->DOES('Taskwarrior::Kusarigama::Hook::OnAdd') } @$plugins;
100 0 say to_json($task);
101 }
102  
103  
104 # TODO document the $old, $new, $diff
105  
106 sub run_modify {
107 0 ▶ my( $self, $plugins, $old, $new ) = @_;
Subroutine run_modify: not called CC=2 SCAR=11.6
Pod documented
108 0 for( grep { $_->DOES('Taskwarrior::Kusarigama::Hook::OnModify') } @$plugins ) {
109 5 ▶ use Hash::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 0 my $diff = Hash::Diff::diff( $old, $new );
111 0 $_->on_modify( $new, $old, $diff );
112 }
113 0 say to_json($new);
114 }
115  
116 5 1;
117  
118 __END__
119  
120 =pod
121  
122 =encoding UTF-8
123  
124 =head1 NAME
125  
126 Taskwarrior::Kusarigama::Hook - Entry-point for Kusarigama's hook scripts
127  
128 =head1 VERSION
129  
130 version 0.10.0
131  
132 =head1 SYNOPSIS
133  
134 # most likely in one of the ~/.task/hooks/on-xxx.pl scripts
135  
136 use Taskwarrior::Kusarigama::Hook;
137  
138 Taskwarrior::Kusarigama::Hook->new(
139 raw_args => \@ARGV
140 )->run_event( 'launch' );
141  
142 =head1 DESCRIPTION
143  
144 This is the entry point for kusarigama when running it as a
145 Taskwarrior hook.
146  
147 =head1 METHODS
148  
149 =head2 new
150  
151 my $kusarigama = Taskwarrior::Kusarigama::Hook->new(
152 raw_args => [],
153 );
154  
155 Constructor. Recognizes the following arguments
156  
157 =over
158  
159 =item raw_args
160  
161 Reference to the list of arguments as passed to the taskwarrior hooks.
162  
163 =item exit_on_failure
164  
165 =for TODO We don't really need that, do we? Let's just die right there
166  
167 If the system should exit with an error code when one of the plugin
168 throws an exception (and thus abort the executiong of the remaining of the
169 taskwarrior
170 pipeline).
171  
172 Defaults to C<true>.
173  
174 =back
175  
176 =head2 config
177  
178 my $config = $kusarigama->config;
179  
180 Returns taskwarrior's configuration as C<task show> would.
181  
182 =head2 run_event
183  
184 $kusarigama->run_event( 'launch' );
185  
186 Runs all plugins associated with the provided stage.
187  
188 If C<exit_on_failure> is true, it will die if a plugin throws an
189 exception.
190  
191 =head2 run_exit
192  
193 $kusarigama->run_exit;
194  
195 Runs the exit stage part of the plugins.
196  
197 =head2 run_launch
198  
199 $kusarigama->run_launch;
200  
201 Runs the launch stage part of the plugins. Also intercepts and
202 run custom commands.
203  
204 =head2 run_add
205  
206 $kusarigama->run_add;
207  
208 Runs the add stage part of the plugins.
209  
210 =head2 run_modify
211  
212 $kusarigama->run_modify;
213  
214 Runs the modify stage part of the plugins.
215  
216 =head1 AUTHOR
217  
218 Yanick Champoux <yanick@cpan.org>
219  
220 =head1 COPYRIGHT AND LICENSE
221  
222 This software is copyright (c) 2018, 2017 by Yanick Champoux.
223  
224 This is free software; you can redistribute it and/or modify it under
225 the same terms as the Perl 5 programming language system itself.
226  
227 =cut
« Kusarigama/Plugin/Command/AndAfter.pm ↑ Summary Kusarigama/App/Install.pm »