[pacman-dev] [PATCH] 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
On 22.04.2015 20:37, Dominik Fischer wrote:
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
--- 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.
Just use gettext in your code, the translation files will be updated upon release. Also please make sure to adapt the existing tests (you broke a couple) and maybe add a test or two. They are in ./test/scripts/makepkg-template-tests/. Run them with make check and make distcheck (make sure both work).
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.
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
On 23.04.2015 13:59, Dominik Fischer wrote:
diff --git a/scripts/makepkg-template.pl.in b/scripts/makepkg-template.pl.in index 71d2aae..49e0e37 100755 --- a/scripts/makepkg-template.pl.in +++ b/scripts/makepkg-template.pl.in @@ -98,26 +98,32 @@ 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;
Please declare $path inside the loop.
+ 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});
Since we now have multiple dirs this could be ambiguous and should probably use $path in the error instead of $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"), $template_name, $!)
You don't actually fail to open anything, you fail to find a matching template. die sprintf(gettext("Failed to find template file matching '%s'), $template_name); would better reflect that.
@@ -128,7 +134,7 @@ sub process_file { my $nesting_level = 0; my $linenumber = 0;
- open (my $fh, "<", $filename) or die sprintf(gettext("failed to open '%s': %s\n"), $filename, $!); + open (my $fh, "<", $filename);
Removing this die introduces a race condition and issues when the file has wrong permissions and is not readable (-e will succeed, open will not). I'd say keep it here (the way it was) in addition to the die after the for loop above. They catch different kinds of errors. The rest of the patch and the tests look good.
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
On 23.04.2015 16:41, Dominik Fischer wrote:
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
Signed-off-by: Florian Pritz
participants (2)
-
Dominik Fischer
-
Florian Pritz