| 1 | package CPAN::Index::API::Role::Writable; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 7 | $CPAN::Index::API::Role::Writable::VERSION = '0.007'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # ABSTRACT: Writes index files | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 7 | ▶ | use File::Slurp qw(write_file read_file); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 7 | ▶ | use File::Basename qw(fileparse); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 7 | ▶ | use Path::Class qw(file dir); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 7 | ▶ | use Text::Template qw(fill_in_string); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 7 | ▶ | use Symbol qw(qualify_to_ref); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 7 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 7 | ▶ | use Compress::Zlib qw(gzopen $gzerrno); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 7 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 7 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 7 | requires 'default_location'; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 7 | has tarball_is_default => ( | |||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||
| 26 | isa => 'Bool', | ||||||||||||||||||||
| 27 | lazy_build => 1, | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 7 | has repo_path => ( | |||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||
| 32 | isa => 'Str', | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 7 | has template => ( | |||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||
| 37 | isa => 'Str', | ||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||
| 39 | lazy_build => 1, | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 7 | has content => ( | |||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||
| 44 | isa => 'Str', | ||||||||||||||||||||
| 45 | required => 1, | ||||||||||||||||||||
| 46 | lazy_build => 1, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub _build_template { | ||||||||||||||||||||
| 50 | 6 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_template: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | 6 | my $glob = qualify_to_ref("DATA", blessed $self); | |||||||||||||||||||
| 52 | 6 | return read_file($glob); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | sub _build_content { | ||||||||||||||||||||
| 56 | 7 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_content: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | 7 | my $content = fill_in_string( | |||||||||||||||||||
| 58 | $self->template, | ||||||||||||||||||||
| 59 | DELIMITERS => [ '[%', '%]' ], | ||||||||||||||||||||
| 60 | HASH => { self => \$self }, | ||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||
| 62 | 7 | chomp $content; | |||||||||||||||||||
| 63 | 7 | return $content; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub _build_tarball_is_default { | ||||||||||||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_tarball_is_default: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 68 | 0 | ▶ | return $self->default_location =~ /\.gz$/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub rebuild_content { | ||||||||||||||||||||
| 72 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
rebuild_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 73 | 1 | my $meta = (blessed $self)->meta; | |||||||||||||||||||
| 74 | 1 | $meta->get_attribute('content')->set_value($self, $self->_build_content); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub write_to_tarball { | ||||||||||||||||||||
| 78 | 1 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||
|
Subroutine
write_to_tarball: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 79 | 1 | my $file = $self->_prepare_file($filename, 1); | |||||||||||||||||||
| 80 | 1 | ▶ | my $gz = gzopen($file->stringify, 'wb') or croak "Cannot open $file: $gzerrno"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 1 | $gz->gzwrite($self->content); | |||||||||||||||||||
| 82 | 1 | ▶ | $gz->gzclose and croak "Error closing $file"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub write_to_file { | ||||||||||||||||||||
| 86 | 5 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||
|
Subroutine
write_to_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 87 | 5 | my $file = $self->_prepare_file($filename); | |||||||||||||||||||
| 88 | 5 | write_file($file, { err_mode => 'carp' }, $self->content); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | sub write_to_default_location { | ||||||||||||||||||||
| 92 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
write_to_default_location: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 93 | 0 | ▶ | $self->tarball_is_default | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | ? $self->write_to_tarball | ||||||||||||||||||||
| 95 | : $self->write_to_file; | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub _prepare_file { | ||||||||||||||||||||
| 99 | 6 | ▶ | my ( $self, $file, $is_tarball ) = @_; | ||||||||||||||||||
|
Subroutine
_prepare_file: called CC=6 SCAR=28.1
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 6 | ▶ | if ( defined $file ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 102 | 6 | $file = file($file); | |||||||||||||||||||
| 103 | ▶ | } elsif ( not defined $file and $self->repo_path ) { | |||||||||||||||||||
|
Condition0%0/3
not defined $file and $self->repo_path
MC/DC0%0/2
not defined $file and $self->repo_path
not defined $file
$self->repo_path
Truth table0%0/3
not(A) and B
A= not defined $fileB= $self->repo_path
| |||||||||||||||||||||
| 104 | 0 | my $location = $self->default_location; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # first normalize the extension | ||||||||||||||||||||
| 107 | 0 | $location =~ s/\.gz$//; | |||||||||||||||||||
| 108 | # then make sure we have it if we need a tarball | ||||||||||||||||||||
| 109 | 0 | ▶ | $location .= '.gz' if $is_tarball; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | $file = file( $self->repo_path, $location); | |||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||
| 113 | 0 | croak "Unable to write to file without a filename or repo path"; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 6 | ▶ | $file->dir->mkpath unless -e $file->dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 6 | return $file; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 7 | 1; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | CPAN::Index::API::Role::Writable - Writes index files | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 VERSION | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | version 0.007 | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | This role provides attributes and methods shared between classes that write | ||||||||||||||||||||
| 138 | index files. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 REQUIRES | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head2 default_location | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Class method that returns a string specifying the path to the default location | ||||||||||||||||||||
| 145 | of this file relative to the repository root. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head2 C<__DATA__> | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Consuming packages are expected to have a C<DATA> section that contains the | ||||||||||||||||||||
| 150 | template to use for generating the file contents. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 PROVIDES | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 tarball_is_default | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Required attribute. Boolean - indicates whether the file should be compressed | ||||||||||||||||||||
| 157 | by default. Automatically set to true if the file path in C<default_location> | ||||||||||||||||||||
| 158 | ends in C<.gz>. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head2 repo_path | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Optional attribute. Path to the repository root. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 template | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | Optional attribute. The template to use for generating the index files. The | ||||||||||||||||||||
| 167 | defalt is fetched from the C<DATA> section of the consuming package. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head2 content | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Optional attribute. The index file content. Built by default from the | ||||||||||||||||||||
| 172 | provided L</template>. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 rebuild_content | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | C<content> is a lazy read-only attribute which normally is built only once. | ||||||||||||||||||||
| 177 | Use C<rebuild_content> to generate a new value for C<content> if you've made | ||||||||||||||||||||
| 178 | changes to the list of packages. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head2 write_to_file | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | This method builds the file content if necessary and writes it to a file. A | ||||||||||||||||||||
| 183 | path to a file to write to can be passed as an argument, otherwise the default | ||||||||||||||||||||
| 184 | location will be used (a C<.gz> suffix, if it exists, will be removed). | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head2 write_to_tarball | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | This method builds the file content if necessary and writes it to a tarball. A | ||||||||||||||||||||
| 189 | path to a file to write to can be passed as an argument, otherwise the default | ||||||||||||||||||||
| 190 | location will be used. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head2 write_to_default_location | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | This method builds the file content if necessary and writes it to the default | ||||||||||||||||||||
| 195 | location. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head1 AUTHOR | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | Peter Shangov <pshangov@yahoo.com> | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | This software is copyright (c) 2012 by Venda, Inc.. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 206 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||
| 209 | |||||||||||||||||||||