Skip to content

Commit c57d87a

Browse files
committed
Load Info.plist string value using generated function
1 parent 030c0d6 commit c57d87a

File tree

3 files changed

+33
-49
lines changed

3 files changed

+33
-49
lines changed

ResourceApp/ResourceAppTests/InfoPlistTests.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,6 @@ class InfoPlistTests: XCTestCase {
1717
}
1818

1919
func testVariable() {
20-
// let x = (Bundle.main.object(forInfoDictionaryKey: "NSExtension") as? [String: Any])?["NSExtensionPrincipalClass"] as? String
2120
XCTAssertEqual(R.info.nsExtension.nsExtensionPrincipalClass, "ResourceApp.IntentHandler")
2221
}
2322
}
24-
25-
//<key>NSExtension</key>
26-
//<dict>
27-
// <key>NSExtensionAttributes</key>
28-
// <dict>
29-
// <key>IntentsRestrictedWhileLocked</key>
30-
// <array/>
31-
// <key>IntentsRestrictedWhileProtectedDataUnavailable</key>
32-
// <array/>
33-
// <key>IntentsSupported</key>
34-
// <array>
35-
// <string>PlanTripIntent</string>
36-
// <string>ShowDeparturesIntent</string>
37-
// </array>
38-
// </dict>
39-
// <key>NSExtensionPointIdentifier</key>
40-
// <string>com.apple.intents-service</string>
41-
// <key>NSExtensionPrincipalClass</key>
42-
// <string>$(PRODUCT_MODULE_NAME).IntentHandler</string>
43-
//</dict>

Sources/RswiftCore/Generators/PropertyListGenerator.swift

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
7777

7878
private func propertyFromInfoString(key: String, value: String, path: [String], at externalAccessLevel: AccessLevel) -> Let {
7979

80+
let steps = path.map { "\"\($0.escapedStringLiteral)\"" }.joined(separator: ", ")
81+
8082
let isKey = key == "_key"
8183
let letValue: String = isKey
8284
? "\"\(value.escapedStringLiteral)\""
83-
: "_infoDictionary?[\"\(key)\"] as? String ?? \"\(value.escapedStringLiteral)\""
85+
: "infoPlistString(path: [\(steps)], key: \"\(key.escapedStringLiteral)\") ?? \"\(value.escapedStringLiteral)\""
8486

8587
return Let(
86-
comments: isKey ? [] : [value],
88+
comments: [],
8789
accessModifier: externalAccessLevel,
8890
isStatic: true,
8991
name: SwiftIdentifier(name: key),
@@ -99,35 +101,18 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
99101
var ps = path
100102
ps.append(key)
101103

102-
let info = path.reduce("hostingBundle.infoDictionary", { (source, step) in
103-
"(\(source)?[\"\(step)\"] as? [String: Any])"
104-
})
105-
let object = Let(
106-
comments: [],
107-
accessModifier: .privateLevel,
108-
isStatic: true,
109-
name: "_infoDictionary",
110-
typeDefinition: .inferred(nil),
111-
value: info
112-
)
113-
114104
switch value {
115105
case let array as [String]:
116-
var props = array.map { item in
117-
propertyFromInfoString(key: item, value: item, path: ps, at: externalAccessLevel)
118-
}
119-
if !props.isEmpty {
120-
props.append(object)
121-
}
122-
123106
return Struct(
124107
availables: [],
125108
comments: [],
126109
accessModifier: externalAccessLevel,
127110
type: Type(module: .host, name: SwiftIdentifier(name: key)),
128111
implements: [],
129112
typealiasses: [],
130-
properties: props,
113+
properties: array.map { item in
114+
propertyFromInfoString(key: item, value: item, path: ps, at: externalAccessLevel)
115+
},
131116
functions: [],
132117
structs: [],
133118
classes: [],
@@ -136,10 +121,6 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
136121

137122
case var dict as [String: Any]:
138123
dict["_key"] = key
139-
var props = propertiesFromInfoPlist(contents: dict, path: ps, at: externalAccessLevel)
140-
if !props.isEmpty {
141-
props.append(object)
142-
}
143124

144125
return Struct(
145126
availables: [],
@@ -148,7 +129,7 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
148129
type: Type(module: .host, name: SwiftIdentifier(name: key)),
149130
implements: [],
150131
typealiasses: [],
151-
properties: props,
132+
properties: propertiesFromInfoPlist(contents: dict, path: ps, at: externalAccessLevel),
152133
functions: [],
153134
structs: structsFromInfoPlist(contents: dict, path: ps, at: externalAccessLevel),
154135
classes: [],

Sources/RswiftCore/Util/Struct+InternalProperties.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ extension Struct {
3333
]
3434

3535
let internalFunctions = [
36+
Function(
37+
availables: [],
38+
comments: ["Load string from Info.plist file"],
39+
accessModifier: .filePrivate,
40+
isStatic: true,
41+
name: "infoPlistString",
42+
generics: nil,
43+
parameters: [
44+
.init(name: "path", type: Type._Array.withGenericArgs([Type._String])),
45+
.init(name: "key", type: Type._String)
46+
],
47+
doesThrow: false,
48+
returnType: Type._String.asOptional(),
49+
body: """
50+
var dict = hostingBundle.infoDictionary
51+
for step in path {
52+
guard let obj = dict?[step] as? [String: Any] else { return nil }
53+
dict = obj
54+
}
55+
return dict?[key] as? String
56+
""",
57+
os: []
58+
),
3659
Function(
3760
availables: [],
3861
comments: ["Find first language and bundle for which the table exists"],
@@ -106,7 +129,8 @@ extension Struct {
106129
// If table is not found for requested languages, key will be shown
107130
return nil
108131
""",
109-
os: [])
132+
os: []
133+
)
110134
]
111135

112136
var externalStruct = self

0 commit comments

Comments
 (0)