|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Build the Playwright driver bundles from upstream source. |
| 17 | +# |
| 18 | +# Instead of downloading pre-built bundles from cdn.playwright.dev, this script |
| 19 | +# clones microsoft/playwright at the tag matching the desired version and runs |
| 20 | +# upstream's utils/build/build-playwright-driver.sh. That script cross-builds |
| 21 | +# the per-platform bundles (playwright-<version>-<suffix>.zip) that setup.py |
| 22 | +# embeds into the platform wheels -- the same artifacts the CDN serves. |
| 23 | +# |
| 24 | +# A single host builds all platform bundles at once: the upstream script |
| 25 | +# downloads the matching Node.js binary for each target, so the host platform |
| 26 | +# does not constrain which bundles can be produced. |
| 27 | +# |
| 28 | +# This is intentionally a shell script (rather than language-specific code) so |
| 29 | +# the same build step can be shared across the Playwright language forks. |
| 30 | +# |
| 31 | +# Usage: scripts/build_driver.sh <version> |
| 32 | + |
| 33 | +set -euo pipefail |
| 34 | + |
| 35 | +VERSION="${1:-}" |
| 36 | +if [[ -z "$VERSION" ]]; then |
| 37 | + echo "usage: scripts/build_driver.sh <version>" >&2 |
| 38 | + exit 2 |
| 39 | +fi |
| 40 | + |
| 41 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 42 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 43 | +DRIVER_DIR="$REPO_ROOT/driver" |
| 44 | +SOURCE_DIR="$DRIVER_DIR/playwright-src" |
| 45 | +PLAYWRIGHT_REPO="https://github.com/microsoft/playwright" |
| 46 | + |
| 47 | +# Bundle suffixes produced by utils/build/build-playwright-driver.sh. Keep in |
| 48 | +# sync with the "zip_name" values in setup.py. |
| 49 | +SUFFIXES=(mac mac-arm64 linux linux-arm64 win32_x64 win32_arm64) |
| 50 | + |
| 51 | +require_tools() { |
| 52 | + local missing=() |
| 53 | + local tool |
| 54 | + for tool in git node npm bash; do |
| 55 | + if ! command -v "$tool" >/dev/null 2>&1; then |
| 56 | + missing+=("$tool") |
| 57 | + fi |
| 58 | + done |
| 59 | + if [[ ${#missing[@]} -gt 0 ]]; then |
| 60 | + echo "Building the Playwright driver from source requires the following tools," >&2 |
| 61 | + echo "which were not found on PATH: ${missing[*]}." >&2 |
| 62 | + echo "Install Node.js (with npm), git and bash, then retry. On Windows, run the" >&2 |
| 63 | + echo "build from a bash shell (e.g. Git Bash)." >&2 |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +cloned_version() { |
| 69 | + if [[ -f "$SOURCE_DIR/package.json" ]]; then |
| 70 | + (cd "$SOURCE_DIR" && node -p "require('./package.json').version") 2>/dev/null || true |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +clone_source() { |
| 75 | + # Reuse an existing checkout only if it is at the exact version we want; |
| 76 | + # otherwise wipe it so a stale ref can never leak into the bundles. |
| 77 | + if [[ -d "$SOURCE_DIR" && "$(cloned_version)" != "$VERSION" ]]; then |
| 78 | + rm -rf "$SOURCE_DIR" |
| 79 | + fi |
| 80 | + if [[ ! -d "$SOURCE_DIR" ]]; then |
| 81 | + mkdir -p "$DRIVER_DIR" |
| 82 | + echo "Cloning $PLAYWRIGHT_REPO at v$VERSION" |
| 83 | + git clone --depth 1 --branch "v$VERSION" "$PLAYWRIGHT_REPO" "$SOURCE_DIR" |
| 84 | + fi |
| 85 | + local cloned |
| 86 | + cloned="$(cloned_version)" |
| 87 | + if [[ "$cloned" != "$VERSION" ]]; then |
| 88 | + echo "Cloned Playwright source reports version '$cloned' but '$VERSION' was requested." >&2 |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +} |
| 92 | + |
| 93 | +build_source() { |
| 94 | + echo "Installing Playwright dependencies (npm ci)" |
| 95 | + (cd "$SOURCE_DIR" && npm ci) |
| 96 | + echo "Building Playwright (npm run build)" |
| 97 | + (cd "$SOURCE_DIR" && npm run build) |
| 98 | + echo "Building driver bundles" |
| 99 | + (cd "$SOURCE_DIR" && bash utils/build/build-playwright-driver.sh) |
| 100 | +} |
| 101 | + |
| 102 | +copy_bundles() { |
| 103 | + local output_dir="$SOURCE_DIR/utils/build/output" |
| 104 | + # The output dir also holds build intermediates (downloaded Node.js binaries, |
| 105 | + # tgz archives, extracted package dirs), so copy only the versioned bundles |
| 106 | + # rather than the whole directory. |
| 107 | + cp "$output_dir"/playwright-"$VERSION"-*.zip "$DRIVER_DIR/" |
| 108 | + # Fail fast if upstream produced fewer bundles than the platforms we ship. |
| 109 | + local count |
| 110 | + count="$(find "$DRIVER_DIR" -maxdepth 1 -name "playwright-$VERSION-*.zip" | wc -l)" |
| 111 | + if [[ "$count" -ne "${#SUFFIXES[@]}" ]]; then |
| 112 | + echo "Expected ${#SUFFIXES[@]} driver bundles, found $count in $output_dir" >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +require_tools |
| 118 | +clone_source |
| 119 | +build_source |
| 120 | +copy_bundles |
0 commit comments