Skip to content

Commit 2cf732a

Browse files
committed
Adding fork support to the scripts
Shared * Moved version parsing/detection to a separate function * Added path/version parameters to various functions * Fixed whitespace metacharacters so they work on both macOS and Linux * Updated tag generation logic to account for forks generate-stackbrew-library.sh * Updated sort algorithm to sort the non-fork versions first test_build.sh * Fixed a bug where it could only handle one version update.sh * Added support for configurable node download URIs * Added some informational and error output * Updated it to use the templates for each fork
1 parent 25f2614 commit 2cf732a

File tree

5 files changed

+194
-45
lines changed

5 files changed

+194
-45
lines changed

config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baseuri https://nodejs.org/dist

functions.sh

Lines changed: 150 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
# Utlity functions
44

5+
info() {
6+
printf "%s\\n" "$@"
7+
}
8+
9+
fatal() {
10+
printf "**********\\n"
11+
printf "Fatal Error: %s\\n" "$@"
12+
printf "**********\\n"
13+
exit 1
14+
}
15+
516
# Get system architecture
617
#
718
# This is used to get the target architecture for docker image.
@@ -40,10 +51,14 @@ function get_arch() {
4051
# <architecutre 1> <supported variant 1 >,<supported variant 2>...
4152
# <architecutre 2> <supported variant 1 >,<supported variant 2>...
4253
function get_variants() {
54+
local dir
55+
dir=${1:-.}
56+
shift
57+
4358
local arch
4459
arch=$(get_arch)
4560
local variants
46-
variants=$(grep "^$arch" architectures | sed -E 's/'"$arch"'\s*//' | sed -E 's/,/ /g')
61+
variants=$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')
4762
echo "$variants"
4863
}
4964

@@ -58,15 +73,146 @@ function get_supported_arches () {
5873
local version
5974
local variant
6075
local arches
76+
local lines
77+
local line
6178
version="$1"; shift
6279
variant="$1"; shift
6380

6481
# Get default supported arches
65-
arches=$( grep "$variant" architectures 2>/dev/null | cut -d' ' -f1 )
82+
lines=$( grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1 )
6683

6784
# Get version specific supported architectures if there is specialized information
6885
if [ -a "$version"/architectures ]; then
69-
arches=$( grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1 )
86+
lines=$( grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1 )
87+
fi
88+
89+
while IFS='' read -r line; do
90+
arches+=( "$line" )
91+
done <<< "$lines"
92+
93+
echo "${arches[@]}"
94+
}
95+
96+
# Get configuration values from the config file
97+
#
98+
# The configuration entries are simple key/value pairs which are whitespace separated.
99+
function get_config () {
100+
local dir
101+
dir=${1:-.}
102+
shift
103+
104+
local name
105+
name=$1
106+
shift
107+
108+
local value
109+
value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//')
110+
echo "$value"
111+
}
112+
113+
# Get available versions for a given path
114+
#
115+
# If full or partial versions are provided then they are processed and
116+
# validated. e.g. "4 chakracore" returns "4 chakracore/8" since it processed the
117+
# chakracore entry and found it to be a fork rather than a complete version.
118+
#
119+
# The result is a list of valid versions.
120+
function get_versions () {
121+
local prefix
122+
prefix=${1:-.}
123+
shift
124+
125+
local versions
126+
local dirs=( "$@" )
127+
if [ ${#dirs[@]} -eq 0 ]; then
128+
IFS=' ' read -ra dirs <<< "$(echo "${prefix%/}/"*/)"
70129
fi
71-
echo "$arches"
130+
131+
for dir in "${dirs[@]}"; do
132+
if [ -a "$dir/config" ]; then
133+
local subdirs
134+
IFS=' ' read -ra subdirs <<< "$(get_versions "${dir#./}")"
135+
for subdir in "${subdirs[@]}"; do
136+
versions+=( "$subdir" )
137+
done
138+
elif [ -a "$dir/Dockerfile" ]; then
139+
versions+=( "${dir#./}" )
140+
fi
141+
done
142+
143+
if [ ${#versions[@]} -gt 0 ]; then
144+
echo "${versions[@]%/}"
145+
fi
146+
}
147+
148+
function get_fork_name () {
149+
local version
150+
version=$1
151+
shift
152+
153+
IFS='/' read -ra versionparts <<< "$version"
154+
if [ ${#versionparts[@]} -gt 1 ]; then
155+
echo "${versionparts[0]}"
156+
fi
157+
}
158+
159+
function get_full_version () {
160+
local version
161+
version=$1
162+
shift
163+
164+
grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3
165+
}
166+
167+
function get_major_minor_version () {
168+
local version
169+
version=$1
170+
shift
171+
172+
local fullversion
173+
fullversion=$(get_full_version "$version")
174+
175+
echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)"
176+
}
177+
178+
function get_tag () {
179+
local version
180+
version=$1
181+
shift
182+
183+
local versiontype
184+
versiontype=${1:-full}
185+
shift
186+
187+
local tagversion
188+
if [ "$versiontype" = full ]; then
189+
tagversion=$(get_full_version "$version")
190+
elif [ "$versiontype" = majorminor ]; then
191+
tagversion=$(get_major_minor_version "$version")
192+
fi
193+
194+
local tagparts
195+
IFS=' ' read -ra tagparts <<< "$(get_fork_name "$version") $tagversion"
196+
IFS='-'; echo "${tagparts[*]}"; unset IFS
197+
}
198+
199+
function sort_versions () {
200+
local versions=( "$@" )
201+
local sorted
202+
local lines
203+
local line
204+
205+
IFS=$'\n'
206+
lines="${versions[*]}"
207+
unset IFS
208+
209+
while IFS='' read -r line; do
210+
sorted+=( "$line" )
211+
done <<< "$(echo "$lines" | grep "^[0-9]" | sort -r)"
212+
213+
while IFS='' read -r line; do
214+
sorted+=( "$line" )
215+
done <<< "$(echo "$lines" | grep -v "^[0-9]" | sort -r)"
216+
217+
echo "${sorted[@]}"
72218
}

generate-stackbrew-library.sh

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ cd "$(cd "${0%/*}" && pwd -P)";
1818

1919
self="$(basename "${BASH_SOURCE[0]}")"
2020

21-
versions=( */ )
22-
versions=( "${versions[@]%/}" )
21+
IFS=' ' read -ra versions <<< "$(get_versions)"
22+
IFS=' ' read -ra versions <<< "$(sort_versions "${versions[@]}")"
2323
url='https://github.com/nodejs/docker-node'
2424

25-
# sort version numbers with highest first
26-
IFS=$'\n'; versions=( $(echo "${versions[*]}" | sort -r) ); unset IFS
27-
2825
# get the most recent commit which modified any of "$@"
2926
fileCommit() {
3027
git log -1 --format='format:%H' HEAD -- "$@"
@@ -43,18 +40,25 @@ join() {
4340
echo "${out#$sep}"
4441
}
4542

43+
get_stub() {
44+
local version="$1"; shift
45+
IFS='/' read -ra versionparts <<< "$version"
46+
local stub; eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')";
47+
echo "$stub"
48+
}
49+
4650
for version in "${versions[@]}"; do
4751
# Skip "docs" and other non-docker directories
4852
[ -f "$version/Dockerfile" ] || continue
4953

50-
eval stub="$(echo "$version" | awk -F. '{ print "$array_" $1 }')";
54+
stub=$(get_stub "$version")
5155
commit="$(fileCommit "$version")"
52-
fullVersion="$(grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3)"
53-
minorVersion="$(echo "$fullVersion" | cut -d'.' -f2)"
56+
fullVersion="$(get_tag "$version" full)"
57+
majorMinorVersion="$(get_tag "$version" majorminor)"
5458

55-
versionAliases=( $fullVersion $version.$minorVersion ${stub} )
59+
IFS=' ' read -ra versionAliases <<< "$fullVersion $majorMinorVersion $stub"
5660
# Get supported architectures for a specific version. See details in function.sh
57-
supportedArches=( $(get_supported_arches "$version" "default") )
61+
IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "default")"
5862

5963
echo "Tags: $(join ', ' "${versionAliases[@]}")"
6064
echo "Architectures: $(join ', ' "${supportedArches[@]}")"
@@ -64,7 +68,7 @@ for version in "${versions[@]}"; do
6468

6569
# Get supported variants according to the target architecture.
6670
# See details in function.sh
67-
variants=$(get_variants | tr ' ' '\n')
71+
variants=$(get_variants "$(dirname "$version")")
6872
for variant in $variants; do
6973
# Skip non-docker directories
7074
[ -f "$version/$variant/Dockerfile" ] || continue
@@ -76,7 +80,7 @@ for version in "${versions[@]}"; do
7680
variantAliases=( "${variantAliases[@]//latest-/}" )
7781
# Get supported architectures for a specific version and variant.
7882
# See details in function.sh
79-
supportedArches=( $(get_supported_arches "$version" "$variant") )
83+
IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "$variant")"
8084

8185
echo "Tags: $(join ', ' "${variantAliases[@]}")"
8286
echo "Architectures: $(join ', ' "${supportedArches[@]}")"

test-build.sh

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,22 @@
33
# Run a test build for all images.
44

55
set -uo pipefail
6-
IFS=$'\n\t'
76

87
. functions.sh
98

10-
info() {
11-
printf "%s\n" "$@"
12-
}
13-
14-
fatal() {
15-
printf "**********\n"
16-
printf "%s\n" "$@"
17-
printf "**********\n"
18-
exit 1
19-
}
20-
219
cd "$(cd "${0%/*}" && pwd -P)" || exit;
2210

23-
versions=( "$@" )
11+
IFS=' ' read -ra versions <<< "$(get_versions . "$@")"
2412
if [ ${#versions[@]} -eq 0 ]; then
25-
versions=( */ )
13+
fatal "No valid versions found!"
2614
fi
27-
versions=( "${versions[@]%/}" )
2815

2916
for version in "${versions[@]}"; do
3017
# Skip "docs" and other non-docker directories
3118
[ -f "$version/Dockerfile" ] || continue
3219

33-
tag=$(grep "ENV NODE_VERSION" "$version/Dockerfile" | cut -d' ' -f3)
20+
tag=$(get_tag "$version")
21+
full_version=$(get_full_version "$version")
3422

3523
info "Building $tag..."
3624

@@ -40,14 +28,14 @@ for version in "${versions[@]}"; do
4028
info "Build of $tag succeeded."
4129

4230
OUTPUT=$(docker run --rm -it node:"$tag" node -e "process.stdout.write(process.versions.node)")
43-
if [ "$OUTPUT" != "$tag" ]; then
31+
if [ "$OUTPUT" != "$full_version" ]; then
4432
fatal "Test of $tag failed!"
4533
fi
4634
info "Test of $tag succeeded."
4735

4836
# Get supported variants according to the target architecture.
4937
# See details in function.sh
50-
variants=$(get_variants | tr ' ' '\n')
38+
variants=$(get_variants "$(dirname "$version")")
5139

5240
for variant in $variants; do
5341
# Skip non-docker directories
@@ -61,7 +49,7 @@ for version in "${versions[@]}"; do
6149
info "Build of $tag-$variant succeeded."
6250

6351
OUTPUT=$(docker run --rm -it node:"$tag-$variant" node -e "process.stdout.write(process.versions.node)")
64-
if [ "$OUTPUT" != "$tag" ]; then
52+
if [ "$OUTPUT" != "$full_version" ]; then
6553
fatal "Test of $tag-$variant failed!"
6654
fi
6755
info "Test of $tag-$variant succeeded."

update.sh

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/bin/bash
2-
set -e
2+
set -ue
33

44
. functions.sh
55

66
cd "$(cd "${0%/*}" && pwd -P)";
77

8-
versions=( "$@" )
8+
IFS=' ' read -ra versions <<< "$(get_versions . "$@")"
99
if [ ${#versions[@]} -eq 0 ]; then
10-
versions=( */ )
10+
fatal "No valid versions found!"
1111
fi
12-
versions=( "${versions[@]%/}" )
1312

1413
# Global variables
1514
# Get architecure and use this as target architecture for docker image
@@ -21,6 +20,10 @@ yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)"
2120

2221
function update_node_version {
2322

23+
local baseuri=$1
24+
shift
25+
local version=$1
26+
shift
2427
local template=$1
2528
shift
2629
local dockerfile=$1
@@ -31,12 +34,12 @@ function update_node_version {
3134
shift
3235
fi
3336

34-
fullVersion="$(curl -sSL --compressed 'https://nodejs.org/dist' | grep '<a href="v'"$version." | sed -E 's!.*<a href="v([^"/]+)/?".*!\1!' | cut -d'.' -f2,3| sort -n | tail -1)"
37+
fullVersion="$(curl -sSL --compressed "$baseuri" | grep '<a href="v'"$version." | sed -E 's!.*<a href="v([^"/]+)/?".*!\1!' | cut -d'.' -f2,3| sort -n | tail -1)"
3538
(
3639
cp "$template" "$dockerfile"
3740
local fromprefix=
3841
if [[ "$arch" != "amd64" && "$variant" != "onbuild" ]]; then
39-
fromprefix="$arch\/"
42+
fromprefix="$arch\\/"
4043
fi
4144

4245
sed -E -i.bak 's/^FROM (.*)/FROM '"$fromprefix"'\1/' "$dockerfile" && rm "$dockerfile".bak
@@ -53,16 +56,23 @@ for version in "${versions[@]}"; do
5356
# Skip "docs" and other non-docker directories
5457
[ -f "$version/Dockerfile" ] || continue
5558

56-
update_node_version "Dockerfile.template" "$version/Dockerfile"
59+
info "Updating version $version..."
60+
61+
parentpath=$(dirname "$version")
62+
versionnum=$(basename "$version")
63+
baseuri=$(get_config "$parentpath" "baseuri")
64+
65+
update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile.template" "$version/Dockerfile"
5766

5867
# Get supported variants according the target architecture
5968
# See details in function.sh
60-
variants=$(get_variants)
69+
variants=$(get_variants "$parentpath")
6170

6271
for variant in $variants; do
6372
# Skip non-docker directories
6473
[ -f "$version/$variant/Dockerfile" ] || continue
65-
update_node_version "Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant"
66-
74+
update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant"
6775
done
6876
done
77+
78+
info "Done!"

0 commit comments

Comments
 (0)