-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·247 lines (211 loc) · 8.23 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·247 lines (211 loc) · 8.23 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
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps unzip nix-prefetch nix-prefetch-github
# shellcheck shell=bash
set -euo pipefail
set -x
root="$(dirname "$(readlink -f "$0")")"
repo_root="$(git -C "$root" rev-parse --show-toplevel)"
cd "$repo_root"
github_api_curl_args=()
if [ -n "${GITHUB_TOKEN:-}" ]; then
github_api_curl_args=(-u ":$GITHUB_TOKEN")
fi
playwright_browsers_file="$root/playwright-driver/browsers.json"
playwright_driver_file="$root/playwright-driver/driver.nix"
playwright_raw_repo_url="https://raw.githubusercontent.com/microsoft/playwright"
driver_version=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s https://api.github.com/repos/microsoft/playwright/releases/latest | jq -r '.tag_name | sub("^v"; "")')
browser_names=(chromium chromium-headless-shell firefox webkit ffmpeg)
browser_platforms=(linux darwin)
github_api_get() {
curl "${github_api_curl_args[@]}" -fsSL "$1"
}
major_minor() {
echo "${1%.*}"
}
sed -i "s|version =\s*\"[^\$]*\"|version = \"$driver_version\"|" "$playwright_driver_file"
driver_new_hash=$(nix-prefetch-github --rev "v$driver_version" "Microsoft" "playwright" | jq -r '.hash')
sed -i "s|hash =\s*\"[^\$]*\"|hash = \"$driver_new_hash\"|" "$playwright_driver_file"
temp_dir=$(mktemp -d)
trap 'rm -rf "$temp_dir"' EXIT
# update binaries of browsers, used by playwright.
replace_sha() {
local target_file="$1"
local attr_name="$2"
local new_hash="$3"
sed -i "s|$attr_name = \".\{44,52\}\"|$attr_name = \"$new_hash\"|" "$target_file"
}
prefetch_browser() {
local url="$1"
local strip_root="$2"
# nix-prefetch is used to obtain sha with `stripRoot = false`
# doesn't work on macOS https://github.com/msteen/nix-prefetch/issues/53
nix-prefetch --option extra-experimental-features flakes -q "{ stdenv, fetchzip }: stdenv.mkDerivation { name=\"browser\"; src = fetchzip { url = \"$url\"; stripRoot = $strip_root; }; }"
}
get_revision() {
local name="$1"
local platform="$2"
local arch="$3"
local base_revision="$4"
local override_key=""
# Determine the revision override key based on platform and arch
if [ "$platform" = "darwin" ]; then
if [ "$name" = "webkit" ]; then
if [ "$arch" = "x86_64" ]; then
override_key="mac14"
else
override_key="mac14-arm64"
fi
elif [ "$name" = "ffmpeg" ]; then
if [ "$arch" = "x86_64" ]; then
override_key="mac12"
else
override_key="mac12-arm64"
fi
fi
fi
# Check for revision override
if [ -n "$override_key" ]; then
local override_revision
override_revision="$(jq -r ".browsers[\"$name\"].revisionOverrides[\"$override_key\"] // empty" "$playwright_browsers_file")"
if [ -n "$override_revision" ]; then
echo "$override_revision"
return
fi
fi
echo "$base_revision"
}
browser_download_url() {
local name="$1"
local buildname="$2"
local platform="$3"
local arch="$4"
local revision="$5"
local browser_version="$6"
local suffix="$7"
local artifact
local cft_platform
# Chromium and chromium-headless-shell use Chrome for Testing artifacts on
# Linux/macOS on x86_64 and aarch64-darwin.
if [ "$name" = "chromium" ] || [ "$name" = "chromium-headless-shell" ]; then
if [ "$name" = "chromium" ]; then
artifact="chrome"
else
artifact="chrome-headless-shell"
fi
if [ "$platform" = "linux" ] && [ "$arch" = "x86_64" ]; then
echo "https://cdn.playwright.dev/chrome-for-testing-public/${browser_version}/linux64/${artifact}-linux64.zip"
return
fi
if [ "$platform" = "darwin" ]; then
if [ "$arch" = "x86_64" ]; then
cft_platform="mac-x64"
else
cft_platform="mac-arm64"
fi
echo "https://cdn.playwright.dev/chrome-for-testing-public/${browser_version}/${cft_platform}/${artifact}-${cft_platform}.zip"
return
fi
fi
# Webkit on darwin uses a different CDN path
if [ "$name" = "webkit" ] && [ "$platform" = "darwin" ]; then
echo "https://cdn.playwright.dev/builds/${buildname}/${revision}/${name}-${suffix}.zip"
return
fi
echo "https://cdn.playwright.dev/dbazure/download/playwright/builds/${buildname}/${revision}/${name}-${suffix}.zip"
}
update_browser() {
local name="$1"
local platform="$2"
local stripRoot="false"
local suffix
local aarch64_suffix
local buildname
local revision
local browser_version
local x86_64_url
local aarch64_url
if [ "$platform" = "darwin" ]; then
if [ "$name" = "webkit" ]; then
suffix="mac-14"
else
suffix="mac"
fi
else
if [ "$name" = "ffmpeg" ] || [ "$name" = "chromium-headless-shell" ]; then
suffix="linux"
elif [ "$name" = "chromium" ]; then
stripRoot="true"
suffix="linux"
elif [ "$name" = "firefox" ]; then
stripRoot="true"
suffix="ubuntu-24.04"
else
suffix="ubuntu-24.04"
fi
fi
aarch64_suffix="$suffix-arm64"
if [ "$name" = "chromium-headless-shell" ]; then
buildname="chromium"
else
buildname="$name"
fi
local base_revision
base_revision="$(jq -r ".browsers[\"$buildname\"].revision" "$playwright_browsers_file")"
browser_version="$(jq -r ".browsers[\"$buildname\"].browserVersion // empty" "$playwright_browsers_file")"
# Get platform-specific revisions (handles revisionOverrides)
local x86_64_revision
local aarch64_revision
x86_64_revision="$(get_revision "$name" "$platform" "x86_64" "$base_revision")"
aarch64_revision="$(get_revision "$name" "$platform" "aarch64" "$base_revision")"
x86_64_url="$(browser_download_url "$name" "$buildname" "$platform" "x86_64" "$x86_64_revision" "$browser_version" "$suffix")"
aarch64_url="$(browser_download_url "$name" "$buildname" "$platform" "aarch64" "$aarch64_revision" "$browser_version" "$aarch64_suffix")"
replace_sha "$root/playwright-driver/$name.nix" "x86_64-$platform" \
"$(prefetch_browser "$x86_64_url" "$stripRoot")"
replace_sha "$root/playwright-driver/$name.nix" "aarch64-$platform" \
"$(prefetch_browser "$aarch64_url" "$stripRoot")"
}
curl -fsSL \
"https://raw.githubusercontent.com/microsoft/playwright/v${driver_version}/packages/playwright-core/browsers.json" \
| jq '
.comment = "This file is kept up to date via update.sh"
| .browsers |= (
[.[]
| select(.installByDefault) | del(.installByDefault)]
| map({(.name): . | del(.name)})
| add
)
' > "$playwright_browsers_file"
for platform in "${browser_platforms[@]}"; do
for browser in "${browser_names[@]}"; do
update_browser "$browser" "$platform"
done
done
# Update package-lock.json files for all npm deps that are built in playwright
# Download `package-lock.json` for a given sourceRoot path and update its hash.
update_hash() {
local source_root_path="$1"
local download_url
local lock_file
local new_hash
local source_root_pattern
download_url="${playwright_raw_repo_url}/v${driver_version}${source_root_path}/package-lock.json"
lock_file="${temp_dir}/$(echo "$source_root_path" | tr '/.' '__').package-lock.json"
curl -fsSL -o "$lock_file" "$download_url"
new_hash=$(prefetch-npm-deps "$lock_file")
source_root_pattern=$(printf '%s\n' "$source_root_path" | sed 's/[][\\/.*^$+?(){}|]/\\&/g')
sed -E -i "/sourceRoot = \"\\\$\\{src.name\\}${source_root_pattern}\";/,/npmDepsHash = / s#npmDepsHash = \"[^\"]*\";#npmDepsHash = \"${new_hash}\";#" "$playwright_driver_file"
}
while IFS= read -r source_root_path; do
update_hash "$source_root_path"
done < <(
# shellcheck disable=SC2016
sed -n 's#^[[:space:]]*sourceRoot = "${src.name}\(.*\)";.*$#\1#p' "$playwright_driver_file"
)
echo "$driver_version" > "$root/version.txt"
# Check if files have changed
if git diff --exit-code; then
echo "No changes"
echo "updated=false" > updated.txt
exit 0
fi
echo "updated=true" > updated.txt