Kusarigama/Wrapper.pm

filter: Statementstmt 58.9% 56 / 95 Branchbran 45.8% 11 / 24 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 87.5% 14 / 16 Podpod 33.3% 1 / 3 totaltotal 57.0% 89 / 156 CC 17 SCAR 26.6
CC
17
Cov
55%
RUN
56.0
_parse_args
5.0
_map_to_arg
4.1
CRAP 14.3
« Kusarigama/Plugin/Command/Github.pm ↑ Summary Kusarigama/Plugin/Command/ButBefore.pm »
1 package Taskwarrior::Kusarigama::Wrapper;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: interface to the taskwarrior's 'task' command
4 5 $Taskwarrior::Kusarigama::Wrapper::VERSION = '0.11.0';
5  
6 # TODO use Test::Pod::Snippet for that example ^^^
7  
8  
9 5 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use IPC::Open3 qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Taskwarrior::Kusarigama::Wrapper::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Taskwarrior::Kusarigama::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use List::Util qw/ pairmap /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use experimental 'signatures', 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 has task => (
23 is => 'ro',
24 1 ▶ default => sub { 'task' },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 5 );
26  
27 has $_ => (
28 is => 'rw',
29 clearer => 1,
30 5 ) for qw/ ERR OUT /;
31  
32 5 our $DEBUG;
33  
34 ▶sub RUN($self,$cmd,@args) {
Subroutine RUN: not called CC=7 SCAR=40.3
Pod undocumented
35  
36 0 $self->clear_OUT;
37 0 $self->clear_ERR;
38  
39 0 my( $parts , $stdin ) = $self->_parse_args( $cmd , @args );
40  
41 0 my @cmd = ( $self->task , @$parts );
42  
43 0 my( @out , @err );
44  
45 {
46  
47 0 my ($wtr, $rdr, $err);
48  
49 0 local *TEMP;
50  
51 0 ▶ if ($^O eq 'MSWin32' && defined $stdin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and defined $stdin) 0 0 0
Condition0%0/3
$^O eq "MSWin32" and defined $stdin
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and defined $stdin
$^O eq "MSWin32" defined $stdin
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= defined $stdin
ABresult
0X0
100
111
52 0 my $file = File::Temp->new;
53 0 $file->autoflush(1);
54 0 $file->print($stdin);
55 0 $file->seek(0,0);
56 0 open TEMP, '<&=', $file;
57 0 $wtr = '<&TEMP';
58 0 undef $stdin;
59 }
60  
61 0 $err = Symbol::gensym;
62  
63 0 ▶ print STDERR join(' ',@cmd),"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Taskwarrior::Kusarigama::Wrapper::DEBUG 0 0 0
64  
65  
66 0 my $pid = IPC::Open3::open3($wtr, $rdr, $err, @cmd);
67  
68 0 ▶ print $wtr $stdin if defined $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdin 0 0 0
69 0 close $wtr;
70  
71 0 chomp(@out = <$rdr>);
72 0 chomp(@err = <$err>);
73  
74 0 waitpid $pid, 0;
75 };
76  
77 0 ▶ print "status: $?\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Taskwarrior::Kusarigama::Wrapper::DEBUG 0 0 0
78  
79 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
80 0 die Taskwarrior::Kusarigama::Wrapper::Exception->new(
81 output => \@out,
82 error => \@err,
83 status => $? >> 8,
84 );
85 }
86  
87 0 chomp(@err);
88 0 $self->ERR(\@err);
89  
90 0 chomp(@out);
91 0 $self->OUT(\@err);
92  
93 0 return @out;
94
95 }
96  
97 ▶sub _map_to_arg ( $self, $entry ) {
Subroutine _map_to_arg: called CC=4 SCAR=14.1
98  
99 10 ▶ if( not ref $entry ) { # simple string
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $entry) 3 7 10
100 # extract the attributes so that they are not dealt
101 # with as part of the definition
102 3 my %opts;
103
104 3 ▶ while ( $entry =~ s/\b(?<key>[^\s:]+):(?<value>\S+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ s/\b(?<key>[^\s:]+):(?<value>\S+)//u) 0 3 3
105 0 $opts{ $+{key} } = $+{value};
106 }
107 3 return $entry, $self->_map_to_arg(\%opts);
108 }
109  
110 7 ▶ return $entry unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 7 7
111  
112 7 ▶ return pairmap { join( ( $a =~ /^rc/ ? '=' : ':' ), $a, $b ) } %$entry;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^rc/u ? : 1 3 4
113 }
114  
115 ▶sub _parse_args($self,$cmd,@args) {
Subroutine _parse_args: called CC=5 SCAR=16.2
116 3 my @command = ( $cmd );
117  
118 # arrayrefs are for pre-command arguments, like
119 # task 123 list => ( 'list', [ 123 ] )
120 3 ▶ if( @args and ref $args[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "ARRAY") 2 1 3
Condition66%2/3
@args and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
012
MC/DC50%1/2
@args and ref $args[0] eq "ARRAY"
@args ref $args[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
121 2 unshift @command, map { $self->_map_to_arg($_) } ( shift @args )->@*;
122 }
123  
124 3 my @stdin;
125 3 ▶ push @stdin, ${pop @args} if @args and ref $args[-1] eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if @args and ref $args[-1] eq "SCALAR" 1 2 3
Condition66%2/3
@args and ref $args[-1] eq "SCALAR"
!ll&&!rl&&r
021
MC/DC50%1/2
@args and ref $args[-1] eq "SCALAR"
@args ref $args[-1] eq "SCALAR"
Truth table66%2/3
A and B
A= @argsB= ref $args[-1] eq "SCALAR"
ABresult
0X0
100
111
126  
127 3 return ( [ @command, map { $self->_map_to_arg($_) } @args ], @stdin );
128 }
129  
130 sub save {
131 1 ▶ my( $self, $task ) = @_;
Subroutine save: called CC=2 SCAR=7.0
Pod undocumented
132  
133 1 require JSON;
134  
135 1 ▶ my $id = $task->{uuid} || '+LATEST';
Condition50%1/2
$task->{'uuid'} || '+LATEST'
l!l
01
MC/DC0%0/1
$task->{'uuid'} || '+LATEST'
$task->{'uuid'}
Truth table50%1/2
A
A= $task->{'uuid'}
Aresult
11
00
136  
137 1 my $json = JSON::to_json([ $task ]);
138  
139 1 $self->RUN('import', \$json );
140  
141 1 my ( $new ) = $self->export($id);
142  
143 1 return $new;
144 }
145  
146 sub export {
147 0 ▶ my( $self, @args ) = @_;
Subroutine export: not called CC=1 SCAR=6.9
Pod documented
148 0 require JSON;
149  
150 return map {
151 0 Taskwarrior::Kusarigama::Task->new( $self => $_ )
152 } JSON::from_json( join '', $self->RUN( export => @args ) )->@*;
153 }
154  
155 sub AUTOLOAD {
156 1 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=2 SCAR=8.4
157  
158 1 (my $meth = our $AUTOLOAD) =~ s/.+:://;
159 1 ▶ return if $meth eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $meth eq "DESTROY" 1 0 1
160  
161 0 $meth =~ s/(?<=.)_/-/;
162  
163 0 return $self->RUN($meth, @_);
164 }
165  
166 5 1;
167  
168 __END__
169  
170 =pod
171  
172 =encoding UTF-8
173  
174 =head1 NAME
175  
176 Taskwarrior::Kusarigama::Wrapper - interface to the taskwarrior's 'task' command
177  
178 =head1 VERSION
179  
180 version 0.11.0
181  
182 =head1 SYNOPSIS
183  
184 use TaskWarrior::Kusarigama::Wrapper;
185  
186 my $tw = TaskWarrior::Kusarigama::Wrapper->new;
187  
188 say for $tw->next( [ '+focus' ] );
189  
190 =head1 DESCRIPTION
191  
192 Inspired by L<Git::Wrapper> (i.e., I lifted and stole
193 the code, and tweaked to work with 'task'). At its core
194 beats a dark AUTOLOAD heart, which convert any method
195 call into an invocation of C<task> with whatever
196 parameters are passed.
197  
198 If the first parameter to be passed to a command is an array ref,
199 it's understood to be a filter that will be inserted before the command.
200 Also, any parameter will be a hahsref, will be also be understood as a
201 key-value pair, and given the right separator (C<=> for C<rc.*> arguments, C<:> for regular ones).
202 For example:
203  
204 $tw->mod( [ '+focus', '+PENDING', { 'due.before' => 'today' } ], { priority => 'H' } );
205 # runs task +focus +PENDING due.before:today mod priority:H
206  
207 =head1 METHODS
208  
209 =head2 export
210  
211 As a convenience, C<export> returns the list of tasks exported (as
212 L<Taskwarrior::Kusarigama::Task> objects) instead than as raw text.
213  
214 =head1 AUTHOR
215  
216 Yanick Champoux <yanick@cpan.org>
217  
218 =head1 COPYRIGHT AND LICENSE
219  
220 This software is copyright (c) 2018, 2017 by Yanick Champoux.
221  
222 This is free software; you can redistribute it and/or modify it under
223 the same terms as the Perl 5 programming language system itself.
224  
225 =cut
« Kusarigama/Plugin/Command/Github.pm ↑ Summary Kusarigama/Plugin/Command/ButBefore.pm »