Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make references more deterministic #154

Merged
merged 2 commits into from
Nov 15, 2017
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
3 changes: 2 additions & 1 deletion Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class PBXProjGenerator {
if !carthageFrameworksByPlatform.isEmpty {
var platforms: [PBXGroup] = []
for (platform, fileReferences) in carthageFrameworksByPlatform {
let platformGroup = PBXGroup(reference: referenceGenerator.generate(PBXGroup.self, platform), children: fileReferences.sorted(), sourceTree: .group, name: platform, path: platform)
let platformGroup = PBXGroup(reference: referenceGenerator.generate(PBXGroup.self, "Carthage" + platform), children: fileReferences.sorted(), sourceTree: .group, name: platform, path: platform)
addObject(platformGroup)
platforms.append(platformGroup)
}
Expand Down Expand Up @@ -159,6 +159,7 @@ public class PBXProjGenerator {

func generateTarget(_ target: Target) throws -> PBXNativeTarget {

sourceGenerator.targetName = target.name
let carthageDependencies = getAllCarthageDependencies(target: target)

let sourceFiles = try sourceGenerator.getAllSourceFiles(sources: target.sources)
Expand Down
5 changes: 3 additions & 2 deletions Sources/XcodeGenKit/ReferenceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public class ReferenceGenerator {
public func generate<T: PBXObject>(_ element: T.Type, _ id: String) -> String {
var uuid: String = ""
var counter: UInt = 0
let characterCount = 16
let className: String = String(describing: T.self)
.replacingOccurrences(of: "PBX", with: "")
.replacingOccurrences(of: "XC", with: "")
let classAcronym = String(className.filter { String($0).lowercased() != String($0) })
let stringID = String(abs(id.hashValue).description.prefix(10 - classAcronym.count))
let stringID = String(abs(id.hashValue).description.prefix(characterCount - classAcronym.count - 2))
repeat {
uuid = "\(classAcronym)_\(stringID)\(counter > 0 ? "-\(counter)" : "")"
counter += 1
uuid = "\(classAcronym)\(stringID)\(String(format: "%02d", counter))"
} while (references.contains(uuid))
references.insert(uuid)
return uuid
Expand Down
14 changes: 8 additions & 6 deletions Sources/XcodeGenKit/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SourceGenerator {
private let proj: PBXProj
var addObject: (PBXObject) -> Void

var targetName: String = ""

init(spec: ProjectSpec, proj: PBXProj, referenceGenerator: ReferenceGenerator, addObject: @escaping (PBXObject) -> Void) {
self.spec = spec
self.proj = proj
Expand Down Expand Up @@ -59,15 +61,15 @@ class SourceGenerator {
}

//TODO: add the target name to the reference generator string so shared files don't have same reference (that will be escaped by appending a number)
let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, fileReference), fileRef: fileReference, settings: settings.isEmpty ? nil : settings)
let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, fileReference + targetName), fileRef: fileReference, settings: settings.isEmpty ? nil : settings)
return SourceFile(path: path, fileReference: fileReference, buildFile: buildFile, buildPhase: buildPhase)
}

func getFileReference(path: Path, inPath: Path, name: String? = nil, sourceTree: PBXSourceTree = .group) -> String {
if let fileReference = fileReferencesByPath[path] {
return fileReference
} else {
let fileReference = PBXFileReference(reference: referenceGenerator.generate(PBXFileReference.self, path.lastComponent), sourceTree: sourceTree, name: name, path: path.byRemovingBase(path: inPath).string)
let fileReference = PBXFileReference(reference: referenceGenerator.generate(PBXFileReference.self, path.byRemovingBase(path: spec.basePath).string), sourceTree: sourceTree, name: name, path: path.byRemovingBase(path: inPath).string)
addObject(fileReference)
fileReferencesByPath[path] = fileReference.reference
return fileReference.reference
Expand Down Expand Up @@ -109,7 +111,7 @@ class SourceGenerator {
let isTopLevelGroup = (isBaseGroup && !createIntermediateGroups) || isRootPath

group = PBXGroup(
reference: referenceGenerator.generate(PBXGroup.self, path.lastComponent),
reference: referenceGenerator.generate(PBXGroup.self, path.byRemovingBase(path: spec.basePath).string),
children: children,
sourceTree: .group,
name: name ?? path.lastComponent,
Expand All @@ -132,7 +134,7 @@ class SourceGenerator {
if let cachedGroup = variantGroupsByPath[path] {
variantGroup = cachedGroup
} else {
variantGroup = PBXVariantGroup(reference: referenceGenerator.generate(PBXVariantGroup.self, path.byRemovingBase(path: inPath).string),
variantGroup = PBXVariantGroup(reference: referenceGenerator.generate(PBXVariantGroup.self, path.byRemovingBase(path: spec.basePath).string),
children: [],
name: path.lastComponent,
sourceTree: .group)
Expand Down Expand Up @@ -236,7 +238,7 @@ class SourceGenerator {
groupChildren.append(variantGroup.reference)
baseLocalisationVariantGroups.append(variantGroup)

let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, variantGroup.reference), fileRef: variantGroup.reference, settings: nil)
let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, variantGroup.reference + targetName), fileRef: variantGroup.reference, settings: nil)
allSourceFiles.append(SourceFile(path: filePath, fileReference: variantGroup.reference, buildFile: buildFile, buildPhase: .resources))
}
}
Expand All @@ -258,7 +260,7 @@ class SourceGenerator {
}
} else {
// add SourceFile to group if there is no Base.lproj directory
let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, fileReference),
let buildFile = PBXBuildFile(reference: referenceGenerator.generate(PBXBuildFile.self, fileReference + targetName),
fileRef: fileReference,
settings: nil)
allSourceFiles.append(SourceFile(path: filePath, fileReference: fileReference, buildFile: buildFile, buildPhase: .resources))
Expand Down
Loading