| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Project::Easy::Helper; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 10 | use Class::Easy; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1180 | use Project::Easy::Config::File; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 23 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | sub config { | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 0 |  |  | 0 | 0 |  | my @params = @ARGV; | 
| 10 | 0 | 0 |  |  |  |  | @params = @_ | 
| 11 |  |  |  |  |  |  | if scalar @_; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 0 |  |  |  |  |  | my ($package, $libs) = &_script_wrapper(); # Project name and "libs" path | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 0 |  |  |  |  |  | my $core   = $::project;  # Project singleton | 
| 16 | 0 |  |  |  |  |  | my $config = $core->config; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 0 |  |  |  |  |  | my $templates = file->__data__files ();   # Got all "data files" at end of file | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # Local config absolute path | 
| 21 | 0 |  |  |  |  |  | my $config_file  = $core->fixup_path_instance ($core->instance); | 
| 22 | 0 |  |  |  |  |  | my @config_serializer = (); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # first, we need to detect scope of config | 
| 25 | 0 | 0 |  |  |  |  | if ($params[0] =~ /--(project|global)/) { | 
|  |  | 0 |  |  |  |  |  | 
| 26 | 0 |  |  |  |  |  | shift @params; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Global config absolute path | 
| 29 | 0 |  |  |  |  |  | $config_file = $core->conf_path; | 
| 30 | 0 |  |  |  |  |  | push @config_serializer, 'undef_keys_in_patch'; | 
| 31 |  |  |  |  |  |  | } elsif ($params[0] =~ /-{1,2}f(?:ile)?/) { | 
| 32 | 0 |  |  |  |  |  | shift @params; | 
| 33 | 0 |  |  |  |  |  | $config_file = Project::Easy::Config::File->new (shift @params); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # try to load config | 
| 36 | 0 |  |  |  |  |  | local $@; | 
| 37 | 0 |  |  |  |  |  | $config = eval {$config_file->deserialize;}; | 
|  | 0 |  |  |  |  |  |  | 
| 38 | 0 | 0 |  |  |  |  | die "config file ($config_file) access error: $!" | 
| 39 |  |  |  |  |  |  | if $!; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 0 | 0 |  |  |  |  | die "config file ($config_file) cannot be deserialized" | 
| 42 |  |  |  |  |  |  | if $@; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | my ($key, $command, @remains) = @params; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 | 0 |  |  |  |  | unless (defined $key) { | 
| 48 | 0 |  |  |  |  |  | print "requested config file name is: ", $config_file, "\n"; | 
| 49 | 0 |  |  |  |  |  | print $templates->{'config-usage'}, "\n"; | 
| 50 | 0 |  |  |  |  |  | return; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 | 0 |  |  |  |  | if ($key eq '-e') { | 
| 54 | 0 |  |  |  |  |  | system ($ENV{EDITOR}, $config_file); | 
| 55 | 0 |  |  |  |  |  | return; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | #  $key      = "key1.key2.key3..." | 
| 59 |  |  |  |  |  |  | #  $key_eval = "{key1}->{key2}->{key3}..." | 
| 60 | 0 |  |  |  |  |  | my $key_path = "{" . join ('}->{', split (/\./, $key)) . '}'; | 
| 61 | 0 |  |  |  |  |  | my $key_eval = "\$config->$key_path"; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | my $struct     = eval $key_eval; | 
| 64 | 0 |  |  |  |  |  | my $ref_struct = ref $struct; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 | 0 | 0 |  |  |  | if (!defined $command or ($command eq 'dump' and !$ref_struct)) { | 
|  |  |  | 0 |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | print "'$key' => "; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  |  | if ($ref_struct eq 'HASH') { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | print "HASH with keys: ", join ', ', keys %$struct; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | } elsif ($ref_struct eq 'ARRAY') { | 
| 73 | 0 |  |  |  |  |  | print "ARRAY of ", scalar @$struct; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | } elsif (!$ref_struct) { | 
| 76 | 0 | 0 |  |  |  |  | print "'", (defined $struct ? $struct : 'null'), "'"; | 
| 77 |  |  |  |  |  |  | } | 
| 78 | 0 |  |  |  |  |  | print "\n"; | 
| 79 | 0 |  |  |  |  |  | return 1; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | my $conf_package = $package->conf_package;		# Project::Easy::Config | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # Init serializer to parse config file | 
| 85 | 0 |  |  |  |  |  | my $serializer_json = $conf_package->serializer ('json'); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 0 | 0 |  |  |  |  | if ($command =~ /^(?:--)?dump$/) { | 
| 88 | 0 |  |  |  |  |  | print "\"$key\" => "; | 
| 89 | 0 |  |  |  |  |  | print $serializer_json->dump_struct ($struct); | 
| 90 | 0 |  |  |  |  |  | print "\n"; | 
| 91 | 0 |  |  |  |  |  | return 1; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # set or = can: create new key (any depth), modify existing | 
| 95 |  |  |  |  |  |  | # template can: create new key (any depth) | 
| 96 | 0 | 0 | 0 |  |  |  | if ($command eq 'set' or $command eq '=' or $command eq 'template') { | 
|  |  |  | 0 |  |  |  |  | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 | 0 |  |  |  |  | die "you must supply value for modify config" | 
| 99 |  |  |  |  |  |  | unless scalar @remains; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # check for legitimity | 
| 102 | 0 | 0 |  |  |  |  | die "you cannot set/template complex value such as HASH/ARRAY. remove old key first" | 
| 103 |  |  |  |  |  |  | if $ref_struct; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 0 | 0 | 0 |  |  |  | die "you cannot update scalar value with template. remove old key first" | 
| 106 |  |  |  |  |  |  | if $command eq 'template' and defined $struct; # any setter | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | # patch creation for config files | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 |  |  |  |  |  | my $fixup_struct = {}; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 | 0 |  |  |  |  | if ($command eq 'template') { | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | my $template = $serializer_json->parse_string ( | 
| 115 |  |  |  |  |  |  | $templates->{'template-' . $remains[0]} | 
| 116 |  |  |  |  |  |  | ); | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | eval "\$fixup_struct->$key_path = \$template"; | 
| 119 |  |  |  |  |  |  | } else { | 
| 120 | 0 |  |  |  |  |  | eval "\$fixup_struct->$key_path = \$remains[0]"; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | # storing modified config | 
| 125 | 0 |  |  |  |  |  | $config_file->patch ($fixup_struct, @config_serializer); | 
| 126 |  |  |  |  |  |  | #warn(Dumper($config_file->contents)); | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 0 |  |  |  |  |  | return 1; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | print $templates->{'config-usage'}, "\n"; | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  |  |  |  | return; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | 1; | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | __DATA__ |