blib/lib/App/Cme/Command/meta.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Config/Model/Itself/BackendDetector.pm ↑ Summary blib/lib/Config/Model/models/Itself/Application.pl »
1 #
2 # This file is part of Config-Model-Itself
3 #
4 # This software is Copyright (c) 2007-2026 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 # ABSTRACT: Work on the configuration model of an application
11  
12 package App::Cme::Command::meta 2.032;
13  
14 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use App::Cme -command ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use base qw/App::Cme::Common/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Config::Model 2.075;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Config::Model::Itself ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use YAML::PP qw/Load Dump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶use Tk ;
Subroutine BEGIN: called CC=1 SCAR=2.6
30 use Config::Model::TkUI 1.381;
31 use Config::Model::Itself::TkEditUI ;
32 use Path::Tiny 0.125; # for mkdir
33 use List::Util 1.33 qw/all/;
34  
35 binmode STDOUT, ':encoding(UTF-8)';
36  
37 my %meta_cmd = (
38 check => \&check,
39 dump => \&dump_cds,
40 'dump-yaml' => \&dump_yaml,
41 'gen-dot' => \&gen_dot,
42 edit => \&edit,
43 save => \&save,
44 plugin => \&plugin,
45 );
46  
47 sub validate_args {
48 my ($self, $opt, $args) = @_;
49  
50 my $mc = $opt->{'_meta_command'} = shift @$args || die "please specify meta sub command\n";
51  
52 if (not $meta_cmd{$mc}) {
53 die "Unexpected meta sub command: '$mc'. Expected ".join(' ', sort keys %meta_cmd)."\n";
54 }
55  
56 my ( $categories, $appli_info, $appli_map ) = Config::Model::Lister::available_models;
57 my $application = shift @$args;
58  
59 if ($mc eq 'plugin') {
60 unless ($application) {
61 die "Missing application name after 'plugin' command";
62 }
63 $opt->{_root_model} = $appli_map->{$application}
64 || die "Unknown application $application";
65 }
66 elsif ($application) {
67 $opt->{_root_model} = $appli_map->{$application} || $application;
68 }
69  
70 Config::Model::Exception::Any->Trace(1) if $opt->{trace};
71  
72 $opt->{_application} = $application ;
73  
74 return;
75 }
76  
77 sub validate_factorize ($flag) {
78 return 1 if $flag eq 'all';
79 return all {/^description|summary|warp|status|level$/;} split /,/,$flag;
80 }
81  
82 sub opt_spec {
83 my ( $class, $app ) = @_;
84  
85 return (
86 [
87 "dir=s" => "directory where to read and write a model",
88 {default => 'lib/Config/Model'}
89 ],
90 [
91 "dumptype=s" => "dump every values (full), only preset values "
92 . "or only customized values (default)",
93 {callbacks => { 'expected values' => sub { $_[0] =~ m/^full|preset|custom$/ ; }}}
94 ],
95 [ "dev!" => 'use model in ./lib to create a plugin'],
96 [
97 "factorize=s" => 'factorize class parameters. Comma separated list of either '.
98 '"description", "summary", "level", "warp", "status, or just "all"',
99 {callbacks => { 'expected "all" or a list' => sub { validate_factorize($_[0]); }}},
100 ],
101 [ "open-item=s" => "force the UI to open the specified node"],
102 [ "plugin-file=s" => "create a model plugin in this file" ],
103 [ "load-yaml=s" => "load model from YAML file. Use '-' to load from STDIN" ],
104 [ "load=s" => "load model from cds file (Config::Model serialisation file). "
105 ."Use '-' to load from STDIN"],
106 [ "system!" => "read model from system files" ],
107 [ "test-and-quit=s" => "Used for tests" ],
108 $class->cme_global_options()
109 );
110 }
111  
112 sub usage_desc {
113 my ($self) = @_;
114 my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
115 return "$desc [ ".join(' | ', sort keys %meta_cmd)." ] your_model_class ";
116 }
117  
118 sub description {
119 my ($self) = @_;
120 return $self->get_documentation;
121 }
122  
123 sub read_data {
124 my $load_file = shift ;
125  
126 my @data ;
127 if ( $load_file eq '-' ) {
128 ## no critic (InputOutput::ProhibitExplicitStdin)
129 # user called cme with -load - or -load-yaml -, cannot use ARGV
130 @data = <STDIN> ;
131 }
132 else {
133 open my $load, '<', $load_file || die "cannot open load file $load_file:$!";
134 @data = <$load> ;
135 close $load;
136 }
137  
138 return wantarray ? @data : join('',@data);
139 }
140  
141 sub load_optional_data {
142 my ($self, $args, $opt, $root_model, $meta_root) = @_;
143  
144 if (defined $opt->{load}) {
145 my $data = read_data($opt->{load}) ;
146 $data = qq(class:"$root_model" ).$data unless $data =~ /^\s*class:/ ;
147 $meta_root->load($data);
148 }
149  
150 if (defined $opt->{'load-yaml'}) {
151 my $yaml = read_data($opt->{'load-yaml'}) ;
152 my $pdata = Load($yaml) ;
153 $meta_root->load_data($pdata) ;
154 }
155 return;
156 }
157  
158 # create meta model (i.e. loaded with Config::Model::Itself)
159 # returns:
160 # - meta instance
161 # - meta root
162 # - config::model lib dir, where the model to check or edit is (e.g. ./lib/Config/Model/models)
163 # - system config::model lib dir (e.g. /usr/share/perl5/Config/Model/models/ on Debian)
164 sub load_meta_model ($self, $opt, $args) {
165 my $cm_lib_dir = path($opt->{dir}) ;
166  
167 if (! $cm_lib_dir->is_dir) {
168 $cm_lib_dir->mkdir();
169 }
170  
171 my $meta_model = $self->{meta_model} = Config::Model -> new();
172  
173 my $meta_inst = $meta_model->instance(
174 root_class_name => 'Itself::Model',
175 instance_name => 'meta',
176 check => $opt->{'force-load'} ? 'no' : 'yes',
177 );
178  
179 my $meta_root = $meta_inst -> config_root ;
180  
181 my $system_cm_lib_dir = $INC{'Config/Model.pm'} ;
182 $system_cm_lib_dir =~ s/\.pm//;
183  
184 return ($meta_inst, $meta_root, $cm_lib_dir, path($system_cm_lib_dir));
185 }
186  
187 # create and load meta model (i.e. meta instance loaded with model to edit or check)
188 # returns:
189 # - object to read/write model (instance of Config::Model::Itself)
190 # - config::model lib dir, where the model to check or edit is (e.g. ./lib/Config/Model/models)
191 # - meta root
192 # - write callback. To be called after edition to write back model.
193 sub load_meta_root ($self, $opt, $args) {
194 my ($meta_inst, $meta_root, $cm_lib_dir, $system_cm_lib_dir) = $self->load_meta_model($opt,$args);
195  
196 say "Reading model from $system_cm_lib_dir" if $opt->system();
197  
198 my $rw_obj = Config::Model::Itself -> new(
199 model_object => $meta_root,
200 cm_lib_dir => $cm_lib_dir->canonpath
201 );
202  
203 $meta_inst->initial_load_start ;
204  
205 my $root_model = $opt->{_root_model};
206 my @read_args = (
207 force_load => $opt->{'force-load'},
208 root_model => $root_model,
209 # legacy => 'ignore',
210 );
211 if ($opt->system()) {
212 push @read_args,
213 application => $opt->{_application},
214 read_from => $system_cm_lib_dir ;
215 }
216  
217 # now load model
218 $rw_obj->read_all(@read_args);
219  
220 $meta_inst->initial_load_stop ;
221  
222 $self->load_optional_data($args, $opt, $root_model, $meta_root) ;
223  
224 my $write_sub = sub {
225 my $wr_dir = shift || $cm_lib_dir ;
226 $rw_obj->write_all(factorize => $opt->{factorize} // 'none');
227 return;
228 } ;
229 return ($rw_obj, $cm_lib_dir, $meta_root, $write_sub);
230 }
231  
232 sub load_meta_plugin {
233 my ($self, $opt, $args) = @_;
234  
235 my ($meta_inst, $meta_root, $cm_lib_dir, $system_cm_lib_dir) = $self->load_meta_model($opt, $args);
236  
237 my $root_model = $opt->{_root_model};
238 my $meta_cm_lib_dir = $opt->dev ? $cm_lib_dir : $system_cm_lib_dir ;
239 my $plugin_name = shift @$args or die "missing plugin file name after application name.";
240  
241 if ($plugin_name =~ s/\.pl$//) {
242 warn "removed '.pl' deprecated suffix from plugin name\n";
243 }
244  
245 say "Preparing plugin $plugin_name for model $root_model found in $meta_cm_lib_dir";
246 say "Use -dev option to create a plugin for a local model (i.e. in $cm_lib_dir)"
247 unless $opt->dev;
248  
249 # now load model
250 my $rw_obj = Config::Model::Itself -> new(
251 model_object => $meta_root,
252 cm_lib_dir => $meta_cm_lib_dir->canonpath,
253 ) ;
254  
255 $meta_inst->initial_load_start ;
256 $meta_inst->layered_start;
257  
258 $rw_obj->read_all(
259 force_load => $opt->{'force-load'},
260 root_model => $root_model,
261 # legacy => 'ignore',
262 );
263  
264 $meta_inst->layered_stop;
265  
266 # load any existing plugin file
267 $rw_obj->read_model_plugin(
268 plugin_dir => $cm_lib_dir.'/models/',
269 plugin_name => $plugin_name
270 ) ;
271  
272 $meta_inst->initial_load_stop ;
273  
274 $self->load_optional_data($args, $opt, $root_model, $meta_root) ;
275 my $root_model_dir = $root_model ;
276 $root_model_dir =~ s!::!/!g;
277 my $write_sub = sub {
278 $rw_obj->write_model_plugin(
279 plugin_dir => $cm_lib_dir->child("models/$root_model_dir.d"),
280 plugin_name => $plugin_name
281 );
282 } ;
283  
284 return ($rw_obj, $cm_lib_dir, $meta_root, $write_sub);
285 }
286  
287 sub execute {
288 my ($self, $opt, $args) = @_;
289  
290 # how to specify root-model when starting from scratch ?
291 # ask question and fill application file ?
292  
293 my $cmd_sub = $meta_cmd{$opt->{_meta_command}};
294  
295 $self->$cmd_sub($opt, $args);
296 return;
297 }
298  
299 sub save {
300 my ($self, $opt, $args) = @_;
301 my ($rw_obj, $cm_lib_dir, $meta_root, $write_sub) = $self->load_meta_root($opt, $args) ;
302  
303 say "Saving ",$rw_obj->root_model. ' model'. ($opt->dir ? ' in '.$opt->dir : '');
304 &$write_sub;
305 return;
306 }
307  
308 sub gen_dot {
309 my ($self, $opt, $args) = @_;
310 my ($rw_obj, $cm_lib_dir, $meta_root, $write_sub) = $self->load_meta_root($opt, $args) ;
311  
312 my $out = shift @$args || "model.dot";
313 say "Creating dot file $out";
314 path($out) -> spew( $rw_obj->get_dot_diagram );
315 return;
316 }
317  
318 sub check {
319 my ($self, $opt, $args) = @_;
320  
321 say "loading model" unless $opt->{quiet};
322 my ($rw_obj, $cm_lib_dir, $meta_root, $write_sub) = $self->load_meta_root($opt, $args) ;
323  
324 Config::Model::ObjTreeScanner->new( leaf_cb => sub { } )->scan_node( undef, $meta_root );
325  
326 say "checking data" unless $opt->{quiet};
327 $meta_root->dump_tree( mode => 'full' );
328 say "check done" unless $opt->{quiet};
329  
330 my $ouch = $meta_root->instance->has_warning;
331  
332 if ( $opt->{strict} and $ouch ) {
333 die "Found $ouch warnings in strict mode\n";
334 }
335  
336 return;
337 }
338  
339 sub dump_cds {
340 my ($self, $opt, $args) = @_;
341 my ($rw_obj, $cm_lib_dir, $meta_root, $write_sub) = $self->load_meta_root($opt, $args) ;
342  
343 my $dump_file = shift @$args || 'model.cds';
344 say "Dumping ".$rw_obj->root_model." in $dump_file";
345  
346 my $dump_string = $meta_root->dump_tree( mode => $opt->{dumptype} || 'custom' ) ;
347  
348 path($dump_file)->spew_utf8($dump_string);
349 return;
350 }
351  
352 sub dump_yaml{
353 my ($self, $opt, $args) = @_;
354 my ($rw_obj, $cm_lib_dir, $meta_root, $write_sub) = $self->load_meta_root($opt, $args) ;
355  
356 my $dump_file = shift @$args || 'model.yml';
357 say "Dumping ".$rw_obj->root_model." in $dump_file";
358  
359 my $dump_string = Dump($meta_root->dump_as_data(ordered_hash_as_list => 0)) ;
360  
361 path($dump_file)->spew_utf8($dump_string);
362  
363 return;
364 }
365  
366 sub plugin {
367 my ($self, $opt, $args) = @_;
368 my @info = $self->load_meta_plugin($opt, $args) ;
369 $self->_edit($opt, $args, @info);
370 return;
371 }
372  
373 sub edit {
374 my ($self, $opt, $args) = @_;
375 my @info = $self->load_meta_root($opt, $args) ;
376 $self->_edit($opt, $args, @info);
377 return;
378 }
379  
380 sub _edit {
381 my ($self, $opt, $args, $rw_obj, $cm_lib_dir, $meta_root, $write_sub) = @_;
382  
383 my $root_model = $rw_obj->root_model;
384 my $mw = MainWindow-> new;
385  
386 $mw->withdraw ;
387 # Thanks to Jerome Quelin for the tip
388 $mw->optionAdd('*BorderWidth' => 1);
389  
390 my $cmu = $mw->ConfigModelEditUI(
391 -instance => $meta_root->instance,
392 -store_sub => $write_sub,
393 -model_name => $root_model,
394 -cm_lib_dir => $cm_lib_dir
395 );
396  
397 my $open_item = $opt->{'open-item'};
398 if ($root_model and not $meta_root->fetch_element('class')->fetch_size) {
399 $open_item ||= qq(class:"$root_model" );
400 }
401 else {
402 $open_item ||= 'class';
403 }
404  
405 my $obj = $meta_root->grab($open_item) ;
406 $cmu->after(10, sub { $cmu->force_element_display($obj) });
407  
408 if (my $taq = $opt->test_and_quit ) {
409 my $bail_out = sub {
410 warn "save failed: $_[0]\n" if @_;
411 $cmu -> quit;
412 } ;
413  
414 $cmu->after( 2000 , sub {
415 if ($taq =~ /s/) {
416 say "Test mode: save and quit";
417 $cmu->save( $bail_out );
418 }
419 else {
420 say "Test mode: quit only";
421 &$bail_out
422 }
423 });
424 }
425 &MainLoop ; # Tk's
426 say "Exited GUI";
427 return;
428 }
429  
430 1;
431  
432 __END__
« blib/lib/Config/Model/Itself/BackendDetector.pm ↑ Summary blib/lib/Config/Model/models/Itself/Application.pl »