Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active April 21, 2022 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennanMKE/6a32a1e94ca44547cbe4eec4410c3399 to your computer and use it in GitHub Desktop.
Save brennanMKE/6a32a1e94ca44547cbe4eec4410c3399 to your computer and use it in GitHub Desktop.
Locate Checkout Paths

Locate Checkout Paths

When dependencies are used with Swift Package Manager the remote Git repos will be cloned locally and placed into the build directory under Derived Data. This script will look for build directories which match the project directory and return the directory where dependency checkouts are located.

It works by reading the info.plist which can be found in each build directory under Derived Data. It has a value for WorkspacePath which is the source directory which ran the build. It could be the root directory for a Swift package but workspaces are also Xcode workspaces or even Xcode projects. The findup function is used to look for README.md to find the root of a project so that the workspace path is able to match what is in info.plist.


#!/bin/sh
PROJECT_DIR=$1
if [ -z "${PROJECT_DIR}" ]; then
PROJECT_DIR=$(pwd)
fi
PLIST_BUDDY=/usr/libexec/PlistBuddy
if [ ! -f ${PLIST_BUDDY} ]; then
echo "PlistBuddy is not available."
exit 1
fi
DERIVED_DATA_DIR="${HOME}/Library/Developer/Xcode/DerivedData"
findup()
{
TARGET=$1
PWD=$(pwd)
START="${PWD}"
while [ ! "${PWD}" -ef .. ]; do
[ -e "${TARGET}" ] && echo "$PWD" && return
cd .. || return
PWD=$(pwd)
done
}
read_build_dir()
{
BUILD_DIR=$1
INFO_PLIST="${BUILD_DIR}/info.plist"
if [ ! -f "${INFO_PLIST}" ]; then
return
fi
WORKSPACE_PATH=$(${PLIST_BUDDY} -c "Print WorkspacePath" "${INFO_PLIST}")
if [ -z "${WORKSPACE_PATH}" ]; then
return
fi
EFFECTIVE_WORKSPACE_PATH=$(findup README.md)
if [ "${PROJECT_DIR}" == "${EFFECTIVE_WORKSPACE_PATH}" ] ; then
CHECKOUTS_PATH="${BUILD_DIR}/SourcePackages/checkouts"
if [ -d "${CHECKOUTS_PATH}" ]; then
echo "${CHECKOUTS_PATH}"
exit 0
fi
fi
}
read_build_dirs()
{
for BUILD_DIR in $(find "${DERIVED_DATA_DIR}" -type d -maxdepth 1); do
read_build_dir ${BUILD_DIR}
done
exit 0
}
read_build_dirs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment