File Coverage

blib/lib/Pinto/Target/Package.pm
Criterion Covered Total %
statement 53 59 89.8
branch 9 14 64.2
condition 6 8 75.0
subroutine 16 19 84.2
pod 4 7 57.1
total 88 107 82.2


line stmt bran cond sub pod time code
1             # ABSTRACT: Specifies a package by name and version
2              
3             package Pinto::Target::Package;
4              
5 56     56   52154 use Moose;
  56         369524  
  56         399  
6 56     56   353338 use MooseX::MarkAsMethods ( autoclean => 1 );
  56         17435  
  56         460  
7 56     56   183790 use MooseX::Types::Moose qw(Str);
  56         42782  
  56         542  
8              
9 56     56   253726 use Try::Tiny;
  56         166  
  56         3793  
10 56     56   2418 use Module::CoreList;
  56         71202  
  56         559  
11 56     56   16866 use CPAN::Meta::Requirements;
  56         119161  
  56         1950  
12              
13 56     56   759 use Pinto::Types qw(Version);
  56         134  
  56         495  
14 56     56   354926 use Pinto::Util qw(throw trim_text);
  56         130  
  56         3235  
15              
16 56     56   729 use version;
  56         128  
  56         416  
17 56     56   4704 use overload ( '""' => 'to_string');
  56         124  
  56         456  
18              
19             #------------------------------------------------------------------------------
20              
21             our $VERSION = '0.13'; # VERSION
22              
23             #------------------------------------------------------------------------------
24              
25             has name => (
26             is => 'ro',
27             isa => Str,
28             required => 1,
29             );
30              
31             has version => (
32             is => 'ro',
33             isa => Str | Version,
34             default => '0',
35             coerce => 1,
36             );
37              
38             has _vreq => (
39             is => 'ro',
40             isa => 'CPAN::Meta::Requirements',
41             writer => '_set_vreq',
42             init_arg => undef,
43             );
44              
45             #------------------------------------------------------------------------------
46              
47             around BUILDARGS => sub {
48             my $orig = shift;
49             my $class = shift;
50              
51             my @args = @_;
52              
53             if ( @args == 1 and not ref $args[0] ) {
54              
55             throw "Invalid package specification: $_[0]"
56             unless $_[0] =~ m{^ ([A-Z0-9_:]+) (?:~)? (.*)}ix;
57              
58             my ($name, $version) = ($1, $2);
59             $version =~ s/^\@/==/; # Allow "@" as a synonym for "=="
60             @args = ( name => $name, version => trim_text($version) || 0 );
61             }
62              
63             return $class->$orig(@args);
64             };
65              
66             #------------------------------------------------------------------------------
67              
68             sub BUILD {
69 170     170 0 383 my $self = shift;
70              
71             # We want to construct the C::M::Requirements object right away to ensure
72             # $self->version is a valid string. But if we do this in a builder, it
73             # has to be lazy because it depends on other attributes. So instead, we
74             # construct it during the BUILD and use a private writer to set it.
75              
76 170         4566 my $args = {$self->name => $self->version};
77              
78 170     170   9813 my $req = try { CPAN::Meta::Requirements->from_string_hash( $args) }
79 170     0   1950 catch { throw "Invalid package target ($self): $_" };
  0         0  
80              
81 170         44410 $self->_set_vreq($req);
82 170         4296 return $self;
83             }
84              
85             #------------------------------------------------------------------------------
86              
87              
88             sub is_core {
89 105     105 1 1253 my ( $self, %args ) = @_;
90              
91             ## no critic qw(PackageVar);
92              
93             # Note: $PERL_VERSION is broken on old perls, so we must make
94             # our own version object from the old $] variable
95 105   66     2370 my $pv = version->parse( $args{in} ) || version->parse($]);
96              
97             # If it ain't in here, it ain't in the core
98 105         2652 my $core_modules = $Module::CoreList::version{ $pv->numify + 0 };
99 105 50       410 throw "Invalid perl version $pv" if not $core_modules;
100 105 100       3085 return 0 if not exists $core_modules->{ $self->name };
101              
102             # We treat deprecated modules as if they have already been removed
103 12         121 my $deprecated_modules = $Module::CoreList::deprecated{ $pv->numify + 0 };
104 12 100 100     195 return 0 if $deprecated_modules && exists $deprecated_modules->{ $self->name };
105              
106             # on some perls, we'll get an 'uninitialized' warning when
107             # the $core_version is undef. So force to zero in that case
108 11   50     268 my $core_version = $core_modules->{ $self->name } || 0;
109              
110 11 100       48 return 1 if $self->is_satisfied_by( $core_version );
111 2         117 return 0;
112             }
113              
114             #-------------------------------------------------------------------------------
115              
116              
117             sub is_perl {
118 64     64 1 881 my ($self) = @_;
119              
120 64         1674 return $self->name eq 'perl';
121             }
122              
123             #-------------------------------------------------------------------------------
124              
125              
126             sub is_satisfied_by {
127 147     147 1 3895 my ($self, $version) = @_;
128              
129 147         3893 return $self->_vreq->accepts_module($self->name => $version);
130             }
131              
132             #-------------------------------------------------------------------------------
133              
134             sub unversioned {
135 0     0 0 0 my ($self) = @_;
136              
137 0         0 return (ref $self)->new(name => $self->name);
138             }
139              
140             #-------------------------------------------------------------------------------
141              
142              
143             sub to_string {
144 1065     1065 1 5458 my ($self) = @_;
145 1065 100       26997 my $format = $self->version =~ m/^ [=<>!\@] /x ? '%s%s' : '%s~%s';
146 1065         25880 return sprintf $format, $self->name, $self->version;
147             }
148              
149             #------------------------------------------------------------------------------
150             # XXX Are we using this?
151              
152             sub gte {
153 0     0 0   my ($self, $other, $flip) = @_;
154 0 0         return $self->is_satisfied_by($other) if not $flip;
155 0 0         return $other->is_satisfied_by($self) if $flip;
156             }
157              
158             #------------------------------------------------------------------------------
159              
160             __PACKAGE__->meta->make_immutable;
161              
162             #------------------------------------------------------------------------------
163             1;
164              
165             __END__
166              
167             =pod
168              
169             =encoding UTF-8
170              
171             =for :stopwords Jeffrey Ryan Thalhammer
172              
173             =head1 NAME
174              
175             Pinto::Target::Package - Specifies a package by name and version
176              
177             =head1 VERSION
178              
179             version 0.13
180              
181             =head1 METHODS
182              
183             =head2 is_core
184              
185             =head2 is_core(in => $version)
186              
187             Returns true if this Target is satisfied by the perl core as-of a particular
188             version. If the version is not specified, it defaults to whatever version you
189             are using now.
190              
191             =head2 is_perl()
192              
193             Returns true if this Target is a perl version of perl itself.
194              
195             =head2 is_satisfied_by($version)
196              
197             Returns true if this Target is satisfied by version C<$version> of the package.
198              
199             =head2 to_string()
200              
201             Serializes this Target to its string form. This method is called whenever the
202             Target is evaluated in string context.
203              
204             =head1 AUTHOR
205              
206             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
211              
212             This is free software; you can redistribute it and/or modify it under
213             the same terms as the Perl 5 programming language system itself.
214              
215             =cut