@@ -25,7 +25,7 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
2525
2626 guard plists. all ( where: { $0. url == plist. url } ) else {
2727 let configs = plists. map { $0. buildConfigurationName }
28- warn ( " Build configrurations \( configs) use different \( name) files, this is not yet supported " )
28+ warn ( " Build configurations \( configs) use different \( name) files, this is not yet supported " )
2929 return . empty
3030 }
3131
@@ -45,15 +45,15 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
4545 type: Type ( module: . host, name: name) ,
4646 implements: [ ] ,
4747 typealiasses: [ ] ,
48- properties: propertiesFromInfoPlist ( contents: contents, at: externalAccessLevel) ,
48+ properties: propertiesFromInfoPlist ( contents: contents, path : [ ] , at: externalAccessLevel) ,
4949 functions: [ ] ,
50- structs: structsFromInfoPlist ( contents: contents, at: externalAccessLevel) ,
50+ structs: structsFromInfoPlist ( contents: contents, path : [ ] , at: externalAccessLevel) ,
5151 classes: [ ] ,
5252 os: [ ]
5353 )
5454 }
5555
56- private func propertiesFromInfoPlist( contents: [ String : Any ] , at externalAccessLevel: AccessLevel ) -> [ Let ] {
56+ private func propertiesFromInfoPlist( contents: [ String : Any ] , path : [ String ] , at externalAccessLevel: AccessLevel ) -> [ Let ] {
5757
5858 return contents
5959 . compactMap { ( key, value) -> Let ? in
@@ -68,43 +68,66 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
6868 value: " \( value) "
6969 )
7070 case let value as String :
71- return Let (
72- comments: [ ] ,
73- accessModifier: externalAccessLevel,
74- isStatic: true ,
75- name: SwiftIdentifier ( name: key) ,
76- typeDefinition: . inferred( Type . _String) ,
77- value: " \" \( value. escapedStringLiteral) \" "
78- )
71+ return propertyFromInfoString ( key: key, value: value, path: path, at: externalAccessLevel)
7972 default :
8073 return nil
8174 }
82- }
75+ }
76+ }
77+
78+ private func propertyFromInfoString( key: String , value: String , path: [ String ] , at externalAccessLevel: AccessLevel ) -> Let {
79+
80+ let isKey = key == " _key "
81+ let letValue : String = isKey
82+ ? " \" \( value. escapedStringLiteral) \" "
83+ : " _infoDictionary?[ \" \( key) \" ] as? String ?? \" \( value. escapedStringLiteral) \" "
84+
85+ return Let (
86+ comments: isKey ? [ ] : [ value] ,
87+ accessModifier: externalAccessLevel,
88+ isStatic: true ,
89+ name: SwiftIdentifier ( name: key) ,
90+ typeDefinition: . inferred( Type . _String) ,
91+ value: letValue
92+ )
8393 }
8494
85- private func structsFromInfoPlist( contents: [ String : Any ] , at externalAccessLevel: AccessLevel ) -> [ Struct ] {
95+ private func structsFromInfoPlist( contents: [ String : Any ] , path : [ String ] , at externalAccessLevel: AccessLevel ) -> [ Struct ] {
8696
8797 return contents
8898 . compactMap { ( key, value) -> Struct ? in
99+ var ps = path
100+ ps. append ( key)
101+
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+
89114 switch value {
90115 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+
91123 return Struct (
92124 availables: [ ] ,
93125 comments: [ ] ,
94126 accessModifier: externalAccessLevel,
95127 type: Type ( module: . host, name: SwiftIdentifier ( name: key) ) ,
96128 implements: [ ] ,
97129 typealiasses: [ ] ,
98- properties: array. map { item in
99- return Let (
100- comments: [ ] ,
101- accessModifier: externalAccessLevel,
102- isStatic: true ,
103- name: SwiftIdentifier ( name: item) ,
104- typeDefinition: . inferred( Type . _String) ,
105- value: " \" \( item. escapedStringLiteral) \" "
106- )
107- } ,
130+ properties: props,
108131 functions: [ ] ,
109132 structs: [ ] ,
110133 classes: [ ] ,
@@ -113,22 +136,27 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
113136
114137 case var dict as [ String : Any ] :
115138 dict [ " _key " ] = key
139+ var props = propertiesFromInfoPlist ( contents: dict, path: ps, at: externalAccessLevel)
140+ if !props. isEmpty {
141+ props. append ( object)
142+ }
143+
116144 return Struct (
117145 availables: [ ] ,
118146 comments: [ ] ,
119147 accessModifier: externalAccessLevel,
120148 type: Type ( module: . host, name: SwiftIdentifier ( name: key) ) ,
121149 implements: [ ] ,
122150 typealiasses: [ ] ,
123- properties: propertiesFromInfoPlist ( contents : dict , at : externalAccessLevel ) ,
151+ properties: props ,
124152 functions: [ ] ,
125- structs: structsFromInfoPlist ( contents: dict, at: externalAccessLevel) ,
153+ structs: structsFromInfoPlist ( contents: dict, path : ps , at: externalAccessLevel) ,
126154 classes: [ ] ,
127155 os: [ ]
128156 )
129157
130158 case let dicts as [ [ String : Any ] ] where arrayOfDictionariesPrimaryKeys. keys. contains ( key) :
131- return structForArrayOfDictionaries ( key: key, dicts: dicts, at: externalAccessLevel)
159+ return structForArrayOfDictionaries ( key: key, dicts: dicts, path : path , at: externalAccessLevel)
132160
133161 default :
134162 return nil
@@ -146,7 +174,7 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
146174 " CFBundleURLTypes " : " CFBundleURLName "
147175 ]
148176
149- private func structForArrayOfDictionaries( key: String , dicts: [ [ String : Any ] ] , at externalAccessLevel: AccessLevel ) -> Struct {
177+ private func structForArrayOfDictionaries( key: String , dicts: [ [ String : Any ] ] , path : [ String ] , at externalAccessLevel: AccessLevel ) -> Struct {
150178 let kvs = dicts. compactMap { dict -> ( String , [ String : Any ] ) ? in
151179 if
152180 let primaryKey = arrayOfDictionariesPrimaryKeys [ key] ,
@@ -158,6 +186,9 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
158186 return nil
159187 }
160188
189+ var ps = path
190+ ps. append ( key)
191+
161192 let contents = Dictionary ( kvs, uniquingKeysWith: { ( l, _) in l } )
162193 return Struct (
163194 availables: [ ] ,
@@ -168,7 +199,7 @@ struct PropertyListGenerator: ExternalOnlyStructGenerator {
168199 typealiasses: [ ] ,
169200 properties: [ ] ,
170201 functions: [ ] ,
171- structs: structsFromInfoPlist ( contents: contents, at: externalAccessLevel) ,
202+ structs: structsFromInfoPlist ( contents: contents, path : ps , at: externalAccessLevel) ,
172203 classes: [ ] ,
173204 os: [ ]
174205 )
0 commit comments