|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +usage() { |
| 6 | + cat <<'USAGE' |
| 7 | +Usage: prepare-package.sh [options] |
| 8 | +
|
| 9 | +Options: |
| 10 | + --package-file PATH Path to the Package.swift file (default: Package.swift) |
| 11 | + --is-pr true|false Whether this run simulates a pull request (default: false) |
| 12 | + --remove-duplicate true|false |
| 13 | + Whether to strip duplicate targets (default: false) |
| 14 | + --change-path true|false Whether to swap SPM binary URLs for local paths (default: false) |
| 15 | + --only-keep-distribution true|false |
| 16 | + Whether to keep only SentryDistribution product/target (default: false) |
| 17 | + -h, --help Show this help message |
| 18 | +USAGE |
| 19 | +} |
| 20 | + |
| 21 | +is_enabled() { |
| 22 | + case "$1" in |
| 23 | + true|TRUE|1|yes|YES|on|ON) return 0 ;; |
| 24 | + *) return 1 ;; |
| 25 | + esac |
| 26 | +} |
| 27 | + |
| 28 | +PACKAGE_FILE="Package.swift" |
| 29 | +IS_PR="false" |
| 30 | +REMOVE_DUPLICATE="false" |
| 31 | +CHANGE_PATH="false" |
| 32 | +ONLY_KEEP_DISTRIBUTION="false" |
| 33 | + |
| 34 | +while [[ $# -gt 0 ]]; do |
| 35 | + case "$1" in |
| 36 | + --package-file) |
| 37 | + [[ $# -lt 2 ]] && { echo "Missing value for $1" >&2; exit 1; } |
| 38 | + PACKAGE_FILE="$2" |
| 39 | + shift 2 |
| 40 | + ;; |
| 41 | + --is-pr) |
| 42 | + [[ $# -lt 2 ]] && { echo "Missing value for $1" >&2; exit 1; } |
| 43 | + IS_PR="$2" |
| 44 | + shift 2 |
| 45 | + ;; |
| 46 | + --remove-duplicate) |
| 47 | + [[ $# -lt 2 ]] && { echo "Missing value for $1" >&2; exit 1; } |
| 48 | + REMOVE_DUPLICATE="$2" |
| 49 | + shift 2 |
| 50 | + ;; |
| 51 | + --change-path) |
| 52 | + [[ $# -lt 2 ]] && { echo "Missing value for $1" >&2; exit 1; } |
| 53 | + CHANGE_PATH="$2" |
| 54 | + shift 2 |
| 55 | + ;; |
| 56 | + --only-keep-distribution) |
| 57 | + [[ $# -lt 2 ]] && { echo "Missing value for $1" >&2; exit 1; } |
| 58 | + ONLY_KEEP_DISTRIBUTION="$2" |
| 59 | + shift 2 |
| 60 | + ;; |
| 61 | + -h|--help) |
| 62 | + usage |
| 63 | + exit 0 |
| 64 | + ;; |
| 65 | + *) |
| 66 | + echo "Unknown option: $1" >&2 |
| 67 | + usage >&2 |
| 68 | + exit 1 |
| 69 | + ;; |
| 70 | + esac |
| 71 | +done |
| 72 | + |
| 73 | +if [[ ! -f "$PACKAGE_FILE" ]]; then |
| 74 | + echo "Package file not found: $PACKAGE_FILE" >&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +if is_enabled "$IS_PR"; then |
| 79 | + # Remove Sentry-Dynamic-WithARM64e target definitions and clean up empty binary blocks. |
| 80 | + sed -i '' '/Sentry-Dynamic-WithARM64e/d' "$PACKAGE_FILE" |
| 81 | + sed -i '' '/Sentry-WithoutUIKitOrAppKit-WithARM64e/d' "$PACKAGE_FILE" |
| 82 | + sed -i '' '/^[[:space:]]*\.binaryTarget($/{N;/\n[[:space:]]*),\{0,1\}$/d;}' "$PACKAGE_FILE" |
| 83 | +fi |
| 84 | + |
| 85 | +if is_enabled "$REMOVE_DUPLICATE"; then |
| 86 | + sed -i '' '/Sentry-Dynamic/d' "$PACKAGE_FILE" |
| 87 | + sed -i '' '/Sentry-WithoutUIKitOrAppKit/d' "$PACKAGE_FILE" |
| 88 | + sed -i '' '/^[[:space:]]*\.binaryTarget($/{N;/\n[[:space:]]*),\{0,1\}$/d;}' "$PACKAGE_FILE" |
| 89 | +fi |
| 90 | + |
| 91 | +if is_enabled "$CHANGE_PATH"; then |
| 92 | + # Remove Sentry binary framework URLs and convert checksums to local paths. |
| 93 | + sed -i '' 's/url: "https:\/\/github\.com\/getsentry\/sentry-cocoa\/releases\/download\/.*"//g' "$PACKAGE_FILE" |
| 94 | + sed -i '' 's/checksum: ".*" \/\/Sentry-Static/path: "Sentry.xcframework.zip"/g' "$PACKAGE_FILE" |
| 95 | + sed -i '' 's/checksum: ".*" \/\/Sentry-Dynamic-WithARM64e/path: "Sentry-Dynamic-WithARM64e.xcframework.zip"/g' "$PACKAGE_FILE" |
| 96 | + sed -i '' 's/checksum: ".*" \/\/Sentry-Dynamic/path: "Sentry-Dynamic.xcframework.zip"/g' "$PACKAGE_FILE" |
| 97 | + sed -i '' 's/checksum: ".*" \/\/Sentry-WithoutUIKitOrAppKit-WithARM64e/path: "Sentry-WithoutUIKitOrAppKit-WithARM64e.xcframework.zip"/g' "$PACKAGE_FILE" |
| 98 | + sed -i '' 's/checksum: ".*" \/\/Sentry-WithoutUIKitOrAppKit/path: "Sentry-WithoutUIKitOrAppKit.xcframework.zip"/g' "$PACKAGE_FILE" |
| 99 | + |
| 100 | + # Clean up orphaned commas and fix syntax. |
| 101 | + sed -i '' '/^[[:space:]]*,$/d' "$PACKAGE_FILE" |
| 102 | + sed -i '' 's/name: "Sentry\(-.*\)\?"$/name: "Sentry\1",/g' "$PACKAGE_FILE" |
| 103 | + sed -i '' 's/platforms: \[\.iOS(\.v11), \.macOS(\.v10_13), \.tvOS(\.v11), \.watchOS(\.v4)\]$/platforms: [.iOS(.v11), .macOS(.v10_13), .tvOS(.v11), .watchOS(.v4)],/g' "$PACKAGE_FILE" |
| 104 | +fi |
| 105 | + |
| 106 | +if is_enabled "$ONLY_KEEP_DISTRIBUTION"; then |
| 107 | + # Remove all binary targets. |
| 108 | + sed -i '' '/^[[:space:]]*\.binaryTarget(/,/^[[:space:]]*),\{0,1\}$/d' "$PACKAGE_FILE" |
| 109 | + |
| 110 | + # Keep only the SentryDistribution library in the products array. |
| 111 | + sed -i '' '/^var products: \[Product\] = \[/,/^]/c\ |
| 112 | +var products: [Product] = [\ |
| 113 | + .library(name: "SentryDistribution", targets: ["SentryDistribution"]),\ |
| 114 | +]\ |
| 115 | +' "$PACKAGE_FILE" |
| 116 | + |
| 117 | + # Keep only the SentryDistribution target in the targets array. |
| 118 | + sed -i '' '/^var targets: \[Target\] = \[/,/^]/c\ |
| 119 | +var targets: [Target] = [\ |
| 120 | + .target(name: "SentryDistribution", path: "Sources/SentryDistribution"),\ |
| 121 | +]\ |
| 122 | +' "$PACKAGE_FILE" |
| 123 | + |
| 124 | + # Replace dependency declarations with an empty list. |
| 125 | + sed -i '' '/^ dependencies: \[/,/^ ],/c\ |
| 126 | + dependencies: [],\ |
| 127 | +' "$PACKAGE_FILE" |
| 128 | + |
| 129 | + # Remove conditional append blocks that reintroduce other targets/products. |
| 130 | + sed -i '' '/^let env = getenv("EXPERIMENTAL_SPM_BUILDS")/,/^}/d' "$PACKAGE_FILE" |
| 131 | +fi |
| 132 | + |
| 133 | +echo |
| 134 | +echo "===== $PACKAGE_FILE (after prepare-package.sh) =====" |
| 135 | +cat "$PACKAGE_FILE" |
| 136 | +echo "===== end of $PACKAGE_FILE (after prepare-package.sh) =====" |
| 137 | +echo |
0 commit comments