| 1 | # | ||
| 2 | # This file is part of App-CPAN2Pkg | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | use 5.012; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | package App::CPAN2Pkg::UI::Text; | ||
| 14 | # ABSTRACT: text interface for cpan2pkg | ||
| 15 | $App::CPAN2Pkg::UI::Text::VERSION = '3.004'; | ||
| 16 | use DateTime; | ||
| 17 | use List::Util qw{ first }; | ||
| 18 | use Moose; | ||
| 19 | use MooseX::Has::Sugar; | ||
| 20 | use MooseX::POE; | ||
| 21 | use MooseX::SemiAffordanceAccessor; | ||
| 22 | use POE; | ||
| 23 | use Readonly; | ||
| 24 | use Term::ANSIColor qw{ :constants }; | ||
| 25 | |||
| 26 | Readonly my $K => $poe_kernel; | ||
| 27 | |||
| 28 | |||
| 29 | # -- attributes | ||
| 30 | |||
| 31 | # keep track of module outpus | ||
| 32 | has _outputs => ( ro, isa => 'HashRef', default=>sub {{}} ); | ||
| 33 | |||
| 34 | |||
| 35 | # -- initialization | ||
| 36 | |||
| 37 | # | ||
| 38 | # START() | ||
| 39 | # | ||
| 40 | # called as poe session initialization. | ||
| 41 | # | ||
| 42 | sub START { | ||
| 43 | my $self = shift; | ||
| 44 | # $poe_kernel->alias_set('main'); | ||
| 45 | POE::Kernel->alias_set('main'); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | # -- public logging events | ||
| 50 | |||
| 51 | { | ||
| 52 | |||
| 53 | |||
| 54 | event log_out => sub { | ||
| 55 | my ($self, $modname, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 56 | $self->_outputs->{$modname} .= "$line\n"; | ||
| 57 | }; | ||
| 58 | event log_err => sub { | ||
| 59 | my ($self, $modname, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 60 | $self->_outputs->{$modname} .= "$line\n"; | ||
| 61 | }; | ||
| 62 | event log_comment => sub { | ||
| 63 | my ($self, $module, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 64 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 65 | $line =~ s/\n$//; | ||
| 66 | print "$timestamp [$module] $line\n"; | ||
| 67 | }; | ||
| 68 | event log_result => sub { | ||
| 69 | my ($self, $module, $result) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 70 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 71 | local $Term::ANSIColor::AUTORESET = 1; | ||
| 72 | print BLUE "$timestamp [$module] => $result\n"; | ||
| 73 | }; | ||
| 74 | event log_step => sub { | ||
| 75 | my ($self, $module, $step) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 76 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 77 | local $Term::ANSIColor::AUTORESET = 1; | ||
| 78 | print BOLD "$timestamp [$module] ** $step\n"; | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | event module_state => sub { | ||
| 84 | my ($self, $module) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 85 | my $app = App::CPAN2Pkg->instance; | ||
| 86 | my $modname = $module->name; | ||
| 87 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 88 | |||
| 89 | if ( $module->local->status eq "error" or | ||
| 90 | $module->upstream->status eq "error" ) { | ||
| 91 | local $Term::ANSIColor::AUTORESET = 1; | ||
| 92 | print RED "$timestamp [$modname] error encountered\n"; | ||
| 93 | print "$timestamp [$modname] output follows:\n"; | ||
| 94 | print $self->_outputs->{$modname}; | ||
| 95 | print RED "$timestamp [$modname] aborting\n"; | ||
| 96 | $app->forget_module( $modname ); | ||
| 97 | } | ||
| 98 | |||
| 99 | if ( $module->local->status eq "available" and | ||
| 100 | $module->upstream->status eq "available" ) { | ||
| 101 | local $Term::ANSIColor::AUTORESET = 1; | ||
| 102 | print GREEN "$timestamp [$modname] success\n"; | ||
| 103 | $app->forget_module( $modname ); | ||
| 104 | } | ||
| 105 | |||
| 106 | { | ||
| 107 | local $Term::ANSIColor::AUTORESET = 1; | ||
| 108 | my $nb = $app->nb_modules; | ||
| 109 | my @mods = $app->all_modules; | ||
| 110 | print YELLOW "$timestamp cpan2pkg - $nb modules remaining: @mods\n"; | ||
| 111 | exit if $nb == 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->_outputs->{$modname} = ""; | ||
| 115 | }; | ||
| 116 | |||
| 117 | # -- public events | ||
| 118 | |||
| 119 | |||
| 120 | event new_module => sub { | ||
| 121 | my ($self, $module) = @_[OBJECT, ARG0]; | ||
| 122 | my $modname = $module->name; | ||
| 123 | }; | ||
| 124 | |||
| 125 | |||
| 126 | no Moose; | ||
| 127 | __PACKAGE__->meta->make_immutable; | ||
| 128 | 1; | ||
| 129 | |||
| 130 | __END__ |