| 1 | package Taskwarrior::Kusarigama; | ||
| 2 | 2 | our $AUTHORITY = 'cpan:YANICK'; | |
| 3 | # ABSTRACT: plugin system for the Taskwarrior task manager | ||
| 4 | 2 | $Taskwarrior::Kusarigama::VERSION = '0.10.0'; | |
| 5 | |||
| 6 | # TODO document the kusarigama.dir key | ||
| 7 | 2 | 1; | |
| 8 | |||
| 9 | __END__ | ||
| 10 | |||
| 11 | =pod | ||
| 12 | |||
| 13 | =encoding UTF-8 | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Taskwarrior::Kusarigama - plugin system for the Taskwarrior task manager | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | version 0.10.0 | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | $ task-kusarigama add GitCommit Command::ButBefore Command::AndAfter | ||
| 26 | |||
| 27 | $ task-kusarigama install | ||
| 28 | |||
| 29 | # enjoy! | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This module provides a plugin-based way to run hooks and custom | ||
| 34 | commands for the | ||
| 35 | cli-based task manager L<Taskwarrior|http://taskwarrior.org/>. | ||
| 36 | |||
| 37 | =head2 Configuring Taskwarrior to use Taskwarrior::Kusarigama | ||
| 38 | |||
| 39 | =head3 Setting up the hooks | ||
| 40 | |||
| 41 | Taskwarrior's main method of customization is via hooks | ||
| 42 | that are executed when the command is run, when it exits, and when | ||
| 43 | tasks are modified or added. (see L<https://taskwarrior.org/docs/hooks.html> | ||
| 44 | for the official documentation) C<Taskwarrior::Kusarigama> leverages this | ||
| 45 | hook system to allow the creation of custom behaviors and commands. | ||
| 46 | |||
| 47 | First, you need to install hook scripts that will invoke C<Taskwarrior::Kusarigama> | ||
| 48 | when C<task> is running. You can do that by either using the helper C<task-kusarigama>: | ||
| 49 | |||
| 50 | $ task-kusarigama install | ||
| 51 | |||
| 52 | Or dropping manually hook scripts in the F<~/.task/hooks> directory. The scripts | ||
| 53 | should look like | ||
| 54 | |||
| 55 | #!/usr/bin/env perl | ||
| 56 | |||
| 57 | # script '~/.task/hooks/on-launch-kusarigama.pl' | ||
| 58 | |||
| 59 | use Taskwarrior::Kusarigama; | ||
| 60 | |||
| 61 | Taskwarrior::Kusarigama->new( raw_args => \@ARGV ) | ||
| 62 | ->run_event( 'launch' ); # change with 'add', 'modify', 'exit' | ||
| 63 | # for the different scripts | ||
| 64 | |||
| 65 | =head3 Setting which plugins to use | ||
| 66 | |||
| 67 | Then you need to tell the system with plugins to use, | ||
| 68 | either via C<task-kusarigama> | ||
| 69 | |||
| 70 | $ task-kusarigama add Command::AndAfter | ||
| 71 | |||
| 72 | or directly via the Taskwarrior config command | ||
| 73 | |||
| 74 | $ task config kusarigama.plugins Command::AndAfter | ||
| 75 | |||
| 76 | =head3 Configure the plugins | ||
| 77 | |||
| 78 | The last step is to configure the different plugins. Read their | ||
| 79 | documentation to do it manually or, again, use C<task-kusarigama>. | ||
| 80 | |||
| 81 | $ task-kusarigama install | ||
| 82 | |||
| 83 | =head2 Writing plugins | ||
| 84 | |||
| 85 | The inner workings of the plugin system are fairly simple. | ||
| 86 | |||
| 87 | The list of plugins we want to be active lives in the taskwarrior | ||
| 88 | configuration under the key <kusarigama.plugins>. E.g., | ||
| 89 | |||
| 90 | kusarigama.plugins=Renew,Command::ButBefore,Command::AndAfter,+FishCurrent | ||
| 91 | |||
| 92 | Plugin namess prefixed with a plus sign are left left alone (minus the '+'), | ||
| 93 | while the other ones get C<Taskwarrior::Kusarigama::Plugin::> prefixed to | ||
| 94 | them. | ||
| 95 | |||
| 96 | The Taskwarrior::Kusarigama system itself is invoked via the | ||
| 97 | scripts put in F<~/.task/hooks> by C<task-kusarigama>. The scripts | ||
| 98 | detect in which stage they are called (launch, exit, add or modified), | ||
| 99 | and execute all plugins that consume the associated role (e.g., | ||
| 100 | L<Taskwarrior::Kusarigama::Hook::OnLaunch>), in the order they have been | ||
| 101 | configured. | ||
| 102 | |||
| 103 | For example, this plugin will runs on a four hook stages: | ||
| 104 | |||
| 105 | package Taskwarrior::Kusarigama::Plugin::PrintStage; | ||
| 106 | |||
| 107 | use 5.10.0; | ||
| 108 | |||
| 109 | use strict; | ||
| 110 | use warnings; | ||
| 111 | |||
| 112 | use Moo; | ||
| 113 | |||
| 114 | extends 'Taskwarrior::Kusarigama::Plugin'; | ||
| 115 | |||
| 116 | with 'Taskwarrior::Kusarigama::Hook::OnLaunch', | ||
| 117 | 'Taskwarrior::Kusarigama::Hook::OnAdd', | ||
| 118 | 'Taskwarrior::Kusarigama::Hook::OnModify', | ||
| 119 | 'Taskwarrior::Kusarigama::Hook::OnExit'; | ||
| 120 | |||
| 121 | sub on_launch { say "launch stage: ", __PACKAGE__; } | ||
| 122 | sub on_add { say "add stage: ", __PACKAGE__; } | ||
| 123 | sub on_modify { say "modify stage: ", __PACKAGE__; } | ||
| 124 | sub on_exit { say "exit stage: ", __PACKAGE__; } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | =head3 The Fifth Column: Taskwarrior::Kusarigama::Hook::OnCommand | ||
| 129 | |||
| 130 | Kusarigama defines a fifth hook role, | ||
| 131 | L<Taskwarrior::Kusarigama::Hook::OnCommand>, to help creating | ||
| 132 | custom commands. This role does two things: when | ||
| 133 | C<task-kusarigama install> is run, it creates a dummy report | ||
| 134 | such that Taskwarrior will accept C<task my_custom_command> as a | ||
| 135 | valid invocation, and then it runs as part of the C<launch> | ||
| 136 | stage and will run the plugin code if the associated command was used. | ||
| 137 | |||
| 138 | =head3 Adding custom fields to tasks | ||
| 139 | |||
| 140 | Taskwarrior allows the creation of I<User-Defined Attributes> (UDAs). Plugins | ||
| 141 | can implement a C<custom_uda> attribute that holds a hashref of | ||
| 142 | new UDAs and their description. Those UDAs will then be fed to Taskwarrior's | ||
| 143 | config via C<task-kusarigama install>, and will thereafter be available like | ||
| 144 | any other task field. | ||
| 145 | |||
| 146 | For example, L<Taskwarrior::Kusarigama::Plugin::Renew> uses UDAs | ||
| 147 | to identify tasks that should create a new, follow-up instance | ||
| 148 | of themselves upon completion: | ||
| 149 | |||
| 150 | package Taskwarrior::Kusarigama::Plugin::Renew; | ||
| 151 | |||
| 152 | use strict; | ||
| 153 | use warnings; | ||
| 154 | |||
| 155 | use Clone 'clone'; | ||
| 156 | use List::AllUtils qw/ any /; | ||
| 157 | |||
| 158 | use Moo; | ||
| 159 | use MooseX::MungeHas; | ||
| 160 | |||
| 161 | extends 'Taskwarrior::Kusarigama::Hook'; | ||
| 162 | |||
| 163 | with 'Taskwarrior::Kusarigama::Hook::OnExit'; | ||
| 164 | |||
| 165 | use experimental 'postderef'; | ||
| 166 | |||
| 167 | has custom_uda => sub{ +{ | ||
| 168 | renew => 'creates a follow-up task upon closing', | ||
| 169 | rdue => 'next task due date', | ||
| 170 | rwait => 'next task wait period', | ||
| 171 | } }; | ||
| 172 | |||
| 173 | sub on_exit { | ||
| 174 | my( $self, @tasks ) = @_; | ||
| 175 | |||
| 176 | return unless $self->command eq 'done'; | ||
| 177 | |||
| 178 | my $renewed; | ||
| 179 | |||
| 180 | for my $task ( @tasks ) { | ||
| 181 | next unless any { $task->{$_} } qw/ renew rdue rwait /; | ||
| 182 | $renewed = 1; | ||
| 183 | |||
| 184 | my $new = clone($task); | ||
| 185 | |||
| 186 | delete $new->@{qw/ end modified entry status uuid /}; | ||
| 187 | |||
| 188 | my $due = $new->{rdue}; | ||
| 189 | $new->{due} = $self->calc($due) if $due; | ||
| 190 | |||
| 191 | my $wait = $new->{rwait}; | ||
| 192 | $wait =~ s/due/$due/; | ||
| 193 | $new->{wait} = $self->calc($wait) if $wait; | ||
| 194 | |||
| 195 | $new->{status} = $wait ? 'waiting' : 'pending'; | ||
| 196 | |||
| 197 | $self->import_task($new); | ||
| 198 | } | ||
| 199 | |||
| 200 | $self .= 'created follow-up tasks' if $renewed; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | =head3 Aborting the pipeline | ||
| 206 | |||
| 207 | Any plugin can abort the taskwarrior process by simply C<die>ing. | ||
| 208 | |||
| 209 | sub on_add { | ||
| 210 | my( $self, $task ) = @_; | ||
| 211 | |||
| 212 | die "need jira ticket for work tasks" | ||
| 213 | if $task->{project} eq 'work' and not $task->{jira}; | ||
| 214 | } | ||
| 215 | |||
| 216 | =head1 SEE ALSO | ||
| 217 | |||
| 218 | =over | ||
| 219 | |||
| 220 | =item L<http://techblog.babyl.ca/entry/taskwarrior> | ||
| 221 | |||
| 222 | the original blog entry | ||
| 223 | |||
| 224 | =back | ||
| 225 | |||
| 226 | =head1 AUTHOR | ||
| 227 | |||
| 228 | Yanick Champoux <yanick@cpan.org> | ||
| 229 | |||
| 230 | =head1 COPYRIGHT AND LICENSE | ||
| 231 | |||
| 232 | This software is copyright (c) 2018, 2017 by Yanick Champoux. | ||
| 233 | |||
| 234 | This is free software; you can redistribute it and/or modify it under | ||
| 235 | the same terms as the Perl 5 programming language system itself. | ||
| 236 | |||
| 237 | =cut |