Skip to content

Instantly share code, notes, and snippets.

@dive
Created November 8, 2020 21:30
Show Gist options
  • Save dive/7dd0f9afb0920e114293f525f223b511 to your computer and use it in GitHub Desktop.
Save dive/7dd0f9afb0920e114293f525f223b511 to your computer and use it in GitHub Desktop.
Shell script to generate .xcworkspace with all .xcodeproj in the current directory
#!/usr/bin/env bash
set -eo pipefail
# Prepare the workspace in the TEMP directory
NAME="Temp.xcworkspace"
DATA_FILE="contents.xcworkspacedata"
TEMP_DIR=$(mktemp -d)
mkdir "${TEMP_DIR}/${NAME}"
touch "${TEMP_DIR}/${NAME}/${DATA_FILE}"
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workspace version = \"1.0\">" >> "${TEMP_DIR}/${NAME}/${DATA_FILE}"
# Find all .xcodeproj and include them to the .xcworkspace we generate
find . \
-name "*.xcodeproj" -type d \
-exec echo "<FileRef location = \"container:{}\"></FileRef>" ';' \
>> "${TEMP_DIR}/${NAME}/${DATA_FILE}"
echo "</Workspace>" >> "${TEMP_DIR}/${NAME}/${DATA_FILE}"
# Remove existed .xcworkspace
rm -Rf "$NAME"
# Copy the new one from the TEMP to the current directory
cp -r "${TEMP_DIR}/${NAME}" .
printf "Generated %s.\n" "$NAME"
@dive
Copy link
Author

dive commented Nov 8, 2020

Generate .xcworkspace with .xcodeproj in the current directory

Can be useful for automation, etc. The idea is straight forward:

  1. Find all the .xcodeproj in the current directory (recursively)
  2. Generate the .xcworkspace
  3. Include all the .xcodeproj files to the .xcworkspace
  4. Copy the .xcworkspace directory from the Temp directory to the current one

A few caveats:

  • There will be problems if you have projects with the same name and there will be more than one .xcodeproj with the same name included (most probably, compilation will fail)
  • Use -not -path "PATH" to exclude any directory from the find

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment