summaryrefslogtreecommitdiffabout
path: root/lib/SlackBuild/Request/Auto.pm
blob: 576d39d8550009c80e7df14ea4b7c9e090ff6a5b (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
package SlackBuild::Request::Auto;
use strict;
use warnings;
use parent 'SlackBuild::Request';
use File::Basename;
use File::Spec;
use Net::SBo;
use JSON;
use YAML;
use Carp;

sub req {
    my ($class, $reqname) = @_;
    my $req;
    
    if (-f $reqname) {
	local $/ = undef;
	open(my $fd, $reqname) or croak "can't open file $reqname: $!";
	my $string = <$fd>;
	close $fd;
	if ($string =~ /^\{/) {
	    return decode_json($string);
	}
	return YAML::Load($string);
    }

    if (-d $reqname) {
	if (my $file =
             (glob File::Spec->catfile($reqname, '*.SlackBuild'))[0]) {
	    my ($package,$path,$suffix) = fileparse($file, '.SlackBuild');
	    return { package => $package, slackbuild_uri => $path };
	}
    }

    if ($reqname =~ m{^\w+://}) {
	my $uri = new URI($reqname);
	if ($uri->scheme =~ m{^(?:http|ftp)s?}
	    && $uri->path =~ m{.*/(.+?)\.tar(?:\.(?:[xgl]z|bz2))?}x) {
	    return { package => $1, slackbuild_uri => $reqname };
	}
	if ($uri->scheme eq 'sbo') {
	    return { package => $uri->package, slackbuild_uri => $reqname }
	}
    }

    if (my ($dir,$commit) = Net::SBo->new->find($reqname)) {
	return { package => $reqname, slackbuild_uri => "sbo://$commit/$dir"};
    }
    
    croak "unrecognized request type";
}

sub new {
    my ($class, $arg) = @_;
    return $class->SUPER::new(__PACKAGE__->req($arg));
}

1;
__END__

=head1 NAME

SlackBuild::Request::Auto - automatic request convertor for SlackBuilder

=head1 SYNOPSIS

    $req = new SlackBuild::Request::Auto($arg)

=head1 DESCRIPTION

Attempts to recognize the format of I<$arg> and convert it to the SlackBuilder
build request.

Argument can be any of:

=over 4

=item Name of an existing file

The file is read and parsed as a JSON request file.

=item Name of an existing directory

If it contains a file B<*.SlackBuild>, a request referring to files
in this directory is returned.

=item A http, https, ftp, or ftps URL to a tar file

The file component of the URL must end with B<.tar>, followed with a
compression suffix (B<.gz>, B<.xz>, or B<.bz2>). The archive must contain
at least the B<*.SlackBuild> file.    
    
Example:

    https://slackbuilds.org/slackbuilds/14.2/system/mailutils.tar.gz
    
=item An SBo URL

Example:

    sbo://HEAD/system/mailutils

=back

=cut

Return to:

Send suggestions and report system problems to the System administrator.