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