| 1 | package P5U::Lib::Reprove; | ||
| 2 | |||
| 3 | BEGIN { | ||
| 4 | $P5U::Lib::Reprove::AUTHORITY = 'cpan:TOBYINK'; | ||
| 5 | $P5U::Lib::Reprove::VERSION = '0.100'; | ||
| 6 | }; | ||
| 7 | |||
| 8 | use 5.010; | ||
| 9 | use autodie; | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | use App::Prove qw//; | ||
| 13 | use Class::Load qw/load_class/; | ||
| 14 | use Carp qw/confess/; | ||
| 15 | use JSON qw/from_json/; | ||
| 16 | use File::pushd qw/pushd/; | ||
| 17 | use File::Temp qw//; | ||
| 18 | use Module::Info qw//; | ||
| 19 | use Path::Tiny qw//; | ||
| 20 | use LWP::Simple qw/get/; | ||
| 21 | use Module::Manifest qw//; | ||
| 22 | use Object::AUTHORITY qw/AUTHORITY/; | ||
| 23 | use Types::Standard qw< ArrayRef Bool Str >; | ||
| 24 | use Type::Utils qw< class_type >; | ||
| 25 | |||
| 26 | has author => ( | ||
| 27 | is => 'lazy', | ||
| 28 | isa => Str, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has release => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => Str, | ||
| 34 | required => 1, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has version => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => Str, | ||
| 40 | required => 1, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has manifest => ( | ||
| 44 | is => 'lazy', | ||
| 45 | isa => ArrayRef, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has testdir => ( | ||
| 49 | is => 'lazy', | ||
| 50 | isa => class_type { class => 'Path::Tiny' }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has working_dir => ( | ||
| 54 | is => 'lazy', | ||
| 55 | isa => class_type { class => 'Path::Tiny' }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | has verbose => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => Bool, | ||
| 61 | required => 1, | ||
| 62 | default => sub { 0 }, | ||
| 63 | ); | ||
| 64 | |||
| 65 | sub BUILDARGS | ||
| 66 | { | ||
| 67 | my ($class, @args) = @_; | ||
| 68 | |||
| 69 | my %args; | ||
| 70 | if (@args == 1 and ref $args[0]) | ||
| 71 | { | ||
| 72 | %args = %{ $args[0] } | ||
| 73 | } | ||
| 74 | elsif (scalar(@args) % 2 == 0) | ||
| 75 | { | ||
| 76 | %args = @args; | ||
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | confess "Called with the wrong number of arguments."; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (defined $args{module} and not defined $args{version}) | ||
| 84 | { | ||
| 85 | $args{version} = Module::Info::->new_from_module($args{module})->version; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (defined $args{module} and not defined $args{author}) | ||
| 89 | { | ||
| 90 | load_class($args{module}); | ||
| 91 | if ($args{module}->can('AUTHORITY')) | ||
| 92 | { | ||
| 93 | ($args{author}) = | ||
| 94 | map { s/^cpan://; $_ } | ||
| 95 | grep { /^cpan:/ } | ||
| 96 | ($args{module}->AUTHORITY); | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | no strict 'refs'; | ||
| 101 | my $auth = ${$args{module}.'::AUTHORITY'}; | ||
| 102 | if (defined $auth and $auth =~ /^cpan:(.+)$/) | ||
| 103 | { | ||
| 104 | $args{author} = $1; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | if (defined $args{module} and not defined $args{release}) | ||
| 110 | { | ||
| 111 | my $d = from_json(get(sprintf('http://api.metacpan.org/v0/module/%s', $args{module}))); | ||
| 112 | $args{release} = $d->{distribution}; | ||
| 113 | $args{author} //= $d->{author}; | ||
| 114 | } | ||
| 115 | |||
| 116 | if (defined $args{release} and not defined $args{author}) | ||
| 117 | { | ||
| 118 | my $d = from_json(get(sprintf('http://api.metacpan.org/v0/release/%s', $args{release}))); | ||
| 119 | $args{author} //= $d->{author}; | ||
| 120 | } | ||
| 121 | |||
| 122 | delete $args{module}; | ||
| 123 | $class->SUPER::BUILDARGS(%args); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _url_for | ||
| 127 | { | ||
| 128 | my ($self, $file) = @_; | ||
| 129 | sprintf( | ||
| 130 | 'http://api.metacpan.org/source/%s/%s-%s/%s', | ||
| 131 | uc $self->author, | ||
| 132 | $self->release, | ||
| 133 | $self->version, | ||
| 134 | $file, | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _getfile_to_handle | ||
| 139 | { | ||
| 140 | my ($self, $file, $fh) = @_; | ||
| 141 | print $fh get($self->_url_for($file)); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub test_files | ||
| 145 | { | ||
| 146 | my $self = shift; | ||
| 147 | grep { m{^t/} } @{ $self->manifest }; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _build_author | ||
| 151 | { | ||
| 152 | my $self = shift; | ||
| 153 | my $d = from_json(get( | ||
| 154 | sprintf('http://api.metacpan.org/v0/release/%s', $self->release) | ||
| 155 | )); | ||
| 156 | $d->{author}; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _build_manifest | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | my $fh = $self->working_dir->child('MANIFEST')->openw; | ||
| 163 | binmode( $fh, ":utf8"); | ||
| 164 | $self->_getfile_to_handle('MANIFEST', $fh); | ||
| 165 | close $fh; | ||
| 166 | |||
| 167 | my $manifest = Module::Manifest->new; | ||
| 168 | $manifest->open(manifest => $self->working_dir->child('MANIFEST')->stringify); | ||
| 169 | return [ $manifest->files ]; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _build_testdir | ||
| 173 | { | ||
| 174 | my $self = shift; | ||
| 175 | my $testdir = $self->working_dir->child('t'); | ||
| 176 | $testdir->mkpath; | ||
| 177 | |||
| 178 | foreach my $file ($self->test_files) | ||
| 179 | { | ||
| 180 | my $dest = $testdir->child($file); | ||
| 181 | Path::Tiny::->new($dest->dirname)->mkpath; | ||
| 182 | $self->_getfile_to_handle($file, $dest->openw); | ||
| 183 | } | ||
| 184 | |||
| 185 | return $testdir; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub _build_working_dir | ||
| 189 | { | ||
| 190 | my $self = shift; | ||
| 191 | Path::Tiny::->tempdir; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _app_prove_args | ||
| 195 | { | ||
| 196 | 't'; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub run | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | printf("Reproving %s/%s (%s)\n", $self->release, $self->version, uc $self->author); | ||
| 203 | printf("Using temp dir '%s'\n", $self->testdir) if $self->verbose; | ||
| 204 | my $chdir = pushd($self->testdir); | ||
| 205 | my $app = App::Prove->new; | ||
| 206 | $app->process_args($self->_app_prove_args); | ||
| 207 | $app->verbose(1) if $self->verbose; | ||
| 208 | $app->run; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ |