Debian Codegraph @

lib/CPAN/Requirements/Dynamic.pm

raw
1 package CPAN::Requirements::Dynamic;
2 $CPAN::Requirements::Dynamic::VERSION = '0.003';
3 use strict;
4 use warnings;
5
6 use Carp 'croak';
7 use Parse::CPAN::Meta;
8
9 sub _version_satisfies {
10 my ($version, $range) = @_;
11 require CPAN::Meta::Requirements::Range;
12 return CPAN::Meta::Requirements::Range->with_string_requirement($range)->accepts($version);
13 }
14
15 my %default_commands = (
16 can_run => sub {
17 my ($self, $command) = @_;
18 require IPC::Cmd;
19 return IPC::Cmd::can_run($command);
20 },
21 config_defined => sub {
22 my ($self, $entry) = @_;
23 return ($self->{config}->get($entry) // '') eq 'define';
24 },
25 has_env => sub {
26 my ($self, $entry) = @_;
27 return !!$ENV{$entry};
28 },
29 has_perl => sub {
30 my ($self, $range) = @_;
31 return _version_satisfies($], $range);
32 },
33 is_extended => sub {
34 return !!$ENV{EXTENDED_TESTING};
35 },
36 is_os => sub {
37 my ($self, @wanted) = @_;
38 return !!grep { $_ eq $^O } @wanted
39 },
40 is_os_type => sub {
41 my ($self, $wanted) = @_;
42 require Perl::OSType;
43 return Perl::OSType::is_os_type($wanted);
44 },
45 is_smoker => sub {
46 return !!$ENV{AUTOMATED_TESTING};
47 },
48 want_xs => sub {
49 my ($self) = @_;
50 return !!0 if $self->{pureperl_only};
51 require ExtUtils::HasCompiler;
52 return ExtUtils::HasCompiler::can_compile_extension(config => $self->{config});
53 },
54 );
55
56 sub new {
57 my ($class, %args) = @_;
58 return bless {
59 config => $args{config} || do { require ExtUtils::Config; ExtUtils::Config->new },
60 prereqs => $args{prereqs} || do { require CPAN::Meta::Prereqs; CPAN::Meta::Prereqs->new },
61 commands => $args{commands} || \%default_commands,
62 pureperl_only => $args{pureperl_only},
63 }, $class;
64 }
65
66 sub _get_command {
67 my ($self, $name) = @_;
68 if ($name eq 'or') {
69 return sub {
70 my ($self, @each) = @_;
71 for my $elem (@each) {
72 return !!1 if $self->_run_condition($elem);
73 }
74 return !!0;
75 };
76 } elsif ($name eq 'and') {
77 return sub {
78 my ($self, @each) = @_;
79 for my $elem (@each) {
80 return !!0 if not $self->_run_condition($elem);
81 }
82 return !!1;
83 };
84 } else {
85 return $self->{commands}{$name} || croak "No such command $name";
86 }
87 }
88
89 sub _run_condition {
90 my ($self, $condition) = @_;
91
92 my $negate = !!0;
93 my ($function, @arguments) = @{ $condition };
94 while ($function eq 'not') {
95 $function = shift @arguments;
96 $negate = !$negate;
97 }
98
99 my $method = $self->_get_command($function);
100 my $primary = $self->$method(@arguments);
101 return $negate ? !$primary : $primary;
102 }
103
104 sub evaluate {
105 my ($self, $argument) = @_;
106 my $version = $argument->{version};
107 croak "Dynamic prereqs spec version $version is not supported" if int $version > 1;
108 my @prereqs;
109
110 for my $entry (@{ $argument->{expressions} }) {
111 if ($self->_run_condition($entry->{condition})) {
112 if ($entry->{error}) {
113 die "$entry->{error}\n";
114 } elsif (my $prereqs = $entry->{prereqs}) {
115 my $phase = $entry->{phase} || 'runtime';
116 my $relation = $entry->{relation} || 'requires';
117 my $prereqs = { $phase => { $relation => $entry->{prereqs} } };
118 push @prereqs, CPAN::Meta::Prereqs->new($prereqs);
119 }
120 }
121 }
122
123 return $self->{prereqs}->with_merged_prereqs(\@prereqs);
124 }
125
126 sub evaluate_file {
127 my ($self, $filename) = @_;
128 my $structure = Parse::CPAN::Meta->load_file($filename);
129 return $self->evaluate($structure);
130 }
131
132 1;
133
134 # ABSTRACT: Dynamic prerequisites in meta files
135
136 __END__
137
138 =pod
139
140 =encoding UTF-8
141
142 =head1 NAME
143
144 CPAN::Requirements::Dynamic - Dynamic prerequisites in meta files
145
146 =head1 VERSION
147
148 version 0.003
149
150 =head1 SYNOPSIS
151
152 my $result = $dynamic->evaluate({
153 expressions => [
154 {
155 condition => [ 'has_perl' => 'v5.20.0' ],
156 prereqs => { Bar => "1.3" },
157 },
158 {
159 condition => [ is_os => 'linux' ],
160 prereqs => { Baz => "1.4" },
161 },
162 {
163 condition => [ config_defined => 'usethreads' ],
164 prereqs => { Quz => "1.5" },
165 },
166 {
167 condition => [ and =>
168 [ config_defined => 'usethreads' ],
169 [ is_os => 'openbsd' ],
170 ],
171 prereqs => { Euz => "1.7" },
172 },
173 {
174 condition => [ not => is_os_type => 'Unix'],
175 error => 'OS unsupported',
176 },
177 ],
178 });
179
180 =head1 DESCRIPTION
181
182 This module implements a format for describing dynamic prerequisites of a distribution.
183
184 =head1 METHODS
185
186 =head2 new(%options)
187
188 This constructor takes two (optional but recommended) named arguments
189
190 =over 4
191
192 =item * config
193
194 This is an L<ExtUtils::Config|ExtUtils::Config> (compatible) object for reading configuration.
195
196 =item * pureperl_only
197
198 This should be the value of the C<pureperl-only> flag.
199
200 =back
201
202 =head2 evaluate(%options)
203
204 This takes a hash with two named arguments: C<version> and C<expressions>. The former is the version of the format, it currently defaults to 1. The latter is a list of hashes that can contain the following keys:
205
206 =over 4
207
208 =item * condition
209
210 The condition of the dynamic requirement. This is an array with a name as first values and zero or more arguments following it. The semantics are described below under L</Conditions>
211
212 =item * prereqs
213
214 The prereqs is a hash with modules for keys and the required version as values (e.g. C<< { Foo => '1.234' } >>).
215
216 =item * phase
217
218 The phase of the requirements. This defaults to C<'runtime'>. Other valid values include C<'build'> and C<'test'>.
219
220 =item * relation
221
222 The relation of the requirements. This defaults to C<'requires'>, but other valid values include C<'recommends'>, C<'suggests'> and C<'conflicts'>.
223
224 =item * error
225
226 It will die with this error if set. The two messages C<"No support for OS"> and C<"OS unsupported"> have special meaning to CPAN Testers and are generally encouraged for situations that indicate not a failed build but an impossibility to build.
227
228 =back
229
230 C<condition> and one of C<prereqs> or C<error> are mandatory.
231
232 =head2 evaluate_file($filename)
233
234 This takes a filename, that can be either a YAML file or a JSON file, and evaluates it.
235
236 =head2 Conditions
237
238 =head3 can_run($command)
239
240 Returns true if a C<$command> can be run.
241
242 =head3 config_defined($variable)
243
244 This returns true if a specific configuration variable is defined.
245
246 =head3 has_env($variable)
247
248 This returns true if the environmental variable with the name in C<$variable> is true.
249
250 =head3 has_perl($version)
251
252 Returns true if the perl version satisfies C<$version>. C<$version> is interpreted exactly as in the CPAN::Meta spec (e.g. C<1.2> equals C<< '>= 1.2' >>).
253
254 =head3 is_extended
255
256 Returns true if extended testing is asked for.
257
258 =head3 is_os(@systems)
259
260 Returns true if the OS name equals any of C<@systems>.
261
262 =head3 is_os_type($type)
263
264 Returns true if the OS type equals C<$type>. Typical values of C<$type> are C<'Unix'> or C<'Windows'>.
265
266 =head3 is_smoker
267
268 Returns true when running on a smoker.
269
270 =head3 has_env
271
272 This returns true if the given environmental variable is true.
273
274 =head3 want_xs
275
276 This returns true if a compiler appears to be available, and the C<pureperl_only> option has not been set.
277
278 =head3 not
279
280 This takes an expression and negates its value.
281
282 =head3 or
283
284 This takes list of arrayrefs, each containing a condition expression. If at least one of the conditions is true this will also return true.
285
286 =head3 and
287
288 This takes a list of arrayrefs, each containing a condition expression. If all of the conditions are true this will also return true.
289
290 =head1 AUTHOR
291
292 Leon Timmermans <[email protected]>
293
294 =head1 COPYRIGHT AND LICENSE
295
296 This software is copyright (c) 2023 by Leon Timmermans.
297
298 This is free software; you can redistribute it and/or modify it under
299 the same terms as the Perl 5 programming language system itself.
300
301 =cut