| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # PODNAME: wtscript2json | ||
| 7 | # ABSTRACT: convert read configuration into JSON | ||
| 8 | |||
| 9 | use v5.10.1; | ||
| 10 | |||
| 11 | use File::Slurp qw(write_file); | ||
| 12 | use Getopt::Long; | ||
| 13 | use JSON (); | ||
| 14 | use List::MoreUtils qw(zip); | ||
| 15 | use Params::Util qw(_ARRAY); | ||
| 16 | use Pod::Usage; | ||
| 17 | |||
| 18 | use WWW::Mechanize::Script::Util qw(:ALL); | ||
| 19 | use WWW::Mechanize::Script; | ||
| 20 | |||
| 21 | our $VERSION = '0.100'; | ||
| 22 | my %opts = ( | ||
| 23 | "input-files" => [], | ||
| 24 | "output-files" => [], | ||
| 25 | "output-pattern" => [] | ||
| 26 | ); | ||
| 27 | my @options = ( | ||
| 28 | "input-files=s@" => $opts{"input-files"}, | ||
| 29 | "output-files=s@" => $opts{"output-files"}, | ||
| 30 | "output-pattern=s{2}" => $opts{"output-pattern"}, | ||
| 31 | "help|h", "usage|?" | ||
| 32 | ); | ||
| 33 | |||
| 34 | GetOptions( \%opts, @options ) or pod2usage(2); | ||
| 35 | |||
| 36 | # clean-up defaults | ||
| 37 | @{ $opts{"input-files"} } or delete $opts{"input-files"}; | ||
| 38 | @{ $opts{"output-files"} } or delete $opts{"output-files"}; | ||
| 39 | @{ $opts{"output-pattern"} } or delete $opts{"output-pattern"}; | ||
| 40 | |||
| 41 | # check ... | ||
| 42 | defined( $opts{help} ) | ||
| 43 | and $opts{help} | ||
| 44 | and pod2usage( | ||
| 45 | { | ||
| 46 | -verbose => 2, | ||
| 47 | -exitval => 0 | ||
| 48 | } | ||
| 49 | ); | ||
| 50 | defined( $opts{usage} ) and $opts{usage} and pod2usage(1); | ||
| 51 | opt_required_all( \%opts, qw(input-files) ); | ||
| 52 | opt_exclusive( \%opts, qw(output-files output-pattern) ); | ||
| 53 | opt_required_one( \%opts, qw(output-files output-pattern) ); | ||
| 54 | |||
| 55 | _ARRAY( $opts{"input-files"} ) | ||
| 56 | and $opts{"input-files"} = [ split( ",", join( ",", @{ $opts{"input-files"} } ) ) ]; | ||
| 57 | _ARRAY( $opts{"output-files"} ) | ||
| 58 | and $opts{"output-files"} = [ split( ",", join( ",", @{ $opts{"output-files"} } ) ) ]; | ||
| 59 | |||
| 60 | _ARRAY( $opts{"input-files"} ) | ||
| 61 | and _ARRAY( $opts{"output-files"} ) | ||
| 62 | and scalar( @{ $opts{"input-files"} } ) != scalar( @{ $opts{"output-files"} } ) | ||
| 63 | and pod2usage( | ||
| 64 | { | ||
| 65 | -message => "Count of --input-files and --output-files doesn't match", | ||
| 66 | -exitval => 1 | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | |||
| 70 | my %in2out = | ||
| 71 | _ARRAY( $opts{"output-files"} ) | ||
| 72 | ? zip( @{ $opts{"input-files"} }, @{ $opts{"output-files"} } ) | ||
| 73 | : (); | ||
| 74 | my %cfg = load_config(); | ||
| 75 | |||
| 76 | my $coder = JSON->new(); | ||
| 77 | _ARRAY( $cfg{wtscript_extensions} ) | ||
| 78 | and Config::Any::WTScript->extensions( @{ $cfg{wtscript_extensions} } ); | ||
| 79 | foreach my $filename ( @{ $opts{"input-files"} } ) | ||
| 80 | { | ||
| 81 | my @script_files = find_scripts( \%cfg, $filename ); | ||
| 82 | my $scripts = Config::Any->load_files( | ||
| 83 | { | ||
| 84 | files => [@script_files], | ||
| 85 | use_ext => 1, | ||
| 86 | flatten_to_hash => 1, | ||
| 87 | } | ||
| 88 | ); | ||
| 89 | if ( $opts{"output-files"} ) | ||
| 90 | { | ||
| 91 | @script_files = keys %{$scripts}; | ||
| 92 | scalar(@script_files) > 1 | ||
| 93 | and pod2usage( | ||
| 94 | { | ||
| 95 | -message => "filename $filename is ambigious: " . join( ", ", @script_files ), | ||
| 96 | -exitval => 1 | ||
| 97 | } | ||
| 98 | ); | ||
| 99 | scalar(@script_files) < 1 | ||
| 100 | and next; # file not found or not parsable ... | ||
| 101 | # merge into default and previous loaded config ... | ||
| 102 | my $json = $coder->pretty->encode( $scripts->{ $script_files[0] } ); | ||
| 103 | write_file( $in2out{$filename}, $json ); | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | while ( my ( $script_file, $script ) = each(%$scripts) ) | ||
| 108 | { | ||
| 109 | my $json = $coder->pretty->encode($script); | ||
| 110 | ( my $target = $script_file ) =~ | ||
| 111 | s/$opts{"output-pattern"}->[0]/$opts{"output-pattern"}->[1]/; | ||
| 112 | write_file( $target, $json ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | __END__ |