Skip to content

Commit

Permalink
Add support for local Swift packages in Xcode 15
Browse files Browse the repository at this point in the history
Solves #1396
  • Loading branch information
kinnarr committed Apr 11, 2024
1 parent 447cc7f commit 479097c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,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/SwiftDocOrg/GraphViz.git", exact: "0.2.0"),
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

0 comments on commit 479097c

Please sign in to comment.