Skip to content

Commit 23e5a49

Browse files
committed
debugging starter project download
Signed-off-by: Michael Hoang <[email protected]>
1 parent 605081d commit 23e5a49

File tree

2 files changed

+256
-34
lines changed

2 files changed

+256
-34
lines changed

build-tools/dl_starter_projects.sh

+109-34
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/bin/bash
22

3-
if [[ -z "$@" ]]
4-
then
5-
echo "No starter projects specified."
6-
exit 0
7-
fi
8-
93
# Path of stacks directory in the registry
104
STACKS_DIR=/registry/stacks
115
# List of starter projects to use offline
126
offline_starter_projects=( "$@" )
7+
# When no starter projects are specifed,
8+
# all starter projects will be downloaded
9+
download_all_starter_projects=false
10+
11+
if [[ -z "$@" ]]
12+
then
13+
download_all_starter_projects=true
14+
fi
1315

1416
# Downloads a starter project from a remote git repository and packages it as a zip archive
1517
# to be used as an offline resource.
@@ -34,14 +36,21 @@ download_git_starter_project() {
3436

3537
if [ "${revision}" != "null" ]
3638
then
37-
cd $local_path && git checkout $revision && cd -
39+
cd $local_path
40+
git checkout $revision
41+
cd -
3842
fi
3943

4044
if [ "${subDir}" != "null" ]
4145
then
42-
cd $local_path/$subDir && zip -q ${local_path}.zip * .[^.]* && cd -
46+
cd $local_path/$subDir
47+
zip -q ../${name}-offline.zip * .[^.]*
48+
cd -
4349
else
44-
cd $local_path && rm -rf ./.git && zip -q ${local_path}.zip * .[^.]* && cd -
50+
cd $local_path
51+
rm -rf ./.git
52+
zip -q ../${name}-offline.zip * .[^.]*
53+
cd -
4554
fi
4655

4756
rm -rf $local_path
@@ -58,12 +67,62 @@ download_zip_starter_project() {
5867
curl -L $remote_url -o ${local_path}.zip
5968
}
6069

61-
# Read stacks list
62-
read -r -a stacks <<< "$(ls ${STACKS_DIR} | tr '\n' ' ')"
70+
download_specific() {
71+
for starter_project in ${offline_starter_projects[@]}
72+
do
73+
for stack in ${stacks[@]}
74+
do
75+
stack_root=$STACKS_DIR/$stack
76+
stack_devfile=$stack_root/devfile.yaml
77+
# Read version list for stack
78+
read -r -a versions <<< "$(ls ${STACKS_DIR}/${stack} | grep -e '[0-9].[0-9].[0-9]' | tr '\n' ' ')"
79+
# If multi version stack
80+
if [[ ${#versions[@]} -gt 0 ]]
81+
then
82+
for version in ${versions[@]}
83+
do
84+
stack_root=$STACKS_DIR/$stack/$version
85+
stack_devfile=$stack_root/devfile.yaml
86+
# If the specified starter project is found
87+
if [ ! -z "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\")" $stack_devfile)" ]
88+
then
89+
# Starter project has a git remote
90+
if [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").git" $stack_devfile)" != "null" ]
91+
then
92+
echo "Downloading ${starter_project} starter project in stack ${stack} version ${version}.."
93+
download_git_starter_project $stack_root $starter_project
94+
echo "Downloading ${starter_project} starter project in stack ${stack} version ${version}..done!"
95+
# Starter project has a zip remote
96+
elif [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").zip" $stack_devfile)" != "null" ]
97+
then
98+
echo "Downloading ${starter_project} starter project in stack ${stack} version ${version}.."
99+
download_zip_starter_project $stack_root $starter_project
100+
echo "Downloading ${starter_project} starter project in stack ${stack} version ${version}..done!"
101+
fi
102+
fi
103+
done
104+
# If not multi version stack & the specified starter project is found
105+
elif [ ! -z "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\")" $stack_devfile)" ]
106+
then
107+
# Starter project has a git remote
108+
if [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").git" $stack_devfile)" != "null" ]
109+
then
110+
echo "Downloading ${starter_project} starter project in stack ${stack}.."
111+
download_git_starter_project $stack_root $starter_project
112+
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
113+
# Starter project has a zip remote
114+
elif [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").zip" $stack_devfile)" != "null" ]
115+
then
116+
echo "Downloading ${starter_project} starter project in stack ${stack}.."
117+
download_zip_starter_project $stack_root $starter_project
118+
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
119+
fi
120+
fi
121+
done
122+
done
123+
}
63124

64-
echo "Downloading offline starter projects.."
65-
for starter_project in ${offline_starter_projects[@]}
66-
do
125+
download_all() {
67126
for stack in ${stacks[@]}
68127
do
69128
stack_root=$STACKS_DIR/$stack
@@ -77,9 +136,10 @@ do
77136
do
78137
stack_root=$STACKS_DIR/$stack/$version
79138
stack_devfile=$stack_root/devfile.yaml
80-
# If the specified starter project is found
81-
if [ ! -z "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\")" $stack_devfile)" ]
82-
then
139+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
140+
141+
for starter_project in $starter_projects
142+
do
83143
# Starter project has a git remote
84144
if [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").git" $stack_devfile)" != "null" ]
85145
then
@@ -93,25 +153,40 @@ do
93153
download_zip_starter_project $stack_root $starter_project
94154
echo "Downloading ${starter_project} starter project in stack ${stack} version ${version}..done!"
95155
fi
156+
done
157+
done
158+
# If not multi version stack
159+
else
160+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
161+
for starter_project in $starter_projects
162+
do
163+
# Starter project has a git remote
164+
if [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").git" $stack_devfile)" != "null" ]
165+
then
166+
echo "Downloading ${starter_project} starter project in stack ${stack}.."
167+
download_git_starter_project $stack_root $starter_project
168+
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
169+
# Starter project has a zip remote
170+
elif [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").zip" $stack_devfile)" != "null" ]
171+
then
172+
echo "Downloading ${starter_project} starter project in stack ${stack}.."
173+
download_zip_starter_project $stack_root $starter_project
174+
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
96175
fi
97176
done
98-
# If not multi version stack & the specified starter project is found
99-
elif [ ! -z "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\")" $stack_devfile)" ]
100-
then
101-
# Starter project has a git remote
102-
if [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").git" $stack_devfile)" != "null" ]
103-
then
104-
echo "Downloading ${starter_project} starter project in stack ${stack}.."
105-
download_git_starter_project $stack_root $starter_project
106-
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
107-
# Starter project has a zip remote
108-
elif [ "$(yq e ".starterProjects[] | select(.name == \"${starter_project}\").zip" $stack_devfile)" != "null" ]
109-
then
110-
echo "Downloading ${starter_project} starter project in stack ${stack}.."
111-
download_zip_starter_project $stack_root $starter_project
112-
echo "Downloading ${starter_project} starter project in stack ${stack}..done!"
113-
fi
114177
fi
178+
echo
115179
done
116-
done
180+
}
181+
182+
# Read stacks list
183+
read -r -a stacks <<< "$(ls ${STACKS_DIR} | tr '\n' ' ')"
184+
185+
echo "Downloading offline starter projects.."
186+
if [ "$download_all_starter_projects" = true ]
187+
then
188+
download_all
189+
else
190+
download_specific
191+
fi
117192
echo "Downloading offline starter projects..done!"
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/bin/bash
2+
3+
# This script will modify the devfiles in each stack to use offline resources.
4+
# Stack devfiles will not be modified if there are no starterProjects or if the
5+
# staterProjects have already been modified.
6+
# - starterProjects will be commented out and replaced with a zip block with
7+
# the location to the offline project in the stack root
8+
9+
# Path of stacks directory in the registry
10+
STACKS_DIR=/registry/stacks
11+
# Automated comment used to check whether the devfile has already been modified
12+
MODIFIED_MESSAGE="# AUTOMATED MODIFICATION -"
13+
14+
cleanup() {
15+
rm -f "$devfile.tmp"
16+
rm -f "$offline_starter_projects"
17+
}
18+
19+
trap cleanup EXIT
20+
21+
comment_out_starter_project() {
22+
stack_root=$1
23+
name=$2
24+
devfile=$3
25+
26+
has_starterProjects=$(yq e '.starterProjects' $devfile 2> /dev/null)
27+
if [[ $has_starterProjects != null ]]
28+
then
29+
# the first line of the diff needs to be removed; example of the diff:
30+
# 44a45,52
31+
# > starterProjects:
32+
# > - name: go-starter
33+
# ...
34+
#
35+
# 's/^../#/' replaces the first 2 characters of each line with #
36+
diff="$(diff -b <(yq e 'del(.starterProjects)' $devfile) $devfile | tail -n +2)"
37+
starter_projects="$(printf '%s\n' "$diff" | sed 's/^../#/')"
38+
39+
# comment out the starter projects
40+
yq e 'del(.starterProjects)' "$devfile" > "$devfile.tmp"
41+
echo "# Commented out original starter projects" >> "$devfile.tmp"
42+
echo "$starter_projects" >> "$devfile.tmp"
43+
mv "$devfile.tmp" "$devfile"
44+
fi
45+
}
46+
47+
# Updates a devfile to use an offline starter project
48+
new_starter_projects() {
49+
stack_root=$1
50+
name=$2
51+
devfile=$3
52+
offline_starter_projects=$4
53+
54+
project="
55+
- name: ${name}
56+
zip:
57+
location: ${name}-offline.zip"
58+
59+
echo -n "$project" >> $offline_starter_projects
60+
}
61+
62+
# Read stacks list
63+
read -r -a stacks <<< "$(ls ${STACKS_DIR} | tr '\n' ' ')"
64+
65+
echo "Updating devfiles.."
66+
for stack in ${stacks[@]}
67+
do
68+
stack_root=$STACKS_DIR/$stack
69+
stack_devfile=$stack_root/devfile.yaml
70+
# Exit early on failure to avoid bad overwriting of the devfile
71+
offline_starter_projects=$(mktemp) || exit 1
72+
# Overwrite the temp file for each stack
73+
echo -n "" > $offline_starter_projects
74+
# Read version list for stack
75+
read -r -a versions <<< "$(ls ${STACKS_DIR}/${stack} | grep -e '[0-9].[0-9].[0-9]' | tr '\n' ' ')"
76+
77+
# If multi version stack
78+
if [[ ${#versions[@]} -gt 0 ]]
79+
then
80+
for version in ${versions[@]}
81+
do
82+
stack_root=$STACKS_DIR/$stack/$version
83+
stack_devfile=$stack_root/devfile.yaml
84+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
85+
echo -n "starterProjects:" > $offline_starter_projects
86+
87+
if [[ $starter_projects == "" ]]
88+
then
89+
echo "Skipping stack ${stack} version ${version}: no starter projects found."
90+
continue
91+
fi
92+
93+
for starter_project in $starter_projects
94+
do
95+
if ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile";
96+
then
97+
echo "Updating the ${starter_project} starter project in stack ${stack} version ${version}.."
98+
new_starter_projects $stack_root $starter_project $stack_devfile $offline_starter_projects
99+
comment_out_starter_project $stack_root $starter_project $stack_devfile
100+
echo "Updating the ${starter_project} starter project in stack ${stack} version ${version}..done!"
101+
else
102+
echo "The ${starter_project} starter project in stack ${stack} version ${version} has already been modified."
103+
fi
104+
done
105+
106+
# Only write to the devfile if starter projects have been commented out
107+
has_starterProjects=$(yq e '.starterProjects' $stack_devfile 2> /dev/null)
108+
if [[ $has_starterProjects == null ]] && ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile"
109+
then
110+
echo "${MODIFIED_MESSAGE} Updated starterProjects to use offline versions" >> $stack_devfile
111+
cat $offline_starter_projects >> $stack_devfile
112+
fi
113+
done
114+
# If not multi version stack
115+
else
116+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
117+
echo -n "starterProjects:" > $offline_starter_projects
118+
119+
if [[ $starter_projects == "" ]]
120+
then
121+
echo "Skipping stack ${stack}: no starter projects found."
122+
continue
123+
fi
124+
125+
for starter_project in $starter_projects
126+
do
127+
if ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile";
128+
then
129+
echo "Updating the ${starter_project} starter project in stack ${stack}.."
130+
new_starter_projects $stack_root $starter_project $stack_devfile $offline_starter_projects
131+
comment_out_starter_project $stack_root $starter_project $stack_devfile
132+
echo "Updating the ${starter_project} starter project in stack ${stack}..done!"
133+
else
134+
echo "The ${starter_project} starter project in stack ${stack} has already been modified."
135+
fi
136+
done
137+
138+
# Only write to the devfile if starter projects have been commented out
139+
has_starterProjects=$(yq e '.starterProjects' $stack_devfile 2> /dev/null)
140+
if [[ $has_starterProjects == null ]] && ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile"
141+
then
142+
echo "${MODIFIED_MESSAGE} Updated starterProjects to use offline versions" >> $stack_devfile
143+
cat $offline_starter_projects >> $stack_devfile
144+
fi
145+
fi
146+
done
147+
echo "Updating devfiles....done!"

0 commit comments

Comments
 (0)