summaryrefslogtreecommitdiff
path: root/lib/SlackBuild/Registry/Record.pm
blob: 81a705c1fe225d252fa3f196f7b408726b806d98 (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
package SlackBuild::Registry::Record;
use strict;
use warnings;
use Carp;
use SlackBuild::Base qw(package arch build date filename);
use SlackBuild::Registry::Version;
use Scalar::Util qw(blessed);
use List::Regexp;
use File::Basename;

=head2 new

    $x = new SlackBuild::Registry::Record(PACKAGE,
                     arch=>X, version=>Y, build=>Z, date=>D, filename=>F)
    
=cut

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->build(1);
    if (my $v = shift) {
	$self->package($v);
	if (@_) {
	    croak "bad number of arguments" if (@_ % 2);
	    local %_ = @_;
	    while (my ($k,$v) = each %_) {
		$self->${\$k}($v);
	    }
	}
    }
    return $self;
}

my @architectures = qw(i386 x86_64 arm noarch);

my $rx = '^(?<pkg>.+)-'
	   . '(?<vpfx>.*?)'
	   . '(?<version>\d+(?:\.\d+)*.*?)'
	   . '-(?<arch>' . regexp_opt(@architectures) . ')'
	   . '-(?<build>\d+)(?<rest>[[:punct:]].*)?$';

=head2 split

    $r = SlackBuild::Registry::Record->split($filename, %opt)

=cut    

sub split {
    my ($class, $filename, %opt) = @_;
    if (basename($filename) =~ m{$rx}) {
	return $class->new($+{pkg},
			   version => $+{version},
			   arch => $+{arch},
			   build => $+{build},
			   filename => $filename,
			   %opt);
    }
}

sub version {
    my $self = shift;
    if (@_) {
	croak "too many arguments" if @_ > 1;
	$self->{version} = new SlackBuild::Registry::Version(shift);
    }
    return $self->{version};
}

sub store {
    my $self = shift;
    croak "store not implemented";
}

sub as_string {
    my $self = shift;
    return $self->package . '-'
	   . ($self->version ? $self->version : '*') . '-'
	   . ($self->arch ? $self->arch : '*') . '-'
	   . ($self->build || '1');
}

sub cmp {
    my ($self, $other) = @_;

    my $v;
    if ($v = ($self->package || '') cmp ($other->package || '')) {
	return $v;
    }
    if ($self->version <=> $other->version) {
	return $v;
    }
    if ($self->arch && $other->arch
	&& ($v = $self->arch cmp $other->arch)) {
	return $v;
    }
    return ($self->build || 1) <=> ($other->build || 1);
}   

use overload
    '""' => sub { shift->as_string },
    'cmp' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other);
	return $swap ? -$res : $res;
    },
    '<=>' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other);
	return $swap ? -$res : $res;
    },
    '==' => sub {
	my ($self, $other) = @_;
	my $res = $self->cmp($other) == 0;
    },
    '!=' => sub {
	my ($self, $other) = @_;
	my $res = $self->cmp($other) != 0;
    },
    '<' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) < 0;
	return $swap ? !$res : $res;
    },
    '<=' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) <= 0;
	return $swap ? !$res : $res;
    },
    '>' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) > 0;
	return $swap ? !$res : $res;
    },
    '>=' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) >= 0;
	return $swap ? !$res : $res;
    };

1;

Return to:

Send suggestions and report system problems to the System administrator.