Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Sources/XcodeGenKit/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ class SourceGenerator {

private(set) var knownRegions: Set<String> = []

/// The effective base path for resolving group and file paths in the generated project.
/// Uses `projectDirectory` when the xcodeproj is generated in a different location than the spec.
private var basePath: Path {
projectDirectory ?? project.basePath
}

init(project: Project, pbxProj: PBXProj, projectDirectory: Path?) {
self.project = project
self.pbxProj = pbxProj
self.projectDirectory = projectDirectory
}

private func resolveGroupPath(_ path: Path, isTopLevelGroup: Bool) -> String {
if isTopLevelGroup, let relativePath = try? path.relativePath(from: projectDirectory ?? project.basePath).string {
if isTopLevelGroup, let relativePath = try? path.relativePath(from: basePath).string {
return relativePath
} else {
return path.lastComponent
Expand All @@ -62,7 +68,7 @@ class SourceGenerator {
let absolutePath = project.basePath + path.normalize()

// Get the local package's relative path from the project root
let fileReferencePath = try? absolutePath.relativePath(from: projectDirectory ?? project.basePath).string
let fileReferencePath = try? absolutePath.relativePath(from: basePath).string

let fileReference = addObject(
PBXFileReference(
Expand Down Expand Up @@ -886,7 +892,7 @@ class SourceGenerator {
element = parent
}

let completePath = project.basePath + Path(paths.joined(separator: "/"))
let completePath = (basePath) + Path(paths.joined(separator: "/"))
let relativePath = try path.relativePath(from: completePath)
let relativePathString = relativePath.string

Expand Down
34 changes: 34 additions & 0 deletions Tests/XcodeGenKitTests/SourceGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,40 @@ class SourceGeneratorTests: XCTestCase {
try pbxProj.expectFile(paths: ["Sources/B", "b.swift"], names: ["B", "b.swift"], buildPhase: .sources)
}

$0.it("generates intermediate groups with different projectDirectory") {

let directories = """
Sources:
- a.swift
- b.swift
Modules:
- m.swift
"""
try createDirectories(directories)

let target = Target(name: "Test", type: .application, platform: .iOS, sources: [
"../Sources",
"../Modules",
])
let options = SpecOptions(createIntermediateGroups: true)
// basePath is a subdirectory (simulating spec in a subdir like ProjectRoot/XcodeGen/)
// projectDirectory is the parent (simulating --project pointing to ProjectRoot/)
let subdir = directoryPath + "SubDir"
try subdir.mkpath()
let project = Project(basePath: subdir, name: "Test", targets: [target], options: options)

let generator = PBXProjGenerator(project: project, projectDirectory: directoryPath)
let pbxProj = try generator.generate()

// Sources and Modules should have path = "Sources"/"Modules" (not "../Sources")
// The intermediate group for TestDirectory should have path = "."
// So Xcode resolves: projectDir/./Sources = correct
// Before fix: path was "../Sources", resolving to projectDir/./../Sources = wrong
try pbxProj.expectFile(paths: [".", "Sources", "a.swift"], names: ["TestDirectory", "Sources", "a.swift"], buildPhase: .sources)
try pbxProj.expectFile(paths: [".", "Sources", "b.swift"], names: ["TestDirectory", "Sources", "b.swift"], buildPhase: .sources)
try pbxProj.expectFile(paths: [".", "Modules", "m.swift"], names: ["TestDirectory", "Modules", "m.swift"], buildPhase: .sources)
}

$0.it("generates custom groups") {

let directories = """
Expand Down