| 1 | # TODO: | ||
| 2 | # - Write::Excel reporting plugin | ||
| 3 | # - Git plugin | ||
| 4 | # - Add option to round numbers | ||
| 5 | |||
| 6 | package YamlTime; | ||
| 7 | our $VERSION = '0.18'; | ||
| 8 | |||
| 9 | # Requires | ||
| 10 | BEGIN { $ENV{PERL_RL} = 'Gnu o=0' } | ||
| 11 | use Mouse; | ||
| 12 | use MouseX::App::Cmd; | ||
| 13 | |||
| 14 | use Capture::Tiny; | ||
| 15 | use DateTime::Format::Natural; | ||
| 16 | use File::ShareDir; | ||
| 17 | use Term::ReadLine; | ||
| 18 | use Text::CSV_XS; | ||
| 19 | use Text::ParseWords; | ||
| 20 | |||
| 21 | #-----------------------------------------------------------------------------# | ||
| 22 | # package YamlTime::Extension; | ||
| 23 | #-----------------------------------------------------------------------------# | ||
| 24 | package YamlTime::Command; | ||
| 25 | use App::Cmd::Setup -command; | ||
| 26 | use Mouse; | ||
| 27 | extends qw[MouseX::App::Cmd::Command]; | ||
| 28 | |||
| 29 | use IO::All; | ||
| 30 | use Cwd qw[cwd abs_path]; | ||
| 31 | # use XXX; | ||
| 32 | |||
| 33 | # _ keeps these from becoming cli options | ||
| 34 | has _conf => ( | ||
| 35 | is => 'ro', | ||
| 36 | lazy => 1, | ||
| 37 | reader => 'conf', | ||
| 38 | builder => sub { | ||
| 39 | my ($self) = @_; | ||
| 40 | return $YamlTime::Conf = | ||
| 41 | YamlTime::Conf->new(base => $self->base); | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | has _base => ( | ||
| 45 | is => 'ro', | ||
| 46 | reader => 'base', | ||
| 47 | default => sub { | ||
| 48 | my $base = | ||
| 49 | $ENV{YAMLTIME_BASE} ? $ENV{YAMLTIME_BASE} : | ||
| 50 | -d "$ENV{HOME}/.yamltime/" ? "$ENV{HOME}/.yamltime/" : | ||
| 51 | '.'; | ||
| 52 | $base =~ s!/+$!!; | ||
| 53 | return abs_path $base; | ||
| 54 | }, | ||
| 55 | ); | ||
| 56 | |||
| 57 | # Not validating any args. Checking the working environment. | ||
| 58 | sub validate_args { | ||
| 59 | my ($self) = @_; | ||
| 60 | my $base = $self->base; | ||
| 61 | chdir $base | ||
| 62 | or $self->error__cant_chdir_base; | ||
| 63 | if (ref($self) !~ /::init$/) { | ||
| 64 | $self->error__not_init | ||
| 65 | unless -e 'conf' and glob('20*'); | ||
| 66 | $self->conf; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | # Semi-brutal hack to suppress extra options I don't care about. | ||
| 71 | around usage => sub { | ||
| 72 | my $orig = shift; | ||
| 73 | my $self = shift; | ||
| 74 | my $opts = $self->{usage}->{options}; | ||
| 75 | @$opts = grep { $_->{name} ne 'help' } @$opts; | ||
| 76 | return $self->$orig(@_); | ||
| 77 | }; | ||
| 78 | |||
| 79 | #-----------------------------------------------------------------------------# | ||
| 80 | package YamlTime; | ||
| 81 | use App::Cmd::Setup -app; | ||
| 82 | use Mouse; | ||
| 83 | extends 'MouseX::App::Cmd'; | ||
| 84 | |||
| 85 | use Module::Pluggable | ||
| 86 | require => 1, | ||
| 87 | search_path => [ 'YamlTime' ]; | ||
| 88 | YamlTime->plugins; | ||
| 89 | |||
| 90 | use YamlTime::Conf; | ||
| 91 | use YamlTime::Task; | ||
| 92 | |||
| 93 | # Global pointer to the YamlTime::Conf singleton object. | ||
| 94 | our $Conf; | ||
| 95 | |||
| 96 | # App::Cmd help helpers | ||
| 97 | use constant usage => 'YamlTime'; | ||
| 98 | use constant text => "yt command [<options>] [<arguments>]\n"; | ||
| 99 | |||
| 100 | sub default_args { | ||
| 101 | my $default = $ENV{YAMLTIME_DEFAULT_ARGS} or return []; | ||
| 102 | return [ Text::ParseWords::shellwords($default) ]; | ||
| 103 | } | ||
| 104 | |||
| 105 | #-----------------------------------------------------------------------------# | ||
| 106 | # A role for time/tag range options | ||
| 107 | #-----------------------------------------------------------------------------# | ||
| 108 | package YamlTime::RangeOpts; | ||
| 109 | use Mouse::Role; | ||
| 110 | |||
| 111 | my $time = time; | ||
| 112 | |||
| 113 | has from => ( | ||
| 114 | is => 'ro', | ||
| 115 | isa => 'Str', | ||
| 116 | default => 0, | ||
| 117 | documentation => | ||
| 118 | 'Range start date/time (natural format) default(midnight)', | ||
| 119 | ); | ||
| 120 | has to => ( | ||
| 121 | is => 'ro', | ||
| 122 | isa => 'Str', | ||
| 123 | default => 0, | ||
| 124 | documentation => | ||
| 125 | 'Range end date/time (natural format) default(now)', | ||
| 126 | ); | ||
| 127 | has tags => ( | ||
| 128 | is => 'ro', | ||
| 129 | isa => 'ArrayRef[Str]', | ||
| 130 | default => sub{[]}, | ||
| 131 | documentation => | ||
| 132 | 'Comma separated tags. Can be used more than once', | ||
| 133 | trigger => sub { | ||
| 134 | my ($self, $new, $old) = @_; | ||
| 135 | $self->{tags} = [ | ||
| 136 | map { | ||
| 137 | [ split /\s*,\s*/ ] | ||
| 138 | } @$new | ||
| 139 | ]; | ||
| 140 | }, | ||
| 141 | ); | ||
| 142 | |||
| 143 | #-----------------------------------------------------------------------------# | ||
| 144 | # YamlTime (yt) Commands | ||
| 145 | # | ||
| 146 | # This is the set of App::Cmd classes that support each command. | ||
| 147 | #-----------------------------------------------------------------------------# | ||
| 148 | package YamlTime::Command::init; | ||
| 149 | YamlTime->import( -command ); | ||
| 150 | use Mouse; | ||
| 151 | extends qw[YamlTime::Command]; | ||
| 152 | |||
| 153 | use constant abstract => 'Initialize a new YamlTime store directory'; | ||
| 154 | use constant usage_desc => 'yt init [--force]'; | ||
| 155 | |||
| 156 | has force => ( | ||
| 157 | is => 'ro', | ||
| 158 | isa => 'Bool', | ||
| 159 | documentation => 'Force an init operation', | ||
| 160 | ); | ||
| 161 | |||
| 162 | sub execute { | ||
| 163 | my ($self, $opt, $args) = @_; | ||
| 164 | if ($self->empty_directory or $self->force) { | ||
| 165 | my $share = $self->share; | ||
| 166 | $self->copy_files("$share/conf", "./conf"); | ||
| 167 | mkdir($self->date('now')->year); | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | $self->error__wont_init; | ||
| 171 | } | ||
| 172 | |||
| 173 | $self->log(sprintf "Initialized YamlTime directory: %s", $self->base); | ||
| 174 | $self->log("\nNow edit the conf files and run 'yt help'.") | ||
| 175 | } | ||
| 176 | |||
| 177 | #-----------------------------------------------------------------------------# | ||
| 178 | package YamlTime::Command::new; | ||
| 179 | use Mouse; | ||
| 180 | YamlTime->import( -command ); | ||
| 181 | extends qw[YamlTime::Command]; | ||
| 182 | |||
| 183 | use constant abstract => 'Create a new task and start the timer'; | ||
| 184 | use constant usage_desc => 'yt new ["Task description"]'; | ||
| 185 | |||
| 186 | sub execute { | ||
| 187 | my ($self, $opt, $args) = @_; | ||
| 188 | $self->error__already_in_progress | ||
| 189 | if $self->current_task and | ||
| 190 | $self->current_task->in_progress; | ||
| 191 | |||
| 192 | my $task = YamlTime::Task->new(id => undef); | ||
| 193 | $self->populate($task, $args); | ||
| 194 | $task->start; | ||
| 195 | |||
| 196 | $self->log(sprintf "Started task %s.", $task->id); | ||
| 197 | $self->log("\nGet to work!") | ||
| 198 | unless $self->conf->be_serious; | ||
| 199 | } | ||
| 200 | |||
| 201 | #-----------------------------------------------------------------------------# | ||
| 202 | package YamlTime::Command::stop; | ||
| 203 | use Mouse; | ||
| 204 | YamlTime->import( -command ); | ||
| 205 | extends qw[YamlTime::Command]; | ||
| 206 | |||
| 207 | use constant abstract => 'Stop the timer on a running task'; | ||
| 208 | use constant usage_desc => 'yt stop'; | ||
| 209 | |||
| 210 | sub execute { | ||
| 211 | my ($self, $opt, $args) = @_; | ||
| 212 | my $task = $self->current_task or | ||
| 213 | $self->error__no_current_task; | ||
| 214 | $self->error__not_in_progress | ||
| 215 | unless $task->in_progress; | ||
| 216 | |||
| 217 | $task->stop; | ||
| 218 | $self->log(sprintf "Stopped task %s. Time: %s", $task->id, $task->elapsed); | ||
| 219 | $self->log("\nSTOP! ... YAML TIME!") | ||
| 220 | unless $self->conf->be_serious; | ||
| 221 | } | ||
| 222 | |||
| 223 | #-----------------------------------------------------------------------------# | ||
| 224 | package YamlTime::Command::go; | ||
| 225 | use Mouse; | ||
| 226 | YamlTime->import( -command ); | ||
| 227 | extends qw[YamlTime::Command]; | ||
| 228 | |||
| 229 | use constant abstract => 'Restart the timer on a task'; | ||
| 230 | use constant usage_desc => 'yt go [<task-id>]'; | ||
| 231 | |||
| 232 | sub execute { | ||
| 233 | my ($self, $opt, $args) = @_; | ||
| 234 | my $id = $args->[0]; | ||
| 235 | my $task = $self->get_task(@$args) | ||
| 236 | or $self->error__no_current_task; | ||
| 237 | $self->error__already_in_progress | ||
| 238 | if $task->in_progress; | ||
| 239 | |||
| 240 | $task->start; | ||
| 241 | $self->log(sprintf "Restarted task %s - %s.", $task->id, $task->task); | ||
| 242 | $self->log("\nGet back to work!") | ||
| 243 | unless $self->conf->be_serious; | ||
| 244 | } | ||
| 245 | |||
| 246 | #-----------------------------------------------------------------------------# | ||
| 247 | package YamlTime::Command::status; | ||
| 248 | use Mouse; | ||
| 249 | YamlTime->import( -command ); | ||
| 250 | extends qw[YamlTime::Command]; | ||
| 251 | with 'YamlTime::RangeOpts'; | ||
| 252 | |||
| 253 | use constant abstract => 'Show the status of a range of tasks'; | ||
| 254 | use constant usage_desc => do { $_ = <<'...'; chomp; $_ }; | ||
| 255 | yt # today's tasks | ||
| 256 | yt status [<task-ids>] | ||
| 257 | yt status [--from=...] [--to=...] | ||
| 258 | ... | ||
| 259 | |||
| 260 | sub execute { | ||
| 261 | my ($self, $opt, $args) = @_; | ||
| 262 | my $total = 0; | ||
| 263 | for my $task ($self->get_task_range(@$args)) { | ||
| 264 | if ($task->elapsed =~ /^(\d+):(\d+)$/) { | ||
| 265 | $total += $1 * 60 + $2; | ||
| 266 | } | ||
| 267 | printf "%1s %12s %5s %s\n", | ||
| 268 | ($task->in_progress ? '+' : '-'), | ||
| 269 | $task->id, | ||
| 270 | $task->elapsed, | ||
| 271 | $task->task; | ||
| 272 | } | ||
| 273 | my $hours = int($total / 60); | ||
| 274 | my $mins = $total % 60; | ||
| 275 | printf ' ' x 11 . "Total: % 2d:%02d\n", $hours, $mins | ||
| 276 | if $total; | ||
| 277 | } | ||
| 278 | |||
| 279 | #-----------------------------------------------------------------------------# | ||
| 280 | package YamlTime::Command::check; | ||
| 281 | use Mouse; | ||
| 282 | YamlTime->import( -command ); | ||
| 283 | extends qw[YamlTime::Command]; | ||
| 284 | with 'YamlTime::RangeOpts'; | ||
| 285 | use IO::All; | ||
| 286 | |||
| 287 | use constant abstract => 'Check the validity of a range of tasks'; | ||
| 288 | use constant usage_desc => 'yt check [--verbose] [--from=...] [--to=...]'; | ||
| 289 | |||
| 290 | has verbose => ( | ||
| 291 | is => 'ro', | ||
| 292 | isa => 'Bool', | ||
| 293 | default => 0, | ||
| 294 | documentation => 'Increase output verbosity', | ||
| 295 | ); | ||
| 296 | |||
| 297 | sub execute { | ||
| 298 | my ($self, $opt, $args) = @_; | ||
| 299 | my $errors = 0; | ||
| 300 | for my $task ($self->get_task_range(@$args)) { | ||
| 301 | my @errors = $task->check; | ||
| 302 | if (@errors) { | ||
| 303 | $errors++; | ||
| 304 | printf "\n%s - found errors:\n", $task->id; | ||
| 305 | my $dump = YAML::XS::Dump \@errors; | ||
| 306 | $dump =~ s/^---\n//; | ||
| 307 | my $text = io($task->id)->all; | ||
| 308 | $text =~ s/^/ |/gm; | ||
| 309 | print $dump, $text; | ||
| 310 | } | ||
| 311 | elsif ($self->verbose) { | ||
| 312 | printf "%s - no errors found\n", $task->id; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | if ($self->verbose and not $errors) { | ||
| 316 | print "No errors found\n"; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | #-----------------------------------------------------------------------------# | ||
| 321 | package YamlTime::Command::report; | ||
| 322 | use Mouse; | ||
| 323 | YamlTime->import( -command ); | ||
| 324 | extends 'YamlTime::Command'; | ||
| 325 | with 'YamlTime::RangeOpts'; | ||
| 326 | use Text::CSV_XS; | ||
| 327 | use Cwd qw[abs_path]; | ||
| 328 | |||
| 329 | use constant abstract => 'Produce a billing report from a range of tasks'; | ||
| 330 | use constant usage_desc => 'yt report [--file=...] [--from=...] [--to=...]'; | ||
| 331 | |||
| 332 | has file => ( | ||
| 333 | is => 'ro', | ||
| 334 | isa => 'Str', | ||
| 335 | default => abs_path('report.csv'), | ||
| 336 | documentation => 'Name of file to write the report to', | ||
| 337 | ); | ||
| 338 | |||
| 339 | sub execute { | ||
| 340 | my ($self, $opt, $args) = @_; | ||
| 341 | my $report_file = $self->file; | ||
| 342 | my $csv = Text::CSV_XS->new ({ binary => 1 }) or die; | ||
| 343 | $csv->eol ("\r\n"); | ||
| 344 | open my $fh, ">:encoding(utf8)", $report_file or die "new.csv: $!"; | ||
| 345 | $csv->print( | ||
| 346 | $fh, | ||
| 347 | [qw(Date Time Hours Project Task Tags Refs Notes)] | ||
| 348 | ); | ||
| 349 | for my $task ($self->get_task_range(@$args)) { | ||
| 350 | $task->id =~ m!^(.*)/(\d\d)(\d\d)$! or die $task->id; | ||
| 351 | my $date = $1; | ||
| 352 | my $time = "$2:$3"; | ||
| 353 | my $row = [ | ||
| 354 | $date, | ||
| 355 | $time, | ||
| 356 | $task->time, | ||
| 357 | $task->proj, | ||
| 358 | $task->task, | ||
| 359 | join(', ', @{$task->tags}), | ||
| 360 | join(', ', @{$task->refs}), | ||
| 361 | $task->note || '', | ||
| 362 | ]; | ||
| 363 | $csv->print ($fh, $row); | ||
| 364 | } | ||
| 365 | close $fh or die "report.csv: $!"; | ||
| 366 | print "Created $report_file\n"; | ||
| 367 | } | ||
| 368 | |||
| 369 | #-----------------------------------------------------------------------------# | ||
| 370 | package YamlTime::Command::edit; | ||
| 371 | use Mouse; | ||
| 372 | YamlTime->import( -command ); | ||
| 373 | extends qw[YamlTime::Command]; | ||
| 374 | |||
| 375 | use constant abstract => 'Edit a task\'s YAML in $EDITOR'; | ||
| 376 | use constant usage_desc => 'yt edit [<task-id>]'; | ||
| 377 | |||
| 378 | sub execute { | ||
| 379 | my ($self, $opt, $args) = @_; | ||
| 380 | my $editor = $ENV{EDITOR} | ||
| 381 | or $self->error('You need to set $EDITOR env var to edit'); | ||
| 382 | my $task = $self->get_task(@$args) | ||
| 383 | or $self->error("No task to edit"); | ||
| 384 | exec $editor . " " . $task->id; | ||
| 385 | } | ||
| 386 | |||
| 387 | #-----------------------------------------------------------------------------# | ||
| 388 | package YamlTime::Command::base; | ||
| 389 | use Mouse; | ||
| 390 | YamlTime->import( -command ); | ||
| 391 | extends qw[YamlTime::Command]; | ||
| 392 | use IO::All; | ||
| 393 | |||
| 394 | use constant abstract => 'Print the YamlTime base directory to STDOUT'; | ||
| 395 | use constant usage_desc => 'yt base'; | ||
| 396 | |||
| 397 | sub execute { | ||
| 398 | my ($self, $opt, $args) = @_; | ||
| 399 | print $self->base . "\n"; | ||
| 400 | } | ||
| 401 | |||
| 402 | #-----------------------------------------------------------------------------# | ||
| 403 | package YamlTime::Command::dump; | ||
| 404 | use Mouse; | ||
| 405 | YamlTime->import( -command ); | ||
| 406 | extends qw[YamlTime::Command]; | ||
| 407 | use IO::All; | ||
| 408 | |||
| 409 | use constant abstract => 'Print a task file to STDOUT'; | ||
| 410 | use constant usage_desc => 'yt dump [<task-id>]'; | ||
| 411 | |||
| 412 | sub execute { | ||
| 413 | my ($self, $opt, $args) = @_; | ||
| 414 | my $task = $self->get_task(@$args) | ||
| 415 | or $self->error("No task to dump"); | ||
| 416 | print io($task->id)->all; | ||
| 417 | } | ||
| 418 | |||
| 419 | #-----------------------------------------------------------------------------# | ||
| 420 | package YamlTime::Command::create; | ||
| 421 | use Mouse; | ||
| 422 | YamlTime->import( -command ); | ||
| 423 | extends qw[YamlTime::Command]; | ||
| 424 | |||
| 425 | use constant abstract => 'Create a new task for a particular date/time'; | ||
| 426 | use constant usage_desc => 'yt create <new-task-id>'; | ||
| 427 | |||
| 428 | sub execute { | ||
| 429 | my ($self, $opt, $args) = @_; | ||
| 430 | $self->error("Requires a task-id of the form '2011/12/31/1234'") | ||
| 431 | unless @$args == 1 and $args->[0] =~ m!^(20\d\d/\d\d/\d\d/\d\d\d\d)$!; | ||
| 432 | my $id = $args->[0]; | ||
| 433 | my $task = $self->get_task($id); | ||
| 434 | $self->error("Task '$id' already exists") | ||
| 435 | if $task->exists; | ||
| 436 | |||
| 437 | $self->populate($task, []); | ||
| 438 | $task->write; | ||
| 439 | |||
| 440 | $self->log("Created task $id"); | ||
| 441 | } | ||
| 442 | |||
| 443 | #-----------------------------------------------------------------------------# | ||
| 444 | package YamlTime::Command::delete; | ||
| 445 | use Mouse; | ||
| 446 | YamlTime->import( -command ); | ||
| 447 | extends qw[YamlTime::Command]; | ||
| 448 | |||
| 449 | use constant abstract => 'Delete an entry by task-id'; | ||
| 450 | use constant usage_desc => 'yt delete <task-id>'; | ||
| 451 | |||
| 452 | sub execute { | ||
| 453 | my ($self, $opt, $args) = @_; | ||
| 454 | |||
| 455 | my $id = $args->[0]; | ||
| 456 | $self->error("'yt delete' require a single task-id") if | ||
| 457 | @$args != 1 or | ||
| 458 | $id !~ m!^20\d{2}/\d{2}/\d{2}/\d{4}! or | ||
| 459 | not(-f $id); | ||
| 460 | |||
| 461 | my $task = $self->get_task($id); | ||
| 462 | |||
| 463 | $self->error("'$id' is in progress. Run 'stop' first") | ||
| 464 | if $task->in_progress; | ||
| 465 | |||
| 466 | $task->delete(); | ||
| 467 | } | ||
| 468 | |||
| 469 | #-----------------------------------------------------------------------------# | ||
| 470 | # Guts of the machine | ||
| 471 | #-----------------------------------------------------------------------------# | ||
| 472 | package YamlTime::Command; | ||
| 473 | |||
| 474 | sub run { | ||
| 475 | my ($self, $cmd, $options) = @_; | ||
| 476 | my $error; | ||
| 477 | my $output = Capture::Tiny::capture_merged { | ||
| 478 | system($cmd) == 0 or $error = 1; | ||
| 479 | }; | ||
| 480 | print $output; | ||
| 481 | if ($error) { | ||
| 482 | return 0; | ||
| 483 | } | ||
| 484 | return 1; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub current_task { | ||
| 488 | my ($self) = @_; | ||
| 489 | return $self->get_task; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub get_task { | ||
| 493 | my ($self, $id) = @_; | ||
| 494 | $id ||= readlink('_') or return; | ||
| 495 | return YamlTime::Task->new(id => $id); | ||
| 496 | } | ||
| 497 | |||
| 498 | sub get_task_range { | ||
| 499 | my $self = shift; | ||
| 500 | my @files; | ||
| 501 | if (@_) { | ||
| 502 | @files = @_; | ||
| 503 | } | ||
| 504 | else { | ||
| 505 | my $from = $self->format_date($self->from || 'today'); | ||
| 506 | my $to = $self->format_date($self->to || 'now'); | ||
| 507 | OUTER: | ||
| 508 | for my $dir (sort grep /^20\d\d$/, glob("20*")) { | ||
| 509 | for my $file (sort map $_->name, -d $dir ? io->dir($dir)->All_Files : ()) { | ||
| 510 | next if $file lt $from; | ||
| 511 | last OUTER if $file gt $to; | ||
| 512 | push @files, $file; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | return grep { | ||
| 517 | $self->match_tags($_); | ||
| 518 | } map { | ||
| 519 | $self->get_task($_); | ||
| 520 | } sort @files; | ||
| 521 | } | ||
| 522 | |||
| 523 | sub match_tags { | ||
| 524 | my ($self, $task) = @_; | ||
| 525 | my $want = $self->tags; | ||
| 526 | return 1 unless @$want; | ||
| 527 | my $have = $task->tags; | ||
| 528 | return 0 unless @$have; | ||
| 529 | OUTER: | ||
| 530 | for my $w (@$want) { | ||
| 531 | for my $t (@$w) { | ||
| 532 | next OUTER unless grep {$_ eq $t} @$have; | ||
| 533 | } | ||
| 534 | return 1; | ||
| 535 | } | ||
| 536 | return 0; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub format_date { | ||
| 540 | my ($self, $str) = @_; | ||
| 541 | my $date = DateTime::Format::Natural->new( | ||
| 542 | time_zone => $self->conf->timezone, | ||
| 543 | )->parse_datetime($str); | ||
| 544 | return sprintf "%4d/%02d/%02d/%02d%02d", | ||
| 545 | $date->year, | ||
| 546 | $date->month, | ||
| 547 | $date->day, | ||
| 548 | $date->hour, | ||
| 549 | $date->minute; | ||
| 550 | } | ||
| 551 | |||
| 552 | my $date_parser = DateTime::Format::Natural->new; | ||
| 553 | |||
| 554 | sub date { | ||
| 555 | my ($self, $string) = @_; | ||
| 556 | return eval { | ||
| 557 | $date_parser->parse_datetime($string); | ||
| 558 | } || undef; | ||
| 559 | } | ||
| 560 | |||
| 561 | sub empty_directory { | ||
| 562 | io('.')->empty; | ||
| 563 | } | ||
| 564 | |||
| 565 | sub share { | ||
| 566 | my $class = shift; | ||
| 567 | my $path = $INC{'YamlTime.pm'} or die; | ||
| 568 | if ($path =~ s!(\S.*?)[\\/]?\bb?lib\b.*!$1! and | ||
| 569 | -e "$path/Makefile.PL" and | ||
| 570 | -e "$path/share" | ||
| 571 | ) { | ||
| 572 | return abs_path "$path/share"; | ||
| 573 | } | ||
| 574 | else { | ||
| 575 | return File::ShareDir::dist_dir('YamlTime'); | ||
| 576 | } | ||
| 577 | } | ||
| 578 | |||
| 579 | sub copy_files { | ||
| 580 | my ($self, $source, $target) = @_; | ||
| 581 | for my $file (io($source)->All_Files) { | ||
| 582 | my $short = $file->name; | ||
| 583 | $short =~ s!^\Q$source\E/?!! or die $short; | ||
| 584 | next if $short =~ /^\./; | ||
| 585 | io("$target/$short")->assert->print($file->all); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | my $prompts = { | ||
| 590 | task => 'Task Description: ', | ||
| 591 | cust => 'Customer Id: ', | ||
| 592 | tags => 'A Tag Word: ', | ||
| 593 | proj => 'Project Id: ', | ||
| 594 | }; | ||
| 595 | |||
| 596 | sub prompt { | ||
| 597 | my ($self, $key, $default, $task) = @_; | ||
| 598 | my $prompt = $prompts->{$key}; | ||
| 599 | my $term = new Term::ReadLine 'YamlTime'; | ||
| 600 | $term->Attribs->{completion_function} = sub{ | ||
| 601 | return sort keys %{$self->conf->{$key}} | ||
| 602 | if $key =~ /^(cust|tags)$/; | ||
| 603 | return sort keys %{$self->conf->{proj}{$task->{cust}}} | ||
| 604 | if $key =~ /^(proj)$/; | ||
| 605 | return (); | ||
| 606 | }; | ||
| 607 | my $val = $term->readline($prompt, $default); | ||
| 608 | exit unless defined $val; | ||
| 609 | $val =~ s/\A\s*(.*?)\s*\z/$1/s; | ||
| 610 | $term->addhistory($val) if $val =~ /\S/; | ||
| 611 | |||
| 612 | return $val; | ||
| 613 | } | ||
| 614 | |||
| 615 | # Prompt the user for the info needed in a task | ||
| 616 | sub populate { | ||
| 617 | my ($self, $task, $args) = @_; | ||
| 618 | my $old = $self->current_task || {}; | ||
| 619 | $task->{task} = join ' ', @$args if @$args; | ||
| 620 | for my $key (qw[task cust proj tags]) { | ||
| 621 | my $val = $task->$key; | ||
| 622 | my $list = ref($val); | ||
| 623 | my $default = $list ? '' : ($task->{$key} || $old->{$key}); | ||
| 624 | while (1) { | ||
| 625 | my $new_val = $self->prompt($key, $default, $task); | ||
| 626 | |||
| 627 | if (not length $new_val) { | ||
| 628 | if ($key =~ /^(task|cust)$/) { | ||
| 629 | warn " Required field.\n"; | ||
| 630 | next; | ||
| 631 | } | ||
| 632 | else { | ||
| 633 | last; | ||
| 634 | } | ||
| 635 | } | ||
| 636 | elsif ($key =~ /^(cust|tags)$/) { | ||
| 637 | if (not exists $self->conf->{$key}{$new_val}) { | ||
| 638 | warn " '$new_val' is invalid.\n"; | ||
| 639 | next; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | elsif ($key =~ /^(proj)$/) { | ||
| 643 | if (not exists $self->conf->{proj}{$task->{cust}}{$new_val}) { | ||
| 644 | warn " '$new_val' is invalid.\n"; | ||
| 645 | next; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | if ($list) { | ||
| 650 | push @$val, $new_val; | ||
| 651 | } | ||
| 652 | else { | ||
| 653 | $task->$key($new_val); | ||
| 654 | last; | ||
| 655 | } | ||
| 656 | } | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | sub log { | ||
| 661 | my $self = shift; | ||
| 662 | print "@_\n"; | ||
| 663 | } | ||
| 664 | |||
| 665 | #-----------------------------------------------------------------------------# | ||
| 666 | # Errors happen | ||
| 667 | sub error { | ||
| 668 | my ($self, $msg) = splice(@_, 0, 2); | ||
| 669 | chomp $msg; | ||
| 670 | $msg .= $/; | ||
| 671 | die sprintf($msg, @_); | ||
| 672 | } | ||
| 673 | |||
| 674 | sub error__cant_chdir_base { | ||
| 675 | my ($self) = @_; | ||
| 676 | my $base = $self->base; | ||
| 677 | $self->error(<<"..."); | ||
| 678 | Can't chdir to '$base'. | ||
| 679 | YAMLTIME_BASE is set to '$base', | ||
| 680 | but it does not yet exist. You should create it and rerun your command. | ||
| 681 | ... | ||
| 682 | } | ||
| 683 | |||
| 684 | sub error__not_init { | ||
| 685 | my ($self) = @_; | ||
| 686 | my $base = $self->base; | ||
| 687 | if ($ENV{YAMLTIME_BASE} or $self->empty_directory) { | ||
| 688 | $self->error(<<"..."); | ||
| 689 | '$base' not yet initialized. | ||
| 690 | Run 'yt init'. | ||
| 691 | ... | ||
| 692 | } | ||
| 693 | else { | ||
| 694 | $self->error(<<"..."); | ||
| 695 | '$base' is not a yt directory and it's not empty. | ||
| 696 | |||
| 697 | You should mkdir and cd into a new directory, or set the YAMLTIME_BASE | ||
| 698 | environment variable to such a directory. | ||
| 699 | ... | ||
| 700 | } | ||
| 701 | } | ||
| 702 | |||
| 703 | sub error__wont_init { | ||
| 704 | my ($self) = @_; | ||
| 705 | $self->error( | ||
| 706 | "Won't 'init' in a non empty directory, unless you use --force" | ||
| 707 | ); | ||
| 708 | } | ||
| 709 | |||
| 710 | sub error__already_in_progress { | ||
| 711 | my ($self) = @_; | ||
| 712 | $self->error(<<'...'); | ||
| 713 | Command invalid. | ||
| 714 | A task is already in progress. | ||
| 715 | Stop the current one first. | ||
| 716 | ... | ||
| 717 | } | ||
| 718 | |||
| 719 | sub error__not_in_progress { | ||
| 720 | my ($self) = @_; | ||
| 721 | $self->error(<<'...'); | ||
| 722 | Command invalid. | ||
| 723 | There is no task is currently in progress. | ||
| 724 | ... | ||
| 725 | } | ||
| 726 | |||
| 727 | sub error__no_current_task { | ||
| 728 | my ($self) = @_; | ||
| 729 | $self->error(<<'...'); | ||
| 730 | Command invalid. | ||
| 731 | There is no current task. | ||
| 732 | You may need to specify one. | ||
| 733 | ... | ||
| 734 | } | ||
| 735 | |||
| 736 | 1; |