File Coverage

blib/lib/Dist/Zilla/Plugin/MetaJSON.pm
Criterion Covered Total %
statement 31 34 91.1
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 38 43 88.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::MetaJSON 6.029;
2             # ABSTRACT: produce a META.json
3              
4 8     8   7179 use Moose;
  8         25  
  8         100  
5             with 'Dist::Zilla::Role::FileGatherer';
6 8     8   56155 use Moose::Util::TypeConstraints;
  8         23  
  8         94  
7              
8 8     8   18240 use Dist::Zilla::Pragmas;
  8         31  
  8         79  
9              
10 8     8   70 use namespace::autoclean;
  8         31  
  8         77  
11              
12             #pod =head1 DESCRIPTION
13             #pod
14             #pod This plugin will add a F<META.json> file to the distribution.
15             #pod
16             #pod This file is meant to replace the old-style F<META.yml>. For more information
17             #pod on this file, see L<Module::Build::API> and L<CPAN::Meta>.
18             #pod
19             #pod =attr filename
20             #pod
21             #pod If given, parameter allows you to specify an alternate name for the generated
22             #pod file. It defaults, of course, to F<META.json>.
23             #pod
24             #pod =cut
25              
26             has filename => (
27             is => 'ro',
28             isa => 'Str',
29             default => 'META.json',
30             );
31              
32             #pod =attr version
33             #pod
34             #pod This parameter lets you pick what version of the spec to use when generating
35             #pod the output. It defaults to 2 at present, but may be updated to new specs as
36             #pod they are released and adopted.
37             #pod
38             #pod If you want a fixed version, specify it.
39             #pod
40             #pod =cut
41              
42             my $version_type = subtype(
43             as 'Num',
44             where { $_ >= 2 },
45             message { "MetaJSON version must be 2 or greater" },
46             );
47              
48             has version => (
49             is => 'ro',
50             isa => $version_type,
51             default => '2',
52             );
53              
54             sub gather_files {
55 9     9 0 45 my ($self, $arg) = @_;
56              
57 9         283 my $zilla = $self->zilla;
58              
59 9         67 require JSON::MaybeXS;
60 9         1562 require Dist::Zilla::File::FromCode;
61 9         2594 require CPAN::Meta::Converter;
62 9         53882 CPAN::Meta::Converter->VERSION(2.101550); # improved downconversion
63 9         429 require CPAN::Meta::Validator;
64 9         122 CPAN::Meta::Validator->VERSION(2.101550); # improved downconversion
65              
66             my $file = Dist::Zilla::File::FromCode->new({
67             name => $self->filename,
68             encoding => 'ascii',
69             code_return_type => 'text',
70             code => sub {
71 9     9   287 my $distmeta = $zilla->distmeta;
72              
73 9         76 my $validator = CPAN::Meta::Validator->new($distmeta);
74              
75 9 50       192 unless ($validator->is_valid) {
76 0         0 my $msg = "Invalid META structure. Errors found:\n";
77 0         0 $msg .= join( "\n", $validator->errors );
78 0         0 $self->log_fatal($msg);
79             }
80              
81 9         5270 my $converter = CPAN::Meta::Converter->new($distmeta);
82 9         699 my $output = $converter->convert(version => $self->version);
83              
84 9         17003 my $backend = JSON::MaybeXS::JSON();
85 9         274 $output->{x_serialization_backend} = sprintf '%s version %s',
86             $backend, $backend->VERSION;
87              
88 9         124 JSON::MaybeXS->new(canonical => 1, pretty => 1, ascii => 1)->encode($output)
89             . "\n";
90             },
91 9         365 });
92              
93 9         78 $self->add_file($file);
94 9         55 return;
95             }
96              
97             __PACKAGE__->meta->make_immutable;
98             1;
99              
100             #pod =head1 SEE ALSO
101             #pod
102             #pod Core Dist::Zilla plugins:
103             #pod L<@Basic|Dist::Zilla::PluginBundle::Basic>,
104             #pod L<Manifest|Dist::Zilla::Plugin::Manifest>.
105             #pod
106             #pod Dist::Zilla roles:
107             #pod L<FileGatherer|Dist::Zilla::Role::FileGatherer>.
108             #pod
109             #pod Other modules:
110             #pod L<CPAN::Meta>,
111             #pod L<CPAN::Meta::Spec>, L<JSON::MaybeXS>.
112             #pod
113             #pod =cut
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             Dist::Zilla::Plugin::MetaJSON - produce a META.json
124              
125             =head1 VERSION
126              
127             version 6.029
128              
129             =head1 DESCRIPTION
130              
131             This plugin will add a F<META.json> file to the distribution.
132              
133             This file is meant to replace the old-style F<META.yml>. For more information
134             on this file, see L<Module::Build::API> and L<CPAN::Meta>.
135              
136             =head1 PERL VERSION
137              
138             This module should work on any version of perl still receiving updates from
139             the Perl 5 Porters. This means it should work on any version of perl released
140             in the last two to three years. (That is, if the most recently released
141             version is v5.40, then this module should work on both v5.40 and v5.38.)
142              
143             Although it may work on older versions of perl, no guarantee is made that the
144             minimum required version will not be increased. The version may be increased
145             for any reason, and there is no promise that patches will be accepted to lower
146             the minimum required perl.
147              
148             =head1 ATTRIBUTES
149              
150             =head2 filename
151              
152             If given, parameter allows you to specify an alternate name for the generated
153             file. It defaults, of course, to F<META.json>.
154              
155             =head2 version
156              
157             This parameter lets you pick what version of the spec to use when generating
158             the output. It defaults to 2 at present, but may be updated to new specs as
159             they are released and adopted.
160              
161             If you want a fixed version, specify it.
162              
163             =head1 SEE ALSO
164              
165             Core Dist::Zilla plugins:
166             L<@Basic|Dist::Zilla::PluginBundle::Basic>,
167             L<Manifest|Dist::Zilla::Plugin::Manifest>.
168              
169             Dist::Zilla roles:
170             L<FileGatherer|Dist::Zilla::Role::FileGatherer>.
171              
172             Other modules:
173             L<CPAN::Meta>,
174             L<CPAN::Meta::Spec>, L<JSON::MaybeXS>.
175              
176             =head1 AUTHOR
177              
178             Ricardo SIGNES 😏 <cpan@semiotic.systems>
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             This software is copyright (c) 2022 by Ricardo SIGNES.
183              
184             This is free software; you can redistribute it and/or modify it under
185             the same terms as the Perl 5 programming language system itself.
186              
187             =cut