Add support for local Swift packages in Xcode 15 (#1465)

* Add support for local Swift packages in Xcode 15

Solves yonaskolb/XcodeGen#1396

* Updated CHANGELOG.md

* add test for local package creation

---------

Co-authored-by: Yonas Kolb <yonaskolb@users.noreply.github.com>
This commit is contained in:
Franz Höpfner 2024-07-11 13:19:37 +02:00 committed by GitHub
parent 02f9ea4e51
commit 5af3bf3797
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 30 additions and 7 deletions

View File

@ -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

View File

@ -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"
}
},
{

View File

@ -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")

View File

@ -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] = []
@ -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
@ -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) })
}
}
@ -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
@ -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
}
@ -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
}

View File

@ -251,6 +251,8 @@
5BA91390AE78D2EE15C60091 /* XCRemoteSwiftPackageReference "Codability" */,
348C81C327DB1710B742C370 /* XCRemoteSwiftPackageReference "Prefire" */,
E3887F3CB2C069E70D98092F /* XCRemoteSwiftPackageReference "SwiftRoaring" */,
630A8CE9F2BE39704ED9D461 /* XCLocalSwiftPackageReference "FooFeature" */,
C6539B364583AE96C18CE377 /* XCLocalSwiftPackageReference "../../.." */,
);
projectDirPath = "";
projectRoot = "";
@ -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;

View File

@ -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 }