Skip to content

Commit 4fe0edc

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

File tree

2 files changed

+240
-34
lines changed

2 files changed

+240
-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!"
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/bin/bash
2+
3+
# This script will modify the devfiles in each stack to use offline resources.
4+
5+
# Path of stacks directory in the registry
6+
STACKS_DIR=/registry/stacks
7+
# Automated comment used to check whether the devfile has already been modified
8+
MODIFIED_MESSAGE="# AUTOMATED MODIFICATION -"
9+
10+
cleanup() {
11+
rm -f "$devfile.tmp"
12+
rm -f "$offline_starter_projects"
13+
}
14+
15+
trap cleanup EXIT
16+
17+
comment_out_starter_project() {
18+
stack_root=$1
19+
name=$2
20+
devfile=$3
21+
22+
has_starterProjects=$(yq e '.starterProjects' $devfile 2> /dev/null)
23+
if [[ $has_starterProjects != null ]]
24+
then
25+
# the first line of the diff needs to be removed; example of the diff:
26+
# 44a45,52
27+
# > starterProjects:
28+
# > - name: go-starter
29+
# ...
30+
#
31+
# 's/^../#/' replaces the first 2 characters of each line with #
32+
diff="$(diff -b <(yq e 'del(.starterProjects)' $devfile) $devfile | tail -n +2)"
33+
starter_projects="$(printf '%s\n' "$diff" | sed 's/^../#/')"
34+
35+
# comment out the starter projects
36+
yq e 'del(.starterProjects)' "$devfile" > "$devfile.tmp"
37+
echo "# Commented out original starter projects" >> "$devfile.tmp"
38+
echo "$starter_projects" >> "$devfile.tmp"
39+
mv "$devfile.tmp" "$devfile"
40+
fi
41+
}
42+
43+
# Updates a devfile to use an offline starter project
44+
new_starter_projects() {
45+
stack_root=$1
46+
name=$2
47+
devfile=$3
48+
offline_starter_projects=$4
49+
50+
project="
51+
- name: ${name}
52+
zip:
53+
location: ${name}-offline.zip"
54+
55+
echo -n "$project" >> $offline_starter_projects
56+
}
57+
58+
# Read stacks list
59+
read -r -a stacks <<< "$(ls ${STACKS_DIR} | tr '\n' ' ')"
60+
61+
echo "Updating devfiles.."
62+
for stack in ${stacks[@]}
63+
do
64+
stack_root=$STACKS_DIR/$stack
65+
stack_devfile=$stack_root/devfile.yaml
66+
# Exit early on failure to avoid bad overwriting of the devfile
67+
offline_starter_projects=$(mktemp) || exit 1
68+
# Overwrite the temp file for each stack
69+
echo -n "" > $offline_starter_projects
70+
# Read version list for stack
71+
read -r -a versions <<< "$(ls ${STACKS_DIR}/${stack} | grep -e '[0-9].[0-9].[0-9]' | tr '\n' ' ')"
72+
73+
# If multi version stack
74+
if [[ ${#versions[@]} -gt 0 ]]
75+
then
76+
for version in ${versions[@]}
77+
do
78+
stack_root=$STACKS_DIR/$stack/$version
79+
stack_devfile=$stack_root/devfile.yaml
80+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
81+
echo -n "starterProjects:" > $offline_starter_projects
82+
83+
for starter_project in $starter_projects
84+
do
85+
if ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile";
86+
then
87+
echo "Updating the ${starter_project} starter project in stack ${stack} version ${version}.."
88+
new_starter_projects $stack_root $starter_project $stack_devfile $offline_starter_projects
89+
comment_out_starter_project $stack_root $starter_project $stack_devfile
90+
echo "Updating the ${starter_project} starter project in stack ${stack} version ${version}..done!"
91+
else
92+
echo "The ${starter_project} starter project in stack ${stack} version ${version} has already been modified."
93+
fi
94+
done
95+
96+
# Only write to the devfile if starter projects have been commented out
97+
has_starterProjects=$(yq e '.starterProjects' $stack_devfile 2> /dev/null)
98+
if [[ $has_starterProjects == null ]] && ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile"
99+
then
100+
echo "${MODIFIED_MESSAGE} Updated starterProjects to use offline versions" >> $stack_devfile
101+
cat $offline_starter_projects >> $stack_devfile
102+
fi
103+
done
104+
# If not multi version stack
105+
else
106+
starter_projects="$(yq e ".starterProjects[].name" $stack_devfile)"
107+
echo -n "starterProjects:" > $offline_starter_projects
108+
109+
for starter_project in $starter_projects
110+
do
111+
if ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile";
112+
then
113+
echo "Updating the ${starter_project} starter project in stack ${stack}.."
114+
new_starter_projects $stack_root $starter_project $stack_devfile $offline_starter_projects
115+
comment_out_starter_project $stack_root $starter_project $stack_devfile
116+
echo "Updating the ${starter_project} starter project in stack ${stack}..done!"
117+
else
118+
echo "The ${starter_project} starter project in stack ${stack} has already been modified."
119+
fi
120+
done
121+
122+
# Only write to the devfile if starter projects have been commented out
123+
has_starterProjects=$(yq e '.starterProjects' $stack_devfile 2> /dev/null)
124+
if [[ $has_starterProjects == null ]] && ! grep -q "$MODIFIED_MESSAGE" "$stack_devfile"
125+
then
126+
echo "${MODIFIED_MESSAGE} Updated starterProjects to use offline versions" >> $stack_devfile
127+
cat $offline_starter_projects >> $stack_devfile
128+
fi
129+
fi
130+
done
131+
echo "Updating devfiles....done!"

0 commit comments

Comments
 (0)