1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps unzip
3set -euo pipefail
4
5root="$(dirname "$(readlink -f "$0")")"
6
7version=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s https://api.github.com/repos/microsoft/playwright-python/releases/latest | jq -r '.tag_name | sub("^v"; "")')
8# Most of the time, this should be the latest stable release of the Node-based
9# Playwright version, but that isn't a guarantee, so this needs to be specified
10# as well:
11setup_py_url="https://github.com/microsoft/playwright-python/raw/v${version}/setup.py"
12driver_version=$(curl -Ls "$setup_py_url" | grep '^driver_version =' | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
13
14# TODO: skip if update-source-version reported the same version
15update-source-version playwright-driver "$driver_version"
16update-source-version python3Packages.playwright "$version"
17
18playwright_dir="$root/../../web/playwright"
19driver_file="$playwright_dir/driver.nix"
20repo_url_prefix="https://github.com/microsoft/playwright/raw"
21
22temp_dir=$(mktemp -d)
23trap 'rm -rf "$temp_dir"' EXIT
24
25
26
27# update binaries of browsers, used by playwright.
28replace_sha() {
29 sed -i "s|$2 = \".\{44,52\}\"|$2 = \"$3\"|" "$1"
30}
31
32prefetch_browser() {
33 nix store prefetch-file --json --hash-type sha256 --unpack "$1" | jq -r .hash
34}
35
36update_browser() {
37 name="$1"
38 suffix="$2"
39 arm64_suffix="${3:-$2-arm64}"
40 revision="$(jq -r ".browsers.$name.revision" "$playwright_dir/browsers.json")"
41 replace_sha "$playwright_dir/$name.nix" "x86_64-linux" \
42 "$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$suffix.zip")"
43 replace_sha "$playwright_dir/$name.nix" "aarch64-linux" \
44 "$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$arm64_suffix.zip")"
45}
46
47curl -fsSl \
48 "https://raw.githubusercontent.com/microsoft/playwright/v${driver_version}/packages/playwright-core/browsers.json" \
49 | jq '
50 .comment = "This file is kept up to date via update.sh"
51 | .browsers |= (
52 [.[]
53 | select(.installByDefault) | del(.installByDefault)]
54 | map({(.name): . | del(.name)})
55 | add
56 )
57 ' > "$playwright_dir/browsers.json"
58
59# We currently use Chromium from nixpkgs, so we don't need to download it here
60# Likewise, darwin can be ignored here atm as we are using an impure install anyway.
61update_browser "firefox" "ubuntu-22.04"
62update_browser "webkit" "ubuntu-22.04"
63update_browser "ffmpeg" "linux"
64
65
66# Update package-lock.json files for all npm deps that are built in playwright
67
68# Function to download `package-lock.json` for a given source path and update hash
69update_hash() {
70 local source_root_path="$1"
71 local existing_hash="$2"
72
73 # Formulate download URL
74 local download_url="${repo_url_prefix}/v${driver_version}${source_root_path}/package-lock.json"
75 # Download package-lock.json to temporary directory
76 curl -fsSL -o "${temp_dir}/package-lock.json" "$download_url"
77
78 # Calculate the new hash
79 local new_hash
80 new_hash=$(prefetch-npm-deps "${temp_dir}/package-lock.json")
81
82 # Update npmDepsHash in the original file
83 sed -i "s|$existing_hash|${new_hash}|" "$driver_file"
84}
85
86while IFS= read -r source_root_line; do
87 [[ "$source_root_line" =~ sourceRoot ]] || continue
88 source_root_path=$(echo "$source_root_line" | sed -e 's/^.*"${src.name}\(.*\)";.*$/\1/')
89 # Extract the current npmDepsHash for this sourceRoot
90 existing_hash=$(grep -A1 "$source_root_line" "$driver_file" | grep 'npmDepsHash' | sed -e 's/^.*npmDepsHash = "\(.*\)";$/\1/')
91
92 # Call the function to download and update the hash
93 update_hash "$source_root_path" "$existing_hash"
94done < "$driver_file"