Skip to content

Commit d3081e6

Browse files
committed
Initial POC of a script for swift api-digester
1 parent e9012b8 commit d3081e6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

compare-api.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash -eu
2+
3+
# This scripts tries to replicate what `swift package diagnose-api-breaking-changes` does but for an iOS SDK
4+
#
5+
# I haven't found a way to do this with `swift package diagnose-api-breaking-changes` so I'm doing it manually
6+
#
7+
# Closest I've found is:
8+
# ```
9+
# swift package \
10+
# --sdk "$(xcrun --sdk iphoneos --show-sdk-path)" \
11+
# --triple arm64-apple-ios \
12+
# diagnose-api-breaking-changes 2.1.0
13+
# ```
14+
# but it doesn't work as well:
15+
# - If I point to `2.1.0`, it generates errors during compilation about `type 'Bundle' has no member 'module'`. I think that might be because the swift-tools-version have changed since 2.1.0 though
16+
# - If I point to `3.2.0-rc.1`, it succeeds to compile... but generates empty `.build//arm64-apple-ios/apidiff/*/*/json` files, which makes the `swift api-digester` command fail with "Unable to parse empty data."
17+
#
18+
# So I'm working around by doing the intermediate steps manually compiling with `xcodebuild` then using `swift api-digester` instead.
19+
20+
21+
# Prints a blue header (level 1)
22+
function h1() {
23+
printf '\033[1;34m======================================================\033[0m\n'
24+
printf '\033[1;34m| %-50s |\033[0m\n' "$1"
25+
printf '\033[1;34m======================================================\033[0m\n'
26+
}
27+
28+
# Prints a blue header (level 2)
29+
function h2() {
30+
printf '\033[1;34m------------------------------------------------------\033[0m\n'
31+
printf '\033[1;34m%s\033[0m\n' "$1"
32+
printf '\033[1;34m------------------------------------------------------\033[0m\n'
33+
}
34+
35+
# Build the SDK (from current branch) in .build/
36+
#
37+
function build_sdk() {
38+
h2 "Building SDK"
39+
xcodebuild clean build \
40+
-scheme "Gravatar-Package" \
41+
-derivedDataPath .build \
42+
-sdk "$(xcrun --sdk iphoneos --show-sdk-path)" \
43+
-destination "generic/platform=iOS" \
44+
BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcpretty
45+
}
46+
47+
# Use api-digester to dump the SDK API to a JSON file
48+
#
49+
# $1: output file
50+
#
51+
function dump_sdk_api() {
52+
h2 "Dumping SDK API"
53+
swift api-digester -dump-sdk \
54+
-module Gravatar \
55+
-o "$1" \
56+
-I .build/Build/Products/Debug-iphoneos \
57+
-sdk "`xcrun --sdk iphoneos --show-sdk-path`" \
58+
-target "arm64-apple-ios"
59+
echo "==> Generated API JSON to $1"
60+
}
61+
62+
# Generate the API JSON for a given branch/treeish
63+
#
64+
# Checks out the branch, then calls `build_sdk`, then `dump_sdk_api`
65+
#
66+
# $1: branch/treeish to build + dump the API for
67+
#
68+
function generate_api_for_branch() {
69+
h1 "Generating API JSON for $1"
70+
git checkout "$1"
71+
build_sdk
72+
dump_sdk_api ".build/api-${1/\//-}.json"
73+
}
74+
75+
# Compare the API of two branches/treeishs
76+
#
77+
# Calls `generate_api_for_branch` for both branches, then `swift api-digester` to compare the APIs
78+
#
79+
# $1: old branch/treeish to compare against
80+
# $2: new branch/treeish to compare against, or current branch if not provided
81+
#
82+
function compare_api() {
83+
local current_branch
84+
current_branch=$(git rev-parse --abbrev-ref HEAD)
85+
86+
local old_branch="$1"
87+
local new_branch="${2:-$current_branch}"
88+
89+
generate_api_for_branch "$old_branch"
90+
git restore Package.resolved
91+
generate_api_for_branch "$new_branch"
92+
93+
git checkout "$current_branch"
94+
95+
h1 "Diagnosing API differences"
96+
swift api-digester -diagnose-sdk \
97+
--input-paths ".build/api-${old_branch/\//-}.json" \
98+
--input-paths ".build/api-${new_branch/\//-}.json" \
99+
2>&1 | sed $'s|^/\\*.*\\*/|\x1b[1;33m&\x1b[0m|' # the use of `sed` is to colorize the diff output
100+
}
101+
102+
compare_api "$@"

0 commit comments

Comments
 (0)