| 1 | package Pod::Weaver::PluginBundle::ATOOMIC; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '1.00'; | ||
| 7 | |||
| 8 | use namespace::autoclean -also => ['_exp']; | ||
| 9 | |||
| 10 | use Dist::Zilla::Plugin::PodWeaver; | ||
| 11 | use List::Util qw( first ); | ||
| 12 | use Module::Runtime qw( use_module ); | ||
| 13 | use PadWalker qw( peek_sub ); | ||
| 14 | use Pod::Elemental::Transformer::List; | ||
| 15 | use Pod::Elemental::Transformer::Verbatim; | ||
| 16 | use Pod::Weaver::Config::Assembler; | ||
| 17 | use Pod::Weaver::Plugin::SingleEncoding; | ||
| 18 | use Pod::Weaver::Plugin::Transformer; | ||
| 19 | use Pod::Weaver::Section::AllowOverride; | ||
| 20 | use Pod::Weaver::Section::Authors; | ||
| 21 | use Pod::Weaver::Section::Collect; | ||
| 22 | use Pod::Weaver::Section::Contributors; | ||
| 23 | use Pod::Weaver::Section::GenerateSection; | ||
| 24 | use Pod::Weaver::Section::Generic; | ||
| 25 | use Pod::Weaver::Section::Leftovers; | ||
| 26 | use Pod::Weaver::Section::Legal; | ||
| 27 | use Pod::Weaver::Section::Name; | ||
| 28 | use Pod::Weaver::Section::Region; | ||
| 29 | use Pod::Weaver::Section::Version; | ||
| 30 | |||
| 31 | sub _exp { | ||
| 32 | return Pod::Weaver::Config::Assembler->expand_package( $_[0] ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub configure { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | # this sub behaves somewhat like a Dist::Zilla pluginbundle's configure() | ||
| 39 | # -- it returns a list of strings or 1, 2 or 3-element arrayrefs | ||
| 40 | # containing plugin specifications. The goal is to make this look as close | ||
| 41 | # to what weaver.ini looks like as possible. | ||
| 42 | |||
| 43 | # I wouldn't have to do this ugliness if I could have some configuration | ||
| 44 | # values passed in from weaver.ini or the [PodWeaver] plugin's use of | ||
| 45 | # config_plugin (where I could define a 'licence' option) | ||
| 46 | |||
| 47 | ## no critic (Subroutines::ProhibitCallsToUnexportedSubs) | ||
| 48 | my $podweaver_plugin | ||
| 49 | = ${ peek_sub( \&Dist::Zilla::Plugin::PodWeaver::weaver )->{'$self'} | ||
| 50 | }; | ||
| 51 | my $zilla = $podweaver_plugin->zilla; | ||
| 52 | |||
| 53 | my $license_plugin = $self->_plugin_matching( $zilla, 'License' ); | ||
| 54 | my $license_filename | ||
| 55 | = $license_plugin ? $license_plugin->filename : 'LICENSE'; | ||
| 56 | |||
| 57 | my @weaver_config = ( | ||
| 58 | '@CorePrep', | ||
| 59 | [ '-SingleEncoding' => { encoding => 'UTF-8' } ], | ||
| 60 | [ '-Transformer' => List => { transformer => 'List' } ], | ||
| 61 | [ '-Transformer' => Verbatim => { transformer => 'Verbatim' } ], | ||
| 62 | [ 'Region' => 'header' ], | ||
| 63 | 'Name', | ||
| 64 | 'Version', | ||
| 65 | [ 'Region' => 'prelude' ], | ||
| 66 | [ 'Generic' => 'SYNOPSIS' ], | ||
| 67 | [ 'Generic' => 'DESCRIPTION' ], | ||
| 68 | [ 'Generic' => 'OVERVIEW' ], | ||
| 69 | [ 'Collect' => 'ATTRIBUTES' => { command => 'attr' } ], | ||
| 70 | [ 'Collect' => 'METHODS' => { command => 'method' } ], | ||
| 71 | [ 'Collect' => 'FUNCTIONS' => { command => 'func' } ], | ||
| 72 | [ 'Collect' => 'TYPES' => { command => 'type' } ], | ||
| 73 | 'Leftovers', | ||
| 74 | [ 'Region' => 'postlude' ], | ||
| 75 | [ $self->_support_section ], | ||
| 76 | [ | ||
| 77 | 'AllowOverride' => 'allow override SUPPORT' => { | ||
| 78 | header_re => '^(SUPPORT|BUGS)\b', | ||
| 79 | action => 'prepend', | ||
| 80 | match_anywhere => 0, | ||
| 81 | }, | ||
| 82 | ], | ||
| 83 | |||
| 84 | # We don't enable GitHub::Meta under Travis so we don't have | ||
| 85 | # repository metadata. That means we end up generating an empty L<> | ||
| 86 | # tag and our pod syntax test fails. | ||
| 87 | ( $ENV{TRAVIS} ? () : [ $self->_source_section ] ), | ||
| 88 | ); | ||
| 89 | |||
| 90 | my $config = $self->_plugin_matching( $zilla, 'ATOOMIC::WeaverConfig' ); | ||
| 91 | push @weaver_config, [ $self->_donation_section ] | ||
| 92 | if $zilla->copyright_holder =~ /Rolsky/ | ||
| 93 | && $config->include_donations_pod; | ||
| 94 | |||
| 95 | push @weaver_config, ( | ||
| 96 | 'Authors', | ||
| 97 | [ 'Contributors' => { ':version' => '0.008' } ], | ||
| 98 | [ | ||
| 99 | 'Legal' => { | ||
| 100 | ':version' => '4.011', | ||
| 101 | header => 'COPYRIGHT AND ' . $license_filename, | ||
| 102 | license_file => $license_filename, | ||
| 103 | } | ||
| 104 | ], | ||
| 105 | [ 'Region' => 'footer' ], | ||
| 106 | ); | ||
| 107 | |||
| 108 | return @weaver_config; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _plugin_matching { | ||
| 112 | my $self = shift; | ||
| 113 | my $zilla = shift; | ||
| 114 | my $name = shift; | ||
| 115 | |||
| 116 | return first { $_->plugin_name =~ m{/\Q$name} } @{ $zilla->plugins }; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _support_section { | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | # It'd be simpler to just get the dist meta stuff right now but the pod | ||
| 123 | # weaver bundle is configured before this is ready, leading to an endless | ||
| 124 | # loop in Dist::Zilla::_build_distmeta (a bug in DZ? maybe). | ||
| 125 | my $template = <<'SUPPORT'; | ||
| 126 | {{ | ||
| 127 | join "\n\n", ( | ||
| 128 | ( | ||
| 129 | $bugtracker_email | ||
| 130 | ? "Bugs may be submitted at L<$bugtracker_web> or via email to L<$bugtracker_email|mailto:$bugtracker_email>." | ||
| 131 | : $bugtracker_web ? "Bugs may be submitted at L<$bugtracker_web>." | ||
| 132 | : (), | ||
| 133 | ), | ||
| 134 | ( | ||
| 135 | $distmeta->{resources}{x_MailingList} | ||
| 136 | ? 'There is a mailing list available for users of this distribution,' | ||
| 137 | . "\nL<mailto:" | ||
| 138 | . $distmeta->{resources}{x_MailingList} . '>.' | ||
| 139 | : (), | ||
| 140 | ), | ||
| 141 | ( | ||
| 142 | $distmeta->{resources}{x_IRC} | ||
| 143 | ? 'This distribution also has an IRC channel at' . "\nL<" . do { | ||
| 144 | |||
| 145 | # try to extract the channel | ||
| 146 | if ( | ||
| 147 | my ( $network, $channel ) = ( | ||
| 148 | $distmeta->{resources}{x_IRC} | ||
| 149 | =~ m!(?:://)?(\w+(?:\.\w+)*)/?(#\w+)! | ||
| 150 | ) | ||
| 151 | ) { | ||
| 152 | 'C<' | ||
| 153 | . $channel | ||
| 154 | . '> on C<' | ||
| 155 | . $network . '>|' | ||
| 156 | . $distmeta->{resources}{x_IRC}; | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | $distmeta->{resources}{x_IRC}; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | . '>.' | ||
| 163 | : (), | ||
| 164 | ), | ||
| 165 | ( | ||
| 166 | ( $distmeta->{x_authority} // '' ) eq 'cpan:ATOOMIC' | ||
| 167 | ? q{I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.} | ||
| 168 | : (), | ||
| 169 | ), | ||
| 170 | ) | ||
| 171 | }} | ||
| 172 | SUPPORT | ||
| 173 | |||
| 174 | return ( | ||
| 175 | 'GenerateSection' => 'generate SUPPORT' => { | ||
| 176 | title => 'SUPPORT', | ||
| 177 | main_module_only => 0, | ||
| 178 | is_template => 1, | ||
| 179 | text => [$template], | ||
| 180 | }, | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _source_section { | ||
| 185 | my $self = shift; | ||
| 186 | |||
| 187 | my $template = <<'SOURCE'; | ||
| 188 | The source code repository for {{ $name }} can be found at L<{{ $repository_web }}>. | ||
| 189 | SOURCE | ||
| 190 | |||
| 191 | return ( | ||
| 192 | 'GenerateSection' => 'generate SOURCE' => { | ||
| 193 | title => 'SOURCE', | ||
| 194 | main_module_only => 0, | ||
| 195 | is_template => 1, | ||
| 196 | text => [$template], | ||
| 197 | }, | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _donation_section { | ||
| 202 | return ( | ||
| 203 | 'GenerateSection' => 'generate DONATIONS' => { | ||
| 204 | title => 'DONATIONS', | ||
| 205 | main_module_only => 1, | ||
| 206 | is_template => 0, | ||
| 207 | text => [ | ||
| 208 | <<'DONATIONS', | ||
| 209 | If you'd like to thank me for the work I've done on this module, please | ||
| 210 | consider making a "donation" to me via PayPal. I spend a lot of free time | ||
| 211 | creating free software, and would appreciate any support you'd care to offer. | ||
| 212 | |||
| 213 | Please note that B<I am not suggesting that you must do this> in order for me | ||
| 214 | to continue working on this particular software. I will continue to do so, | ||
| 215 | inasmuch as I have in the past, for as long as it interests me. | ||
| 216 | |||
| 217 | Similarly, a donation made in this way will probably not make me work on this | ||
| 218 | software much more, unless I get so many donations that I can consider working | ||
| 219 | on free software full time (let's all have a chuckle at that together). | ||
| 220 | |||
| 221 | To donate, log into PayPal and send money to autarch@urth.org, or use the | ||
| 222 | button at L<http://www.urth.org/~autarch/fs-donation.html>. | ||
| 223 | DONATIONS | ||
| 224 | ] | ||
| 225 | }, | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub mvp_bundle_config { | ||
| 230 | my $self = shift || __PACKAGE__; | ||
| 231 | |||
| 232 | return map { $self->_expand_config($_) } $self->configure; | ||
| 233 | } | ||
| 234 | |||
| 235 | { | ||
| 236 | my $prefix; | ||
| 237 | |||
| 238 | sub _prefix { | ||
| 239 | my $self = shift; | ||
| 240 | return $prefix if defined $prefix; | ||
| 241 | ( $prefix = ( ref($self) || $self ) ) | ||
| 242 | =~ s/^Pod::Weaver::PluginBundle:://; | ||
| 243 | return $prefix; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _expand_config { | ||
| 248 | my ( $self, $this_spec ) = @_; | ||
| 249 | |||
| 250 | die 'undefined config' if not $this_spec; | ||
| 251 | die 'unrecognized config format: ' . ref($this_spec) | ||
| 252 | if ref($this_spec) | ||
| 253 | and ref($this_spec) ne 'ARRAY'; | ||
| 254 | |||
| 255 | my ( $name, $class, $payload ); | ||
| 256 | |||
| 257 | if ( not ref $this_spec ) { | ||
| 258 | ( $name, $class, $payload ) = ( $this_spec, _exp($this_spec), {} ); | ||
| 259 | } | ||
| 260 | elsif ( @$this_spec == 1 ) { | ||
| 261 | ( $name, $class, $payload ) | ||
| 262 | = ( $this_spec->[0], _exp( $this_spec->[0] ), {} ); | ||
| 263 | } | ||
| 264 | elsif ( @$this_spec == 2 ) { | ||
| 265 | $name = ref $this_spec->[1] ? $this_spec->[0] : $this_spec->[1]; | ||
| 266 | $class | ||
| 267 | = _exp( ref $this_spec->[1] ? $this_spec->[0] : $this_spec->[0] ); | ||
| 268 | $payload = ref $this_spec->[1] ? $this_spec->[1] : {}; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | ( $name, $class, $payload ) | ||
| 272 | = ( $this_spec->[1], _exp( $this_spec->[0] ), $this_spec->[2] ); | ||
| 273 | } | ||
| 274 | |||
| 275 | $name =~ s/^[@=-]//; | ||
| 276 | |||
| 277 | # Region plugins have the custom plugin name moved to 'region_name' parameter, | ||
| 278 | # because we don't want our bundle name to be part of the region name. | ||
| 279 | if ( $class eq _exp('Region') ) { | ||
| 280 | $name = $this_spec->[1]; | ||
| 281 | $payload = { region_name => $this_spec->[1], %$payload }; | ||
| 282 | } | ||
| 283 | |||
| 284 | use_module( $class, $payload->{':version'} ) if $payload->{':version'}; | ||
| 285 | |||
| 286 | # prepend '@ATOOMIC/' to each class name, | ||
| 287 | # except for Generic and Collect which are left alone. | ||
| 288 | $name = '@' . $self->_prefix . '/' . $name | ||
| 289 | if $class ne _exp('Generic') | ||
| 290 | and $class ne _exp('Collect'); | ||
| 291 | |||
| 292 | return [ $name => $class => $payload ]; | ||
| 293 | } | ||
| 294 | |||
| 295 | 1; | ||
| 296 | |||
| 297 | # ABSTRACT: A plugin bundle for pod woven by ATOOMIC | ||
| 298 | |||
| 299 | __END__ |