summaryrefslogtreecommitdiff
blob: a6bc9fb1c83216d8ac01d93310afc438d12ac71e (plain)
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
#!/usr/bin/env perl 

eval 'echo "Called with something not perl"' && exit 1    # Non-Perl protection.
  if 0;

use 5.14.2;
use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";

use env::gentoo::perl_experimental;
use metacpan qw( mcpan );
use Term::ANSIColor qw( :constants );
use Try::Tiny;
use coloriterator
  coloriser => { -as => 'author_colour' },
  coloriser => { -as => 'dist_colour' };

# FILENAME: pvlist.pl
# CREATED: 16/10/11 20:16:03 by Kent Fredric (kentnl) <kentfredric@gmail.com>
# ABSTRACT: Show version history for interesting perl dists

# DEPENDENCIES:
#
# * MetaCPAN::API
# * CHI
# * WWW::Mechanize::Cached
# * HTTP::Tiny::Mech
# * Data::Dump
# * Term::ANSIColor
# * Gentoo::PerlMod::Version
# * CPAN::Changes
#

my $flags;
my $singleflags;

@ARGV = grep { defined } map {
  $_ =~ /^--(\w+)/
    ? do { $flags->{$1}++; undef }
    : do {
    $_ =~ /^-(\w+)/
      ? do { $singleflags->{$1}++; undef }
      : do { $_ }
    }
} @ARGV;

if ( $flags->{help} or $singleflags->{h} ) { print help(); exit 0; }

my $oldest_date = '2011-10-01T00:00:00.000Z';
my $newest_date = '2012-02-01T00:00:00.000Z';

my $search = {};

my $and = [];

if ( not $flags->{all} ) {
    push @{$and}, {
        range => {
            date => {
                from => $oldest_date,
                to   => $newest_date,
            }
        }
    };
}

push @{$and} , {
    term => {
    'distribution'  => @ARGV,
#     minimum_match => 1,
    }
};

$search->{query} = {
     constant_score => {
         filter => {
              and => $and,
         }
     }
};

$search->{sort} = [

  #   { 'author' => 'asc', },
  { 'date' => 'desc', },
];
$search->{size} = 10;

$search->{fields} = [qw( author name date distribution version )];

if ( $flags->{deps} ) {
  push @{ $search->{fields} }, '_source.dependency';
}


_log( ['initialized: fetching search results'] );

my $results = mcpan->post( 'release/_search', $search );

_log( [ 'fetched %s results', scalar @{ $results->{hits}->{hits} } ] );

for my $result ( @{ $results->{hits}->{hits} } ) {

  #  use Data::Dump qw(pp);
  #  pp $result;
  say $_ for format_result( $result->{fields}, $flags );
}

exit 0;

# Utils

sub ac { return author_colour( $_[0] ) . $_[0] . RESET }
sub dc { return dist_colour( $_[0] ) . $_[1] . RESET }
sub pp { require Data::Dump; goto \&Data::Dump::pp }
sub gv { require Gentoo::PerlMod::Version; goto \&Gentoo::PerlMod::Version::gentooize_version }

sub _log {
  return unless $flags->{trace};
  return *STDERR->print(@_) if ( not ref $_[0] );

  state $prefix = "\e[7m* package_log.pl:\e[0m ";

  my ( $str, @args ) = @{ $_[0] };
  $str =~ s/\n?$/\n/;

  *STDERR->print($prefix);
  *STDERR->printf( $str, @args );
  return;

}

sub format_result {

  my %f = %{ $_[0] };
  my %opts = %{ $_[1] || {} };

  _log( [ 'formatting entry for %s', $f{name} ] );

  my @out;

  push @out, entry_heading( @f{qw( date author distribution name version)} );

  my $name   = $f{name};
  my $author = $f{author};

  if ( $opts{deps} ) {
    my $deps = $f{'_source.dependency'};
    _log( [ 'processing %s deps for %s', scalar @{$deps}, $f{name} ] );
    push @out, sort map { dep_line($_) } @{$deps};
  }
  if ( $opts{changes} ) {
    _log( [ 'processing changes deps for %s', $name ] );
  }
  if ( $opts{changes} and my $message = change_for( $author, $name ) ) {
    push @out, "\e[1;38m" . $message . "\e[0m";
  }
  return @out;
}

sub entry_heading {
  my ( $date, $author, $distribution, $name, $version ) = @_;
  state $date_style     = UNDERLINE . CYAN;
  state $gentoo_version = BOLD . CYAN;
  return sprintf "%s - %s/%s %s",
    $date_style . $date . RESET,
    ac($author),
    dc( $distribution, $name ),
    $gentoo_version . gv( $version, { lax => 1 } ) . RESET;
}

sub dep_line {
  my ($dep) = @_;
  state $gentoo_version = BOLD . CYAN;
  my $rel = ( $dep->{relationship} ne 'requires' ? BRIGHT_BLUE . $dep->{relationship} : q[] );
  my $phase = ( $dep->{phase} eq 'develop' ? BRIGHT_GREEN : q[] ) . $dep->{phase};
  my $version = $gentoo_version . gv( $dep->{version}, { lax => 1 } ) . RESET;
  return sprintf "%s %s: %s %s %s", $rel, $phase, $dep->{module}, $dep->{version}, $version;
}

sub change_for {
  my ( $author, $release ) = @_;
  my $file;
  my @trylist = qw( Changes CHANGES ChangeLog );
  my @errors;

  my $success;

  for my $basename (@trylist) {
    try {
      _log( [ 'trying %s for %s', $basename, $release ] );
      $file = mcpan->source(
        author  => $author,
        release => $release,
        path    => $basename,
      );
      $success = $basename;
    }
    catch {
      $success = 0;
      _log( [ 'failed with %s for %s : %s', $basename, $release, $_ ] );
      push @errors, $_;
    };
    last if $success;
  }
  if ( !$success ) {
    _log( [ 'no changes file %s ', $release ] );
    warn for @errors;
  }

  return unless $file;

  if ( $flags->{'nosummarize'} ) {
    return $file;
  }

  require CPAN::Changes;
  my $changes = CPAN::Changes->load_string($file);
  if ($changes) {
    my @releases = $changes->releases();
    return $releases[-1]->serialize() if @releases;
    _log( [ 'No releases reported by CPAN::Changes for file %s on %s', $success, $release ] );

    #warn "No releases :( ";
  }

  #warn "Cant load \$file with CPAN::Changes";
  my @out = split /$/m, $file;
  return join qq{\n}, splice @out, 0, 10;

}

sub help {
  return <<"EOF";
package_log.pl

USAGE:

  package_log.pl PACKAGE [PACKAGE*] [--all] [--help] [--changes] [--deps] [--trace]

  ie:

  # Just show the recent log for Moose, Catalyst-Runtime and Dist-Zilla
  package_log.pl Moose Catalyst-Runtime Dist-Zilla

  # show all log events for Moose
  package_log.pl Moose --all

  # show recent Moose log events with attached changelog data and dependenices
  package_log.pl Moose --changes --deps

  # Be verbose about what we're doing
  package_log.pl Moose --trace --all

    --all         Show all releases in the log.
    --help        Show this message
    --changes     Show ChangeLog Excerpts using CPAN::Changes where possible
    --deps        Show Dependency data ( as reported via metadata )
    --trace       Turn on extra debugging.
    --nosummarize Do no processing of Changes data and report it verbatim
                  ( Useful when CPAN::Changes gets it wrong :( )
EOF

}