| 1 | package CPAN::Maker::Bootstrapper::Role::ResolveVars; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(choose slurp); | ||
| 8 | |||
| 9 | use English qw(-no_match_vars); | ||
| 10 | use Data::Dumper; | ||
| 11 | use Role::Tiny; | ||
| 12 | |||
| 13 | use Readonly; | ||
| 14 | Readonly::Scalar our $PLACEHOLDER => qr/[@]([A-Z0-9_]+)[@]/xsm; | ||
| 15 | |||
| 16 | ######################################################################## | ||
| 17 | sub cmd_resolve_vars { | ||
| 18 | ######################################################################## | ||
| 19 | my ($self) = @_; | ||
| 20 | |||
| 21 | my ($source) = $self->get_args; | ||
| 22 | |||
| 23 | my $file_list = $self->get_file_list; | ||
| 24 | |||
| 25 | die "ERROR: usage: cmb resolve-vars [--vars-file var-file] [--file-list file-list | source-file]\n" | ||
| 26 | if !$source && !$file_list; | ||
| 27 | |||
| 28 | die "ERROR: source-file and --file-list are mutually exclusive\n" | ||
| 29 | if $source && $file_list; | ||
| 30 | |||
| 31 | if ($file_list) { | ||
| 32 | die "ERROR: $file_list not found or not readable\n" | ||
| 33 | if !-f $file_list || !-r $file_list; | ||
| 34 | |||
| 35 | foreach my $file ( split /\n/xsm, slurp($file_list) ) { | ||
| 36 | next if !$file || $file =~ /^[#]/xsm; | ||
| 37 | |||
| 38 | die "ERROR: $file not found or not readable\n" | ||
| 39 | if !-f $file || !-r $file; | ||
| 40 | |||
| 41 | my $output = $file; | ||
| 42 | $output =~ s/[.]in\z/.rendered/xsm; | ||
| 43 | |||
| 44 | die "ERROR: cannot determine rendered output for $file\n" | ||
| 45 | if $output eq $file; | ||
| 46 | |||
| 47 | open my $fh, '>', $output | ||
| 48 | or die "ERROR: cannot write $output: $ERRNO\n"; | ||
| 49 | |||
| 50 | print {$fh} $self->_resolve_source($file); | ||
| 51 | |||
| 52 | close $fh | ||
| 53 | or die "ERROR: cannot close $output: $ERRNO\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | return $SUCCESS; | ||
| 57 | } | ||
| 58 | |||
| 59 | die "ERROR: $source not found or not readable\n" | ||
| 60 | if !-f $source || !-r $source; | ||
| 61 | |||
| 62 | print {*STDOUT} $self->_resolve_source($source); | ||
| 63 | |||
| 64 | return $SUCCESS; | ||
| 65 | } | ||
| 66 | |||
| 67 | ######################################################################## | ||
| 68 | sub _resolve_source { | ||
| 69 | ######################################################################## | ||
| 70 | my ( $self, $source ) = @_; | ||
| 71 | |||
| 72 | local %ENV = %ENV; | ||
| 73 | |||
| 74 | my $vars_file = $self->get_vars_file; | ||
| 75 | |||
| 76 | die "ERROR: $vars_file is not found or unreadable!\n" | ||
| 77 | if $vars_file && ( !-f $vars_file || !-r $vars_file ); | ||
| 78 | |||
| 79 | $vars_file //= "$source.vars"; | ||
| 80 | |||
| 81 | if ( -f $vars_file && -r $vars_file ) { | ||
| 82 | |||
| 83 | foreach my $kv ( split /\n/xsm, slurp($vars_file) ) { | ||
| 84 | next if !$kv || $kv =~ /^[#]/xsm; | ||
| 85 | my ( $k, $v ) = split /[=]/xsm, $kv, 2; | ||
| 86 | $ENV{$k} = $v; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return $self->_resolve_vars( slurp($source) ); | ||
| 91 | } | ||
| 92 | |||
| 93 | ######################################################################## | ||
| 94 | sub _resolve_vars { | ||
| 95 | ######################################################################## | ||
| 96 | my ( $self, $text ) = @_; | ||
| 97 | |||
| 98 | # default strict unless explicitly disabled with --no-strict | ||
| 99 | my $strict = $self->get_strict; | ||
| 100 | $strict //= $TRUE; | ||
| 101 | |||
| 102 | # Scrub a COPY for the missing-value check only. Placeholders that | ||
| 103 | # appear solely in POD or comments are references, not substitutions, | ||
| 104 | # so they must not count toward "no value present". Substitution | ||
| 105 | # below still runs against the original, untouched $text. | ||
| 106 | ( my $code = $text ) =~ s/^=\w+.*?^=cut[^\n]*$//gxsm; # strip POD blocks | ||
| 107 | $code =~ s/[#][^\n]*//gxsm; # strip comments | ||
| 108 | |||
| 109 | my %in_code = map { $_ => 1 } $code =~ /$PLACEHOLDER/gxsm; | ||
| 110 | |||
| 111 | my @missing = sort grep { !( defined $ENV{$_} && length $ENV{$_} ) } | ||
| 112 | keys %in_code; | ||
| 113 | |||
| 114 | if (@missing) { | ||
| 115 | my $msg = sprintf "no value present for:\n\t%s\n", join "\n\t", @missing; | ||
| 116 | |||
| 117 | die "ERROR: $msg" | ||
| 118 | if $strict; | ||
| 119 | |||
| 120 | warn "WARNING: $msg"; | ||
| 121 | } | ||
| 122 | |||
| 123 | # Substitute wherever a value exists; anything without a value -- | ||
| 124 | # including POD/comment references and (in --no-strict) missing code | ||
| 125 | # vars -- is left literal rather than blanked out. | ||
| 126 | $text =~ s/$PLACEHOLDER/ ( defined $ENV{$1} && length $ENV{$1} ) ? $ENV{$1} : "\@$1\@" /gxsme; | ||
| 127 | |||
| 128 | return $text; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; |