-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_blockly_assets.sh
More file actions
executable file
·145 lines (126 loc) · 4.68 KB
/
Copy pathbuild_blockly_assets.sh
File metadata and controls
executable file
·145 lines (126 loc) · 4.68 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
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Part of PyBLE (https://pyble.dev) — see /LICENSE.
#
# Build the minimal offline Blockly runtime admitted by ADR-0013. The source
# submodule must remain pristine and at the exact reviewed commit.
set -euo pipefail
readonly EXPECTED_BLOCKLY_SHA='f4ad3f5117d6744120b05ccc5af666fcdc29df5f'
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
readonly REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
readonly UPSTREAM_DIR="$REPO_ROOT/app/upstream/blockly"
readonly BLOCKLY_PACKAGE_DIR="$UPSTREAM_DIR/packages/blockly"
readonly ASSET_DIR="$REPO_ROOT/app/assets/blockly"
readonly VENDOR_DIR="$ASSET_DIR/vendor"
fail() {
printf 'build_blockly_assets: %s\n' "$*" >&2
exit 1
}
[ -e "$UPSTREAM_DIR/.git" ] ||
fail "Blockly submodule is not initialized at $UPSTREAM_DIR"
actual_sha="$(git -C "$UPSTREAM_DIR" rev-parse HEAD 2>/dev/null)" ||
fail 'cannot read the Blockly submodule revision'
[ "$actual_sha" = "$EXPECTED_BLOCKLY_SHA" ] ||
fail "revision mismatch: expected $EXPECTED_BLOCKLY_SHA, got $actual_sha"
git -C "$UPSTREAM_DIR" diff --quiet -- ||
fail 'Blockly submodule has tracked working-tree changes'
git -C "$UPSTREAM_DIR" diff --cached --quiet -- ||
fail 'Blockly submodule has staged changes'
upstream_status="$(
git -C "$UPSTREAM_DIR" status --porcelain --untracked-files=normal
)"
[ -z "$upstream_status" ] ||
fail 'Blockly submodule has untracked files'
command -v npm >/dev/null 2>&1 || fail 'npm is required'
command -v node >/dev/null 2>&1 || fail 'node is required'
command -v file >/dev/null 2>&1 || fail 'file is required'
(
cd "$UPSTREAM_DIR"
npm ci --no-audit --no-fund
npm run build --workspace=blockly
)
git -C "$UPSTREAM_DIR" diff --quiet -- ||
fail 'Blockly build changed tracked upstream files'
git -C "$UPSTREAM_DIR" diff --cached --quiet -- ||
fail 'Blockly build changed staged upstream files'
upstream_status="$(
git -C "$UPSTREAM_DIR" status --porcelain --untracked-files=normal
)"
[ -z "$upstream_status" ] ||
fail 'Blockly build left untracked upstream files'
readonly CORE_SOURCE="$BLOCKLY_PACKAGE_DIR/dist/blockly_compressed.js"
readonly BLOCKS_SOURCE="$BLOCKLY_PACKAGE_DIR/dist/blocks_compressed.js"
readonly PYTHON_SOURCE="$BLOCKLY_PACKAGE_DIR/dist/python_compressed.js"
readonly EN_SOURCE="$BLOCKLY_PACKAGE_DIR/build/msg/en.js"
readonly CATEGORY_EN_SOURCE="$BLOCKLY_PACKAGE_DIR/demos/code/msg/en.js"
readonly MEDIA_SOURCE="$BLOCKLY_PACKAGE_DIR/media"
readonly LICENSE_SOURCE="$UPSTREAM_DIR/LICENSE"
for required_path in \
"$CORE_SOURCE" \
"$BLOCKS_SOURCE" \
"$PYTHON_SOURCE" \
"$EN_SOURCE" \
"$CATEGORY_EN_SOURCE" \
"$MEDIA_SOURCE" \
"$LICENSE_SOURCE"; do
[ -e "$required_path" ] ||
fail "expected build output is missing: $required_path"
done
mkdir -p "$ASSET_DIR"
BUILD_TMP="$(mktemp -d "$ASSET_DIR/.vendor.XXXXXX")"
readonly BUILD_TMP
cleanup() {
if [ -n "${BUILD_TMP:-}" ] && [ -d "$BUILD_TMP" ]; then
rm -rf -- "$BUILD_TMP"
fi
}
trap cleanup EXIT
mkdir -p "$BUILD_TMP/msg" "$BUILD_TMP/media"
cp "$CORE_SOURCE" "$BUILD_TMP/blockly_compressed.js"
cp "$BLOCKS_SOURCE" "$BUILD_TMP/blocks_compressed.js"
node "$SCRIPT_DIR/normalize_blockly_python_asset.mjs" \
"$PYTHON_SOURCE" \
"$BUILD_TMP/python_compressed.js"
cp "$EN_SOURCE" "$BUILD_TMP/msg/en.js"
cp "$CATEGORY_EN_SOURCE" "$BUILD_TMP/msg/categories_en.js"
cp -R "$MEDIA_SOURCE/." "$BUILD_TMP/media/"
cp "$LICENSE_SOURCE" "$BUILD_TMP/LICENSE"
printf '%s\n' \
'{' \
' "SPDX-License-Identifier": "MIT",' \
' "name": "Blockly offline runtime",' \
' "source": "https://github.com/RaspberryPiFoundation/blockly",' \
" \"commit\": \"$EXPECTED_BLOCKLY_SHA\"," \
' "release": "blockly-v13.1.0",' \
' "upstreamLicense": "Apache-2.0",' \
' "upstreamLicenseFile": "LICENSE",' \
' "generatedBy": "tools/build_blockly_assets.sh",' \
' "contents": [' \
' "blockly_compressed.js",' \
' "blocks_compressed.js",' \
' "python_compressed.js",' \
' "msg/en.js",' \
' "msg/categories_en.js",' \
' "media/"' \
' ]' \
'}' \
>"$BUILD_TMP/manifest.json"
find "$BUILD_TMP" -type d -exec chmod 0755 {} +
find "$BUILD_TMP" -type f -exec chmod 0644 {} +
while IFS= read -r -d '' vendor_asset; do
asset_kind="$(file -b "$vendor_asset")"
case "$asset_kind" in
*"script"*"executable"*)
fail "vendored data looks executable: $vendor_asset ($asset_kind)"
;;
esac
done < <(find "$BUILD_TMP" -type f -print0)
case "$VENDOR_DIR" in
"$REPO_ROOT/app/assets/blockly/vendor") ;;
*) fail "refusing to replace unexpected path: $VENDOR_DIR" ;;
esac
rm -rf -- "$VENDOR_DIR"
mv "$BUILD_TMP" "$VENDOR_DIR"
trap - EXIT
printf 'build_blockly_assets: wrote %s from %s\n' \
"$VENDOR_DIR" "$EXPECTED_BLOCKLY_SHA"