-
Notifications
You must be signed in to change notification settings - Fork 876
Expand file tree
/
Copy pathPBXVariantGroupGenerator.swift
More file actions
155 lines (129 loc) · 6.86 KB
/
PBXVariantGroupGenerator.swift
File metadata and controls
155 lines (129 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
}
}