forked from docker-library/busybox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-stackbrew-library.sh
executable file
·139 lines (122 loc) · 3.47 KB
/
generate-stackbrew-library.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
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
133
134
135
136
137
138
139
#!/usr/bin/env bash
set -Eeuo pipefail
gitHubUrl='https://github.com/docker-library/busybox'
rawGitUrl="$gitHubUrl/raw"
self="$(basename "$BASH_SOURCE")"
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
if [ "$#" -eq 0 ]; then
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
eval "set -- $versions"
fi
archMaps=( $(
git ls-remote --heads "${gitHubUrl}.git" \
| awk -F '[\t/]' '$4 ~ /^dist-/ { gsub(/^dist-/, "", $4); print $4 "=" $1 }' \
| sort
) )
arches=()
declare -A archCommits=()
for archMap in "${archMaps[@]}"; do
arch="${archMap%%=*}"
commit="${archMap#${arch}=}"
arches+=( "$arch" )
archCommits[$arch]="$commit"
done
selfCommit="$(git log --format='format:%H' -1)"
cat <<-EOH
# this file is generated via $gitHubUrl/blob/$selfCommit/$self
Maintainers: Tianon Gravi <[email protected]> (@tianon),
Joseph Ferguson <[email protected]> (@yosifkit)
GitRepo: $gitHubUrl.git
GitCommit: $selfCommit
EOH
for arch in "${arches[@]}"; do
commit="${archCommits[$arch]}"
cat <<-EOA
# $gitHubUrl/tree/dist-${arch}
${arch}-GitFetch: refs/heads/dist-${arch}
${arch}-GitCommit: $commit
EOA
done
# prints "$2$1$3$1...$N"
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
# if stable is 1.32.1 and unstable is 1.33.0, we want busybox:1.33 to point to unstable but busybox:1 (and busybox:latest) to point to stable
# since stable always comes first, we'll just let it take all the tags it calculates, and use this to remove any overlap when we process unstable :)
declare -A usedTags=()
_tags() {
local tag first=
for tag; do
[ -z "${usedTags[$tag]:-}" ] || continue
usedTags[$tag]=1
if [ -z "$first" ]; then
echo
echo -n 'Tags: '
first=1
else
echo -n ', '
fi
echo -n "$tag"
done
if [ -z "$first" ]; then
return 1
fi
echo
return 0
}
allVersions=()
for version; do
export version
variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)"
eval "variants=( $variants )"
fullVersion="$(jq -r '.[env.version].version' versions.json)"
allVersions+=( "$fullVersion" )
latestVersion="$(xargs -n1 <<<"${allVersions[*]}" | sort -V | tail -1)"
if [ "$latestVersion" != "$fullVersion" ]; then
# if "unstable" is older than "stable" (1.32.0 unstable vs 1.32.1 stable, for example), skip unstable
continue
fi
versionAliases=()
while [ "${fullVersion%.*}" != "$fullVersion" ]; do
versionAliases+=( $fullVersion )
fullVersion="${fullVersion%.*}"
done
versionAliases+=(
$fullVersion
$version # "stable", "unstable"
latest
)
declare -A archLatestDir=()
for variant in "${variants[@]}"; do
dir="$version/$variant"
variantAliases=( "${versionAliases[@]/%/-$variant}" )
variantAliases=( "${variantAliases[@]//latest-/}" )
variantArches=()
for arch in "${arches[@]}"; do
archCommit="${archCommits[$arch]}"
if wget --quiet --spider -O /dev/null -o /dev/null "$rawGitUrl/$archCommit/$dir/busybox.tar.xz"; then
variantArches+=( "$arch" )
: "${archLatestDir[$arch]:=$dir}" # record the first supported directory per architecture for "latest" and friends
fi
done
if _tags "${variantAliases[@]}"; then
cat <<-EOE
Architectures: $(join ', ' "${variantArches[@]}")
Directory: $dir
EOE
fi
done
if _tags "${versionAliases[@]}"; then
cat <<-EOE
Architectures: $(join ', ' "${arches[@]}")
EOE
for arch in "${arches[@]}"; do
archDir="${archLatestDir[$arch]}"
cat <<-EOA
${arch}-Directory: $archDir
EOA
done
fi
done