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