1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
package Dist::Zilla::Plugin::OurPkgVersion;
use 5.008;
use strict;
use warnings;
our $VERSION = '0.14'; # VERSION
use Moose;
with (
'Dist::Zilla::Role::FileMunger',
'Dist::Zilla::Role::FileFinderUser' => {
default_finders => [
':InstallModules',
':PerlExecFiles',
],
},
'Dist::Zilla::Role::PPI',
);
use Carp qw( confess );
use PPI;
use MooseX::Types::Perl qw( LaxVersionStr );
use namespace::autoclean;
has underscore_eval_version => (
is => 'ro',
isa => 'Int',
default => 0,
);
has skip_main_module => (
is => 'ro',
isa => 'Int',
default => 0,
);
sub munge_files {
my $self = shift;
$self->munge_file($_) for
grep { $self->skip_main_module ? $_->name ne $self->zilla->main_module->name : 1 }
@{ $self->found_files };
return;
}
sub munge_file {
my ( $self, $file ) = @_;
if ( $file->name =~ m/\.pod$/ixms ) {
$self->log_debug( 'Skipping: "' . $file->name . '" is pod only');
return;
}
my $version = $self->zilla->version;
confess 'invalid characters in version'
unless LaxVersionStr->check( $version ); ## no critic (Modules::RequireExplicitInclusion)
my $doc = $self->ppi_document_for_file($file);
return unless defined $doc;
my $comments = $doc->find('PPI::Token::Comment');
my $version_regex
= q{
^
(\s*) # capture all whitespace before comment
(
\#\#?\s*VERSION # capture # VERSION or ## VERSION
\b # and ensure it ends on a word boundary
[ # conditionally
[:print:] # all printable characters after VERSION
\s # any whitespace including newlines - GH #5
]* # as many of the above as there are
)
$ # until the EOL}
;
my $munged_version = 0;
if ( ref($comments) eq 'ARRAY' ) {
foreach ( @{ $comments } ) {
if ( /$version_regex/xms ) {
my ( $ws, $comment ) = ( $1, $2 );
$comment =~ s/(?=\bVERSION\b)/TRIAL /x if $self->zilla->is_trial;
my $code
= "$ws"
. q{our $VERSION = '}
. $version
. qq{'; $comment}
;
if ( $version =~ /_/ && $self->underscore_eval_version ) {
$code.= "\$VERSION = eval \$VERSION;\n";
}
$_->set_content("$code");
$munged_version++;
}
}
}
if ( $munged_version ) {
$self->save_ppi_document_to_file( $doc, $file);
$self->log_debug([ 'adding $VERSION assignment to %s', $file->name ]);
}
else {
$self->log( 'Skipping: "'
. $file->name
. '" has no "# VERSION" comment'
);
}
return;
}
__PACKAGE__->meta->make_immutable;
1;
# ABSTRACT: No line insertion and does Package version with our
__END__
=pod
=head1 NAME
Dist::Zilla::Plugin::OurPkgVersion - No line insertion and does Package version with our
=head1 VERSION
version 0.14
=head1 SYNOPSIS
in dist.ini
[OurPkgVersion]
in your modules
# VERSION
=head1 DESCRIPTION
This module was created as an alternative to
L<Dist::Zilla::Plugin::PkgVersion> and uses some code from that module. This
module is designed to use a the more readable format C<our $VERSION =
$version;> as well as not change then number of lines of code in your files,
which will keep your repository more in sync with your CPAN release. It also
allows you slightly more freedom in how you specify your version.
=head2 EXAMPLES
in dist.ini
...
version = 0.01;
[OurPkgVersion]
in lib/My/Module.pm
package My::Module;
# VERSION
...
output lib/My/Module.pm
package My::Module;
our $VERSION = '0.01'; # VERSION
...
please note that whitespace before the comment is significant so
package My::Module;
BEGIN {
# VERSION
}
...
becomes
package My::Module;
BEGIN {
our $VERSION = '0.01'; # VERSION
}
...
while
package My::Module;
BEGIN {
# VERSION
}
...
becomes
package My::Module;
BEGIN {
our $VERSION = '0.01'; # VERSION
}
...
you can also add additional comments to your comment
...
# VERSION: generated by DZP::OurPkgVersion
...
becomes
...
our $VERSION = '0.1.0'; # VERSION: generated by DZP::OurPkgVersion
...
you can also use perltidy's default static side comments (##)
...
## VERSION
...
becomes
...
our $VERSION = '0.1.0'; ## VERSION
...
Also note, the package line is not in any way significant, it will insert the
C<our $VERSION> line anywhere in the file before C<# VERSION> as many times as
you've written C<# VERSION> regardless of whether or not inserting it there is
a good idea. OurPkgVersion will not insert a version unless you have C<#
VERSION> so it is a bit more work.
If you make a trial release, the comment will be altered to say so:
# VERSION
becomes
our $VERSION = '0.01'; # TRIAL VERSION
=encoding UTF-8
=head1 METHODS
=over
=item munge_files
Override the default provided by L<Dist::Zilla::Role::FileMunger> to limit
the number of files to search to only be modules and executables.
=item munge_file
tells which files to munge, see L<Dist::Zilla::Role::FileMunger>
=item finder
Override the default L<FileFinder|Dist::Zilla::Role::FileFinder> for
finding files to check and update. The default value is C<:InstallModules>
and C<:PerlExecFiles> (when available; otherwise C<:ExecFiles>)
-- see also L<Dist::Zilla::Plugin::ExecDir>, to make sure the script
files are properly marked as executables for the installer.
=back
=head1 PROPERTIES
=over
=item underscore_eval_version
For version numbers that have an underscore in them, add this line
immediately after the our version assignment:
$VERSION = eval $VERSION;
This is arguably the correct thing to do, but changes the line numbering
of the generated Perl module or source, and thus optional.
=item skip_main_module
Set to true to ignore the main module in the distribution. This prevents
a warning when using L<Dist::Zilla::Plugin::VersionFromMainModule> to
obtain the version number instead of the C<dist.ini> file.
=back
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
L<https://github.com/plicease/dist-zilla-plugin-ourpkgversion/issues>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 CONTRIBUTORS
=for stopwords Alexandr Ciornii Stephan Loyd Alexei Znamensky Christian Walde Christopher J. Madsen David Golden Graham Ollis Graham✈️✈️ Ian Sealy Michael Jemmeson
=over 4
=item *
Alexandr Ciornii <alexchorny@gmail.com>
=item *
Stephan Loyd <stephanloyd9@gmail.com>
=item *
Alexei Znamensky <russoz@cpan.org>
=item *
Christian Walde <walde.christian@googlemail.com>
=item *
Christopher J. Madsen <perl@cjmweb.net>
=item *
David Golden <dagolden@cpan.org>
=item *
Graham Ollis <perl@wdlabs.com>
=item *
Graham Ollis <plicease@cpan.org>
=item *
Graham✈️✈️ <plicease@cpan.org>
=item *
Ian Sealy <git@iansealy.com>
=item *
Michael Jemmeson <mjemmeson@cpan.org>
=back
=head1 AUTHORS
=over 4
=item *
Caleb Cushing <xenoterracide@gmail.com>
=item *
Grahan Ollis <plicease@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2019 by Caleb Cushing.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut
|