Debian Codegraph @

lib/App/Yath/Options/PreCommand.pm

raw
1 package App::Yath::Options::PreCommand;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use App::Yath::Util qw/find_pfile/;
8 use Test2::Harness::Util qw/mod2file clean_path/;
9
10 use App::Yath::Options;
11
12 option_group {prefix => 'harness', pre_command => 1} => sub {
13 option plugins => (
14 type => 'm',
15 short => 'p',
16 alt => ['plugin'],
17
18 category => 'Plugins',
19 long_examples => [' PLUGIN', ' +App::Yath::Plugin::PLUGIN', ' PLUGIN=arg1,arg2,...'],
20 short_examples => ['PLUGIN'],
21 description => 'Load a yath plugin.',
22
23 action => \&plugin_action,
24 );
25
26 option no_scan_plugins => (
27 type => 'b',
28
29 category => 'Plugins',
30 description => 'Normally yath scans for and loads all App::Yath::Plugin::* modules in order to bring in command-line options they may provide. This flag will disable that. This is useful if you have a naughty plugin that is loading other modules when it should not.',
31 );
32
33 option project => (
34 type => 's',
35 alt => ['project-name'],
36 category => 'Environment',
37 description => 'This lets you provide a label for your current project/codebase. This is best used in a .yath.rc file. This is necessary for a persistent runner.',
38 );
39
40 option persist_dir => (
41 type => 's',
42 category => 'Environment',
43 description => 'Where to find persistence files.',
44 normalize => \&clean_path,
45 );
46
47 option persist_file => (
48 type => 's',
49 category => 'Environment',
50 alt => ['pfile'],
51 normalize => \&clean_path,
52 description => "Where to find the persistence file. The default is /{system-tempdir}/project-yath-persist.json. If no project is specified then it will fall back to the current directory. If the current directory is not writable it will default to /tmp/yath-persist.json which limits you to one persistent runner on your system.",
53 );
54
55 option dev_libs => (
56 type => 'D',
57 short => 'D',
58 name => 'dev-lib',
59
60 category => 'Developer',
61 description => 'Add paths to @INC before loading ANYTHING. This is what you use if you are developing yath or yath plugins to make sure the yath script finds the local code instead of the installed versions of the same code. You can provide an argument (-Dfoo) to provide a custom path, or you can just use -D without and arg to add lib, blib/lib and blib/arch.',
62
63 long_examples => ['', '=lib'],
64 short_examples => ['', '=lib', 'lib'],
65
66 normalize => \&normalize_dev_libs,
67 action => \&dev_libs_action,
68 );
69
70 post \&post_process;
71 };
72
73 sub plugin_action {
74 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler, $options) = @_;
75
76 my ($class, $args) = split /=/, $norm, 2;
77 $args = [split ',', $args] if $args;
78
79 $class = "App::Yath::Plugin::$class"
80 unless $class =~ s/^\+//;
81
82 return if grep { $class eq (ref($_) || $_) } @{$settings->harness->plugins};
83
84 my $file = mod2file($class);
85 require $file;
86
87 $options->include_from($class) if $class->can('options');
88
89 my $plugin = $class->can('new') ? $class->new(@{$args // []}) : $class;
90
91 $handler->($slot, $plugin);
92 }
93
94 sub normalize_dev_libs {
95 my $val = shift;
96
97 return $val if $val eq '1';
98
99 return clean_path($val);
100 }
101
102 sub dev_libs_action {
103 my ($prefix, $field, $raw, $norm, $slot, $settings) = @_;
104
105 my %seen = map { $_ => 1 } @{$$slot};
106
107 my @new = grep { !$seen{$_}++ } ($norm eq '1') ? (map { clean_path($_) } 'lib', 'blib/lib', 'blib/arch') : ($norm);
108
109 return unless @new;
110
111 warn <<" EOT" for @new;
112 dev-lib '$_' added to \@INC late, it is possible some yath libraries were already loaded from other paths.
113 (Maybe you need to move the -D or --dev-lib argument(s) to be earlier in your command line or config file?)
114 EOT
115
116 unshift @INC => @new;
117 unshift @{$$slot} => @new;
118 }
119
120 sub post_process {
121 my %params = @_;
122 my $settings = $params{settings};
123
124 $settings->harness->field(persist_file => find_pfile($settings, vivify => 1, no_checks => 1))
125 unless defined $settings->harness->persist_file;
126 }
127
128 1;
129
130 __END__
131
132
133 =pod
134
135 =encoding UTF-8
136
137 =head1 NAME
138
139 App::Yath::Options::PreCommand - Options for yath before command is specified.
140
141 =head1 DESCRIPTION
142
143 This is where many pre-command options are defined.
144
145 =head1 PROVIDED OPTIONS
146
147 =head2 YATH OPTIONS (PRE-COMMAND)
148
149 =head3 Developer
150
151 =over 4
152
153 =item --dev-lib
154
155 =item --dev-lib=lib
156
157 =item -D
158
159 =item -D=lib
160
161 =item -Dlib
162
163 =item --no-dev-lib
164
165 Add paths to @INC before loading ANYTHING. This is what you use if you are developing yath or yath plugins to make sure the yath script finds the local code instead of the installed versions of the same code. You can provide an argument (-Dfoo) to provide a custom path, or you can just use -D without and arg to add lib, blib/lib and blib/arch.
166
167 Can be specified multiple times
168
169
170 =back
171
172 =head3 Environment
173
174 =over 4
175
176 =item --persist-dir ARG
177
178 =item --persist-dir=ARG
179
180 =item --no-persist-dir
181
182 Where to find persistence files.
183
184
185 =item --persist-file ARG
186
187 =item --persist-file=ARG
188
189 =item --pfile ARG
190
191 =item --pfile=ARG
192
193 =item --no-persist-file
194
195 Where to find the persistence file. The default is /{system-tempdir}/project-yath-persist.json. If no project is specified then it will fall back to the current directory. If the current directory is not writable it will default to /tmp/yath-persist.json which limits you to one persistent runner on your system.
196
197
198 =item --project ARG
199
200 =item --project=ARG
201
202 =item --project-name ARG
203
204 =item --project-name=ARG
205
206 =item --no-project
207
208 This lets you provide a label for your current project/codebase. This is best used in a .yath.rc file. This is necessary for a persistent runner.
209
210
211 =back
212
213 =head3 Plugins
214
215 =over 4
216
217 =item --no-scan-plugins
218
219 =item --no-no-scan-plugins
220
221 Normally yath scans for and loads all App::Yath::Plugin::* modules in order to bring in command-line options they may provide. This flag will disable that. This is useful if you have a naughty plugin that is loading other modules when it should not.
222
223
224 =item --plugins PLUGIN
225
226 =item --plugins +App::Yath::Plugin::PLUGIN
227
228 =item --plugins PLUGIN=arg1,arg2,...
229
230 =item --plugin PLUGIN
231
232 =item --plugin +App::Yath::Plugin::PLUGIN
233
234 =item --plugin PLUGIN=arg1,arg2,...
235
236 =item -pPLUGIN
237
238 =item --no-plugins
239
240 Load a yath plugin.
241
242 Can be specified multiple times
243
244
245 =back
246
247 =head1 SOURCE
248
249 The source code repository for Test2-Harness can be found at
250 F<http://github.com/Test-More/Test2-Harness/>.
251
252 =head1 MAINTAINERS
253
254 =over 4
255
256 =item Chad Granum E<lt>[email protected]<gt>
257
258 =back
259
260 =head1 AUTHORS
261
262 =over 4
263
264 =item Chad Granum E<lt>[email protected]<gt>
265
266 =back
267
268 =head1 COPYRIGHT
269
270 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
271
272 This program is free software; you can redistribute it and/or
273 modify it under the same terms as Perl itself.
274
275 See F<http://dev.perl.org/licenses/>
276
277 =cut