summaryrefslogtreecommitdiffabout
path: root/lib/SlackBuild/Rc.pm
blob: 022386a78bb17214a90835832b4d9abd91bb64c6 (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
package SlackBuild::Rc;
use strict;
use warnings;
use Carp;
use SlackBuild::Registry::Record;
use SlackBuild::Registry::Pattern;

sub new {
    my ($class, $builder) = @_;
    return bless { _builder => $builder }, $class;
}

sub builder { shift->{_builder} }

sub resolve {
    my ($self, $reg) = @_;
    my @packages;
    my @unresolved;
    foreach my $pkg (@{$self->builder->prereq}) {
	my %q;
	if (ref($pkg) eq 'HASH') {
	    %q = %$pkg;
	} else {
	    $q{package} = $pkg;
	}
	$q{arch} = { -in => [ $self->builder->arch, 'noarch' ] }
	             unless exists $q{arch};
	my $pred = new SlackBuild::Registry::Pattern(%q);
	if (my $rec = $reg->lookup($pred)) {
	    push @packages, $rec->filename;
	} else {
	    push @unresolved, $pred->as_string;
	}
    }
    return (\@packages, \@unresolved);
}

sub code {
    my ($self, $reg) = @_;
    my ($pkglist, $unresolved) = $self->resolve($reg);
    if (@$unresolved) {
	$self->builder->error("$_: package not resolved") for (@$unresolved);
	return;
    }
    my @code = ('#! /bin/sh',
		'set -e',
		'(. /etc/os-release; echo SLACKBUILDER: DISTRO $NAME; echo SLACKBUILDER: VERSION $VERSION)',
		map { "/sbin/upgradepkg --install-new /var/pkg/$_" } @$pkglist);

    my $env = $self->builder->environ;
    while (my ($k,$v) = each %$env) {
	next unless defined($v);
	$v =~ s/[`"]/\\"/g;
	push @code, "$k=\"$v\"", "export $k";
    }

    if (my $rc = $self->builder->rc) {
	if (ref($rc) eq 'ARRAY') {
	    push @code, @$rc;
	} else {
	    push @code, split /\n+/, $rc;
	}
    }
    
    push @code, 'exec /bin/sh $@';
    return \@code;
}

sub write {
    my ($self, $reg, $file) = @_;
    my $code = $self->code($reg) or return;
    open(my $fd, '>', $file)
	or croak "can't open \"$file\" for writing: $!";
    print $fd join("\n", @$code)."\n";
    unless (chmod(0755, $fd)) {
	croak "can't chmod \"$file\": $!";
    }
    close $fd;
    return 1;
}

1;

Return to:

Send suggestions and report system problems to the System administrator.