From 06b6c90dd7a8517737f1741bc49fa97486b198fe Mon Sep 17 00:00:00 2001 From: Justin Davis Date: Thu, 18 Aug 2011 09:44:47 -0400 Subject: Abstract PKGBUILD field printing into separate script: pbfields. pbfields will recognize any PKGBUILD fields provided in the data passed to STDIN. The fields are printed out to STDOUT as they would appear in the PKGBUILD. Arrays are wordwrapped to fit in 78 columns. --- bin/templ/pbfields | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/templ/perl-pkg | 76 ++++++++++++++++-------------------------------------- 2 files changed, 90 insertions(+), 54 deletions(-) create mode 100755 bin/templ/pbfields (limited to 'bin') diff --git a/bin/templ/pbfields b/bin/templ/pbfields new file mode 100755 index 0000000..8597910 --- /dev/null +++ b/bin/templ/pbfields @@ -0,0 +1,68 @@ +#!/usr/bin/awk -f + +BEGIN { + fieldstr = "pkgname pkgver pkgrel pkgdesc epoch " \ + "*arch *license *options " \ + "install changelog " \ + "*depends *makedepends *checkdepends *conflicts *provides " \ + "url *source *noextract *md5sums *sha512sums " + max = split(fieldstr, fields) + for (i=1; i<=max; i++) { + if (sub(/^[*]/, "", fields[i])) arrfield[fields[i]] = 1; + else strfield[fields[i]] = 1; + } + + COLS = 78 + FS = "\n"; RS = ""; OFS = "="; ORS = "\n"; +} + +$1 ~ /depends$|conflicts|provides/ { + for (i=2; i<=NF; i++) $i = sprintf("'%s'", $i) +} + +$1 == "pkgdesc" { gsub(/[$"`]/, "\\&", $2) } + +strfield[$1] { output[$1] = $2 } + +arrfield[$1] { + output[$1] = wraparray(length($1) + 2) +} + +END { + for (i=1; i<=max; i++) { + name = fields[i] + if (name in output) print name, output[name] + } +} + +function wraparray (indent) +{ + if (NF == 1) return "()" # this shouldn't happen but just in case. + + line = "" + delete lines + linecount = 0 + + i = 2 + while (i <= NF) { + linelen = length(line) + + if ((indent + linelen + 1 + length($i) > COLS) && linelen > 0) { + lines[++linecount] = line + line = "" + } + else { + if (linelen == 0) line = $(i++) + else line = line " " $(i++) + } + } + + if (length(line) > 0) lines[++linecount] = line + + indtxt = sprintf("%" indent "s", "") + txt = "(" lines[1] + for (i=2; i<=linecount; i++) txt = txt "\n" indtxt lines[i] + txt = txt ")" + + return txt +} \ No newline at end of file diff --git a/bin/templ/perl-pkg b/bin/templ/perl-pkg index e2cfbdf..82a5537 100755 --- a/bin/templ/perl-pkg +++ b/bin/templ/perl-pkg @@ -17,25 +17,8 @@ my %ACTIONS_OF = ('MM', } ); my $PBBEG = <<'END_BEG'; -# Maintainer : {: packager :} -# Generator : pbjam {: pbjver :} - -pkgname={: pkgname :} -pkgver={: pkgver :} -pkgrel={: pkgrel :} -pkgdesc={: pkgdesc :} -arch=({: arch :}) -license=(PerlArtistic GPL) -options=('!emptydirs') -depends=({: depends :}) -makedepends=({: makedepends :}) -checkdepends=({: checkdepends :}) -conflicts=({: conflicts :}) -url={: url :} -source=({: source :}) -md5sums=({: md5sums :}) -sha512sums=({: sha512sums :}) -_distdir="${srcdir}/{: distdir :}" +# Maintainer : %s +# Generator : pbjam %s END_BEG my %FUNCFMTS; @@ -78,39 +61,10 @@ my $PBEND = <<'END_END'; # vim:set ts=2 sw=2 et: END_END -# Expands one variable into its bash string representation. -sub expandvar -{ - my ($pbvars, $name) = @_; - - my $val = $pbvars->{$1}; - if ($name eq 'pkgdesc') { - $val->[0] =~ s/([\$\"\`])/\\$1/g; - return qq{"$val->[0]"}; - } - elsif ($name =~ /conflicts|depends$/) { - my @deps = map { qq{'$_'} } @$val; - return wrap(q{}, q{ } x (length($name) + 2), @deps); - } - return ($val ? join q{ }, @$val : q{}); -} - -# Expand variables used in the template text. -sub expandvars -{ - my ($tmpl, $pbvars) = @_; - - my $txt = $tmpl; - $txt =~ s/ {: \s* (\w+) \s* :} /expandvar($pbvars, $1)/gex; - return $txt; -} - # Convert actions array into lines of bash to insert into template. sub bashify { my (@lines) = @_; -# use Data::Dump qw(dump); -# print STDERR dump($lines), "\n"; my $txt = join qq{\n}, map { s/^/ /gm; $_ } @lines; return $txt } @@ -121,9 +75,24 @@ sub printpb my $acts = $ACTIONS_OF{$btype} or die "$0: unknown build type ($btype)\n"; - print expandvars($PBBEG, $pbvars), "\n"; + my $pkger = $pbvars->{'packager'}[0] || 'Anonymous'; + printf $PBBEG, $pkger, $pbvars->{'pbjver'}[0]; + print "\n"; + + # 'pbfields' will recognize and PKGBUILD fields in the data + # and print them out in order, wordwrapping arrays. + open my $pbfields, '|pbfields' or die "failed to pipe to pbfields: $!"; + while (my ($name, $vals) = each %$pbvars) { + print $pbfields $name, "\n"; + print $pbfields $_, "\n" for @$vals; + print $pbfields "\n"; + } + close $pbfields; + print "\n"; + for my $func (qw/build check package/) { - printf $FUNCFMTS{$func}, bashify(@{$acts->{$func}}); + my $funclines = $acts->{$func}; + printf $FUNCFMTS{$func}, bashify(@$funclines); print "\n"; } print $PBEND; @@ -131,17 +100,16 @@ sub printpb sub readvars { - my %pbvars; - my $name; local $/ = ""; # split records on empty lines + my (%pbvars); while () { my ($name, @vals) = split /\n/; - $pbvars{$name} = \@vals; + $pbvars{$name} = [ @vals ]; } - return \%pbvars; } my $type = shift or die qq{$0: please provide "MM" or "MB" as argument\n}; + my $vars = readvars(); printpb($type, $vars); -- cgit v1.2.3-24-g4f1b