Skip to content

Instantly share code, notes, and snippets.

@dougboutwell
Created September 22, 2022 21:54
Show Gist options
  • Save dougboutwell/cd5333aa2d7e424fd37dd3ce0d5f1c5b to your computer and use it in GitHub Desktop.
Save dougboutwell/cd5333aa2d7e424fd37dd3ce0d5f1c5b to your computer and use it in GitHub Desktop.
Shell script to build a .xcframework from the ViewInspector library for SwiftUI
#!/bin/bash
# Script for building a .xcframework, specifically for ViewInspector
#
# ViewInspector GitHub:
# - https://github.com/nalexn/ViewInspector
#
# Apple's docs:
# - https://help.apple.com/xcode/mac/11.4/#/dev544efab96
#
# Based on https://medium.com/macoclock/how-to-build-and-use-an-xcframework-40e4e7a53e2c
PROJECT_NAME="ViewInspector"
SCHEME_NAME="ViewInspector-Package"
ARCHIVE_DIR="${PWD}/archives"
OUTPUT_PATH="${PWD}/xcframeworks/${PROJECT_NAME}.xcframework"
PLATFORMS=( "iOS" "iOS Simulator" )
# clean this, else errors
rm -rf "${OUTPUT_PATH}"
# Parameters:
# 1: Platform name (String)
archive() {
xcodebuild archive \
-project "${PROJECT_NAME}.xcodeproj" \
-scheme "${SCHEME_NAME}" \
-destination "generic/platform=$1" \
-archivePath "${ARCHIVE_DIR}/$1" \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
}
for platform in "${PLATFORMS[@]}"
do
archive "${platform}"
done
XC_CMD="xcodebuild -create-xcframework"
for platform in "${PLATFORMS[@]}"
do
XC_CMD+=" -framework \"${ARCHIVE_DIR}/${platform}.xcarchive/Products/Library/Frameworks/${PROJECT_NAME}.framework\""
# have to use the full path to the dSYMs, per
# https://developer.apple.com/forums/thread/655768
XC_CMD+=" -debug-symbols \"${ARCHIVE_DIR}/${platform}.xcarchive/dSYMs/${PROJECT_NAME}.framework.dSYM\""
# Not sure if -archive is needed. Doesn't seem to hurt.
XC_CMD+=" -archive \"${ARCHIVE_DIR}/${platform}.xcarchive\""
done
XC_CMD+=" -output ${OUTPUT_PATH}"
eval "${XC_CMD}"
# For some reason, the framework output above refuses to build when incorporated
# into another project. It fails by pointing to the `.swiftinterface` file with
#
# 'XCTestExpectation' is not a member type of class 'XCTest.XCTest'
#
# As a workaround, removing the XCTest prefix / namespace from the symbol seems
# to work. I have no idea how to tell xcodebuild to NOT use the module name in
# this context, but just tweaking the files with sed seems to work.
#
# For context, see
# - https://developer.apple.com/forums/thread/123253
# - https://forums.swift.org/t/frameworkname-is-not-a-member-type-of-frameworkname-errors-inside-swiftinterface/28962
REGEX="s/XCTest\.XCTestExpectation/XCTestExpectation/g"
find . -name "*.swiftinterface" -exec sed -i '' "${REGEX}" {} +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment