| 1 | package Perl::Achievements; | ||||||||||||||||||||
| 2 | 7 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 7 | $Perl::Achievements::AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | { | ||||||||||||||||||||
| 6 | 7 | $Perl::Achievements::VERSION = '0.4.0'; | |||||||||||||||||||
| 7 | } | ||||||||||||||||||||
| 8 | # ABSTRACT: whoever die()s with the most badges win | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 7 | ▶ | use 5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 7 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 7 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 7 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 7 | ▶ | use MooseX::ClassAttribute; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 7 | ▶ | use Perl::Achievements::User; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | use Module::Pluggable | ||||||||||||||||||||
| 25 | 7 | ▶ | search_path => ['Perl::Achievements::Achievement'], | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 7 | require => 1; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 7 | ▶ | use YAML::Any qw/ LoadFile Load Dump /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 7 | ▶ | use PPI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 7 | ▶ | use File::HomeDir; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 7 | ▶ | use Path::Class qw/ file dir /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 7 | ▶ | use Method::Signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 7 | ▶ | use DateTime::Functions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 7 | ▶ | use Data::Printer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 7 | ▶ | use Digest::SHA qw/ sha1_hex /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 7 | ▶ | use File::Touch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 7 | extends 'MooseX::App::Cmd'; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 7 | with qw/ | |||||||||||||||||||
| 41 | MooseX::Role::Loggable | ||||||||||||||||||||
| 42 | MooseX::ConfigFromFile | ||||||||||||||||||||
| 43 | /; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 7 | with 'MooseX::Role::BuildInstanceOf' => { | |||||||||||||||||||
| 46 | target => 'Perl::Achievements::User', | ||||||||||||||||||||
| 47 | prefix => 'user', | ||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | sub get_config_from_file { | ||||||||||||||||||
|
Subroutine
get_config_from_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 51 | 0 | my ( undef, $file ) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | return {} if not -f $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | my $config = LoadFile( $file ); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # massage the config a wee bit | ||||||||||||||||||||
| 58 | 0 | ▶ | if ( $config->{user} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $_ = [ %$_ ] for $config->{user_args} = delete $config->{user}; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | return $config; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | my $configfile = ''.file( | ||||||||||||||||||||
| 66 | $ENV{PERL_ACHIEVEMENTS_HOME} | ||||||||||||||||||||
| 67 | 7 | ▶ | || dir( File::HomeDir->my_home, '.perl-achievements' ), | ||||||||||||||||||
|
Condition33%1/3
$ENV{'PERL_ACHIEVEMENTS_HOME'} || dir('File::HomeDir'->my_home, '.perl-achievements')
MC/DC0%0/2
$ENV{'PERL_ACHIEVEMENTS_HOME'} || dir('File::HomeDir'->my_home, '.perl-achievements')
$ENV{'PERL_ACHIEVEMENTS_HOME'}
dir('File::HomeDir'->my_home, '.perl-achievements')
Truth table33%1/3
A || B
A= $ENV{'PERL_ACHIEVEMENTS_HOME'}B= dir('File::HomeDir'->my_home, '.perl-achievements')
| |||||||||||||||||||||
| 68 | 'config' | ||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 7 | has '+configfile' => ( | |||||||||||||||||||
| 72 | default => $configfile, | ||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | class_has rc => ( | ||||||||||||||||||||
| 76 | is => 'ro', | ||||||||||||||||||||
| 77 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 78 | $ENV{PERL_ACHIEVEMENTS_HOME} | ||||||||||||||||||||
| 79 | 0 | ▶ | || dir( File::HomeDir->my_home, '.perl-achievements' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||
| 81 | 7 | lazy => 1, | |||||||||||||||||||
| 82 | ); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub rc_file_path { | ||||||||||||||||||
|
Subroutine
rc_file_path: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 85 | 0 | my ( $self, @path ) = @_; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | return file( $self->rc, @path ); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 7 | has _achievements => ( | |||||||||||||||||||
| 92 | traits => [ 'Array' ], | ||||||||||||||||||||
| 93 | is => 'ro', | ||||||||||||||||||||
| 94 | builder => '_achievements_builder', | ||||||||||||||||||||
| 95 | handles => { | ||||||||||||||||||||
| 96 | achievements => 'elements', | ||||||||||||||||||||
| 97 | add_achievements => 'push', | ||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 7 | has ppi => ( | |||||||||||||||||||
| 102 | is => 'rw', | ||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # will change | ||||||||||||||||||||
| 106 | has history => ( | ||||||||||||||||||||
| 107 | is => 'ro', | ||||||||||||||||||||
| 108 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 109 | 0 | my $file = $_[0]->rc_file_path( 'history' ); | |||||||||||||||||||
| 110 | 0 | ▶ | return [] unless -f $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | return [ map { Load( $_ ) } split /^---\n/, scalar $file->slurp ]; | |||||||||||||||||||
| 113 | }, | ||||||||||||||||||||
| 114 | 7 | ); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 7 | has interactive => ( | |||||||||||||||||||
| 117 | is => 'ro', | ||||||||||||||||||||
| 118 | isa => 'Bool', | ||||||||||||||||||||
| 119 | default => 1, | ||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 7 | has dry_run => ( | |||||||||||||||||||
| 123 | is => 'ro', | ||||||||||||||||||||
| 124 | isa => 'Bool', | ||||||||||||||||||||
| 125 | default => 0, | ||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | method scan ($file) { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
scan: not called CC=5 SCAR=31.5
Branch: 2 branches
| |||||||||||||||||||||
| 129 | 0 | $self->set_ppi( PPI::Document->new( $file ) ); | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | my $digest = sha1_hex($self->ppi->serialize); | |||||||||||||||||||
| 132 | 0 | my $digest_file = $self->rc_file_path( 'scanned', $digest ); | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | ▶ | if ( -f $digest_file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | $self->log_debug( "file '$file' already has been scanned" ); | |||||||||||||||||||
| 136 | 0 | return; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | $_->scan for $self->achievements; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | $digest_file->touch; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | sub _achievements_builder { | ||||||||||||||||||
|
Subroutine
_achievements_builder: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 145 | 0 | my $self = shift; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | my @checks; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | push @checks, $_->load_or_new( app => $self ) for $self->plugins; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | return \@checks; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | method initialize_environment { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
initialize_environment: not called CC=3 SCAR=21.8
Branch: 1 branch
| |||||||||||||||||||||
| 155 | 0 | my $dir = $self->rc; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | die "'$dir' already exist, aborting" if -e $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | mkdir $dir; | |||||||||||||||||||
| 160 | 0 | mkdir dir( $dir, 'achievements' ); | |||||||||||||||||||
| 161 | 0 | mkdir dir( $dir, 'scanned' ); | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 0 | my $config = file( $dir, 'config' )->openw; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | print $config <<'END_CONFIG'; | |||||||||||||||||||
| 166 | # user: | ||||||||||||||||||||
| 167 | # name: Your Name Here | ||||||||||||||||||||
| 168 | # url: http://yoursite.org/ | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | END_CONFIG | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | ▶ | sub unlock_achievement { | ||||||||||||||||||
|
Subroutine
unlock_achievement: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 175 | 0 | my ( $self, %info ) = @_; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | $self->log_debug( "achievement unlocked:\n" | |||||||||||||||||||
| 178 | . p( %info, colored => 0 ) | ||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | $self->add_to_history( %info ) unless $self->dry_run; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 0 | ▶ | sub add_to_history { | ||||||||||||||||||
|
Subroutine
add_to_history: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 185 | 0 | my $self = shift; | |||||||||||||||||||
| 186 | 0 | my %info = @_; | |||||||||||||||||||
| 187 | 0 | my $file = $self->rc_file_path( 'history' ); | |||||||||||||||||||
| 188 | 0 | open my $fh, '>>', $file; | |||||||||||||||||||
| 189 | 0 | print {$fh} Dump \%info; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 0 | ▶ | after unlock_achievement => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 193 | 0 | my( $self, %info ) = @_; | |||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 0 | ▶ | return unless $self->interactive; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | say 'Congrats! You have unlocked a new achievement!'; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 0 | say '*' x 60; | |||||||||||||||||||
| 200 | 0 | say '*** ', $info{achievement}; | |||||||||||||||||||
| 201 | 0 | ▶ | say '*** level ', $info{level} if $info{level}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | 0 | say ''; | |||||||||||||||||||
| 203 | 0 | ▶ | say $info{details} if $info{details}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 0 | say '*' x 60; | |||||||||||||||||||
| 205 | 7 | }; | |||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 0 | ▶ | method generate_report( $format ) { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
generate_report: not called CC=4 SCAR=26.9
Branch: 2 branches
| |||||||||||||||||||||
| 208 | 0 | my $class = 'Perl::Achievements::Report::'. ucfirst $format; | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 0 | ▶ | eval "use $class; 1" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | or die "could not use format '$format': $@\n"; | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 0 | my $report = $class->new( | |||||||||||||||||||
| 214 | who => $self->user->name, | ||||||||||||||||||||
| 215 | history => $self->history, | ||||||||||||||||||||
| 216 | ); | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 0 | return $report->generate; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 7 | 1; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||