Skip to content

Commit

Permalink
Refactor generate-supported-tags.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
victorpopkov committed Apr 15, 2024
1 parent 2763898 commit 007c113
Showing 1 changed file with 55 additions and 61 deletions.
116 changes: 55 additions & 61 deletions generate-supported-tags.sh
Original file line number Diff line number Diff line change
@@ -1,97 +1,91 @@
#!/usr/bin/env bash

BASE_DIR="$(cd "$(dirname "$0")" && pwd)"
COMMIT_ID=$(git rev-parse --verify HEAD)
COMMIT_ID="$(git rev-parse --verify HEAD)"
DISTS=('alpine' 'debian')
VERSIONS=()
JSON="$(cat ./versions.json)"
REPOSITORY='https://github.com/dstmodders/docker-ktools'
VERSIONS_KEYS=()

mapfile -t VERSIONS < <(jq -r 'keys[]' ./versions.json)
IFS=$'\n' VERSIONS=($(sort -rV <<< "${VERSIONS[*]}")); unset IFS
mapfile -t VERSIONS_KEYS < <(jq -r 'keys[]' <<< "$JSON")
# shellcheck disable=SC2207
IFS=$'\n' VERSIONS_KEYS=($(sort -rV <<< "${VERSIONS_KEYS[*]}")); unset IFS

readonly BASE_DIR
readonly COMMIT_ID
readonly DISTS
readonly VERSIONS

# https://stackoverflow.com/a/17841619
function join_by {
local d="${1-}"
local f="${2-}"
if shift 2; then
printf %s "$f" "${@/#/$d}";
fi
}

function jq_value {
local from="$1"
local key="$2"
local name="$3"
jq -r ".[${key}] | .${name}" "${from}"
}
readonly JSON
readonly REPOSITORY
readonly VERSIONS_KEYS

function print_url() {
local tags="$1"
local commit="$2"
local dist="$3"
local official="$4"

local url="[$tags](https://github.com/dstmodders/docker-ktools/blob/${commit}/latest/${dist}/Dockerfile)"
if [ "${official}" == 'true' ]; then
url="[$tags](https://github.com/dstmodders/docker-ktools/blob/${commit}/official/${dist}/Dockerfile)"
fi

echo "- ${url}"
local directory="$3"
local url="[$tags]($REPOSITORY/blob/$commit/$directory/Dockerfile)"
echo "- $url"
}

cd "${BASE_DIR}" || exit 1
cd "$BASE_DIR" || exit 1

printf "## Supported tags and respective \`Dockerfile\` links\n\n"

for v in "${VERSIONS[@]}"; do
# reference:
# 4.5.1-imagemagick-7.1.1-30-alpine, 4.5.1, alpine, latest
# official-4.4.0-imagemagick-6.9.13-8-alpine, official-4.4.0, official-alpine, official-latest
for key in "${VERSIONS_KEYS[@]}"; do
for dist in "${DISTS[@]}"; do
commit="${COMMIT_ID}"
version=$(jq -r ".[${v}] | .version" ./versions.json)
imagemagick=$(jq_value ./versions.json "${v}" 'imagemagick')
latest=$(jq_value ./versions.json "${v}" 'latest')
official=$(jq_value ./versions.json "${v}" 'official')
previous=$(jq -c ".[${v}] | .previous" < ./versions.json)

tag_dist="${dist}"
tag_full="${version}-imagemagick-${imagemagick}-${dist}"
tag_version="${version}"

if [ "${official}" == 'true' ]; then
tag_dist="official-${tag_dist}"
tag_full="official-${tag_full}"
tag_version="official-${tag_version}"
commit="$COMMIT_ID"
version=$(jq -r ".[$key] | .version" <<< "$JSON")
imagemagick=$(jq -r ".[$key].imagemagick" <<< "$JSON")
latest=$(jq -r ".[$key].latest" <<< "$JSON")
official=$(jq -r ".[$key].official" <<< "$JSON")
previous=$(jq -r ".[$key].previous" <<< "$JSON")

tag_dist="$dist"
tag_full="$version-imagemagick-$imagemagick-$dist"
tag_version="$version"

if [ "$official" == 'true' ]; then
tag_dist="official-$tag_dist"
tag_full="official-$tag_full"
tag_version="official-$tag_version"
fi

tags=''
if [ "${dist}" == 'alpine' ]; then
tags="\`${tag_full}\`, \`${tag_version}\`, \`${tag_dist}\`"
if [ "${latest}" == 'true' ]; then
if [ "${official}" == 'true' ]; then
tags="${tags}, \`official-latest\`"
if [ "$dist" == 'alpine' ]; then
tags="\`$tag_full\`, \`$tag_version\`, \`$tag_dist\`"
if [ "$latest" == 'true' ]; then
if [ "$official" == 'true' ]; then
tags="$tags, \`official-latest\`"
else
tags="${tags}, \`latest\`"
tags="$tags, \`latest\`"
fi
fi
else
tags="\`${tag_full}\`, \`${tag_dist}\`"
tags="\`$tag_full\`, \`$tag_dist\`"
fi

print_url "${tags}" "${commit}" "${dist}" "${official}"
if [ "$official" == 'true' ]; then
print_url "$tags" "$commit" "official/$dist"
else
print_url "$tags" "$commit" "latest/$dist"
fi
done

if [ "${previous}" != "null" ]; then
mapfile -t commits < <(jq -r 'keys[]' <<< "${previous}")
imagemagick=$(jq -c ".[].imagemagick" <<< "${previous}" | xargs)
if [ "$previous" != "null" ]; then
mapfile -t commits < <(jq -r 'keys[]' <<< "$previous")
imagemagick=$(jq -c ".[].imagemagick" <<< "$previous" | xargs)

for dist in "${DISTS[@]}"; do
for commit in "${commits[@]}"; do
tag_full="${version}-imagemagick-${imagemagick}-${dist}"
tags="\`${tag_full}\`"
print_url "${tags}" "${commit}" "${dist}" "${official}"
tag_full="$version-imagemagick-$imagemagick-$dist"
tags="\`$tag_full\`"
if [ "$official" == 'true' ]; then
print_url "$tags" "$commit" "official/$dist"
else
print_url "$tags" "$commit" "latest/$dist"
fi
done
done
fi
Expand Down

0 comments on commit 007c113

Please sign in to comment.