Skip to content

Commit 1b710b1

Browse files
Fix localized files in files_and_groups (#2509)
Also fixed comments for file element paths. Signed-off-by: Brentley Jones <github@brentleyjones.com>
1 parent 572ec8a commit 1b710b1

20 files changed

+365
-68
lines changed

tools/generators/files_and_groups/src/ElementCreator/CreateFileElement.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ sourceTree = \(attributes.elementAttributes.sourceTree.rawValue); }
136136
object: .init(
137137
identifier: createIdentifier(
138138
path: bazelPath.path,
139+
name: attributes.elementAttributes.name ??
140+
attributes.elementAttributes.path,
139141
type: .fileReference
140142
),
141143
content: contentComponents.joined(separator: " ")

tools/generators/files_and_groups/src/ElementCreator/CreateGroupElement.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extension ElementCreator.CreateGroupElement {
105105
object: .init(
106106
identifier: createIdentifier(
107107
path: bazelPath.path,
108+
name: attributes.elementAttributes.path,
108109
type: .group
109110
),
110111
content: content

tools/generators/files_and_groups/src/ElementCreator/CreateIdentifier.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ extension ElementCreator {
1818
/// See `Identifiers.FilesAndGroups.element` for details.
1919
func callAsFunction(
2020
path: String,
21+
name: String,
2122
type: Identifiers.FilesAndGroups.ElementType
2223
) -> String {
2324
return callable(
2425
path,
26+
/*name:*/ name,
2527
/*type:*/ type,
2628
/*hashCache:*/ &hashCache
2729
)
@@ -34,6 +36,7 @@ extension ElementCreator {
3436
extension ElementCreator.CreateIdentifier {
3537
typealias Callable = (
3638
_ path: String,
39+
_ name: String,
3740
_ type: Identifiers.FilesAndGroups.ElementType,
3841
_ hashCache: inout Set<String>
3942
) -> String
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import PBXProj
2+
3+
extension ElementCreator {
4+
struct CreateLocalizedFileElement {
5+
private let createIdentifier: CreateIdentifier
6+
7+
private let callable: Callable
8+
9+
/// - Parameters:
10+
/// - callable: The function that will be called in
11+
/// `callAsFunction()`.
12+
init(
13+
createIdentifier: CreateIdentifier,
14+
callable: @escaping Callable
15+
) {
16+
self.createIdentifier = createIdentifier
17+
18+
self.callable = callable
19+
}
20+
21+
/// Creates a localized `PBXFileReference` element.
22+
func callAsFunction(
23+
name: String,
24+
path: String,
25+
ext: String?,
26+
bazelPath: BazelPath
27+
) -> Element {
28+
return callable(
29+
/*name:*/ name,
30+
/*path:*/ path,
31+
/*ext:*/ ext,
32+
/*bazelPath:*/ bazelPath,
33+
/*createIdentifier:*/ createIdentifier
34+
)
35+
}
36+
}
37+
}
38+
39+
// MARK: - CreateLocalizedFileElement.Callable
40+
41+
extension ElementCreator.CreateLocalizedFileElement {
42+
typealias Callable = (
43+
_ name: String,
44+
_ path: String,
45+
_ ext: String?,
46+
_ bazelPath: BazelPath,
47+
_ createIdentifier: ElementCreator.CreateIdentifier
48+
) -> Element
49+
50+
static func defaultCallable(
51+
name: String,
52+
path: String,
53+
ext: String?,
54+
bazelPath: BazelPath,
55+
createIdentifier: ElementCreator.CreateIdentifier
56+
) -> Element {
57+
let lastKnownFileType = ext.flatMap { Xcode.filetype(extension: $0) } ??
58+
"file"
59+
60+
let explicitFileType: String?
61+
if name == "BUILD" {
62+
explicitFileType = Xcode.filetype(extension: "bazel")
63+
} else if name == "Podfile" {
64+
explicitFileType = Xcode.filetype(extension: "rb")
65+
} else {
66+
explicitFileType = nil
67+
}
68+
69+
var contentComponents = [
70+
"{isa = PBXFileReference;",
71+
]
72+
73+
if let explicitFileType {
74+
contentComponents.append(
75+
"explicitFileType = \(explicitFileType.pbxProjEscaped);"
76+
)
77+
} else {
78+
contentComponents.append(
79+
"lastKnownFileType = \(lastKnownFileType.pbxProjEscaped);"
80+
)
81+
}
82+
83+
contentComponents.append("""
84+
name = \(name.pbxProjEscaped); \
85+
path = \(path.pbxProjEscaped); \
86+
sourceTree = "<group>"; }
87+
""")
88+
89+
return .init(
90+
name: name,
91+
object: .init(
92+
identifier: createIdentifier(
93+
path: bazelPath.path,
94+
name: name,
95+
type: .localized
96+
),
97+
content: contentComponents.joined(separator: " ")
98+
),
99+
sortOrder: .fileLike
100+
)
101+
}
102+
}

tools/generators/files_and_groups/src/ElementCreator/CreateLocalizedFiles.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PBXProj
33
extension ElementCreator {
44
struct CreateLocalizedFiles {
55
private let collectBazelPaths: CollectBazelPaths
6-
private let createFileElement: CreateFileElement
6+
private let createLocalizedFileElement: CreateLocalizedFileElement
77

88
private let callable: Callable
99

@@ -12,11 +12,11 @@ extension ElementCreator {
1212
/// `callAsFunction()`.
1313
init(
1414
collectBazelPaths: CollectBazelPaths,
15-
createFileElement: CreateFileElement,
15+
createLocalizedFileElement: CreateLocalizedFileElement,
1616
callable: @escaping Callable
1717
) {
1818
self.collectBazelPaths = collectBazelPaths
19-
self.createFileElement = createFileElement
19+
self.createLocalizedFileElement = createLocalizedFileElement
2020

2121
self.callable = callable
2222
}
@@ -33,7 +33,7 @@ extension ElementCreator {
3333
/*specialRootGroupType:*/ specialRootGroupType,
3434
/*region:*/ region,
3535
/*collectBazelPaths:*/ collectBazelPaths,
36-
/*createFileElement:*/ createFileElement
36+
/*createLocalizedFileElement:*/ createLocalizedFileElement
3737
)
3838
}
3939
}
@@ -48,7 +48,7 @@ extension ElementCreator.CreateLocalizedFiles {
4848
_ specialRootGroupType: SpecialRootGroupType?,
4949
_ region: String,
5050
_ collectBazelPaths: ElementCreator.CollectBazelPaths,
51-
_ createFileElement: ElementCreator.CreateFileElement
51+
_ createLocalizedFileElement: ElementCreator.CreateLocalizedFileElement
5252
) -> [GroupChild.LocalizedFile]
5353

5454
static func defaultCallable(
@@ -57,9 +57,10 @@ extension ElementCreator.CreateLocalizedFiles {
5757
specialRootGroupType: SpecialRootGroupType?,
5858
region: String,
5959
collectBazelPaths: ElementCreator.CollectBazelPaths,
60-
createFileElement: ElementCreator.CreateFileElement
60+
createLocalizedFileElement: ElementCreator.CreateLocalizedFileElement
6161
) -> [GroupChild.LocalizedFile] {
6262
let bazelPath = parentBazelPath + node
63+
let lprojPrefix = node.name
6364

6465
let files = node.children.map { node in
6566
let childBazelPath = bazelPath + node
@@ -71,11 +72,11 @@ extension ElementCreator.CreateLocalizedFiles {
7172

7273
let (basenameWithoutExt, ext) = node.splitExtension()
7374

74-
let (element, _) = createFileElement(
75+
let element = createLocalizedFileElement(
7576
name: region,
77+
path: "\(lprojPrefix)/\(node.name)",
7678
ext: ext,
77-
bazelPath: childBazelPath,
78-
specialRootGroupType: specialRootGroupType
79+
bazelPath: childBazelPath
7980
)
8081

8182
return GroupChild.LocalizedFile(

tools/generators/files_and_groups/src/ElementCreator/CreateVariantGroupElement.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extension ElementCreator.CreateVariantGroupElement {
6666
object: .init(
6767
identifier: createIdentifier(
6868
path: path,
69+
name: name,
6970
type: .localized
7071
),
7172
content: content

tools/generators/files_and_groups/src/ElementCreator/CreateVersionGroup.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extension ElementCreator.CreateVersionGroup {
7575

7676
let identifier = createIdentifier(
7777
path: bazelPath.path,
78+
name: name,
7879
type: .coreData
7980
)
8081
let selectedModelVersion = selectedModelVersions[bazelPath]

tools/generators/files_and_groups/src/ElementCreator/ElementCreatorEnvironment.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ extension ElementCreator {
3737

3838
let createInternalGroupCallable: CreateInternalGroup.Callable
3939

40+
/// Passed to the `callable` parameter of
41+
/// `CreateLocalizedFileElement.init()`.
42+
let createLocalizedFileElementCallable:
43+
CreateLocalizedFileElement.Callable
44+
4045
/// Passed to the `callable` parameter of `CreateLocalizedFiles.init()`.
4146
let createLocalizedFilesCallable: CreateLocalizedFiles.Callable
4247

@@ -132,9 +137,15 @@ extension ElementCreator.Environment {
132137
callable: createGroupCallable
133138
)
134139

140+
let createLocalizedFileElement =
141+
ElementCreator.CreateLocalizedFileElement(
142+
createIdentifier: createIdentifier,
143+
callable: createLocalizedFileElementCallable
144+
)
145+
135146
let createLocalizedFiles = ElementCreator.CreateLocalizedFiles(
136147
collectBazelPaths: collectBazelPaths,
137-
createFileElement: createFileElement,
148+
createLocalizedFileElement: createLocalizedFileElement,
138149
callable: createLocalizedFilesCallable
139150
)
140151

@@ -202,6 +213,8 @@ extension ElementCreator.Environment {
202213
createIdentifier: ElementCreator.CreateIdentifier(),
203214
createInternalGroupCallable:
204215
ElementCreator.CreateInternalGroup.defaultCallable,
216+
createLocalizedFileElementCallable:
217+
ElementCreator.CreateLocalizedFileElement.defaultCallable,
205218
createLocalizedFilesCallable:
206219
ElementCreator.CreateLocalizedFiles.defaultCallable,
207220
createMainGroupContent: ElementCreator.CreateMainGroupContent(),

tools/generators/files_and_groups/test/ElementCreator/CreateFileElementTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ final class CreateFileElementTests: XCTestCase {
1717
let ext: String? = nil
1818
let bazelPath = BazelPath("a/bazel/path/node_name")
1919

20+
let createAttributes = ElementCreator.CreateAttributes.mock(
21+
elementAttributes: .init(
22+
sourceTree: .group,
23+
name: "a_name",
24+
path: "a_path"
25+
),
26+
resolvedRepository: nil
27+
)
28+
2029
let expectedCreateIdentifierCalled: [
2130
ElementCreator.CreateIdentifier.MockTracker.Called
2231
] = [
2332
.init(
2433
path: "a/bazel/path/node_name",
34+
name: "a_name",
2535
type: .fileReference
2636
)
2737
]
@@ -36,7 +46,7 @@ final class CreateFileElementTests: XCTestCase {
3646
ext: ext,
3747
bazelPath: bazelPath,
3848
specialRootGroupType: nil,
39-
createAttributes: ElementCreator.Stubs.createAttributes,
49+
createAttributes: createAttributes.mock,
4050
createIdentifier: createIdentifier.mock
4151
)
4252

tools/generators/files_and_groups/test/ElementCreator/CreateGroupElementTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ final class CreateGroupElementTests: XCTestCase {
1616
let name = "node_name"
1717
let bazelPath = BazelPath("a/bazel/path/node_name")
1818

19+
let createAttributes = ElementCreator.CreateAttributes.mock(
20+
elementAttributes: .init(
21+
sourceTree: .group,
22+
name: nil,
23+
path: "a_path"
24+
),
25+
resolvedRepository: nil
26+
)
27+
1928
let expectedCreateIdentifierCalled: [
2029
ElementCreator.CreateIdentifier.MockTracker.Called
2130
] = [
2231
.init(
2332
path: bazelPath.path,
33+
name: "a_path",
2434
type: .group
2535
)
2636
]
@@ -35,7 +45,7 @@ final class CreateGroupElementTests: XCTestCase {
3545
bazelPath: bazelPath,
3646
specialRootGroupType: nil,
3747
childIdentifiers: [],
38-
createAttributes: ElementCreator.Stubs.createAttributes,
48+
createAttributes: createAttributes.mock,
3949
createIdentifier: createIdentifier.mock
4050
)
4151

0 commit comments

Comments
 (0)