| 1 | package App::VTide; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | # Created on: 2016-01-28 09:58:07 | ||||||||||||||||||||||||
| 4 | # Create by: Ivan Wills | ||||||||||||||||||||||||
| 5 | # $Id$ | ||||||||||||||||||||||||
| 6 | # $Revision$, $HeadURL$, $Date$ | ||||||||||||||||||||||||
| 7 | # $Revision$, $Source$, $Date$ | ||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | 6 | ▶ | use Moo; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 6 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | 6 | ▶ | use version; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | 6 | ▶ | use Carp; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 13 | 6 | ▶ | use English qw/ -no_match_vars /; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 14 | 6 | ▶ | use Getopt::Alt; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 15 | 6 | ▶ | use App::VTide::Config; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 16 | 6 | ▶ | use App::VTide::Hooks; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 6 | ▶ | use Path::Tiny; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 18 | 6 | ▶ | use YAML::Syck qw/ LoadFile DumpFile /; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||
| 20 | 6 | our $VERSION = version->new('1.0.6'); | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||
| 22 | has config => ( | ||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||
| 24 | lazy => 1, | ||||||||||||||||||||||||
| 25 | 1 | ▶ | default => sub { return App::VTide::Config->new() }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 26 | 6 | ); | |||||||||||||||||||||||
| 27 | has hooks => ( | ||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||||||
| 30 | 0 | ▶ | default => sub { return App::VTide::Hooks->new( vtide => $_[0] ) }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 31 | 6 | ); | |||||||||||||||||||||||
| 32 | 6 | has sub_commands => ( | |||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||
| 34 | lazy => 1, | ||||||||||||||||||||||||
| 35 | builder => '_sub_commands', | ||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 0 | ▶ | sub run { | ||||||||||||||||||||||
|
Subroutine
run: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||
| 39 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 40 | 0 | $self->config->history(@ARGV); | |||||||||||||||||||||||
| 41 | 0 | my @sub_commands = keys %{ $self->sub_commands }; | |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||
| 43 | my ( $options, $cmd, $opt ) = get_options( | ||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||
| 45 | name => 'vtide-cmd', | ||||||||||||||||||||||||
| 46 | conf_prefix => '.', | ||||||||||||||||||||||||
| 47 | helper => 1, | ||||||||||||||||||||||||
| 48 | default => {}, | ||||||||||||||||||||||||
| 49 | 0 | ▶ | auto_complete => sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||
| 50 | 0 | my ( $option, $auto, $errors ) = @_; | |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | my $sub_command = | ||||||||||||||||||||||||
| 53 | $option->cmd | ||||||||||||||||||||||||
| 54 | 0 | ▶ | || $ARGV[ $option->opt->{auto_complete} - 1 ] | ||||||||||||||||||||||
|
Condition0%0/5
$option->cmd || $ARGV[$option->opt->{'auto_complete'} - 1] || ''
$option->cmd or $ARGV[$option->opt->{"auto_complete"} - 1]
MC/DC0%0/2
$option->cmd || $ARGV[$option->opt->{'auto_complete'} - 1] || ''
$option->cmd
$ARGV[$option->opt->{"auto_complete"} - 1]
Truth table0%0/3
A or B
A= $option->cmdB= $ARGV[$option->opt->{"auto_complete"} - 1]
| |||||||||||||||||||||||||
| 55 | || ''; | ||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( $sub_command eq '--' ) { | ||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||
| 57 | 0 | print join ' ', sort @sub_commands; | |||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||
| 60 | 0 | ▶ | elsif ( $sub_command && grep { /^$sub_command./ } | ||||||||||||||||||||||
|
Condition0%0/3
$sub_command and grep {/^$sub_command./;} @sub_commands
MC/DC0%0/2
$sub_command and grep {/^$sub_command./;} @sub_commands
$sub_command
grep {/^$sub_command./;} @sub_commands
Truth table0%0/3
A and B
A= $sub_commandB= grep {/^$sub_command./;} @sub_commands
| |||||||||||||||||||||||||
| 61 | @sub_commands ) | ||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||
| 63 | 0 | print join ' ', sort grep { /^$sub_command/ } @sub_commands; | |||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||
| 66 | elsif ( !$self->sub_commands->{$sub_command} ) { | ||||||||||||||||||||||||
| 67 | 0 | unshift @{ $option->files }, $sub_command; | |||||||||||||||||||||||
| 68 | 0 | ▶ | $sub_command = $ENV{VTIDE_DIR} ? 'edit' : 'start'; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||
| 70 | eval { | ||||||||||||||||||||||||
| 71 | 0 | $self->load_subcommand( $sub_command, $option ) | |||||||||||||||||||||||
| 72 | ->auto_complete($auto); | ||||||||||||||||||||||||
| 73 | 0 | 1; | |||||||||||||||||||||||
| 74 | 0 | ▶ | } or do { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 75 | print join ' ', | ||||||||||||||||||||||||
| 76 | 0 | grep { /$sub_command/xms } sort @sub_commands; | |||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||
| 79 | 0 | ▶ | auto_complete_shortener => sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||
| 80 | 0 | my ( $getopt, @args ) = @_; | |||||||||||||||||||||||
| 81 | 0 | ▶ | my $sub_command = shift @args || ''; | ||||||||||||||||||||||
|
Condition0%0/2
shift @args || ''
MC/DC0%0/1
shift @args || ''
shift @args
Truth table0%0/2
A
A= shift @args
| |||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( grep { /^$sub_command./ } @sub_commands ) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( grep { $_ eq $sub_command } @sub_commands ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 85 | 0 | $getopt->cmd($sub_command); | |||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||
| 88 | 0 | unshift @args, $sub_command; | |||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||
| 91 | elsif ( !$self->sub_commands->{$sub_command} ) { | ||||||||||||||||||||||||
| 92 | 0 | ▶ | $getopt->cmd( $ENV{VTIDE_DIR} ? 'edit' : 'start' ); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 93 | 0 | unshift @args, $sub_command; | |||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||
| 96 | 0 | ▶ | $getopt->cmd($sub_command) if !$getopt->cmd; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | 0 | return @args; | |||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||
| 101 | sub_command => $self->sub_commands, | ||||||||||||||||||||||||
| 102 | help_package => __PACKAGE__, | ||||||||||||||||||||||||
| 103 | help_packages => { | ||||||||||||||||||||||||
| 104 | map { | ||||||||||||||||||||||||
| 105 | 0 | $_ => __PACKAGE__ . '::Command::' . join '', | |||||||||||||||||||||||
| 106 | 0 | map { ucfirst $_ } split /-/, $_ | |||||||||||||||||||||||
| 107 | } @sub_commands, | ||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||
| 110 | [ | ||||||||||||||||||||||||
| 111 | 'add|add-to-session|a', | ||||||||||||||||||||||||
| 112 | 'update|u!', | ||||||||||||||||||||||||
| 113 | 'name|n=s', | ||||||||||||||||||||||||
| 114 | 'test|T!', | ||||||||||||||||||||||||
| 115 | 'verbose|v+', | ||||||||||||||||||||||||
| 116 | ], | ||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( !$self->sub_commands->{ $opt->cmd } ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 120 | 0 | unshift @ARGV, $opt->cmd; | |||||||||||||||||||||||
| 121 | 0 | ▶ | $opt->cmd( $ENV{VTIDE_DIR} ? 'edit' : 'start' ); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 122 | 0 | $opt->files( \@ARGV ); | |||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | 0 | my $subcommand = eval { $self->load_subcommand( $opt->cmd, $opt ) }; | |||||||||||||||||||||||
| 126 | 0 | ▶ | if ( !$subcommand ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 127 | $subcommand = | ||||||||||||||||||||||||
| 128 | 0 | ▶ | $self->load_subcommand( $ENV{VTIDE_DIR} ? 'edit' : 'start', $opt ); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 129 | 0 | my ( undef, $dir ) = $subcommand->session_dir( $opt->cmd ); | |||||||||||||||||||||||
| 130 | 0 | ▶ | if ( !$dir ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 131 | 0 | my $error = $@; | |||||||||||||||||||||||
| 132 | 0 | ▶ | warn $@ if $opt->opt->verbose; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 133 | 0 | warn "Unknown command '$cmd'!\n", | |||||||||||||||||||||||
| 134 | "Valid commands - ", ( join ', ', sort @sub_commands ), | ||||||||||||||||||||||||
| 135 | "\n"; | ||||||||||||||||||||||||
| 136 | 0 | require Pod::Usage; | |||||||||||||||||||||||
| 137 | 0 | Pod::Usage::pod2usage( | |||||||||||||||||||||||
| 138 | -verbose => 1, | ||||||||||||||||||||||||
| 139 | -input => __FILE__, | ||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||
| 142 | 0 | unshift @{ $opt->files }, $opt->cmd; | |||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||
| 145 | $subcommand->options->default( | ||||||||||||||||||||||||
| 146 | 0 | { %{$options}, %{ $subcommand->options->default } } ); | |||||||||||||||||||||||
| 147 | eval { | ||||||||||||||||||||||||
| 148 | 0 | $subcommand->run; | |||||||||||||||||||||||
| 149 | 0 | 1; | |||||||||||||||||||||||
| 150 | 0 | ▶ | } or do { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 151 | 0 | warn "Error running " . $opt->cmd . "!\n"; | |||||||||||||||||||||||
| 152 | 0 | warn $@; | |||||||||||||||||||||||
| 153 | 0 | sleep 5; | |||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||
| 155 | 0 | return; | |||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||
| 158 | 1 | ▶ | sub load_subcommand { | ||||||||||||||||||||||
|
Subroutine
load_subcommand: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 159 | 1 | my ( $self, $cmd, $opt ) = @_; | |||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||
| 161 | 1 | my $lib = join '', map { ucfirst $_ } split /-/, $cmd; | |||||||||||||||||||||||
| 162 | 1 | my $file = "App/VTide/Command/$lib.pm"; | |||||||||||||||||||||||
| 163 | 1 | my $module = "App::VTide::Command::$lib"; | |||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||
| 165 | 1 | require $file; | |||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||
| 167 | 1 | return $module->new( | |||||||||||||||||||||||
| 168 | defaults => $opt->opt, | ||||||||||||||||||||||||
| 169 | options => $opt, | ||||||||||||||||||||||||
| 170 | vtide => $self, | ||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||
| 174 | 0 | ▶ | sub _sub_commands { | ||||||||||||||||||||||
|
Subroutine
_sub_commands: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 175 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 176 | 0 | my $sub_file = path $ENV{HOME}, '.vtide', 'sub-commands.yml'; | |||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||
| 178 | 0 | ▶ | mkdir $sub_file->parent if !-d $sub_file->parent; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||
| 180 | #if ( -f $sub_file && path($0)->stat->mtime ne $sub_file->stat->mtime ) { | ||||||||||||||||||||||||
| 181 | # unlink $sub_file; | ||||||||||||||||||||||||
| 182 | #} | ||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||
| 184 | #return LoadFile("$sub_file") if -f $sub_file; | ||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||
| 186 | 0 | return $self->_generate_sub_command(); | |||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||
| 189 | 0 | ▶ | sub _generate_sub_command { | ||||||||||||||||||||||
|
Subroutine
_generate_sub_command: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 190 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 191 | 0 | my $sub_file = path $ENV{HOME}, '.vtide', 'sub-commands.yml'; | |||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | 0 | require Module::Pluggable; | |||||||||||||||||||||||
| 194 | 0 | Module::Pluggable->import( | |||||||||||||||||||||||
| 195 | require => 1, | ||||||||||||||||||||||||
| 196 | search_path => ['App::VTide::Command'] | ||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||
| 198 | 0 | my @commands = __PACKAGE__->plugins; | |||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||
| 200 | 0 | my $sub_commands = {}; | |||||||||||||||||||||||
| 201 | 0 | for my $command ( reverse sort @commands ) { | |||||||||||||||||||||||
| 202 | 0 | my ( $name, $conf ) = $command->details_sub; | |||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||
| 204 | 0 | $sub_commands->{$name} = $conf; | |||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||
| 207 | 0 | DumpFile( $sub_file, $sub_commands ); | |||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||
| 209 | 0 | return $sub_commands; | |||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||
| 212 | 6 | 1; | |||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||
| 214 | __END__ | ||||||||||||||||||||||||