|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require 'xcodeproj' |
| 18 | + |
| 19 | +# This script removes all Swift Package Manager dependencies from an Xcode project. |
| 20 | +# It's designed to be used in CI to prepare a project for framework-based testing. |
| 21 | + |
| 22 | +# --- Argument Parsing --- |
| 23 | +unless ARGV.length == 1 |
| 24 | + puts "Usage: #{$0} <path_to.xcodeproj>" |
| 25 | + exit 1 |
| 26 | +end |
| 27 | + |
| 28 | +project_path = ARGV[0] |
| 29 | + |
| 30 | +# --- Main Logic --- |
| 31 | +begin |
| 32 | + project = Xcodeproj::Project.open(project_path) |
| 33 | +rescue => e |
| 34 | + puts "Error opening project at #{project_path}: #{e.message}" |
| 35 | + exit 1 |
| 36 | +end |
| 37 | + |
| 38 | +puts "Opened project: #{project.path}" |
| 39 | + |
| 40 | +# Remove package references from the project's root object. |
| 41 | +# This corresponds to the "Package Dependencies" section in Xcode's navigator. |
| 42 | +unless project.root_object.package_references.empty? |
| 43 | + puts "Removing #{project.root_object.package_references.count} package reference(s)..." |
| 44 | + project.root_object.package_references.clear |
| 45 | + puts "All package references removed from the project." |
| 46 | +else |
| 47 | + puts "No package references found in the project." |
| 48 | +end |
| 49 | + |
| 50 | +# Remove package product dependencies from all targets. |
| 51 | +# This removes the link to the package products in the "Frameworks, Libraries, |
| 52 | +# and Embedded Content" section of each target. |
| 53 | +project.targets.each do |target| |
| 54 | + dependencies_to_remove = target.dependencies.select do |dependency| |
| 55 | + dependency.is_a?(Xcodeproj::Project::Object::XCSwiftPackageProductDependency) |
| 56 | + end |
| 57 | + |
| 58 | + unless dependencies_to_remove.empty? |
| 59 | + puts "Found #{dependencies_to_remove.count} SPM product dependencies in target '#{target.name}'. Removing..." |
| 60 | + dependencies_to_remove.each do |dep| |
| 61 | + puts "Removing #{dep.product_name}" |
| 62 | + target.dependencies.delete(dep) |
| 63 | + end |
| 64 | + puts "SPM product dependencies removed from target '#{target.name}'." |
| 65 | + else |
| 66 | + puts "No SPM product dependencies found in target '#{target.name}'." |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +# Save the modified project. |
| 71 | +begin |
| 72 | + project.save |
| 73 | + puts "Project saved successfully." |
| 74 | +rescue => e |
| 75 | + puts "Error saving project: #{e.message}" |
| 76 | + exit 1 |
| 77 | +end |
0 commit comments