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.037;
2             # ABSTRACT: produce a META.json
3              
4 8     8   7852 use Moose;
  8         20  
  8         106  
5             with 'Dist::Zilla::Role::FileGatherer';
6 8     8   66386 use Moose::Util::TypeConstraints;
  8         17  
  8         90  
7              
8 8     8   20375 use Dist::Zilla::Pragmas;
  8         22  
  8         87  
9              
10 8     8   73 use namespace::autoclean;
  8         18  
  8         96  
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 35 my ($self, $arg) = @_;
56              
57 9         359 my $zilla = $self->zilla;
58              
59 9         78 require JSON::MaybeXS;
60 9         2128 require Dist::Zilla::File::FromCode;
61 9         112 require CPAN::Meta::Converter;
62 9         233 CPAN::Meta::Converter->VERSION(2.101550); # improved downconversion
63 9         60 require CPAN::Meta::Validator;
64 9         113 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   361 my $distmeta = $zilla->distmeta;
72              
73 9         113 my $validator = CPAN::Meta::Validator->new($distmeta);
74              
75 9 50       173 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         6126 my $converter = CPAN::Meta::Converter->new($distmeta);
82 9         833 my $output = $converter->convert(version => $self->version);
83              
84 9         19236 my $backend = JSON::MaybeXS::JSON();
85 9         304 $output->{x_serialization_backend} = sprintf '%s version %s',
86             $backend, $backend->VERSION;
87              
88 9         104 JSON::MaybeXS->new(canonical => 1, pretty => 1, ascii => 1)->encode($output)
89             . "\n";
90             },
91 9         407 });
92              
93 9         74 $self->add_file($file);
94 9         68 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.037
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
140             released in the last two to three years. (That is, if the most recently
141             released version is v5.40, then this module should work on both v5.40 and
142             v5.38.)
143              
144             Although it may work on older versions of perl, no guarantee is made that the
145             minimum required version will not be increased. The version may be increased
146             for any reason, and there is no promise that patches will be accepted to
147             lower the minimum required perl.
148              
149             =head1 ATTRIBUTES
150              
151             =head2 filename
152              
153             If given, parameter allows you to specify an alternate name for the generated
154             file. It defaults, of course, to F<META.json>.
155              
156             =head2 version
157              
158             This parameter lets you pick what version of the spec to use when generating
159             the output. It defaults to 2 at present, but may be updated to new specs as
160             they are released and adopted.
161              
162             If you want a fixed version, specify it.
163              
164             =head1 SEE ALSO
165              
166             Core Dist::Zilla plugins:
167             L<@Basic|Dist::Zilla::PluginBundle::Basic>,
168             L<Manifest|Dist::Zilla::Plugin::Manifest>.
169              
170             Dist::Zilla roles:
171             L<FileGatherer|Dist::Zilla::Role::FileGatherer>.
172              
173             Other modules:
174             L<CPAN::Meta>,
175             L<CPAN::Meta::Spec>, L<JSON::MaybeXS>.
176              
177             =head1 AUTHOR
178              
179             Ricardo SIGNES 😏 <cpan@semiotic.systems>
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2026 by Ricardo SIGNES.
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut