Use jq-template.awk

This commit is contained in:
Tianon Gravi 2020-08-27 16:31:53 -07:00
parent ba69817cd8
commit 3d69162f4e
13 changed files with 168 additions and 51 deletions

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
**/Dockerfile linguist-generated
**/start-qemu linguist-generated
Dockerfile.template linguist-language=Dockerfile

22
.github/workflows/verify-templating.yml vendored Normal file
View File

@ -0,0 +1,22 @@
name: Verify Templating
on:
pull_request:
push:
defaults:
run:
shell: 'bash -Eeuo pipefail -x {0}'
jobs:
apply-templates:
name: Check For Uncomitted Changes
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Apply Templates
run: ./apply-templates.sh
- name: Check Git Status
run: |
status="$(git status --short)"
[ -z "$status" ]

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.jq-template.awk

6
4.1/Dockerfile generated
View File

@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM debian:buster-slim
RUN apt-get update && apt-get install -y --no-install-recommends \

6
4.2/Dockerfile generated
View File

@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM debian:buster-slim
RUN apt-get update && apt-get install -y --no-install-recommends \

6
5.0/Dockerfile generated
View File

@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM debian:buster-slim
RUN apt-get update && apt-get install -y --no-install-recommends \

6
5.1/Dockerfile generated
View File

@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM debian:buster-slim
RUN apt-get update && apt-get install -y --no-install-recommends \

View File

@ -14,8 +14,8 @@ ENV QEMU_KEYS \
# https://www.qemu.org/download/#source
# https://download.qemu.org/?C=M;O=D
ENV QEMU_VERSION %%QEMU_VERSION%%
ENV QEMU_URL %%QEMU_URL%%
ENV QEMU_VERSION {{ .version }}
ENV QEMU_URL {{ .url }}
RUN set -eux; \
\

40
apply-templates.sh Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -Eeuo pipefail
[ -f versions.json ] # run "versions.sh" first
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
echo "processing $version ..."
{
generated_warning
gawk -f "$jqt" Dockerfile.template
} > "$version/Dockerfile"
cp -a start-qemu "$version/"
done

View File

@ -10,8 +10,10 @@ declare -A aliases=(
self="$(basename "$BASH_SOURCE")"
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
versions=( */ )
versions=( "${versions[@]%/}" )
if [ "$#" -eq 0 ]; then
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
eval "set -- $versions"
fi
# sort version numbers with highest first
IFS=$'\n'; versions=( $(echo "${versions[*]}" | sort -rV) ); unset IFS
@ -52,10 +54,8 @@ join() {
echo "${out#$sep}"
}
for version in "${versions[@]}"; do
commit="$(dirCommit "$version")"
fullVersion="$(git show "$commit":"$version/Dockerfile" | awk '$1 == "ENV" && $2 == "QEMU_VERSION" { print $3; exit }')"
for version; do
fullVersion="$(jq -r --arg version "$version" '.[$version].version' versions.json)"
rcVersion="${version%-rc}"
@ -69,6 +69,8 @@ for version in "${versions[@]}"; do
${aliases[$version]:-}
)
commit="$(dirCommit "$version")"
echo
cat <<-EOE
Tags: $(join ', ' "${versionAliases[@]}")

View File

@ -3,46 +3,5 @@ set -Eeuo pipefail
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
versions=( */ )
versions=( "${versions[@]%/}" )
# https://download.qemu.org/?C=M;O=D
urls="$(
wget -qO- 'https://www.qemu.org/download/' \
| grep -oE 'https://download[.]qemu[.]org/qemu-([^"]+)[.]tar[.]xz' \
| sort -ruV
)"
for version in "${versions[@]}"; do
rcGrepV='-v'
rcVersion="${version%-rc}"
if [ "$rcVersion" != "$version" ]; then
rcGrepV=
fi
url="$(
grep -E "qemu-$rcVersion([.-])" <<<"$urls" \
| grep $rcGrepV -E -- '-rc' \
| head -1
)"
fullVersion="${url##*/qemu-}"
fullVersion="${fullVersion%%.tar.*}"
echo "$version: $fullVersion"
sed -r \
-e 's/%%QEMU_VERSION%%/'"$fullVersion"'/g' \
-e 's!%%QEMU_URL%%!'"$url"'!g' \
Dockerfile.template > "$version/Dockerfile"
cp -a start-qemu "$version/"
case "$rcVersion" in
# https://github.com/qemu/qemu/commit/b10d49d7619e4957b4b971f816661b57e5061d71
3.0 | 3.1 | 4.0)
sed -ri \
-e 's/libssh-dev/libssh2-1-dev/g' \
-e 's/--enable-libssh/--enable-libssh2/g' \
"$version/Dockerfile"
;;
esac
done
./versions.sh "$@"
./apply-templates.sh "$@"

18
versions.json Normal file
View File

@ -0,0 +1,18 @@
{
"4.1": {
"url": "https://download.qemu.org/qemu-4.1.1.tar.xz",
"version": "4.1.1"
},
"4.2": {
"url": "https://download.qemu.org/qemu-4.2.1.tar.xz",
"version": "4.2.1"
},
"5.0": {
"url": "https://download.qemu.org/qemu-5.0.0.tar.xz",
"version": "5.0.0"
},
"5.1": {
"url": "https://download.qemu.org/qemu-5.1.0.tar.xz",
"version": "5.1.0"
}
}

48
versions.sh Executable file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -Eeuo pipefail
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
versions=( "$@" )
if [ ${#versions[@]} -eq 0 ]; then
versions=( */ )
json='{}'
else
json="$(< versions.json)"
fi
versions=( "${versions[@]%/}" )
# https://download.qemu.org/?C=M;O=D
urls="$(
wget -qO- 'https://www.qemu.org/download/' \
| grep -oE 'https://download[.]qemu[.]org/qemu-([^"]+)[.]tar[.]xz' \
| sort -ruV
)"
for version in "${versions[@]}"; do
rcGrepV='-v'
rcVersion="${version%-rc}"
if [ "$rcVersion" != "$version" ]; then
rcGrepV=
fi
url="$(
grep -E "qemu-$rcVersion([.-])" <<<"$urls" \
| grep $rcGrepV -E -- '-rc' \
| head -1
)"
fullVersion="${url##*/qemu-}"
fullVersion="${fullVersion%%.tar.*}"
echo "$version: $fullVersion"
export version fullVersion url
json="$(jq <<<"$json" '
.[env.version] = {
version: env.fullVersion,
url: env.url,
}
')"
done
jq <<<"$json" -S . > versions.json