File Coverage

blib/lib/Dist/Zilla/Plugin/AutoMetaResources.pm
Criterion Covered Total %
statement 38 39 97.4
branch 7 12 58.3
condition n/a
subroutine 8 8 100.0
pod 1 2 50.0
total 54 61 88.5


line stmt bran cond sub pod time code
1 1     1   475412 use strict;
  1         2  
  1         47  
2 1     1   6 use warnings;
  1         2  
  1         84  
3             package Dist::Zilla::Plugin::AutoMetaResources;
4             our $AUTHORITY = 'cpan:AJGB';
5             #ABSTRACT: Automagical MetaResources
6             $Dist::Zilla::Plugin::AutoMetaResources::VERSION = '1.21';
7 1     1   6 use Moose;
  1         3  
  1         7  
8             with 'Dist::Zilla::Role::MetaProvider';
9              
10              
11             sub _subs(@) {
12 29     29   39 my ($format, $vars) = @_;
13 29 50       88 $format =~ s/%\{([\w_\.]+)\}/$vars->{$1} || ''/ge;
  29         100  
14 29         108 return $format;
15             }
16              
17             has '_resources' => (
18             is => 'ro',
19             isa => 'HashRef',
20             required => 1,
21             );
22              
23              
24             has '_repository_map' => (
25             is => 'ro',
26             lazy => 1,
27             builder => '_build__repository_map',
28             );
29              
30             sub _build__repository_map {
31             # based on Dist::Zilla::PluginBundle::FLORA
32             return {
33 24         304 github => {
34             url => 'git://github.com/%{user}/%{lcdist}.git',
35             web => 'https://github.com/%{user}/%{lcdist}',
36             type => 'git',
37             },
38             gitmo => {
39             url => 'git://git.moose.perl.org/%{dist}.git',
40             web => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo/%{dist}.git;a=summary',
41             type => 'git',
42             },
43             catsvn => {
44             url => 'http://dev.catalyst.perl.org/repos/Catalyst/%{dist}/',
45             web => 'http://dev.catalystframework.org/svnweb/Catalyst/browse/%{dist}',
46             type => 'svn',
47             },
48             bitbucket => {
49             url => 'git://bitbucket.org:%{user}/%{lcdist}.git',
50             web => 'https://bitbucket.org/%{user}/%{lcdist}',
51             type => 'git',
52             },
53             (map {
54 8     8   68 ($_ => {
55             url => "git://git.shadowcat.co.uk/$_/%{dist}.git",
56             web => "http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=$_/%{dist}.git;a=summary",
57             type => 'git',
58             })
59             } qw(catagits p5sagit dbsrgits)),
60             };
61             }
62              
63             has '_bugtracker_map' => (
64             is => 'ro',
65             lazy => 1,
66             builder => '_build__bugtracker_map',
67             );
68              
69             sub _build__bugtracker_map {
70             return {
71 4     4   138 rt => {
72             web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=%{dist}',
73             mailto => 'bug-%{dist}@rt.cpan.org',
74             },
75             github => {
76             web => 'https://github.com/%{user}/%{lcdist}/issues',
77             },
78             bitbucket => {
79             web => 'https://bitbucket.org/%{user}/%{lcdist}/issues',
80             },
81             };
82             }
83              
84             sub BUILDARGS {
85 14     14 1 254510 my ($class, @arg) = @_;
86 14 50       45 my %copy = ref $arg[0] ? %{ $arg[0] } : @arg;
  14         49  
87              
88 14         39 my $zilla = delete $copy{zilla};
89 14         26 my $name = delete $copy{plugin_name};
90              
91             return {
92 14         52 zilla => $zilla,
93             plugin_name => $name,
94             _resources => \%copy,
95             }
96             }
97              
98              
99             sub metadata {
100 14     14 0 130224 my $self = shift;
101              
102 14         23 my $resources = {};
103 14         340 my %names = (
104             dist => $self->zilla->name,
105             lcdist => lc $self->zilla->name,
106             );
107              
108 14         859 while (my ($arg, $opts) = each %{$self->_resources} ) {
  27         773  
109 13 50       27 next unless $opts;
110              
111 13         53 my ($res, $type) = split(/\./, $arg);
112              
113 13 100       80 if ( my $method = $self->can("_${res}_map") ) {
114 12 50       25 next unless $type;
115 12         356 my $map = $method->($self)->{lc $type};
116 12         37 for my $var ( keys %$map ) {
117 0         0 $resources->{$res}->{$var} = _subs $map->{$var}, {
118             %names,
119             (
120             ref $opts ?
121 28         44 %{$opts->[0]} :
122             map {
123 28 50       77 my ($k, $v) = split(/:/, $_);
124 28         91 $k => $v
125             } split(/;/, $opts)
126             )
127             };
128             }
129             } else {
130 1         3 $resources->{$arg} = _subs $opts, \%names;
131             }
132             }
133              
134             return {
135 14         79 resources => $resources,
136             };
137             }
138              
139             __PACKAGE__->meta->make_immutable(inline_constructor => 0);
140              
141              
142             1;
143              
144             __END__
145              
146             =pod
147              
148             =encoding UTF-8
149              
150             =head1 NAME
151              
152             Dist::Zilla::Plugin::AutoMetaResources - Automagical MetaResources
153              
154             =head1 VERSION
155              
156             version 1.21
157              
158             =head1 SYNOPSIS
159              
160             # dist.ini
161             name = Dist-Zilla-Plugin-AutoMetaResources
162             ...
163              
164             [AutoMetaResources]
165             bugtracker.github = user:ajgb
166             repository.github = user:ajgb
167             homepage = https://metacpan.org/release/%{dist}
168              
169             or in PluginBundle::Easy
170              
171             $self->add_plugins(
172             [
173             AutoMetaResources => {
174             'bugtracker.github' => [{ user => 'ajgb' }],
175             'repository.github' => [{ user => 'ajgb' }],
176             'homepage' => 'https://metacpan.org/release/%{dist}',
177             }
178             ]
179             );
180              
181             both for Dist::Zilla::Plugin::AutoMetaResources would be equivalent of
182              
183             [MetaResources]
184             bugtracker.web = https://github.com/ajgb/dist-zilla-plugin-autometaresources/issues
185             repository.url = git://github.com/ajgb/dist-zilla-plugin-autometaresources.git
186             repository.web = http://github.com/ajgb/dist-zilla-plugin-autometaresources
187             repository.type = git
188             homepage = https://metacpan.org/release/Dist-Zilla-Plugin-AutoMetaResources
189              
190             =head1 DESCRIPTION
191              
192             If you find repeating the dist name again in the configuration really
193             annoying, this plugin comes to the rescue!
194              
195             =head1 CONFIGURATION
196              
197             Most of the known resources requires just boolean flag to be enabled, but you
198             are free to pass your own options:
199              
200             bugtracker.rt = dist:Other-Name;lcdist:other-name
201              
202             which is transformed to:
203              
204             [{
205             dist => 'Other-Name',
206             lcdist => 'other-name',
207             }]
208              
209             =head2 BUGTRACKER
210              
211             B<RT bugtracker>:
212              
213             [AutoMetaResources]
214             bugtracker.rt = 1
215              
216             # same as
217             [MetaResources]
218             bugtracker.web = https://rt.cpan.org/Public/Dist/Display.html?Name=%{dist}
219             bugtracker.mailto = bug-%{dist}@rt.cpan.org
220              
221             B<GitHub issues>:
222              
223             [AutoMetaResources]
224             bugtracker.github = user:ajgb
225              
226             # same as
227             [MetaResources]
228             bugtracker.web = http://github.com/ajgb/%{lcdist}/issues
229              
230             B<Bitbucket issues>:
231              
232             [AutoMetaResources]
233             bugtracker.bitbucket = user:ajgb
234              
235             # same as
236             [MetaResources]
237             bugtracker.web = https://bitbucket.org/ajgb/%{lcdist}/issues
238              
239             =head2 REPOSITORY
240              
241             B<GitHub repository>:
242              
243             [AutoMetaResources]
244             repository.github = user:ajgb
245             # or
246             repository.GitHub = user:ajgb
247              
248             # same as
249             [MetaResources]
250             repository.url = git://github.com/ajgb/%{lcdist}.git
251             repository.web = http://github.com/ajgb/%{lcdist}
252             repository.type = git
253              
254             B<Bitbucket repository>:
255              
256             [AutoMetaResources]
257             repository.bitbucket = user:ajgb
258              
259             # same as
260             [MetaResources]
261             repository.url = git://bitbucket.org:ajgb/%{lcdist}.git
262             repository.web = https://bitbucket.org/ajgb/%{lcdist}
263             repository.type = git
264              
265             B<Git Moose repository>:
266              
267             [AutoMetaResources]
268             repository.gitmo = 1
269              
270             # same as
271             [MetaResources]
272             repository.url = git://git.moose.perl.org/%{dist}.git
273             repository.web = http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo/%{dist}.git;a=summary
274             repository.type = git
275              
276             B<Catalyst Subversion repository>:
277              
278             [AutoMetaResources]
279             repository.catsvn = 1
280              
281             # same as
282             [MetaResources]
283             repository.url = http://dev.catalyst.perl.org/repos/Catalyst/%{dist}/
284             repository.web = http://dev.catalystframework.org/svnweb/Catalyst/browse/%{dist}
285             repository.type = svn
286              
287             B<Shadowcat's Git repositories>:
288              
289             [AutoMetaResources]
290             repository.catagits = 1
291              
292             # same as
293             [MetaResources]
294             repository.url = git://git.shadowcat.co.uk/catagits/%{dist}.git
295             repository.web = http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits/%{dist}.git;a=summary
296             repository.type = git
297              
298             and
299              
300             [AutoMetaResources]
301             repository.p5sagit = 1
302              
303             # same as
304             [MetaResources]
305             repository.url = git://git.shadowcat.co.uk/p5sagit/%{dist}.git
306             repository.web = http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/%{dist}.git;a=summary
307             repository.type = git
308              
309             and
310              
311             [AutoMetaResources]
312             repository.dbsrgits = 1
313              
314             # same as
315             [MetaResources]
316             repository.url = git://git.shadowcat.co.uk/dbsrgits/%{dist}.git
317             repository.web = http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/%{dist}.git;a=summary
318             repository.type = git
319              
320             =head2 OTHER
321              
322             [AutoMetaResources]
323             homepage = https://metacpan.org/release/%{dist}
324              
325             You are free to pass other valid metadata options to be included as resources.
326             Values can contain following variables:
327              
328             =over
329              
330             =item I<%{dist}>
331              
332             Name of the distribution
333              
334             =item I<%{lcdist}>
335              
336             Name of the distribution (lowercase)
337              
338             =back
339              
340             =for Pod::Coverage metadata
341              
342             =head1 AUTHOR
343              
344             Alex J. G. BurzyÅ„ski <ajgb@cpan.org>
345              
346             =head1 COPYRIGHT AND LICENSE
347              
348             This software is copyright (c) 2013 by Alex J. G. BurzyÅ„ski <ajgb@cpan.org>.
349              
350             This is free software; you can redistribute it and/or modify it under
351             the same terms as the Perl 5 programming language system itself.
352              
353             =cut