| 1 | package FindApp::Behavior::Exporter; | ||
| 2 | |||
| 3 | ################################################################ | ||
| 4 | # Class importer and its helpers. | ||
| 5 | ################################################################ | ||
| 6 | |||
| 7 | use v5.10; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use mro "c3"; | ||
| 11 | |||
| 12 | use parent qw(Exporter); | ||
| 13 | |||
| 14 | use FindApp::Vars qw(:all); | ||
| 15 | use FindApp::Utils qw(:all); | ||
| 16 | |||
| 17 | use namespace::clean; | ||
| 18 | |||
| 19 | use pluskeys qw{ | ||
| 20 | ORIGINAL_EXPORTS | ||
| 21 | }; | ||
| 22 | |||
| 23 | sub init { &ENTER_TRACE; | ||
| 24 | my($self) = @_; | ||
| 25 | $self->maybe::next::method; | ||
| 26 | $self->original_exports([]); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub import { &ENTER_TRACE; | ||
| 30 | $_[0]->original_exports(@_); | ||
| 31 | my $caller = caller; | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | feature->import(":5.10"); | ||
| 35 | |||
| 36 | # Can't operate on @_ itself because | ||
| 37 | # its values are readonly in an import! | ||
| 38 | my @ARGS = @_; | ||
| 39 | my $adder = function import_adder => sub { | ||
| 40 | my($which, $dir) = @_; | ||
| 41 | my $file = shift(@ARGS) || botch_import_missing(); | ||
| 42 | $self->group($dir)->$which->add($file); | ||
| 43 | }; | ||
| 44 | |||
| 45 | my $add_dir = join "|", alldir_map { quotemeta lc }; | ||
| 46 | my $let_dir = join "|", subdir_map { quotemeta uc }; | ||
| 47 | my $finder = "findapp_and_export"; | ||
| 48 | |||
| 49 | my(@pre_args, @post_args, @subclasses); | ||
| 50 | |||
| 51 | SWITCH: | ||
| 52 | while (@ARGS && $ARGS[0] =~ s/^--?//) { | ||
| 53 | for (shift @ARGS) { | ||
| 54 | /^($add_dir)$/ && case { $adder->(wanted => $1) }; | ||
| 55 | /^($let_dir)$/ && case { $adder->(allowed => $1) }; | ||
| 56 | /^ debug $/x && case { $self->debugging(1) }; | ||
| 57 | /^ here $/x && case { $self->default_origin("cwd") }; | ||
| 58 | /^ nofind $/x && case { $finder = "" }; | ||
| 59 | /^ noload $/x && case { $finder &&= "findapp" }; | ||
| 60 | /^ trace (\d+)? $/x && case { $self->tracing($1 || 1) }; | ||
| 61 | /^ vars $/x && case { push @pre_args, ":vars" }; | ||
| 62 | # This works, but it's more trouble to maintain than it's worth. It's | ||
| 63 | # for dynamic role loading. I may bring it back if I come up with more | ||
| 64 | # roles that it makes sense to leave optional. | ||
| 65 | # /^ subclass $/x && case { push @pre_args, ":subclass" ; | ||
| 66 | # FindApp::Vars->import(":all"); | ||
| 67 | # $self->adopts_children($caller); | ||
| 68 | # }; | ||
| 69 | # | ||
| 70 | # # convert the shortcuts into normal classes to load | ||
| 71 | # s/^($classic)$/my::\u$1/; | ||
| 72 | # s/^my// && case { | ||
| 73 | # my $mod = ((length) ? $_ : shift @ARGS) | ||
| 74 | # || botch_import_missing("my"); | ||
| 75 | # push @subclasses, $mod; | ||
| 76 | # }; | ||
| 77 | ####################################################################### | ||
| 78 | botch_import_unknown(); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # Classes and files aren't "real" imports: | ||
| 83 | @ARGS = filter_implicits($self, @ARGS); | ||
| 84 | |||
| 85 | { # Don't use self here because subclasses don't have "the right stuff". | ||
| 86 | my @import_args = (@pre_args, @ARGS, @post_args); | ||
| 87 | __PACKAGE__->export_to_level(1, $caller, @import_args); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Load our parents and become a (potentially new) subclass of them. | ||
| 91 | $self->adopts_parents(@subclasses) if @subclasses; | ||
| 92 | |||
| 93 | # Don't run the finder if we're just subclassing: that's their job, | ||
| 94 | # either explicitly or else implicitly when this import method is | ||
| 95 | # in turn run on them. | ||
| 96 | if ($finder && !$caller->isa(__PACKAGE__)) { | ||
| 97 | $self->$finder; | ||
| 98 | } | ||
| 99 | |||
| 100 | } ## sub import | ||
| 101 | |||
| 102 | sub original_exports { &ENTER_TRACE; | ||
| 103 | my $self = &myself; | ||
| 104 | if (@_) { | ||
| 105 | if (@_ == 1 && grep { ref && reftype eq "ARRAY" } @_) { | ||
| 106 | $$self{+ORIGINAL_EXPORTS} = shift; | ||
| 107 | } else { | ||
| 108 | @{ $$self{+ORIGINAL_EXPORTS} } = @_; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return @{ $$self{+ORIGINAL_EXPORTS} }; | ||
| 112 | } | ||
| 113 | |||
| 114 | no namespace::clean; | ||
| 115 | |||
| 116 | # Look for things that are either modules with colons | ||
| 117 | # to add to libs wanted or paths with slashes to add to | ||
| 118 | # root wanted. | ||
| 119 | sub filter_implicits { | ||
| 120 | my($self, @input) = @_; | ||
| 121 | my @output; | ||
| 122 | SWITCH: | ||
| 123 | for (@input) { | ||
| 124 | m| ^ !? : |x && case { push @output, $_ }; # import :tag | ||
| 125 | m| ^ !? / [^/]+ / $ |x && case { push @output, $_ }; # import /pat/ | ||
| 126 | m| ^ / |x && case { $self->origin($_) }; | ||
| 127 | m| :: |x && case { $self->add_libdirs_wanted($_) }; | ||
| 128 | m| [/.-] |x && case { $self->add_rootdir_wanted($_) }; | ||
| 129 | push @output, $_; | ||
| 130 | } | ||
| 131 | return @output; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub botch_import_pragma($); | ||
| 135 | |||
| 136 | sub botch_import_missing(_) { | ||
| 137 | my($pragma) = @_; | ||
| 138 | botch_import_pragma("expected argument to '-$pragma' import pragma"); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub botch_import_unknown(_) { | ||
| 142 | my($pragma) = @_; | ||
| 143 | botch_import_pragma("import pragma '-$pragma' is unrecognized"); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub botch_import_pragma($) { | ||
| 147 | my($error) = @_; | ||
| 148 | die __PACKAGE__ . " $error; " . q{valid import pragmas are: | ||
| 149 | |||
| 150 | -root wanted_file_or_dir_in_root | ||
| 151 | |||
| 152 | -lib wanted_dir_in_libs | ||
| 153 | -bin wanted_dir_in_bins | ||
| 154 | -man wanted_dir_in_mans | ||
| 155 | |||
| 156 | -LIB allowed_dir_in_libs | ||
| 157 | -BIN allowed_dir_in_bins | ||
| 158 | -MAN allowed_dir_in_mans | ||
| 159 | |||
| 160 | -vars (import $Root, @Lib, $Lib, @Bin, $Bin, @Man, $Man) | ||
| 161 | -here (to default to cwd not script dir) | ||
| 162 | |||
| 163 | -subclass (use this to make your package also a subclass of this class) | ||
| 164 | |||
| 165 | -git (like -my::Git, so like -my FindApp::Git) | ||
| 166 | -devperl (like -my::Devperl, so like -my FindApp::Devperl) | ||
| 167 | |||
| 168 | -my name_of_subclass (like -my Other::Class, but -my::Git means -my FindApp::Git) | ||
| 169 | |||
| 170 | -nofind (don't try to find the approot yet) | ||
| 171 | -noload (find the approot but don't load @INC or environment variables) | ||
| 172 | |||
| 173 | -debug (enable debugging but not tracing, or by setting $FindApp::Debugging to true) | ||
| 174 | |||
| 175 | -trace (enable tracing, or by setting $FindApp::Tracing to 1, 2, or 3) | ||
| 176 | -trace1 (same as -trace) | ||
| 177 | -trace2 (enable more tracing, or by setting $FindApp::Tracing to 2) | ||
| 178 | -trace3 (enable still more tracing, or by setting $FindApp::Tracing to 3) | ||
| 179 | |||
| 180 | }; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub exported_envars() { | ||
| 184 | state $vars = [ | ||
| 185 | qw($Root), | ||
| 186 | subdir_map { map { ('$'.$_, '@'.$_) } ucfirst }, | ||
| 187 | ]; | ||
| 188 | return @$vars; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub exporter_vars() { | ||
| 192 | return qw( | ||
| 193 | @CARP_NOT | ||
| 194 | @EXPORT_OK | ||
| 195 | %EXPORT_TAGS | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub exported_subs { | ||
| 200 | @{$FindApp::Utils::EXPORT_TAGS{all}}; | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | use namespace::clean; | ||
| 205 | |||
| 206 | our %EXPORT_TAGS; | ||
| 207 | |||
| 208 | $EXPORT_TAGS{vars} = [ exported_envars() ]; | ||
| 209 | $EXPORT_TAGS{subclass_subs} = [ exported_subs() ]; | ||
| 210 | $EXPORT_TAGS{subclass_vars} = [ exported_envars(), exporter_vars() ], | ||
| 211 | |||
| 212 | $EXPORT_TAGS{subclass} = [ | ||
| 213 | (map { @$_ } @EXPORT_TAGS{ <subclass_{sub,var}s> }), | ||
| 214 | ]; | ||
| 215 | |||
| 216 | our @EXPORT_OK = (import => sort map { @$_ } values %EXPORT_TAGS); | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | =encoding utf8 | ||
| 221 | |||
| 222 | =head1 NAME | ||
| 223 | |||
| 224 | FindApp::Exporter - FIXME | ||
| 225 | |||
| 226 | =head1 SYNOPSIS | ||
| 227 | |||
| 228 | use FindApp::Exporter; | ||
| 229 | |||
| 230 | =head1 DESCRIPTION | ||
| 231 | |||
| 232 | =head2 Pluskey Attributes | ||
| 233 | |||
| 234 | These are always private; use the methods insteads. | ||
| 235 | |||
| 236 | =over | ||
| 237 | |||
| 238 | =item ORIGINAL_EXPORTS | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =head2 Public Methods | ||
| 243 | |||
| 244 | =over | ||
| 245 | |||
| 246 | =item botch_import_missing | ||
| 247 | |||
| 248 | =item botch_import_pragma | ||
| 249 | |||
| 250 | =item botch_import_unknown | ||
| 251 | |||
| 252 | =item exported_envars | ||
| 253 | |||
| 254 | =item exporter_vars | ||
| 255 | |||
| 256 | =item filter_implicits | ||
| 257 | |||
| 258 | =item import | ||
| 259 | |||
| 260 | =item import_adder | ||
| 261 | |||
| 262 | =item original_exports | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =item FIXME | ||
| 269 | |||
| 270 | =back | ||
| 271 | |||
| 272 | =head1 EXAMPLES | ||
| 273 | |||
| 274 | =head1 ENVIRONMENT | ||
| 275 | |||
| 276 | =head1 SEE ALSO | ||
| 277 | |||
| 278 | =over | ||
| 279 | |||
| 280 | =item L<FindApp> | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =head1 CAVEATS AND PROVISOS | ||
| 285 | |||
| 286 | =head1 BUGS AND LIMITATIONS | ||
| 287 | |||
| 288 | =head1 HISTORY | ||
| 289 | |||
| 290 | =head1 AUTHOR | ||
| 291 | |||
| 292 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 293 | |||
| 294 | =head1 LICENCE AND COPYRIGHT | ||
| 295 | |||
| 296 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 297 | All Rights Reserved. | ||
| 298 | |||
| 299 | This module is free software; you can redistribute it and/or | ||
| 300 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 301 | |||
| 302 | =cut | ||
| 303 |