Skip to content

Commit

Permalink
Add support for local Swift packages in Xcode 15 (#1465)
Browse files Browse the repository at this point in the history
* Add support for local Swift packages in Xcode 15

Solves #1396

* Updated CHANGELOG.md

* add test for local package creation

---------

Co-authored-by: Yonas Kolb <yonaskolb@users.noreply.github.com>
  • Loading branch information
kinnarr and yonaskolb authored Jul 11, 2024
1 parent 02f9ea4 commit 5af3bf3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Added

- Better support for local Swift packages in Xcode 15 #1465 @kinnarr
- Added `macroExpansion` to test actions in schemes #1468 @erneestoc

### Changed
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tuist/XcodeProj.git",
"state" : {
"revision" : "6e60fb55271c80f83a186c9b1b4982fd991cfc0a",
"version" : "8.13.0"
"revision" : "447c159b0c5fb047a024fd8d942d4a76cf47dde0",
"version" : "8.16.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
.package(url: "https://github.com/yonaskolb/JSONUtilities.git", from: "4.2.0"),
.package(url: "https://github.com/kylef/Spectre.git", from: "0.9.2"),
.package(url: "https://github.com/onevcat/Rainbow.git", from: "4.0.0"),
.package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.13.0"),
.package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.16.0"),
.package(url: "https://github.com/jakeheis/SwiftCLI.git", from: "6.0.3"),
.package(url: "https://github.com/mxcl/Version", from: "2.0.0"),
.package(url: "https://github.com/freddi-kit/ArtifactBundleGen", exact: "0.0.6")
Expand Down
13 changes: 9 additions & 4 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PBXProjGenerator {
var targetFileReferences: [String: PBXFileReference] = [:]
var sdkFileReferences: [String: PBXFileReference] = [:]
var packageReferences: [String: XCRemoteSwiftPackageReference] = [:]
var localPackageReferences: [String: XCLocalSwiftPackageReference] = [:]

var carthageFrameworksByPlatform: [String: Set<PBXFileElement>] = [:]
var frameworkFiles: [PBXFileElement] = []
Expand All @@ -30,7 +31,6 @@ public class PBXProjGenerator {
var generated = false

private var projects: [ProjectReference: PBXProj] = [:]
lazy private var localPackageReferences: [String] = project.packages.compactMap { $0.value.isLocal ? $0.key : nil }

public init(project: Project, projectDirectory: Path? = nil) {
self.project = project
Expand Down Expand Up @@ -170,6 +170,10 @@ public class PBXProjGenerator {
packageReferences[name] = packageReference
addObject(packageReference)
case let .local(path, group):
let packageReference = XCLocalSwiftPackageReference(relativePath: path)
localPackageReferences[name] = packageReference
addObject(packageReference)

try sourceGenerator.createLocalPackage(path: Path(path), group: group.map { Path($0) })
}
}
Expand Down Expand Up @@ -310,7 +314,8 @@ public class PBXProjGenerator {
}
pbxProject.knownRegions = knownRegions.sorted()

pbxProject.packages = packageReferences.sorted { $0.key < $1.key }.map { $1 }
pbxProject.remotePackages = packageReferences.sorted { $0.key < $1.key }.map { $1 }
pbxProject.localPackages = localPackageReferences.sorted { $0.key < $1.key }.map { $1 }

let allTargets: [PBXTarget] = targetObjects.valueArray + targetAggregateObjects.valueArray
pbxProject.targets = allTargets
Expand Down Expand Up @@ -945,7 +950,7 @@ public class PBXProjGenerator {

// If package's reference is none and there is no specified package in localPackages,
// then ignore the package specified as dependency.
if packageReference == nil, !localPackageReferences.contains(dependency.reference) {
if packageReference == nil, localPackageReferences[dependency.reference] == nil {
continue
}

Expand Down Expand Up @@ -1469,7 +1474,7 @@ public class PBXProjGenerator {
func makePackagePluginDependency(for target: ProjectTarget) -> [PBXTargetDependency] {
target.buildToolPlugins.compactMap { buildToolPlugin in
let packageReference = packageReferences[buildToolPlugin.package]
if packageReference == nil, !localPackageReferences.contains(buildToolPlugin.package) {
if packageReference == nil, localPackageReferences[buildToolPlugin.package] == nil {
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/Fixtures/SPM/SPM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@
5BA91390AE78D2EE15C60091 /* XCRemoteSwiftPackageReference "Codability" */,
348C81C327DB1710B742C370 /* XCRemoteSwiftPackageReference "Prefire" */,
E3887F3CB2C069E70D98092F /* XCRemoteSwiftPackageReference "SwiftRoaring" */,
630A8CE9F2BE39704ED9D461 /* XCLocalSwiftPackageReference "FooFeature" */,
C6539B364583AE96C18CE377 /* XCLocalSwiftPackageReference "../../.." */,
);
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -662,6 +664,17 @@
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCLocalSwiftPackageReference section */
630A8CE9F2BE39704ED9D461 /* XCLocalSwiftPackageReference "FooFeature" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = FooFeature;
};
C6539B364583AE96C18CE377 /* XCLocalSwiftPackageReference "../../.." */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ../../..;
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
15DB49096E2978F6BCA8D604 /* FooUI */ = {
isa = XCSwiftPackageProductDependency;
Expand Down
4 changes: 4 additions & 0 deletions Tests/XcodeGenKitTests/ProjectGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,10 @@ class ProjectGeneratorTests: XCTestCase {
let nativeTarget = try unwrap(pbxProject.nativeTargets.first(where: { $0.name == app.name }))
let localPackageFile = try unwrap(pbxProject.fileReferences.first(where: { $0.path == "../XcodeGen" }))
try expect(localPackageFile.lastKnownFileType) == "folder"

let localPackageReference = try unwrap(pbxProject.rootObject?.localPackages.first)
try expect(pbxProject.rootObject?.localPackages.count) == 1
try expect(localPackageReference.relativePath) == "../XcodeGen"

let frameworkPhases = nativeTarget.buildPhases.compactMap { $0 as? PBXFrameworksBuildPhase }

Expand Down

0 comments on commit 5af3bf3

Please sign in to comment.