]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo/Atom.pm
C::D::G::Atom->and ought to handle only one atom
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo / Atom.pm
1 package CPANPLUS::Dist::Gentoo::Atom;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 CPANPLUS::Dist::Gentoo::Version - Gentoo atom object.
9
10 =head1 VERSION
11
12 Version 0.08
13
14 =cut
15
16 our $VERSION = '0.08';
17
18 =head1 DESCRIPTION
19
20 This class models Gentoo atoms.
21
22 =cut
23
24 use Carp         ();
25 use Scalar::Util ();
26
27 use overload (
28  '<=>' => \&_spaceship,
29  'cmp' => \&_cmp,
30  '""'  => \&_stringify,
31 );
32
33 use CPANPLUS::Dist::Gentoo::Version;
34
35 my $version_rx = $CPANPLUS::Dist::Gentoo::Version::version_rx;
36
37 =head1 METHODS
38
39 =head2 C<< new category => $category, name => $name [, version => $version, range => $range, ebuild => $ebuild ] >>
40
41 Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object from the supplied C<$category>, C<$name>, C<$version>, C<$range> and C<$ebuild>.
42
43 =cut
44
45 sub new {
46  my $class = shift;
47  $class = ref($class) || $class;
48
49  my %args = @_;
50
51  my ($category, $name, $version);
52  if (defined $args{name}) {
53   ($category, $name, $version) = @args{qw/category name version/};
54   Carp::confess('Category unspecified') unless defined $category;
55   /[^\w-]/ and Carp::confess('Invalid argument') for $name, $category;
56  } elsif (defined $args{atom}) {
57   my $atom = $args{atom};
58   $atom =~ m{^([\w-]+)/([\w-]+)-v?($version_rx)$}
59                                                or Carp::confess('Invalid atom');
60   ($category, $name, $version) = ($1, $2, $3);
61  } else {
62   Carp::confess('Not enough information for building an atom object');
63  }
64
65  if (defined $version) {
66   unless (Scalar::Util::blessed($version)
67                               and $_->isa('CPANPLUS::Dist::Gentoo::Version')) {
68    $version = CPANPLUS::Dist::Gentoo::Version->new($version);
69   }
70  }
71
72  my $range = $args{range};
73  if (defined $version) {
74   if (defined $range) {
75    Carp::confess("Invalid range $range")
76                                       unless grep $range eq $_, qw|< <= = >= >|;
77   } else {
78    $range = '=';
79   }
80  } else {
81   Carp::confess('Range atoms require a valid version')
82                                             if defined $range and length $range;
83  }
84
85  bless {
86   category => $category,
87   name     => $name,
88   version  => $version,
89   range    => $range,
90   ebuild   => $args{ebuild},
91  }, $class;
92 }
93
94 =head2 C<new_from_ebuild $ebuild>
95
96 Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object by inferring the category, name and version from the given C<$ebuild>
97
98 =cut
99
100 sub new_from_ebuild {
101  my $class = shift;
102  $class = ref($class) || $class;
103
104  my $ebuild = shift;
105  $ebuild = '' unless defined $ebuild;
106
107  $ebuild =~ m{/([\w-]+)/([\w-]+)/\2-v?($version_rx)\.ebuild$}
108                                              or Carp::confess('Invalid ebuild');
109  my ($category, $name, $version) = ($1, $2, $3);
110
111  return $class->new(
112   category => $category,
113   name     => $name,
114   version  => $version,
115   ebuild   => $ebuild,
116  );
117 }
118
119 BEGIN {
120  eval "sub $_ { \$_[0]->{$_} }" for qw/category name version range ebuild/;
121 }
122
123 =head2 C<category>
124
125 Read-only accessor to the atom category.
126
127 =head2 C<name>
128
129 Read-only accessor to the atom name.
130
131 =head2 C<version>
132
133 Read-only accessor to the L<CPANPLUS::Dist::Gentoo::Version> object associated with the atom.
134
135 =head2 C<range>
136
137 Read-only accessor to the atom range.
138
139 =head2 C<ebuild>
140
141 Read-only accessor to the path of an optional ebuild associated with the atom.
142
143 =head2 C<qualified_name>
144
145 Returns the qualified name for the atom, i.e. C<$category/$name>.
146
147 =cut
148
149 sub qualified_name { join '/', $_[0]->category, $_[0]->name }
150
151 sub _spaceship {
152  my ($a1, $a2, $r) = @_;
153
154  my $v1 = $a1->version;
155
156  my $v2;
157  if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) {
158   Carp::confess('Can\'t compare atoms of different packages')
159                     if $a1->category ne $a2->category or $a1->name ne $a2->name;
160   $v2 = $a2->version;
161  } else {
162   $v2 = $a2;
163  }
164
165  ($v1, $v2) = ($v2, $v1) if $r;
166
167  return $v1 <=> $v2;
168 }
169
170 sub _cmp {
171  my ($a1, $a2, $r) = @_;
172
173  my $s1 = $a1->qualified_name;
174  my $v1 = $a1->version;
175  $s1   .= "-$v1" if defined $v1;
176
177  my $s2;
178  if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) {
179   $s2    = $a2->qualified_name;
180   my $v2 = $a2->version;
181   $s2   .= "-$v2" if defined $v2;
182  } else {
183   $s2 = $a2;
184  }
185
186  $s1 cmp $s2;
187 }
188
189 sub _stringify {
190  my ($a) = @_;
191
192  my $atom = $a->qualified_name;
193
194  my $version = $a->version;
195  $atom = $a->range . $atom . '-' . $version if defined $version;
196
197  return $atom;
198 }
199
200 my %order = (
201  '<'  => -2,
202  '<=' => -1,
203   '=' =>  0,
204  '>=' =>  1,
205  '>'  =>  2,
206 );
207
208 =head2 C<and @atoms>
209
210 Compute the ranged atom representing the logical AND between C<@atoms> with the same category and name.
211
212 =cut
213
214 sub and {
215  shift unless length ref $_[0];
216
217  my $a1 = shift;
218  return $a1 unless @_;
219
220  my $a2 = shift;
221  $a2 = $a2->and(@_) if @_;
222
223  my $p1 = $a1->qualified_name;
224  my $p2 = $a2->qualified_name;
225  Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2;
226
227  my $v1 = $a1->version;
228  return $a2 unless defined $v1;
229  my $r1 = $a1->range; # Defined if $v1 is defined
230
231  my $v2 = $a2->version;
232  return $a1 unless defined $v2;
233  my $r2 = $a2->range; # defined if $v2 is defined
234
235  my $o1 = $order{$r1};
236  my $o2 = $order{$r2};
237
238  Carp::confess("Incompatible ranges $r1$p1 and $r2$p2") if $o1 * $o2 < 0;
239
240  if ($r2 eq '=') {
241   ($a1, $a2) = ($a2, $a1);
242   ($v1, $v2) = ($v2, $v1);
243   ($r1, $r2) = ($r2, $r1);
244   ($o1, $o2) = ($o2, $o1);
245  }
246
247  if ($r1 eq '=') {
248   my $r = $r2 eq '=' ? '==' : $r2;
249   Carp::confess("Version mismatch $v1 $r $v2") unless eval "\$a1 $r \$a2";
250   return $a1;
251  } elsif ($o1 > 0) {
252   return $a1 < $a2 ? $a2 : $a1;
253  } else {
254   return $a1 < $a2 ? $a1 : $a2;
255  }
256 }
257
258 =head2 C<fold @atoms>
259
260 Returns a list built from C<@atoms> but where there's only one atom for a given category and name.
261
262 =cut
263
264 sub fold {
265  shift unless length ref $_[0];
266
267  my %seen;
268  for my $atom (@_) {
269   my $key = $atom->qualified_name;
270
271   my $cur = $seen{$key};
272   $seen{$key} = defined $cur ? $cur->and($atom) : $atom;
273  }
274
275  return values %seen;
276 }
277
278 =pod
279
280 This class provides overloaded methods for numerical comparison, string comparison and strigification.
281
282 =head1 SEE ALSO
283
284 L<CPANPLUS::Dist::Gentoo>, L<CPANPLUS::Dist::Gentoo::Version>.
285
286 =head1 AUTHOR
287
288 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
289
290 You can contact me by mail or on C<irc.perl.org> (vincent).
291
292 =head1 BUGS
293
294 Please report any bugs or feature requests to C<bug-cpanplus-dist-gentoo at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPANPLUS-Dist-Gentoo>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
295
296 =head1 SUPPORT
297
298 You can find documentation for this module with the perldoc command.
299
300     perldoc CPANPLUS::Dist::Gentoo
301
302 =head1 COPYRIGHT & LICENSE
303
304 Copyright 2009 Vincent Pit, all rights reserved.
305
306 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
307
308 =cut
309
310 1; # End of CPANPLUS::Dist::Gentoo::Atom