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

Fix logic related PBXVariantGroup #1296

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Sources/ProjectSpec/SourceType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public enum SourceType: String {
case group
case file
case folder
case variantGroup
}
7 changes: 4 additions & 3 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public class PBXProjGenerator {
pbxProject.projects = subprojects
}

try project.targets.forEach(generateTarget)
let pbxVariantGroupInfoList = try PBXVariantGroupGenerator(pbxProj: pbxProj, project: project).generate()
try project.targets.forEach { try generateTarget($0, pbxVariantGroupInfoList: pbxVariantGroupInfoList) }
try project.aggregateTargets.forEach(generateAggregateTarget)

if !carthageFrameworksByPlatform.isEmpty {
Expand Down Expand Up @@ -648,13 +649,13 @@ public class PBXProjGenerator {
return pbxproj
}

func generateTarget(_ target: Target) throws {
func generateTarget(_ target: Target, pbxVariantGroupInfoList: [PBXVariantGroupInfo]) throws {
let carthageDependencies = carthageResolver.dependencies(for: target)

let infoPlistFiles: [Config: String] = getInfoPlists(for: target)
let sourceFileBuildPhaseOverrideSequence: [(Path, BuildPhaseSpec)] = Set(infoPlistFiles.values).map({ (project.basePath + $0, .none) })
let sourceFileBuildPhaseOverrides = Dictionary(uniqueKeysWithValues: sourceFileBuildPhaseOverrideSequence)
let sourceFiles = try sourceGenerator.getAllSourceFiles(targetType: target.type, sources: target.sources, buildPhases: sourceFileBuildPhaseOverrides)
let sourceFiles = try sourceGenerator.getAllSourceFiles(targetName: target.name, targetType: target.type, sources: target.sources, buildPhases: sourceFileBuildPhaseOverrides, pbxVariantGroupInfoList: pbxVariantGroupInfoList)
.sorted { $0.path.lastComponent < $1.path.lastComponent }

var anyDependencyRequiresObjCLinking = false
Expand Down
155 changes: 155 additions & 0 deletions Sources/XcodeGenKit/PBXVariantGroupGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import XcodeProj
import ProjectSpec
import PathKit
import XcodeGenCore

class PBXVariantGroupInfo {
let targetName: String
let variantGroup: PBXVariantGroup
var path: Path

init(targetName: String, variantGroup: PBXVariantGroup, path: Path) {
self.targetName = targetName
self.variantGroup = variantGroup
self.path = path
}
}

class PBXVariantGroupGenerator: TargetSourceFilterable {
let pbxProj: PBXProj
let project: Project

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

var alwaysStoredBaseExtensions: [String] {
[".xib", ".storyboard", ".intentdefinition"]
}

func generate() throws -> [PBXVariantGroupInfo] {
var variantGroupInfoList: [PBXVariantGroupInfo] = []

try project.targets.forEach { target in
try target.sources.forEach { targetSource in
let excludePaths = getSourceMatches(targetSource: targetSource,
patterns: targetSource.excludes)
let includePaths = getSourceMatches(targetSource: targetSource,
patterns: targetSource.includes)

let path = project.basePath + targetSource.path

try generateVarientGroup(targetName: target.name,
targetSource: targetSource,
path: path,
excludePaths: excludePaths,
includePaths: SortedArray(includePaths))
}
}

func generateVarientGroup(targetName: String,
targetSource: TargetSource,
path: Path,
excludePaths: Set<Path>,
includePaths: SortedArray<Path>) throws {
guard path.exists && path.isDirectory && !Xcode.isDirectoryFileWrapper(path: path) else {
return
}

let children = try getSourceChildren(targetSource: targetSource,
dirPath: path,
excludePaths: excludePaths,
includePaths: includePaths)

try children.forEach {
let excludePaths = getSourceMatches(targetSource: targetSource,
patterns: targetSource.excludes)
let includePaths = getSourceMatches(targetSource: targetSource,
patterns: targetSource.includes)

try generateVarientGroup(targetName: targetName,
targetSource: targetSource,
path: $0,
excludePaths: excludePaths,
includePaths: SortedArray(includePaths))
}

let localizeDirs: [Path] = children
.filter ({ $0.extension == "lproj" })
.reduce(into: [Path]()) { partialResult, path in
if path.lastComponentWithoutExtension == "Base" {
partialResult.append(path)
} else {
partialResult.insert(path, at: 0)
}
}

guard localizeDirs.count > 0 else {
return
}

try localizeDirs.forEach { localizedDir in
try localizedDir.children()
.filter { self.isIncludedPath($0, excludePaths: excludePaths, includePaths: includePaths) }
.sorted()
.forEach { localizedDirChildPath in
let fileReferencePath = try localizedDirChildPath.relativePath(from: path)
let fileRef = PBXFileReference(
sourceTree: .group,
name: localizedDir.lastComponentWithoutExtension,
lastKnownFileType: Xcode.fileType(path: localizedDirChildPath),
path: fileReferencePath.string
)
pbxProj.add(object: fileRef)

let variantGroupInfo = getVariantGroupInfo(targetName: targetName, localizedChildPath: localizedDirChildPath)

if localizedDir.lastComponentWithoutExtension == "Base" || project.options.developmentLanguage == localizedDir.lastComponentWithoutExtension {

variantGroupInfo.path = localizedDirChildPath
variantGroupInfo.variantGroup.name = localizedDirChildPath.lastComponent
}

variantGroupInfo.variantGroup.children.append(fileRef)
}
}
}

func getVariantGroupInfo(targetName: String, localizedChildPath: Path) -> PBXVariantGroupInfo {
let pbxVariantGroupInfo = variantGroupInfoList
.filter { $0.targetName == targetName }
.first {
let existsAlwaysStoredBaseFile = alwaysStoredBaseExtensions
.reduce(into: [Bool]()) { $0.append(localizedChildPath.lastComponent.contains($1)) }
.filter { $0 }
.count > 0

if existsAlwaysStoredBaseFile {
return $0.path.lastComponentWithoutExtension == localizedChildPath.lastComponentWithoutExtension
} else {
return $0.path.lastComponent == localizedChildPath.lastComponent
}
}

if let pbxVariantGroupInfo = pbxVariantGroupInfo {
return pbxVariantGroupInfo
} else {
let variantGroup = PBXVariantGroup(
sourceTree: .group,
name: localizedChildPath.lastComponent
)
pbxProj.add(object: variantGroup)

let pbxVariantGroupInfo = PBXVariantGroupInfo(targetName: targetName,
variantGroup: variantGroup,
path: localizedChildPath)
variantGroupInfoList.append(pbxVariantGroupInfo)

return pbxVariantGroupInfo
}
}

return variantGroupInfoList
}
}
Loading