| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Dist::Zilla::Plugin::Doppelgaenger; | ||
| 5 | # ABSTRACT: Creates an evil twin of a CPAN distribution | ||
| 6 | |||
| 7 | our $VERSION = '0.008'; # from Dist-Zilla-Plugin-Doppelgaenger-0.008.tar.gz | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use MooseX::Types::Path::Class qw(Dir File); | ||
| 11 | use MooseX::Types::URI qw(Uri); | ||
| 12 | use MooseX::Types::Perl qw(ModuleName); | ||
| 13 | with 'Dist::Zilla::Role::FileGatherer'; | ||
| 14 | with 'Dist::Zilla::Role::TextTemplate'; | ||
| 15 | with 'Dist::Zilla::Role::FileMunger'; # for Changes | ||
| 16 | with 'Dist::Zilla::Role::AfterRelease'; # for Changes | ||
| 17 | |||
| 18 | use Dist::Zilla::File::InMemory 5; # encoded_content | ||
| 19 | use File::Find::Rule; | ||
| 20 | use File::pushd qw/tempd/; | ||
| 21 | use Path::Class; | ||
| 22 | use Pod::Strip; | ||
| 23 | use Archive::Extract; | ||
| 24 | use HTTP::Tiny; | ||
| 25 | use JSON; | ||
| 26 | |||
| 27 | use namespace::autoclean; | ||
| 28 | |||
| 29 | #--------------------------------------------------------------------------# | ||
| 30 | # public attributes | ||
| 31 | #--------------------------------------------------------------------------# | ||
| 32 | |||
| 33 | #pod =attr source_module (REQUIRED) | ||
| 34 | #pod | ||
| 35 | #pod The name of a CPAN module to imitate. E.g. Foo::Bar | ||
| 36 | #pod | ||
| 37 | #pod =cut | ||
| 38 | |||
| 39 | has source_module => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => ModuleName, | ||
| 42 | required => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | #pod =attr new_name | ||
| 46 | #pod | ||
| 47 | #pod The new name to use in place of the source name. Defaults to | ||
| 48 | #pod the converted form of the distribution name. | ||
| 49 | #pod | ||
| 50 | #pod =cut | ||
| 51 | |||
| 52 | has new_name => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => ModuleName, | ||
| 55 | lazy => 1, | ||
| 56 | required => 1, | ||
| 57 | builder => '_build_new_name', | ||
| 58 | ); | ||
| 59 | |||
| 60 | sub _build_new_name { | ||
| 61 | my ($self) = shift; | ||
| 62 | my $name = $self->zilla->name; | ||
| 63 | $name =~ s{-}{::}g; | ||
| 64 | return $name; | ||
| 65 | } | ||
| 66 | |||
| 67 | #pod =attr cpan_mirror | ||
| 68 | #pod | ||
| 69 | #pod This is a URI to a CPAN mirror. It must be an 'http' URI. | ||
| 70 | #pod Defaults to C<http://www.cpan.org/> | ||
| 71 | #pod | ||
| 72 | #pod =cut | ||
| 73 | |||
| 74 | has cpan_mirror => ( | ||
| 75 | is => 'ro', | ||
| 76 | isa => Uri, | ||
| 77 | coerce => 1, | ||
| 78 | default => 'http://www.cpan.org/' | ||
| 79 | ); | ||
| 80 | |||
| 81 | #pod =attr strip_version | ||
| 82 | #pod | ||
| 83 | #pod Boolean for whether any assignments to C<$VERSION> should be stripped out of | ||
| 84 | #pod the source. This is a crude hack and acts by killing a line of code containing | ||
| 85 | #pod such assignments. This obviously may not work in all cases and should be used | ||
| 86 | #pod with caution. Default is false. | ||
| 87 | #pod | ||
| 88 | #pod =cut | ||
| 89 | |||
| 90 | has strip_version => ( | ||
| 91 | is => 'ro', | ||
| 92 | isa => 'Bool', | ||
| 93 | default => 0, | ||
| 94 | ); | ||
| 95 | |||
| 96 | #pod =attr strip_pod | ||
| 97 | #pod | ||
| 98 | #pod Boolean for whether Pod should be stripped when copying from source. | ||
| 99 | #pod Default is false. | ||
| 100 | #pod | ||
| 101 | #pod | ||
| 102 | #pod =cut | ||
| 103 | |||
| 104 | has strip_pod => ( | ||
| 105 | is => 'ro', | ||
| 106 | isa => 'Bool', | ||
| 107 | default => 0, | ||
| 108 | ); | ||
| 109 | |||
| 110 | #pod =attr changes_file | ||
| 111 | #pod | ||
| 112 | #pod Name of change log file. Defaults to 'Changes'. | ||
| 113 | #pod | ||
| 114 | #pod =cut | ||
| 115 | |||
| 116 | has changes_file => ( | ||
| 117 | is => 'ro', | ||
| 118 | isa => 'Str', | ||
| 119 | default => 'Changes', | ||
| 120 | ); | ||
| 121 | |||
| 122 | #pod =attr update_changes_file | ||
| 123 | #pod | ||
| 124 | #pod Boolean for whether change log should be updated with the | ||
| 125 | #pod source distribution when built. Default is true. | ||
| 126 | #pod | ||
| 127 | #pod =cut | ||
| 128 | |||
| 129 | has update_changes_file => ( | ||
| 130 | is => 'ro', | ||
| 131 | isa => 'Bool', | ||
| 132 | default => 1, | ||
| 133 | ); | ||
| 134 | |||
| 135 | sub mvp_multivalue_args { qw(exclude_filename exclude_match) } | ||
| 136 | |||
| 137 | #pod =attr exclude_filename | ||
| 138 | #pod | ||
| 139 | #pod To exclude certain files from being gathered, use the C<exclude_filename> | ||
| 140 | #pod option. This may be used multiple times to specify multiple files to exclude. | ||
| 141 | #pod | ||
| 142 | #pod =cut | ||
| 143 | |||
| 144 | has exclude_filename => ( | ||
| 145 | is => 'ro', | ||
| 146 | isa => 'ArrayRef', | ||
| 147 | default => sub { [] }, | ||
| 148 | ); | ||
| 149 | |||
| 150 | #pod =attr exclude_match | ||
| 151 | #pod | ||
| 152 | #pod This is just like C<exclude_filename> but provides a regular expression | ||
| 153 | #pod pattern. Files matching the pattern are not gathered. This may be used | ||
| 154 | #pod multiple times to specify multiple patterns to exclude. | ||
| 155 | #pod | ||
| 156 | #pod =cut | ||
| 157 | |||
| 158 | has exclude_match => ( | ||
| 159 | is => 'ro', | ||
| 160 | isa => 'ArrayRef', | ||
| 161 | default => sub { [] }, | ||
| 162 | ); | ||
| 163 | |||
| 164 | #--------------------------------------------------------------------------# | ||
| 165 | # private | ||
| 166 | #--------------------------------------------------------------------------# | ||
| 167 | |||
| 168 | has _cpanidx => ( | ||
| 169 | is => 'ro', | ||
| 170 | isa => 'Str', | ||
| 171 | default => 'http://cpanidx.org/', | ||
| 172 | ); | ||
| 173 | |||
| 174 | has _distfile => ( | ||
| 175 | is => 'ro', | ||
| 176 | isa => 'Str', | ||
| 177 | lazy => 1, | ||
| 178 | builder => '_build_distfile', | ||
| 179 | ); | ||
| 180 | |||
| 181 | sub _build_distfile { | ||
| 182 | my ($self) = @_; | ||
| 183 | my $mod = $self->source_module; | ||
| 184 | my $uri = $self->_cpanidx . join( "/", qw/cpanidx json mod/, $mod ); | ||
| 185 | my $response = HTTP::Tiny->new->get( "$uri", { headers => { accept => '*' }, } ); | ||
| 186 | die "Could not find $mod via $uri: $response->{content}\n" | ||
| 187 | unless $response->{success}; | ||
| 188 | |||
| 189 | my $meta = decode_json( $response->{content} )->[0]; | ||
| 190 | |||
| 191 | return $meta->{dist_file}; | ||
| 192 | } | ||
| 193 | |||
| 194 | has _short_distfile => ( | ||
| 195 | is => 'ro', | ||
| 196 | isa => 'Str', | ||
| 197 | lazy => 1, | ||
| 198 | builder => '_build_short_distfile', | ||
| 199 | ); | ||
| 200 | |||
| 201 | sub _build_short_distfile { | ||
| 202 | my ($self) = @_; | ||
| 203 | my $distfile = $self->_distfile; | ||
| 204 | $distfile =~ s{./../}{}; | ||
| 205 | return $distfile; | ||
| 206 | } | ||
| 207 | |||
| 208 | has _files_added => ( | ||
| 209 | is => 'ro', | ||
| 210 | isa => 'HashRef', | ||
| 211 | default => sub { {} }, | ||
| 212 | ); | ||
| 213 | |||
| 214 | #--------------------------------------------------------------------------# | ||
| 215 | # methods | ||
| 216 | #--------------------------------------------------------------------------# | ||
| 217 | |||
| 218 | sub gather_files { | ||
| 219 | my ($self) = @_; | ||
| 220 | |||
| 221 | my $distfile = $self->_distfile | ||
| 222 | or die "Could not find distfile for " . $self->source_module . "\n"; | ||
| 223 | |||
| 224 | $self->log( [ 'Cloning files from %s', $self->_short_distfile ] ); | ||
| 225 | my $wd = tempd; | ||
| 226 | my $tarball = $self->_download($distfile); | ||
| 227 | my $ae = Archive::Extract->new( archive => $tarball ); | ||
| 228 | $ae->extract | ||
| 229 | or die "Couldn't unpack $tarball: " . $ae->error; | ||
| 230 | |||
| 231 | my ($extracted) = grep { -d } dir($wd)->children; | ||
| 232 | die "Couldn't find untarred folder for $tarball\n" | ||
| 233 | unless $extracted; | ||
| 234 | |||
| 235 | my $exclude_regex = qr/\000/; | ||
| 236 | $exclude_regex = qr/$exclude_regex|$_/ for ( @{ $self->exclude_match } ); | ||
| 237 | |||
| 238 | my %is_excluded = map { ; $_ => 1 } @{ $self->exclude_filename }; | ||
| 239 | |||
| 240 | FILE: for my $filename ( grep { -f } @{ $ae->files } ) { | ||
| 241 | my $file = file($filename)->relative($extracted); | ||
| 242 | next FILE if $file->basename =~ qr/^\./; | ||
| 243 | next FILE if grep { /^\.[^.]/ } $file->dir->dir_list; | ||
| 244 | next FILE unless $file =~ /^(?:lib|t|bin)\//; | ||
| 245 | next FILE if $file =~ $exclude_regex; | ||
| 246 | next FILE if $is_excluded{$file}; | ||
| 247 | $self->log_debug( [ 'selected %s', $filename ] ); | ||
| 248 | my $dz_file = $self->_file_from_filename( $filename, $file ); | ||
| 249 | $self->_munge_filename($dz_file); | ||
| 250 | $self->add_file($dz_file); | ||
| 251 | $self->_files_added->{ $dz_file->name } = 1; | ||
| 252 | } | ||
| 253 | |||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _munge_filename { | ||
| 258 | my ( $self, $file ) = @_; | ||
| 259 | |||
| 260 | my $old_name = $self->source_module; | ||
| 261 | my $new_name = $self->new_name; | ||
| 262 | s{::}{/}g for $new_name, $old_name; | ||
| 263 | |||
| 264 | ( my $new_filename = $file->name ) =~ s{$old_name}{$new_name}; | ||
| 265 | if ( $new_filename ne $file->name ) { | ||
| 266 | $self->log_debug( [ 'renaming %s to %s', $file->name, $new_filename ] ); | ||
| 267 | $file->name($new_filename); | ||
| 268 | } | ||
| 269 | return; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub _munge_file { | ||
| 273 | my ( $self, $file ) = @_; | ||
| 274 | |||
| 275 | my $old_name = $self->source_module; | ||
| 276 | my $new_name = $self->new_name; | ||
| 277 | |||
| 278 | ( my $old_pm = $old_name ) =~ s{::}{/}g; | ||
| 279 | $old_pm .= ".pm"; | ||
| 280 | ( my $new_pm = $new_name ) =~ s{::}{/}g; | ||
| 281 | $new_pm .= ".pm"; | ||
| 282 | |||
| 283 | $self->log_debug( [ 'updating contents of %s', $file->name ] ); | ||
| 284 | |||
| 285 | my $content = $file->content; | ||
| 286 | $content =~ s{$old_name}{$new_name}g; | ||
| 287 | $content =~ s{\Q$old_pm\E}{$new_pm}g; | ||
| 288 | $file->content($content); | ||
| 289 | } | ||
| 290 | |||
| 291 | # match a line that appears to assign to a $VERSION variable | ||
| 292 | # it's a bit more liberal on package names that Perl allows, oh, well | ||
| 293 | my $version_re = qr/\$(?:(?i)[a-z0-9]+::){0,}VERSION\s*=\s*/; | ||
| 294 | |||
| 295 | sub _strip_version { | ||
| 296 | my ( $self, $file ) = @_; | ||
| 297 | return unless $self->strip_version; | ||
| 298 | return | ||
| 299 | unless $file->name =~ m{\.pm\z} | ||
| 300 | or $file->content =~ /\A#!.*?perl/; | ||
| 301 | $self->log_debug( [ 'stripping VERSION from %s', $file->name ] ); | ||
| 302 | # replace | ||
| 303 | my @lines = | ||
| 304 | map { /$version_re/ ? '; # original $VERSION removed by Doppelgaenger' : $_ } | ||
| 305 | split "\n", | ||
| 306 | $file->content; | ||
| 307 | $file->content( join( "\n", @lines ) . "\n" ); | ||
| 308 | } | ||
| 309 | |||
| 310 | sub _strip_pod { | ||
| 311 | my ( $self, $file ) = @_; | ||
| 312 | return unless $self->strip_pod; | ||
| 313 | return unless $file->name =~ m{\.p(?:m|od)\z}; | ||
| 314 | $self->log_debug( [ 'stripping pod from %s', $file->name ] ); | ||
| 315 | my $p = Pod::Strip->new; | ||
| 316 | my $podless; | ||
| 317 | $p->output_string( \$podless ); | ||
| 318 | $p->parse_string_document( $file->content ); | ||
| 319 | $file->content($podless); | ||
| 320 | } | ||
| 321 | |||
| 322 | sub munge_file { | ||
| 323 | my ( $self, $file ) = @_; | ||
| 324 | $self->_munge_changes($file) if $file->name eq $self->changes_file; | ||
| 325 | return unless $self->_files_added->{ $file->name }; | ||
| 326 | $self->_munge_file($file); | ||
| 327 | $self->_strip_version($file); | ||
| 328 | $self->_strip_pod($file); | ||
| 329 | } | ||
| 330 | |||
| 331 | sub _munge_changes { | ||
| 332 | my ( $self, $file ) = @_; | ||
| 333 | return unless $self->update_changes_file; | ||
| 334 | |||
| 335 | my $content = $file->content; | ||
| 336 | my $distfile = $self->_short_distfile; | ||
| 337 | my $delim = $self->delim; | ||
| 338 | my $url = $self->_changes_url($distfile); | ||
| 339 | |||
| 340 | $content =~ s{ (\Q$delim->[0]\E \s* \$NEXT \s* \Q$delim->[1]\E) } | ||
| 341 | {$1\n\n - Generated from $distfile\n\n - See $url for details}xs; | ||
| 342 | |||
| 343 | $file->content($content); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub after_release { | ||
| 347 | my ($self) = @_; | ||
| 348 | return unless $self->update_changes_file && -e $self->changes_file; | ||
| 349 | |||
| 350 | my $file = Dist::Zilla::File::OnDisk->new( { name => $self->changes_file, } ); | ||
| 351 | $self->_munge_changes($file); | ||
| 352 | my $content = $file->content; | ||
| 353 | |||
| 354 | my $filename = $self->changes_file; | ||
| 355 | |||
| 356 | $self->log( [ 'updating contents of %s on disk', $filename ] ); | ||
| 357 | |||
| 358 | # and finally rewrite the changelog on disk | ||
| 359 | open my $out_fh, '>', $filename | ||
| 360 | or Carp::croak("can't open $filename for writing: $!"); | ||
| 361 | |||
| 362 | # Win32. | ||
| 363 | binmode $out_fh, ':raw'; | ||
| 364 | print $out_fh $content or Carp::croak("error writing to $filename: $!"); | ||
| 365 | close $out_fh or Carp::croak("error closing $filename: $!"); | ||
| 366 | } | ||
| 367 | |||
| 368 | sub _download { | ||
| 369 | my ( $self, $distfile ) = @_; | ||
| 370 | ( my $tarball = $distfile ) =~ s{\A.*/([^/]+)\z}{$1}; | ||
| 371 | my $uri = $self->cpan_mirror . join( "/", qw/authors id/, $distfile ); | ||
| 372 | my $response = HTTP::Tiny->new->mirror( "$uri", $tarball ); | ||
| 373 | die "Could not download $uri\n" | ||
| 374 | unless $response->{success}; | ||
| 375 | return $tarball; | ||
| 376 | } | ||
| 377 | |||
| 378 | my $archive_re = qr{\.tar\.gz|\.tgz|\.zip|\.tar\.bz|\.tbz}; | ||
| 379 | |||
| 380 | sub _changes_url { | ||
| 381 | my ( $self, $distfile ) = @_; | ||
| 382 | |||
| 383 | $distfile =~ s{$archive_re$}{}; | ||
| 384 | return "https://metacpan.org/changes/release/$distfile"; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub _file_from_filename { | ||
| 388 | my ( $self, $filename, $rel_name ) = @_; | ||
| 389 | |||
| 390 | open my $fh, "<:unix", "$filename"; | ||
| 391 | binmode $fh; | ||
| 392 | my $raw = do { local $/; <$fh> }; | ||
| 393 | close $fh; | ||
| 394 | |||
| 395 | my $file = Dist::Zilla::File::InMemory->new( | ||
| 396 | { | ||
| 397 | name => "$rel_name", | ||
| 398 | mode => ( stat $filename )[2] & 0755, ## no critic: kill world-writeability | ||
| 399 | encoded_content => $raw, | ||
| 400 | } | ||
| 401 | ); | ||
| 402 | return $file; | ||
| 403 | } | ||
| 404 | |||
| 405 | __PACKAGE__->meta->make_immutable; | ||
| 406 | 1; | ||
| 407 | |||
| 408 | |||
| 409 | # vim: ts=4 sts=4 sw=4 et: | ||
| 410 | |||
| 411 | __END__ |