makepkg-template: support multiple --template-dirs

Especially when maintaining local templates in addition to the ones
stored in /usr/share/makepkg-template, it can be useful to include
templates stored in multiple different locations into one PKGBUILD. This
patch makes this possible by allowing --template-dir to be specified
multiple times.

This also introduces a dedicated error message when a template cannot be
found, in contrast to the already existing "Couldn't detect version for
template '%s'".

If a template of the same name is present in more than one of the given
directories, the last one always takes precedence.

Neither the default behaviour without the option given, nor the handling
of a single template dir is changed.

Signed-off-by: Dominik Fischer <d.f.fischer@web.de>
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Dominik Fischer 2015-04-23 16:41:46 +02:00 committed by Allan McRae
parent d4e5de4bf6
commit b0ae59724e
14 changed files with 81 additions and 22 deletions

View File

@ -72,7 +72,9 @@ Options
Always use the newest available template file.
*\--template-dir* <dir>::
Change the dir where we are looking for template files.
Change the dir where we are looking for template files. This option may be
given multiple times in which case files found in directory given last will
take precedence.
Example PKGBUILD

View File

@ -27,7 +27,7 @@ use Module::Load::Conditional qw(can_load);
my %opts = (
input => '@BUILDSCRIPT@',
template_dir => '@TEMPLATE_DIR@',
template_dir => ['@TEMPLATE_DIR@'],
);
my $template_name_charset = qr/[[:alnum:]+_.@-]/;
@ -98,26 +98,31 @@ sub load_template {
my $ret = "";
my $path;
my $template_name = "$values->{name}";
if (!$opts{newest} and $values->{version}) {
$path = "$opts{template_dir}/$values->{name}-$values->{version}.template";
} else {
$path = "$opts{template_dir}/$values->{name}.template";
$template_name .= "-$values->{version}";
}
$template_name .= ".template";
# resolve symlink(s) and use the real file's name for version detection
my ($version) = (abs_path($path) =~ /-([0-9.]+)[.]template$/);
foreach my $dir (reverse @{$opts{template_dir}}) {
my $path = "$dir/$template_name";
if ( -e $path ) {
# resolve symlink(s) and use the real file's name for version detection
my ($version) = (abs_path($path) =~ /-([0-9.]+)[.]template$/);
if (!$version) {
die sprintf(gettext("Couldn't detect version for template '%s'\n"), $values->{name});
if (!$version) {
die sprintf(gettext("Couldn't detect version for template '%s'\n"), $path);
}
my $parsed = process_file($path);
$ret .= "# template start; name=$values->{name}; version=$version;\n";
$ret .= $parsed;
$ret .= "# template end;\n";
return $ret;
}
}
my $parsed = process_file($path);
$ret .= "# template start; name=$values->{name}; version=$version;\n";
$ret .= $parsed;
$ret .= "# template end;\n";
return $ret;
die sprintf(gettext("Failed to find template file matching '%s'\n"), $template_name);
}
# process input file and load templates for all markers found
@ -199,7 +204,7 @@ GetOptions(
"input|p=s" => \$opts{input},
"output|o=s" => \$opts{output},
"newest|n" => \$opts{newest},
"template-dir=s" => \$opts{template_dir},
"template-dir=s@" => \$opts{template_dir},
) or usage(1);
$opts{output} = $opts{input} unless $opts{output};

View File

@ -0,0 +1,5 @@
pkgname=foo
pkgver=1
# template input; name=build; version=1;
# template input; name=package; version=1;

View File

@ -0,0 +1,18 @@
arguments+=(--template-dir $testdir/$testcase/templates/build)
arguments+=(--template-dir $testdir/$testcase/templates/package)
expected_exitcode=0
IFS="" read -d '' expected_output <<'EOF'
EOF
IFS="" read -d '' expected_result <<'EOF'
pkgname=foo
pkgver=1
# template start; name=build; version=1;
build() {}
# template end;
# template start; name=package; version=1;
package() {}
# template end;
EOF

View File

@ -0,0 +1,4 @@
pkgname=foo
pkgver=1
# template input; name=perl-module; version=1.0;

View File

@ -0,0 +1,6 @@
pkgname=foo
pkgver=1
# template start; name=perl-module; version=1.0;
this should be included
# template end;

View File

@ -0,0 +1,15 @@
arguments+=(--template-dir $testdir/$testcase/templates/first)
arguments+=(--template-dir $testdir/$testcase/templates/last)
expected_exitcode=0
IFS="" read -d '' expected_output <<'EOF'
EOF
IFS="" read -d '' expected_result <<'EOF'
pkgname=foo
pkgver=1
# template start; name=perl-module; version=1.0;
this should be included
# template end;
EOF

View File

@ -7,7 +7,7 @@ _setup_testcase() {
# set IFS="" if you want trailing new lines, otherwise remove it
IFS="" read -d '' expected_output <<'EOF'
failed to open './makepkg-template-tests/missing-template-file/templates/perl-module-1.0.template': No such file or directory
Failed to find template file matching 'perl-module-1.0.template'
EOF
IFS="" read -d '' expected_result <<'EOF'

View File

@ -1,8 +1,8 @@
arguments+=()
expected_exitcode=255
expected_exitcode=2
IFS="" read -d '' expected_output <<'EOF'
Couldn't detect version for template 'perl-module'
Failed to find template file matching 'perl-module.template'
EOF
IFS="" read -d '' expected_result <<'EOF'

View File

@ -2,7 +2,7 @@ arguments+=()
expected_exitcode=255
IFS="" read -d '' expected_output <<'EOF'
Couldn't detect version for template 'template-without_version'
Couldn't detect version for template './makepkg-template-tests/template-without-version/templates/template-without_version.template'
EOF
IFS="" read -d '' expected_result <<'EOF'