forked from docker-library/php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapply-templates.sh
executable file
·101 lines (82 loc) · 2.8 KB
/
apply-templates.sh
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
#!/usr/bin/env bash
set -Eeuo pipefail
# Don't tolerate outdated bash on Darwin
[ "${BASH_VERSINFO:-0}" -ge 4 ] || (echo "Outdated bash version: ${BASH_VERSION}. If you're on MacOS/Darwin, please 'brew install bash' to move away from this comically outdated version." && exit 1)
[ -f versions.json ] || (echo "run 'versions.sh' first so templates can be applied to the right versions" && exit 1)
type gawk >/dev/null 2>&1 || (echo "Please install the gawk command. On MacOS, run 'brew install gawk'" && exit 1)
# GNU sed for Mac.
# Copied from:
# https://gist.github.com/bittner/5436f3dc011d43ab7551#file-gnu-tools-for-mac-sh
sedcmd="sed"
[[ $(uname) == 'Darwin' ]] && {
which gsed >/dev/null && {
sedcmd="gsed"
} || {
echo 'ERROR: GNU sed required for Mac. You may use homebrew to install it: brew install gnu-sed'
exit 1
}
}
jqt='.jq-template.awk'
if [ -n "${BASHBREW_SCRIPTS:-}" ]; then
jqt="$BASHBREW_SCRIPTS/jq-template.awk"
elif [ "$BASH_SOURCE" -nt "$jqt" ]; then
wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/5f0c26381fb7cc78b2d217d58007800bdcfbcfa1/scripts/jq-template.awk'
fi
if [ "$#" -eq 0 ]; then
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
eval "set -- $versions"
fi
generated_warning() {
cat <<-EOH
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
EOH
}
for version; do
export version
variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)"
eval "variants=( $variants )"
for dir in "${variants[@]}"; do
suite="$(dirname "$dir")" # "buster", etc
variant="$(basename "$dir")" # "cli", etc
export suite variant
alpineVer="${suite#alpine}" # "3.12", etc
if [ "$suite" != "$alpineVer" ]; then
template='Dockerfile-alpine.template'
from="alpine:$alpineVer"
else
template='Dockerfile-debian.template'
from="debian:$suite-slim"
fi
export from
case "$variant" in
apache) cmd='["apache2-foreground"]' ;;
fpm) cmd='["php-fpm"]' ;;
*) cmd='["php", "-a"]' ;;
esac
export cmd
echo "processing $version/$dir ..."
variantBlock1="$(if [ -f "Dockerfile-$variant-block-1.template" ]; then gawk -f "$jqt" "Dockerfile-$variant-block-1.template"; fi)"
variantBlock2="$(if [ -f "Dockerfile-$variant-block-2.template" ]; then gawk -f "$jqt" "Dockerfile-$variant-block-2.template"; fi)"
export variantBlock1 variantBlock2
{
generated_warning
gawk -f "$jqt" "$template"
} >"$version/$dir/Dockerfile"
cp -a \
docker-php-entrypoint \
docker-php-ext-* \
docker-php-source \
"$version/$dir/"
if [ "$variant" = 'apache' ]; then
cp -a apache2-foreground "$version/$dir/"
fi
cmd="$(jq <<<"$cmd" -r '.[0]')"
if [ "$cmd" != 'php' ]; then
$sedcmd -i -e 's! php ! '"$cmd"' !g' "$version/$dir/docker-php-entrypoint"
fi
done
done