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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#!/usr/bin/perl
use warnings;
use strict;
use v5.10.1;
use Cwd qw(abs_path);
use Getopt::Long;
use Module::Load;
use Module::Load::Conditional qw(can_load);
my %opts = (
input => '@BUILDSCRIPT@',
template_dir => '@TEMPLATE_DIR@',
);
my $template_name_charset = qr/[[:alnum:]+_.@-]/;
my $template_marker = qr/# template/;
# runtime loading to avoid dependency on cpan since this is the only non-core module
my $loaded_gettext = can_load(modules => {'Locale::gettext' => undef});
if ($loaded_gettext) {
Locale::gettext::bindtextdomain("pacman-scripts", '@localedir@');
Locale::gettext::textdomain("pacman-scripts");
}
sub gettext {
my ($string) = @_;
if ($loaded_gettext) {
return Locale::gettext::gettext($string);
} else {
return $string;
}
}
sub burp {
my ($file_name, @lines) = @_;
open (my $fh, ">", $file_name) || die sprintf(gettext("can't create '%s': %s"), $file_name, $!);
print $fh @lines;
close $fh;
}
# read a template marker line and parse values into a hash
# format is "# template (start|input); key=value; key2=value2; ..."
sub parse_template_line {
my ($line, $filename, $linenumber) = @_;
my %values;
my ($marker, @elements) = split(/;\s?/, $line);
($values{command}) = ($marker =~ /$template_marker (.*)/);
foreach my $element (@elements) {
my ($key, $val) = ($element =~ /^([a-z0-9]+)=(.*)$/);
unless ($key and $val) {
die gettext("invalid key/value pair\n%s:%s: %s"),
"$filename:$linenumber: $line";
}
$values{$key} = $val;
}
# end doesn't take arguments
if ($values{command} ne "end") {
if (!$values{name}) {
die gettext("invalid template line: can't find template name\n"),
"$filename:$linenumber: $line";
}
unless ($values{name} =~ /^$template_name_charset+$/) {
die sprintf(gettext("invalid chars used in name '%s'. allowed: [:alnum:]+_.\@-\n"), $values{name}),
"$filename:$linenumber: $line";
}
}
return \%values;
}
# load a template, process possibly existing markers (nested templates)
sub load_template {
my ($values) = @_;
my $ret = "";
my $path;
if (!$opts{newest} and $values->{version}) {
$path = "$opts{template_dir}/$values->{name}-$values->{version}.template";
} else {
$path = "$opts{template_dir}/$values->{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'"), $values->{name});
}
my $parsed = process_file($path);
$ret .= "# template start; name=$values->{name}; version=$version;\n";
$ret .= $parsed;
$ret .= "# template end;\n";
return $ret;
}
# process input file and load templates for all markers found
sub process_file {
my ($filename) = @_;
my $ret = "";
my $nesting_level = 0;
my $linenumber = 0;
open (my $fh, "<", $filename) or die sprintf(gettext("failed to open '%s': %s"), $filename, $!);
my @lines = <$fh>;
close $fh;
foreach my $line (@lines) {
$linenumber++;
if ($line =~ $template_marker) {
my $values = parse_template_line($line, $filename, $linenumber);
given ($values->{command}) {
when (['start', 'input']) {
if ($nesting_level == 0) {
$ret .= load_template($values);
}
}
when ('end') {
# nothing to do here, just for completeness
}
default {
die sprintf(gettext("Unknown template marker '%s'\n"), $values->{command}),
"$filename:$linenumber: $line";
}
}
$nesting_level++ if $values->{command} eq "start";
$nesting_level-- if $values->{command} eq "end";
# marker lines should never be added
next;
}
# we replace code inside blocks with the template
# so we ignore the content of the block
next if $nesting_level > 0;
$ret .= $line;
}
return $ret;
}
sub usage {
my ($exitstatus) = @_;
print gettext("makepkg-template [options]\n");
print "\n";
print gettext("Options:\n");
printf(gettext(" --input, -p <file> Build script to read (default: %s)\n"), '@BUILDSCRIPT@');
print gettext(" --output, -o <file> file to output to (default: input file)\n");
print gettext(" --newest, -n update templates to newest version\n");
print gettext(" (default: use version specified in the template markers)\n");
print gettext(" --template-dir <dir> directory to search for templates\n");
printf(gettext(" (default: %s)\n"), '@TEMPLATE_DIR@');
print "\n";
exit($exitstatus);
}
Getopt::Long::Configure ("bundling");
GetOptions(
"help" => sub {usage(0); },
"h" => sub {usage(0); },
"input|p=s" => \$opts{input},
"output|o=s" => \$opts{output},
"newest|n" => \$opts{newest},
"template-dir=s" => \$opts{template_dir},
) or usage(1);
$opts{output} = $opts{input} unless $opts{output};
$opts{input} = "/dev/stdin" if $opts{input} eq "-";
$opts{output} = "/dev/stdout" if $opts{output} eq "-";
burp($opts{output}, process_file($opts{input}));
# vim: set noet:
|