summaryrefslogtreecommitdiff
path: root/lib/Carton/Index.pm
blob: 3ce215c557c359d4a2293ae80828ceb90623ca0a (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
package Carton::Index;
use strict;
use Class::Tiny {
    _packages => sub { +{} },
    generator => sub { require Carton; "Carton $Carton::VERSION" },
};

sub add_package {
    my($self, $package) = @_;
    $self->_packages->{$package->name} = $package; # XXX ||=
}

sub count {
    my $self = shift;
    scalar keys %{$self->_packages};
}

sub packages {
    my $self = shift;
    sort { lc $a->name cmp lc $b->name } values %{$self->_packages};
}

sub write {
    my($self, $fh) = @_;

    print $fh <<EOF;
File:         02packages.details.txt
URL:          http://www.perl.com/CPAN/modules/02packages.details.txt
Description:  Package names found in cpanfile.snapshot
Columns:      package name, version, path
Intended-For: Automated fetch routines, namespace documentation.
Written-By:   @{[ $self->generator ]}
Line-Count:   @{[ $self->count ]}
Last-Updated: @{[ scalar localtime ]}

EOF
    for my $p ($self->packages) {
        print $fh $self->_format_line($p->name, $p->version || 'undef', $p->pathname);
    }
}

sub _format_line {
    my($self, @row) = @_;

    # from PAUSE::mldistwatch::rewrite02
    my $one = 30;
    my $two = 8;

    if (length $row[0] > $one) {
        $one += 8 - length $row[1];
        $two = length $row[1];
    }

    sprintf "%-${one}s %${two}s  %s\n", @row;
}

sub pad {
    my($str, $len, $left) = @_;

    my $howmany = $len - length($str);
    return $str if $howmany <= 0;

    my $pad = " " x $howmany;
    return $left ? "$pad$str" : "$str$pad";
}


1;