Cme/Common.pm

filter: Statementstmt 60.1% 89 / 148 Branchbran 36.5% 19 / 52 Conditioncond 33.3% 7 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 71.4% 20 / 28 Podpod 0.0% 0 / 13 totaltotal 49.2% 136 / 276 CC 36 SCAR 32.2
CC
36
Cov
52%
process_args
73.1
get_documentation
20.0
autostash
20.0
CRAP 25.0
« Cme/Command/edit.pm ↑ Summary Cme/Command/update.pm »
1 #
2 # This file is part of App-Cme
3 #
4 # This software is Copyright (c) 2014-2022 by Dominique Dumont <ddumont@cpan.org>.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 #ABSTRACT: Common methods for App::Cme
11  
12 package App::Cme::Common;
13 2 $App::Cme::Common::VERSION = '1.049';
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Config::Model 2.124 qw/initialize_log4perl/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Config::Model::Lister;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Pod::POM;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Pod::POM::View::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 my @store;
29  
30 29 ▶sub cme_global_options {
Subroutine cme_global_options: called CC=1 SCAR=0.0
Pod undocumented
31 29 my ( $class, $app ) = @_;
32  
33 29 my @global_options = (
34 [ "model-dir=s" => "Specify an alternate directory to find model files"],
35 [ "try-app-as-model!" => "try to load a model using directly the application name "
36 . "specified as 3rd parameter on the command line"],
37 [ "save!" => "Force a save even if no change was done" ],
38 [ "force-load!" => "Load file even if error are found in data. Errors must be fixed before saving."],
39 [ "create!" => "start from scratch."],
40 [ "root-dir=s" => "Change root directory. Mostly used for test"],
41 [ "file=s" => "Specify a target file"],
42 # to be deprecated
43 [ "canonical!" => "write back config data according to canonical order" ],
44 [ "trace|stack-trace!" => "Provides a full stack trace when exiting on error"],
45 [ "verbose!" => "Show what's going on"],
46 [ "quiet" => "Suppress all output except error messages"],
47 # no bundling
48 { getopt_conf => [ qw/no_bundling/ ] }
49 );
50  
51 return (
52 29 @global_options,
53 );
54 }
55  
56 29 ▶sub check_unknown_args {
Subroutine check_unknown_args: called CC=2 SCAR=7.0
Pod undocumented
57 29 my ($self, $args) = @_;
58  
59 29 my @unknown_options = grep { /^-/ } @$args ;
60 # $self->usage_error("Unknown option: @unknown_options") if @unknown_options;
61 29 ▶ warn("Unknown option: @unknown_options. Unknown option will soon be a fatal error.\n") if @unknown_options;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown_options 0 29 29
62 29 return;
63 }
64  
65 # modifies $args in place
66 22 ▶sub process_args {
Subroutine process_args: called CC=18 SCAR=42.9
Pod undocumented
67 22 my ($self, $opt, $args) = @_;
68  
69 # see Debian #839593 and perlunicook(1) section X 13
70 22 @$args = map { decode_utf8($_, 1) } @$args;
71  
72 22 my ( $categories, $appli_info, $appli_map ) = Config::Model::Lister::available_models;
73 22 my $application = shift @$args;
74 22 ▶ unless ($application) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($application) 0 22 22
75 0 $self->usage_error(
76 "Missing application parameter. Run 'cme list' to get the "
77 . "list of installed cme applications\n"
78 );
79 }
80  
81 22 my $root_model = $appli_map->{$application};
82 22 ▶ $root_model ||= $application if $opt->{try_app_as_model};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"try_app_as_model"} 0 22 22
Condition0%0/3
$root_model ||= $application
l!l&&r!l&&!r
000
MC/DC0%0/2
$root_model ||= $application
$root_model $application
Truth table0%0/3
A ||= B
A= $root_modelB= $application
ABresult
1X1
011
000
83  
84 22 ▶ Config::Model::Exception::Any->Trace(1) if $opt->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"trace"} 0 22 22
85  
86 22 ▶ if ( not defined $root_model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root_model) 0 22 22
87 0 die "Can't locate model for application '$application'.\n"
88 . "Run 'cme list' for the list of models available on your system.\n"
89 . "You may need to install another Config::Model Perl module.\n"
90 . "See the available models there: https://github.com/dod38fr/config-model/wiki/Available-models-and-backends\n";
91 }
92  
93 22 my $command = (split('::', ref($self)))[-1] ;
94  
95 22 ▶ if ($appli_info->{$application}{require_config_file}
Branch: 1 branch
BranchTrueFalseTotal
if ($appli_info->{$application}{"require_config_file"} and $appli_info->{$application}{"require_backend_argument"}) 0 22 22
Condition33%1/3
$appli_info->{$application}{"require_config_file"} and $appli_info->{$application}{"require_backend_argument"}
!ll&&!rl&&r
2200
MC/DC0%0/2
$appli_info->{$application}{"require_config_file"} and $appli_info->{$application}{"require_backend_argument"}
$appli_info->{$application}{"require_config_file"} $appli_info->{$application}{"require_backend_argument"}
Truth table33%1/3
A and B
A= $appli_info->{$application}{"require_config_file"}B= $appli_info->{$application}{"require_backend_argument"}
ABresult
0X0
100
111
96 and $appli_info->{$application}{require_backend_argument}) {
97 0 die "Error in $root_model model: cannot have both require_config_file and require_backend_argument.\n";
98 }
99  
100 # @ARGV should be [ $config_file ] [ modification_instructions ]
101 22 my $config_file;
102 22 ▶ if ( $appli_info->{$application}{require_config_file} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($appli_info->{$application}{'require_config_file'}) { } 0 22 22
elsif ($appli_info->{$application}{'allow_config_file_override'}) { } 0 22 22
103 0 ▶ $config_file = $opt->{file} || shift @$args ;
Condition0%0/3
$opt->{'file'} || shift @$args
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{'file'} || shift @$args
$opt->{'file'} shift @$args
Truth table0%0/3
A || B
A= $opt->{'file'}B= shift @$args
ABresult
1X1
011
000
104 0 ▶ $self->usage_error(
Branch: 1 branch
BranchTrueFalseTotal
unless $config_file 0 0 0
105 "no config file specified. Command should be 'cme $command $application configuration_file'",
106 ) unless $config_file;
107 }
108 elsif ( $appli_info->{$application}{allow_config_file_override}) {
109 0 $config_file = $opt->{file};
110 }
111  
112 22 ▶ if ( $appli_info->{$application}{require_backend_argument} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($appli_info->{$application}{"require_backend_argument"}) 0 22 22
113 # let the backend handle a missing arg and provide a clear error message
114 0 my $b_arg = $opt->{_backend_arg} = shift @$args ;
115 0 ▶ if (not $b_arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($b_arg) 0 0 0
116 0 my $message = $appli_info->{$application}{backend_argument_info} ;
117 0 ▶ my $insert = $message ? " ( $message )": '';
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
118 0 die "application $application requires a 3rd argument$insert. "
119 . "I.e. 'cme $command $application <backend_arg>'\n";
120 }
121  
122 0 ▶ if ( $appli_info->{$application}{use_backend_argument_as_config_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($appli_info->{$application}{"use_backend_argument_as_config_file"}) 0 0 0
123 0 $config_file = $appli_info->{$application}{config_dir} . '/' . $b_arg;
124 }
125 }
126  
127 # remove legacy '~~'
128 22 ▶ if ($args->[0] and $args->[0] eq '~~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[0] and $args->[0] eq "~~") 0 22 22
Condition66%2/3
$args->[0] and $args->[0] eq "~~"
!ll&&!rl&&r
1750
MC/DC0%0/2
$args->[0] and $args->[0] eq "~~"
$args->[0] $args->[0] eq "~~"
Truth table66%2/3
A and B
A= $args->[0]B= $args->[0] eq "~~"
ABresult
0X0
100
111
129 0 warn "Argument '~~' was a bad idea and is now ignored. Use -file option to "
130 ."specify a target file or just forget about '~~' argument\n";
131 0 shift @$args;
132 }
133  
134 # override (or specify) configuration dir
135 22 $opt->{_config_dir} = $appli_info->{$application}{config_dir};
136  
137 22 $opt->{_application} = $application ;
138 22 $opt->{_config_file} = $config_file;
139 22 $opt->{_root_model} = $root_model;
140  
141 22 return;
142 }
143  
144 48 ▶sub model {
Subroutine model: called CC=3 SCAR=11.0
Pod undocumented
145 48 my ($self, $opt, $args) = @_;
146  
147 48 my %cm_args;
148 48 ▶ $cm_args{model_dir} = $opt->{model_dir} if $opt->{model_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"model_dir"} 3 45 48
149  
150 48 ▶ if (not $self->{_model}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_model"}) 22 26 48
151 22 initialize_log4perl( verbose => $opt->{_verbose} );
152  
153 22 my $model = $self->{_model} = Config::Model->new( %cm_args );
154 22 push @store, $model;
155 }
156 48 return $self->{_model};
157 }
158  
159 48 ▶sub instance {
Subroutine instance: called CC=5 SCAR=16.3
Pod undocumented
160 48 my ($self, $opt, $args) = @_;
161  
162 my %instance_args = (
163 root_class_name => $opt->{_root_model},
164 instance_name => $opt->{_application},
165 application => $opt->{_application},
166 check => $opt->{force_load} ? 'no' : 'yes',
167 auto_create => $opt->{create},
168 backend_arg => $opt->{_backend_arg},
169 config_file => $opt->{_config_file},
170 config_dir => $opt->{_config_dir},
171 48 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'force_load'} ? : 0 48 48
172  
173 48 foreach my $param (qw/root_dir canonical backup instance_name/) {
174 192 ▶ $instance_args{$param} = $opt->{$param} if defined $opt->{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{$param} 26 166 192
175 }
176  
177 48 ▶ return $self->{_instance} ||= $self->model->instance(%instance_args);
Condition66%2/3
$self->{"_instance"} ||= $self->model->instance(%instance_args)
l!l&&r!l&&!r
24240
MC/DC0%0/2
$self->{"_instance"} ||= $self->model->instance(%instance_args)
$self->{"_instance"} $self->model->instance(%instance_args)
Truth table66%2/3
A ||= B
A= $self->{"_instance"}B= $self->model->instance(%instance_args)
ABresult
1X1
011
000
178 }
179  
180 24 ▶sub init_cme {
Subroutine init_cme: called CC=1 SCAR=0.0
Pod undocumented
181 24 my ($self, @args) = @_;
182 # model and inst are deleted if not kept in a scope
183 24 return ( $self->model(@args) , $self->instance(@args), $self->instance->config_root );
184 }
185  
186 18 ▶sub save {
Subroutine save: called CC=3 SCAR=11.0
Pod undocumented
187 18 my ($self,$inst,$opt) = @_;
188  
189 18 ▶ $inst->say_changes unless $opt->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"quiet"} 17 1 18
190  
191 # if load was forced, must write back to clean up errors (even if they are not changes
192 # at semantic level, i.e. removed unnecessary stuff)
193 18 ▶ $inst->write_back( force => $opt->{force_load} || $opt->{save} );
Condition66%2/3
$opt->{'force_load'} || $opt->{'save'}
l!l&&r!l&&!r
0315
MC/DC50%1/2
$opt->{'force_load'} || $opt->{'save'}
$opt->{'force_load'} $opt->{'save'}
Truth table66%2/3
A || B
A= $opt->{'force_load'}B= $opt->{'save'}
ABresult
1X1
011
000
194  
195 18 return;
196 }
197  
198 0 ▶sub run_tk_ui {
Subroutine run_tk_ui: not called CC=2 SCAR=15.4
Pod undocumented
199 0 my ($self, $instance, $opt) = @_;
200  
201 0 require Config::Model::TkUI;
202 0 require Tk;
203 0 require Tk::ErrorDialog;
204 0 Tk->import;
205  
206 2 ▶ no warnings 'once'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
207 0 my $mw = MainWindow->new;
208 0 $mw->withdraw;
209  
210 # Thanks to Jerome Quelin for the tip
211 0 $mw->optionAdd( '*BorderWidth' => 1 );
212  
213 # -root parameter is deprecated
214 0 my $cmu = $mw->ConfigModelUI( -instance => $instance );
215  
216 0 ▶ $instance->on_message_cb(sub{$cmu->show_message(@_);});
Subroutine __ANON__: not called CC=1 SCAR=6.9
217  
218 0 ▶ if ($opt->{open_item}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"open_item"}) 0 0 0
219 0 my $obj = $instance->grab(step => $opt->{open_item}, autoadd => 0);
220 # using afterIdle avoids geometry problem where the right side
221 # of the widget is not visible
222 0 ▶ $mw->afterIdle( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 $cmu->force_element_display($obj);
224 0 my $path = $cmu->{tktree}->selectionGet;
225 0 $cmu->create_element_widget('edit', $path);
226 })
227 0 }
228  
229 0 &MainLoop; # Tk's
230  
231 0 return;
232 }
233  
234 0 ▶sub run_shell_ui ($self, $term_class, $inst) {
Subroutine run_shell_ui: not called CC=1 SCAR=6.9
Pod undocumented
235 0 my $shell_ui = $term_class->new (
236 root => $inst->config_root,
237 title => $inst->application . ' configuration',
238 prompt => ' >',
239 );
240  
241 # engage in user interaction
242 0 $shell_ui->run_loop;
243  
244 0 return;
245 }
246  
247 0 ▶sub get_documentation {
Subroutine get_documentation: not called CC=4 SCAR=30.0
Pod undocumented
248 0 my ($self) = @_;
249  
250 0 my $parser = Pod::POM->new();
251 0 my $pkg = blessed ($self);
252 0 $pkg =~ s!::!/!g;
253 0 ▶ my $pom = $parser->parse_file($INC{$pkg.'.pm'})
Condition0%0/3
$parser->parse_file($INC{$pkg . '.pm'}) || $parser->croak->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$parser->parse_file($INC{$pkg . '.pm'}) || $parser->croak->error
$parser->parse_file($INC{$pkg . '.pm'}) $parser->croak->error
Truth table0%0/3
A || B
A= $parser->parse_file($INC{$pkg . '.pm'})B= $parser->croak->error
ABresult
1X1
011
000
254 || croak $parser->error();
255  
256 0 my $sections = $pom->head1();
257 0 my @ret ;
258 0 foreach my $s (@$sections) {
259 0 ▶ push (@ret ,$s) if $s->title() =~ /DESCRIPTION|USAGE|OPTIONS|EXIT/;
Branch: 1 branch
BranchTrueFalseTotal
if $s->title =~ /DESCRIPTION|USAGE|OPTIONS|EXIT/u 0 0 0
260 }
261 0 return join ("", map { Pod::POM::View::Text->print($_)} @ret) . "Options:\n";;
262 }
263  
264 0 ▶sub commit ($self, $msg) {
Subroutine commit: not called CC=2 SCAR=17.9
Pod undocumented
265 0 ▶ system(qw/git commit -a -m/, $msg) == 0
Branch: 1 branch
BranchTrueFalseTotal
system(("git", "commit", "-a", "-m"), $msg) == 0 or die "git commit failed: $?\n" 0 0 0
266 or die "git commit failed: $?\n";
267  
268 0 return;
269 }
270  
271 0 ▶sub autostash ($self) {
Subroutine autostash: not called CC=4 SCAR=30.0
Pod undocumented
272 ## no critic(InputOutput::ProhibitBacktickOperators)
273 0 my $r = `git status --porcelain --untracked-files=no`;
274 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
275 0 die "git status command failed: $?\n";
276 }
277 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
278 0 ▶ system(qw/git stash push --quiet --message/, "cme run auto stash") == 0
Branch: 1 branch
BranchTrueFalseTotal
system(("git", "stash", "push", "--quiet", "--message"), "cme run auto stash") == 0 or die "git stash push failed: $?\n" 0 0 0
279 or die "git stash push failed: $?\n";
280 0 return 1;
281 }
282 0 return 0;
283 }
284  
285 0 ▶sub pop_stash ($self) {
Subroutine pop_stash: not called CC=2 SCAR=17.9
Pod undocumented
286 0 ▶ system(qw/git stash pop --quiet/) == 0
Branch: 1 branch
BranchTrueFalseTotal
system("git", "stash", "pop", "--quiet") == 0 or die "git stash pop failed: $?\n" 0 0 0
287 or die "git stash pop failed: $?\n";
288 0 return;
289 }
290  
291 2 1;
292  
293 __END__
« Cme/Command/edit.pm ↑ Summary Cme/Command/update.pm »