[pacman-dev] [PATCH] makepkg-template: support multiple --template-dirs

Dominik Fischer d.f.fischer at web.de
Wed Apr 22 18:37:23 UTC 2015


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 at web.de>
---
I reused some already translated strings for the new error message.  Perhaps a
new string should be introduced here, but I do not know what to do to update
the translation files appropriately.

 doc/makepkg-template.1.txt     |  4 +++-
 scripts/makepkg-template.pl.in | 41 ++++++++++++++++++++++++-----------------
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/doc/makepkg-template.1.txt b/doc/makepkg-template.1.txt
index a0a6303..03b2a38 100644
--- a/doc/makepkg-template.1.txt
+++ b/doc/makepkg-template.1.txt
@@ -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
diff --git a/scripts/makepkg-template.pl.in b/scripts/makepkg-template.pl.in
index 71d2aae..6fc0171 100755
--- a/scripts/makepkg-template.pl.in
+++ b/scripts/makepkg-template.pl.in
@@ -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:]+_. at -]/;
@@ -98,26 +98,33 @@ 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$/);
-
-	if (!$version) {
-		die sprintf(gettext("Couldn't detect version for template '%s'\n"), $values->{name});
-	}
+	my $path;
+	foreach my $dir (reverse @{$opts{template_dir}}) {
+		$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});
+			}
 
-	my $parsed = process_file($path);
+			my $parsed = process_file($path);
 
-	$ret .= "# template start; name=$values->{name}; version=$version;\n";
-	$ret .= $parsed;
-	$ret .= "# template end;\n";
-	return $ret;
+			$ret .= "# template start; name=$values->{name}; version=$version;\n";
+			$ret .= $parsed;
+			$ret .= "# template end;\n";
+			return $ret;
+		}
+	}
+	die sprintf(gettext("failed to open '%s': %s\n"), $values->{name},
+		sprintf(gettext("File '%s' not found."), $template_name));
 }
 
 # process input file and load templates for all markers found
@@ -199,7 +206,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};
-- 
2.3.5


More information about the pacman-dev mailing list