summaryrefslogtreecommitdiff
path: root/lib/SlackBuild/Request/Loader/file.pm
blob: 6db35a00492f95e0e215d78e6b93c5680619201e (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
# This file is part of slackbuilder
# Copyright (C) 2017-2021 Sergey Poznyakoff
#
# Slackbuilder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Slackbuilder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with slackbuilder.  If not, see <http://www.gnu.org/licenses/>. */

package SlackBuild::Request::Loader::file;
use strict;
use warnings;
use JSON;
use YAML ();
use Carp;
use Text::ParseWords;
use File::Spec;
use File::Basename;
use parent 'SlackBuild::Request::Loader';

our $PRIORITY = 10;

my @PATH;
my @SUFFIXES = qw(.yml .yaml);

sub Configure {
    my $pkg = shift;
    local %_ = @_;
    if (my $v = delete $_{path}) {
	@PATH = parse_line('\s+', 0, $v);
    }
    if (my $v = delete $_{suffixes}) {
	@SUFFIXES = parse_line('\s+', 0, $v);
    }
    $pkg->SUPER::Configure(%_);
}

sub Load {
    my ($class, $reqname) = @_;
    my @c = File::Spec->splitdir($reqname);
    my ($filename,$dir,$suffix) = fileparse($reqname, @SUFFIXES);	
    my @searchpath = (@c == 1) ? @PATH : ( $dir );
    my @suffixes = $suffix ? ( $suffix ) : @SUFFIXES;
    foreach my $file (map { my $filename = File::Spec->catfile($_, $filename);
			    map { $filename . $_ } @suffixes } @searchpath) {
	if (-f $file) {
	    local $/ = undef;
	    open(my $fd, $file) or croak "can't open file $file: $!";
	    my $string = <$fd>;
	    close $fd;
	    if ($string =~ /^\{/) {
		return decode_json($string);
	    }
	    return YAML::Load($string);
	}
    }
}   

1;

Return to:

Send suggestions and report system problems to the System administrator.