Add a Swift Package to a workspace in a script

55 views Asked by At

I can create a new Swift Package in Xcode and able to add it in a workspace. This can be done using Xcode -> File -> New Package, add the name and select the workspace in "Add to" and select the folder in "Group". Then I can use this local package in another project within the same workspace.

Now I want to write a script that does the same thing. I learn about

swift package init

but this only creates the package and not sure how to add this to the workspace. Has anyone tried it?

1

There are 1 answers

0
Imran On
  • Create a new Swift Package, or make sure you have an existing package with a Package.swift file.
  • Open your Xcode workspace (.xcworkspace) that you want to add the Swift Package to.
  • Use the following script to add the Swift Package to the workspace:

#!/bin/bash

Variables

WORKSPACE_PATH="YourWorkspace.xcworkspace" PACKAGE_PATH="path/to/YourSwiftPackage" # Replace with the actual path to your Swift Package

# Verify that the workspace file exists
if [ -f "$WORKSPACE_PATH" ]; then
    echo "Adding Swift Package to the workspace..."
else
    echo "Workspace file not found: $WORKSPACE_PATH"
    exit 1
fi

# Use `xcodebuild` to add the Swift Package to the workspace
xcodebuild -workspace "$WORKSPACE_PATH" -list

# Verify that the package path exists
if [ -d "$PACKAGE_PATH" ]; then
    echo "Package path found: $PACKAGE_PATH"
else
    echo "Package path not found: $PACKAGE_PATH"
    exit 1
fi

# Add the package to the workspace
xcodebuild -workspace "$WORKSPACE_PATH" -scheme "YourAppScheme" -package-id "com.yourpackageid" -package-path "$PACKAGE_PATH" -package-branch "main"