-
Notifications
You must be signed in to change notification settings - Fork 0
/
makepkg-expanded
executable file
·132 lines (122 loc) · 3.76 KB
/
makepkg-expanded
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
#!/bin/bash
# initialize gettext
export TEXTDOMAIN='pacman-scripts'
# parse arguments
invalid_option() {
echo "$0: $(gettext "invalid option") -- '$OPTARG'" >&2
exit 1
}
add_all() {
while read -r -d $'\0'; do
packages+=("$REPLY")
done < <(find ! -readable -prune -o -iname ${1:-PKGBUILD} -print0)
}
while getopts ':hp:a:t:e:mdr:R:b' argument
do
case $argument in
h)
echo "usage: $0 [options]"
echo 'builds the PKGBUILD found in the current directory, expanding makepkg-templates' \
'but leaving the original intact apart from the version numbers.'
echo 'Valid options are:'
echo ' -h show this help text'
echo ' -p <script> select an alternative file instead of PKGBUILD'
echo ' -a [pattern] select all files matching the given pattern, or all PKGBUILDs'
echo ' -t <dir> directory to look up templates in'
echo ' -e <extension> extension for intermediate expanded build scripts'
echo ' -m keep template markers intact'
echo ' -d keep duplicate templates'
echo ' -r <command> command to execute for each PKGBUILD'
echo ' -R <script> file to read -r command from'
echo ' -b -r alias to aurbranch all files known to Git'
exit ;;
p) packages+=("$OPTARG") ;;
a) add_all "$OPTARG" ;;
t) templates+=(--template-dir "$(realpath "$OPTARG")") ;;
e) extension="$OPTARG" ;;
m)
if declare -f | grep -q sed
then
clean-templates() {
cat
}
else
clean-templates() {
command clean-templates -m
}
fi ;;
d)
if declare -f | grep -q command
then
clean-templates() {
cat
}
else
clean-templates() {
sed '/# vim/d;x;$G;/# template/d;1d'
}
fi ;;
r) process+=("$OPTARG") ;;
R) process+=("exec bash '$(realpath "$OPTARG" | sed "s/'/'\\''/g")'"' "$@"') ;;
b)
process+=('exec aurbranch -vp "$1" "${@/%/:}"') ;;
\?) invalid_option ;;
:)
case "$OPTARG" in
a) add_all ;;
-) invalid_option ;; # No long options.
*) echo "$0: $(gettext "option requires an argument") -- '-$OPTARG'" >&2
exit 1 ;;
esac
esac
done
if [ "${#templates[@]}" -le 0 ]
then
templates=("$(grep -Po "(?<=^\ttemplate_dir => \[').*(?='\],$)" $(which makepkg-template))")
gitdir="$(git rev-parse --show-toplevel)/makepkg-templates"
if [ $? -eq 0 -a -d "$gitdir" ]
then
templates+=("$gitdir")
fi
fi
if [ ${#process[@]} -eq 0 ]
then
process=(
'exec makepkg -p "$1"'
'exec cp-pkgver "$1" "$2"'
)
fi
# process PKGBUILDs
tmpenv="$(mktemp --tmpdir expanded-env.XXXXXXXXXX)"
trap "rm '$tmpenv'" EXIT
for path in "${packages[@]:-PKGBUILD}"
do
if [ ! -f "$path" ]
then
printf "$0: $(gettext "%s file (%s) does not exist or is not a regular file.")\n" "build script" "$path"
else
# Plain suffix removal does not suffice as $path may be just a filename.
dir="$(dirname "$path")"
file="${path##*/}"
expanded="$file${extension:=.expanded}"
( cd "$dir" #(
# expand and clean
makepkg-template -p "$file" -o - "${templates[@]/#/--template-dir=}" | \
clean-templates > "$expanded"
[ ${PIPESTATUS[0]} -ne 0 ] && exit ${PIPESTATUS[0]}
# process
echo "GLOBIGNORE='${GLOBIGNORE:+$GLOBIGNORE:}.*:$expanded:$file'" > "$tmpenv"
for run in "${process[@]}"
do
if [[ "$run" =~ ^"exec bash '"(([^\']|"'\\''")+)\'' "$@"'$ ]]
then
exec=("${BASH_REMATCH[1]}")
else
exec=(-c "$run")
fi
BASH_ENV="$tmpenv" bash "${exec[@]}" process-expanded \
"$expanded" "$file" "${templates[@]}" || exit $?
done
) || exit $? #)
fi
done