| 1 | |||
| 2 | use utf8; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings qw(uninitialized); | ||
| 6 | |||
| 7 | #=============================================== | ||
| 8 | package XML::Pastor::Pastorize; | ||
| 9 | |||
| 10 | use XML::Pastor; | ||
| 11 | |||
| 12 | use File::chdir; | ||
| 13 | use File::Path; | ||
| 14 | use File::Spec; | ||
| 15 | use Getopt::Long; | ||
| 16 | use Pod::Usage; | ||
| 17 | |||
| 18 | use vars qw($VERSION); | ||
| 19 | $VERSION = '1.0.1'; | ||
| 20 | |||
| 21 | #------------------------------------------------------------- | ||
| 22 | # METHOD | ||
| 23 | #------------------------------------------------------------- | ||
| 24 | sub new { | ||
| 25 | my $proto = shift; | ||
| 26 | my $class = ref($proto) || $proto; | ||
| 27 | my $self = {@_}; | ||
| 28 | return bless $self, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | #------------------------------------------------------------ | ||
| 32 | # run(@opts) : Class method | ||
| 33 | #------------------------------------------------------------ | ||
| 34 | sub run ($;@) { | ||
| 35 | my $class = shift; | ||
| 36 | my $opts = {@_}; | ||
| 37 | |||
| 38 | $opts->{class_prefix} ||= 'MyApp::Data::'; # should override | ||
| 39 | $opts->{destination} ||= '/tmp/lib/perl/'; # should override | ||
| 40 | $opts->{mode} ||= 'eval'; | ||
| 41 | $opts->{module} ||= $opts->{class_prefix}; | ||
| 42 | $opts->{style} ||= 'single'; | ||
| 43 | $opts->{schema} ||= []; # Will get it from @ARGV (multiple OK) | ||
| 44 | $opts->{verbose} ||= 0; | ||
| 45 | |||
| 46 | GetOptions($opts, | ||
| 47 | 'class_prefix|prefix|p:s', | ||
| 48 | 'complex_isa|complex|c:s', | ||
| 49 | 'debug|g' => sub {$opts->{verbose}=9, $opts->{debug}=1}, | ||
| 50 | 'destination|dest|d:s', | ||
| 51 | 'help|h+', | ||
| 52 | 'man+', | ||
| 53 | 'mode|m:s', | ||
| 54 | 'module|u:s', | ||
| 55 | 'quiet|q' => sub {$opts->{verbose}=0}, | ||
| 56 | 'simple_isa|simple|i:s', | ||
| 57 | 'style|s:s', | ||
| 58 | 'verbose|v:2', | ||
| 59 | ); | ||
| 60 | |||
| 61 | push @{$opts->{schema}}, @ARGV; | ||
| 62 | |||
| 63 | $class->validate_opts($opts) or die "$0: Invalid syntax!\n"; | ||
| 64 | |||
| 65 | return $class->_run(%$opts); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | #------------------------------------------------------------- | ||
| 70 | # _run : private class method | ||
| 71 | #------------------------------------------------------------- | ||
| 72 | sub _run { | ||
| 73 | my $class = shift; | ||
| 74 | my $opts = {@_}; | ||
| 75 | my $print_result = 0; | ||
| 76 | |||
| 77 | if (lc($opts->{mode}) eq 'print') { | ||
| 78 | $print_result = 1; | ||
| 79 | $opts->{mode} = 'return'; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $pastor = XML::Pastor->new(); | ||
| 83 | |||
| 84 | my $result = $pastor->generate(%$opts); | ||
| 85 | |||
| 86 | if (lc($opts->{mode}) eq 'eval') { | ||
| 87 | print "Pastorize: Eval OK.\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($print_result) { | ||
| 91 | print "\n" . $result . "\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | #------------------------------------------------------------- | ||
| 97 | sub validate_opts($) { | ||
| 98 | my $class = shift; | ||
| 99 | my $opts = shift; | ||
| 100 | |||
| 101 | # Help wanted. Print SYNOPSIS and OPTIONS. | ||
| 102 | if ($opts->{help}) { | ||
| 103 | pod2usage( -message => "Help message\n", | ||
| 104 | -exitval => 0, | ||
| 105 | -verbose => 1, | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | # Man page wanted. Print the entire manual | ||
| 110 | if ($opts->{man}) { | ||
| 111 | pod2usage( -message => "MANUAL PAGE\n", | ||
| 112 | -exitval => 0, | ||
| 113 | -verbose => 2, | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | # Mode undefined. | ||
| 118 | unless ($opts->{mode}) { | ||
| 119 | pod2usage( -message => "Syntax error. Mode required!\n", | ||
| 120 | -exitval => 1, | ||
| 121 | -verbose => 2, | ||
| 122 | ); | ||
| 123 | } | ||
| 124 | |||
| 125 | # convert to lower case | ||
| 126 | my $mode = $opts->{mode} = lc($opts->{mode}); | ||
| 127 | my $style = $opts->{style} = lc($opts->{style}); | ||
| 128 | my $schema = $opts->{schema}; | ||
| 129 | |||
| 130 | if ($mode eq 'offline') { | ||
| 131 | # Destination undefined while we need to generate code. | ||
| 132 | unless ($opts->{destination}) { | ||
| 133 | pod2usage( -message => "Syntax error. 'destination' required when 'mode' is offline!\n", | ||
| 134 | -exitval => 1, | ||
| 135 | -verbose => 2, | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | |||
| 139 | # Module undefined while mode is generate and style is single. | ||
| 140 | if ($style eq 'single') { | ||
| 141 | unless ($opts->{module}) { | ||
| 142 | pod2usage( -message => "Syntax error. 'module' required when 'mode' is offline and 'style' is single!\n", | ||
| 143 | -exitval => 1, | ||
| 144 | -verbose => 2, | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | # We need at least one schema to work on. | ||
| 152 | unless (scalar($schema)) { | ||
| 153 | pod2usage( -message => "Syntax error. At least one schema required as argument!\n", | ||
| 154 | -exitval => 1, | ||
| 155 | -verbose => 2, | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | # Everything is OK. Indicate success to the caller. | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | ##############################################################################" | ||
| 164 | # Utility functions | ||
| 165 | ##############################################################################" | ||
| 166 | |||
| 167 | #------------------------------------------------------------- | ||
| 168 | sub _run_cmd { | ||
| 169 | my ($opts, $cmd) = @_; | ||
| 170 | my $verbose = $opts->{verbose} || $opts->{debug}; | ||
| 171 | my $dryrun = $opts->{dryrun}; | ||
| 172 | my $prompt = $dryrun ? 'sys would > ' : 'sys cmd > '; | ||
| 173 | return unless $cmd; | ||
| 174 | |||
| 175 | print $prompt . $cmd . "\n" if $verbose; | ||
| 176 | |||
| 177 | my $output; | ||
| 178 | unless ($dryrun) { $output = `$cmd`; } | ||
| 179 | else { $output = $dryrun ? 'Command NOT executed (dry-run mode)' : eval($cmd); } | ||
| 180 | |||
| 181 | print "$output\n" if ($output && $verbose > 5); | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | #------------------------------------------------------------- | ||
| 186 | sub _run_perl { | ||
| 187 | my ($opts, $cmd) = @_; | ||
| 188 | my $verbose = $opts->{verbose} || $opts->{debug}; | ||
| 189 | my $dryrun = $opts->{dryrun}; | ||
| 190 | my $prompt = $dryrun ? 'perl would > ' : 'perl cmd > '; | ||
| 191 | |||
| 192 | return unless $cmd; | ||
| 193 | |||
| 194 | print $prompt . $cmd . "\n" if $verbose; | ||
| 195 | |||
| 196 | my $output; | ||
| 197 | unless ($dryrun) { $output = eval($cmd); } | ||
| 198 | else { $output = $dryrun ? 'Command NOT executed (dry-run mode)' : eval($cmd); } | ||
| 199 | |||
| 200 | print "$output\n" if ($output && $verbose > 5); | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | |||
| 208 | __END__ |