Maker/Command/bashcompletion.pm

filter: Statementstmt 29.3% 17 / 58 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 45.4% 5 / 11 Podpod 100% 4 / 4 totaltotal 28.8% 26 / 90 CC 10 SCAR 26.3
CC
10
Cov
24%
_bash_completion
30.0
execute
20.0
_create_bash_completion_files
12.0
CRAP 13.8
↑ Summary Maker/Command/info.pm »
1 package OPM::Maker::Command::bashcompletion;
2 16 $OPM::Maker::Command::bashcompletion::VERSION = '1.17';
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: build bash completion script
7  
8 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use OPM::Maker -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub abstract {
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
14 0 return "create bash completion script and enable it for the user";
15 }
16  
17 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
18 0 return ();
19 }
20  
21 0 ▶sub usage_desc {
Subroutine usage_desc: not called CC=1 SCAR=6.9
Pod documented
22 0 return "opmbuild bashcompletion";
23 }
24  
25 0 ▶sub execute {
Subroutine execute: not called CC=4 SCAR=30.0
Pod documented
26 0 my ($self, $opt, $args) = @_;
27  
28 0 my $app = $self->app;
29  
30 my %commands = map {
31 0 my ($short) = (split /::/, $_)[-1];
32 0 $short => $_;
33 } grep {
34 0 $_ =~ m{\AOPM::Maker::Command};
35 } $app->command_plugins;
36  
37 0 my %completion_data;
38  
39 CMD:
40 0 for my $cmd ( sort keys %commands ) {
41 0 my $module = $commands{$cmd};
42  
43 my @opts = map {
44 0 ▶ my $opt_spec = $_->[0] // '';
Condition0%0/2
$_->[0] // ""
l!l
00
MC/DC0%0/1
$_->[0] // ""
$_->[0]
Truth table0%0/2
A
A= $_->[0]
Aresult
11
00
45 0 my $opt_name = ( split /=/, $opt_spec )[0];
46 0 ▶ $opt_name ? '--' . $opt_name : ();
Branch: 1 branch
BranchTrueFalseTotal
$opt_name ? : 0 0 0
47 } $module->opt_spec;
48  
49 0 $completion_data{$cmd} = \@opts;
50 }
51  
52 0 my $bash_completion = $self->_bash_completion( %completion_data );
53 0 $self->_create_bash_completion_files( $bash_completion );
54 }
55  
56 0 ▶sub _create_bash_completion_files {
Subroutine _create_bash_completion_files: not called CC=3 SCAR=24.8
57 0 my ($self, $content) = @_;
58  
59 0 my $base = dir( $ENV{HOME} );
60 0 my $completion = $base->file( '.bash_completion' );
61 0 ▶ if ( !-f $completion->stringify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $completion->stringify) 0 0 0
62 0 my $completion_content = q!
63 for bcfile in ~/.bash_completion.d/* ; do
64 . $bcfile
65 done
66 !;
67  
68 0 $completion->spew( $completion_content );
69 }
70  
71 0 my $dir = $base->subdir( '.bash_completion.d' );
72 0 ▶ if ( !-d $dir->stringify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir->stringify) 0 0 0
73 0 $dir->mkpath;
74 }
75  
76 0 my $opmbuild_completion = $dir->file('opmbuild');
77 0 $opmbuild_completion->spew( $content );
78 }
79  
80 0 ▶sub _bash_completion {
Subroutine _bash_completion: not called CC=5 SCAR=34.0
81 0 my ($self, %data) = @_;
82  
83 0 my $subcommands = join ' ', sort keys %data;
84  
85 0 my @subcommand_data;
86  
87 CMD:
88 0 for my $cmd ( sort keys %data ) {
89 0 ▶ next CMD if !@{ $data{$cmd} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$data{$cmd} or [];} 0 0 0
Condition0%0/2
$data{$cmd} || []
l!l
00
MC/DC0%0/1
$data{$cmd} || []
$data{$cmd}
Truth table0%0/2
A
A= $data{$cmd}
Aresult
11
00
90  
91 my $case = sprintf q~ %s)
92 local %s_opts="%s"
93 COMPREPLY=( $(compgen -W "${%s_opts}" -- ${cur}) )
94 return 0
95 ;;~,
96 $cmd,
97 $cmd,
98 0 ▶ ( join ' ', @{ $data{$cmd} || [] } ),
Condition0%0/2
$data{$cmd} || []
l!l
00
MC/DC0%0/1
$data{$cmd} || []
$data{$cmd}
Truth table0%0/2
A
A= $data{$cmd}
Aresult
11
00
99 $cmd
100 ;
101  
102 0 push @subcommand_data, $case;
103 }
104  
105 0 my $subcommand_opts = join "\n", @subcommand_data;
106  
107 0 my $completion_script = sprintf q~
108 _opmbuild()
109 {
110 local cur prev opts base
111  
112 COMPREPLY=()
113 cur="${COMP_WORDS[COMP_CWORD]}"
114 prev="${COMP_WORDS[COMP_CWORD-1]}"
115  
116 opts="%s"
117  
118 case "${prev}" in
119 opmbuild)
120 ;;
121 %s
122 *)
123 compopt -o nospace
124 COMPREPLY=( $( compgen -d -f -- $cur ) )
125 return 0
126 ;;
127 esac
128  
129 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
130 return 0;
131 }
132  
133 complete -F _opmbuild opmbuild
134 ~,
135 $subcommands,
136 $subcommand_opts
137 ;
138 }
139  
140 16 1;
141  
142 __END__
↑ Summary Maker/Command/info.pm »