Repository navigation
Expand file tree
/
Copy pathjs-stack-build.sh
More file actions
executable file
·267 lines (224 loc) · 6.64 KB
/
Copy pathjs-stack-build.sh
File metadata and controls
executable file
·267 lines (224 loc) · 6.64 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
# Build @processmaker/* packages in dependency order, cascading downstream.
# Reads processmaker.downstream from each package.json; runs processmaker.build or npm run build-bundle.
set -euo pipefail
STACK_DIR="${STACK_DIR:-js-stack}"
STACK_DIR="$(cd "$(dirname "$STACK_DIR")" 2>/dev/null && pwd)/$(basename "$STACK_DIR")" || STACK_DIR="$(pwd)/${STACK_DIR}"
MANIFEST="${STACK_DIR}/manifest.json"
BRANCH_MAP="${BRANCH_MAP:-"{}"}"
TRIGGER_REPO="${TRIGGER_REPO:-}"
TRIGGER_REF="${TRIGGER_REF:-}"
TRIGGER_BRANCH="${TRIGGER_BRANCH:-}"
GH_ORG="${GH_ORG:-ProcessMaker}"
declare -A NPM_NAMES=(
[vue-multiselect]=vue-multiselect
[vue-form-elements]=vue-form-elements
[screen-builder]=screen-builder
[modeler]=modeler
[processmaker-bpmn-moddle]=processmaker-bpmn-moddle
[processmaker]=processmaker
)
ALL_PACKAGES=(vue-multiselect vue-form-elements screen-builder modeler processmaker-bpmn-moddle processmaker)
is_known_package() {
local pkg="$1"
for p in "${ALL_PACKAGES[@]}"; do
[[ "$p" == "$pkg" ]] && return 0
done
return 1
}
fetch_package_json() {
local repo="$1"
local ref="$2"
ref=$(normalize_ref "$ref")
gh api "repos/${GH_ORG}/${repo}/contents/package.json?ref=${ref}" \
-H "Accept: application/vnd.github.raw" 2>/dev/null
}
default_branch() {
local repo="$1"
gh api "repos/${GH_ORG}/${repo}" --jq .default_branch
}
normalize_ref() {
local ref="$1"
ref="${ref#refs/heads/}"
ref="${ref#refs/tags/}"
echo "$ref"
}
is_commit_sha() {
[[ "$1" =~ ^[0-9a-f]{40}$ ]]
}
branch_exists() {
local repo="$1"
local branch="$2"
gh api "repos/${GH_ORG}/${repo}/branches/${branch}" >/dev/null 2>&1
}
cascade_branch() {
local branch="${TRIGGER_BRANCH:-}"
if [[ -z "$branch" ]]; then
branch=$(normalize_ref "${TRIGGER_REF:-}")
if is_commit_sha "$branch"; then
return 1
fi
fi
[[ -n "$branch" ]] || return 1
echo "$branch"
}
resolve_ref() {
local repo="$1"
local from_map
from_map=$(echo "$BRANCH_MAP" | jq -r --arg r "$repo" '.[$r] // empty')
if [[ -n "$from_map" ]]; then
echo "$from_map"
return
fi
if [[ "$repo" == "$TRIGGER_REPO" && -n "$TRIGGER_REF" ]]; then
echo "$TRIGGER_REF"
return
fi
local shared_branch
if shared_branch=$(cascade_branch); then
if branch_exists "$repo" "$shared_branch"; then
echo "$shared_branch"
return
fi
fi
default_branch "$repo"
}
get_downstream() {
jq -r '(.processmaker.downstream // [])[]?'
}
get_pm_deps() {
jq -r '
[.dependencies, .devDependencies, .peerDependencies]
| map(select(. != null))
| add // {}
| to_entries[]
| select(.key | startswith("@processmaker/"))
| .key
| ltrimstr("@processmaker/")
'
}
uses_yarn() {
[[ -f yarn.lock ]]
}
declare -A BUILD_SET=()
declare -A EXPLICIT_SET=()
add_downstream() {
local repo="$1"
local ref pkg_json down
ref=$(resolve_ref "$repo")
pkg_json=$(fetch_package_json "$repo" "$ref") || return 0
while IFS= read -r down; do
[[ -z "$down" ]] && continue
is_known_package "$down" || continue
if [[ -z "${BUILD_SET[$down]:-}" ]]; then
BUILD_SET[$down]=1
add_downstream "$down"
fi
done < <(echo "$pkg_json" | get_downstream)
}
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
is_known_package "$repo" || continue
BUILD_SET[$repo]=1
EXPLICIT_SET[$repo]=1
done < <(echo "$BRANCH_MAP" | jq -r 'keys[]?')
if [[ -n "$TRIGGER_REPO" ]] && is_known_package "$TRIGGER_REPO" && [[ "$TRIGGER_REPO" != "processmaker" ]]; then
BUILD_SET[$TRIGGER_REPO]=1
EXPLICIT_SET[$TRIGGER_REPO]=1
fi
if [[ ${#BUILD_SET[@]} -eq 0 ]]; then
echo "No JS packages to build."
mkdir -p "$STACK_DIR"
echo '{}' > "$MANIFEST"
exit 0
fi
# Downstream cascade for everything in the build set
for _pass in 1 2 3 4 5; do
for repo in "${!BUILD_SET[@]}"; do
add_downstream "$repo"
done
done
echo "Packages to build: ${!BUILD_SET[*]}"
FINAL_ORDER=()
declare -A VISITED_SORT=()
topo_visit() {
local repo="$1"
[[ -n "${VISITED_SORT[$repo]:-}" ]] && return 0
VISITED_SORT[$repo]=1
local ref pkg_json dep
ref=$(resolve_ref "$repo")
pkg_json=$(fetch_package_json "$repo" "$ref") || { FINAL_ORDER+=("$repo"); return 0; }
while IFS= read -r dep; do
[[ -z "$dep" ]] && continue
[[ -z "${BUILD_SET[$dep]:-}" ]] && continue
topo_visit "$dep"
done < <(echo "$pkg_json" | get_pm_deps)
FINAL_ORDER+=("$repo")
}
for repo in "${!BUILD_SET[@]}"; do
topo_visit "$repo"
done
echo "Build order: ${FINAL_ORDER[*]}"
mkdir -p "$STACK_DIR/tarballs"
echo '{}' > "$MANIFEST"
BUILT_TARBALLS=()
build_package() {
local repo="$1"
local ref workdir
ref=$(normalize_ref "$(resolve_ref "$repo")")
workdir=$(mktemp -d)
echo "=== Building ${repo} @ ${ref} ==="
if is_commit_sha "$ref"; then
git clone "https://github.com/${GH_ORG}/${repo}.git" "$workdir"
git -C "$workdir" checkout "$ref"
elif ! git clone --depth 1 --branch "$ref" "https://github.com/${GH_ORG}/${repo}.git" "$workdir" 2>/dev/null; then
git clone --depth 1 "https://github.com/${GH_ORG}/${repo}.git" "$workdir"
git -C "$workdir" checkout "$ref"
fi
cd "$workdir"
if uses_yarn; then
yarn install --frozen-lockfile 2>/dev/null || yarn install
else
npm ci
fi
if [[ ${#BUILT_TARBALLS[@]} -gt 0 ]]; then
echo "Installing upstream tarballs in one npm install: ${BUILT_TARBALLS[*]}"
npm install "${BUILT_TARBALLS[@]}" --no-save --force
fi
local build_cmd
build_cmd=$(jq -r '.processmaker.build // empty' package.json)
if [[ -n "$build_cmd" && "$build_cmd" != "null" ]]; then
echo "Running: ${build_cmd}"
bash -c "$build_cmd"
elif jq -e '.scripts["build-bundle"]' package.json >/dev/null; then
npm run build-bundle
else
echo "No build-bundle script; skipping build"
fi
local pkg_name tarball dest
pkg_name=$(jq -r .name package.json)
tarball=$(npm pack --json | jq -r '.[0].filename // empty')
[[ -z "$tarball" ]] && tarball=$(npm pack | tail -n 1)
if [[ -z "$tarball" || ! -f "$tarball" ]]; then
echo "::error::npm pack failed for ${repo}"
exit 1
fi
dest="${STACK_DIR}/tarballs/${repo}.tgz"
cp "$tarball" "$dest"
BUILT_TARBALLS+=("$dest")
local npm_pkg MANIFEST_TMP
npm_pkg="@processmaker/${NPM_NAMES[$repo]:-$repo}"
MANIFEST_TMP=$(mktemp)
jq --arg pkg "$npm_pkg" --arg repo "$repo" --arg path "tarballs/${repo}.tgz" --arg ref "$ref" \
'. + {($pkg): {repo: $repo, ref: $ref, tarball: $path}}' "$MANIFEST" > "$MANIFEST_TMP"
mv "$MANIFEST_TMP" "$MANIFEST"
echo "Packed ${pkg_name} -> ${dest}"
cd - >/dev/null
rm -rf "$workdir"
}
for repo in "${FINAL_ORDER[@]}"; do
[[ "$repo" == "processmaker" ]] && continue
build_package "$repo"
done
echo "=== JS stack manifest ==="
cat "$MANIFEST"