|
| 1 | +// |
| 2 | +// PropertyListGenerator.swift |
| 3 | +// R.swift |
| 4 | +// |
| 5 | +// Created by Tom Lokhorst on 2018-07-07. |
| 6 | +// From: https://github.com/mac-cain13/R.swift |
| 7 | +// License: MIT License |
| 8 | +// |
| 9 | + |
| 10 | +import Foundation |
| 11 | + |
| 12 | +struct PropertyListGenerator: ExternalOnlyStructGenerator { |
| 13 | + private let name: SwiftIdentifier |
| 14 | + private let plists: [PropertyList] |
| 15 | + private let toplevelKeysWhitelist: [String]? |
| 16 | + |
| 17 | + init(name: SwiftIdentifier, plists: [PropertyList], toplevelKeysWhitelist: [String]?) { |
| 18 | + self.name = name |
| 19 | + self.plists = plists |
| 20 | + self.toplevelKeysWhitelist = toplevelKeysWhitelist |
| 21 | + } |
| 22 | + |
| 23 | + func generatedStruct(at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Struct { |
| 24 | + guard let plist = plists.first else { return .empty } |
| 25 | + |
| 26 | + guard plists.all(where: { $0.url == plist.url }) else { |
| 27 | + let configs = plists.map { $0.buildConfigurationName } |
| 28 | + warn("Build configurations \(configs) use different \(name) files, this is not yet supported") |
| 29 | + return .empty |
| 30 | + } |
| 31 | + |
| 32 | + let contents: PropertyList.Contents |
| 33 | + if let whitelist = toplevelKeysWhitelist { |
| 34 | + contents = plist.contents.filter { (key, _) in whitelist.contains(key) } |
| 35 | + } else { |
| 36 | + contents = plist.contents |
| 37 | + } |
| 38 | + |
| 39 | + let qualifiedName = prefix + name |
| 40 | + |
| 41 | + return Struct( |
| 42 | + availables: [], |
| 43 | + comments: ["This `\(qualifiedName)` struct is generated, and contains static references to \(contents.count) properties."], |
| 44 | + accessModifier: externalAccessLevel, |
| 45 | + type: Type(module: .host, name: name), |
| 46 | + implements: [], |
| 47 | + typealiasses: [], |
| 48 | + properties: propertiesFromInfoPlist(contents: contents, path: [], at: externalAccessLevel), |
| 49 | + functions: [], |
| 50 | + structs: structsFromInfoPlist(contents: contents, path: [], at: externalAccessLevel), |
| 51 | + classes: [], |
| 52 | + os: [] |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + private func propertiesFromInfoPlist(contents: [String: Any], path: [String], at externalAccessLevel: AccessLevel) -> [Let] { |
| 57 | + |
| 58 | + return contents |
| 59 | + .compactMap { (key, value) -> Let? in |
| 60 | + switch value { |
| 61 | + case let value as Bool: |
| 62 | + return Let( |
| 63 | + comments: [], |
| 64 | + accessModifier: externalAccessLevel, |
| 65 | + isStatic: true, |
| 66 | + name: SwiftIdentifier(name: key), |
| 67 | + typeDefinition: .inferred(Type._Bool), |
| 68 | + value: "\(value)" |
| 69 | + ) |
| 70 | + case let value as String: |
| 71 | + return propertyFromInfoString(key: key, value: value, path: path, at: externalAccessLevel) |
| 72 | + default: |
| 73 | + return nil |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private func propertyFromInfoString(key: String, value: String, path: [String], at externalAccessLevel: AccessLevel) -> Let { |
| 79 | + |
| 80 | + let steps = path.map { "\"\($0.escapedStringLiteral)\"" }.joined(separator: ", ") |
| 81 | + |
| 82 | + let isKey = key == "_key" |
| 83 | + let letValue: String = isKey |
| 84 | + ? "\"\(value.escapedStringLiteral)\"" |
| 85 | + : "infoPlistString(path: [\(steps)], key: \"\(key.escapedStringLiteral)\") ?? \"\(value.escapedStringLiteral)\"" |
| 86 | + |
| 87 | + return Let( |
| 88 | + comments: [], |
| 89 | + accessModifier: externalAccessLevel, |
| 90 | + isStatic: true, |
| 91 | + name: SwiftIdentifier(name: key), |
| 92 | + typeDefinition: .inferred(Type._String), |
| 93 | + value: letValue |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + private func structsFromInfoPlist(contents: [String: Any], path: [String], at externalAccessLevel: AccessLevel) -> [Struct] { |
| 98 | + |
| 99 | + return contents |
| 100 | + .compactMap { (key, value) -> Struct? in |
| 101 | + var ps = path |
| 102 | + ps.append(key) |
| 103 | + |
| 104 | + switch value { |
| 105 | + case let array as [String]: |
| 106 | + return Struct( |
| 107 | + availables: [], |
| 108 | + comments: [], |
| 109 | + accessModifier: externalAccessLevel, |
| 110 | + type: Type(module: .host, name: SwiftIdentifier(name: key)), |
| 111 | + implements: [], |
| 112 | + typealiasses: [], |
| 113 | + properties: array.map { item in |
| 114 | + propertyFromInfoString(key: item, value: item, path: ps, at: externalAccessLevel) |
| 115 | + }, |
| 116 | + functions: [], |
| 117 | + structs: [], |
| 118 | + classes: [], |
| 119 | + os: [] |
| 120 | + ) |
| 121 | + |
| 122 | + case var dict as [String: Any]: |
| 123 | + dict["_key"] = key |
| 124 | + |
| 125 | + return Struct( |
| 126 | + availables: [], |
| 127 | + comments: [], |
| 128 | + accessModifier: externalAccessLevel, |
| 129 | + type: Type(module: .host, name: SwiftIdentifier(name: key)), |
| 130 | + implements: [], |
| 131 | + typealiasses: [], |
| 132 | + properties: propertiesFromInfoPlist(contents: dict, path: ps, at: externalAccessLevel), |
| 133 | + functions: [], |
| 134 | + structs: structsFromInfoPlist(contents: dict, path: ps, at: externalAccessLevel), |
| 135 | + classes: [], |
| 136 | + os: [] |
| 137 | + ) |
| 138 | + |
| 139 | + case let dicts as [[String: Any]] where arrayOfDictionariesPrimaryKeys.keys.contains(key): |
| 140 | + return structForArrayOfDictionaries(key: key, dicts: dicts, path: path, at: externalAccessLevel) |
| 141 | + |
| 142 | + default: |
| 143 | + return nil |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // For arrays of dictionaries we need a primary key. |
| 149 | + // This key will be used as a name for the struct in the generated code. |
| 150 | + private let arrayOfDictionariesPrimaryKeys: [String: String] = [ |
| 151 | + "UIWindowSceneSessionRoleExternalDisplay": "UISceneConfigurationName", |
| 152 | + "UIWindowSceneSessionRoleApplication": "UISceneConfigurationName", |
| 153 | + "UIApplicationShortcutItems": "UIApplicationShortcutItemType", |
| 154 | + "CFBundleDocumentTypes": "CFBundleTypeName", |
| 155 | + "CFBundleURLTypes": "CFBundleURLName" |
| 156 | + ] |
| 157 | + |
| 158 | + private func structForArrayOfDictionaries(key: String, dicts: [[String: Any]], path: [String], at externalAccessLevel: AccessLevel) -> Struct { |
| 159 | + let kvs = dicts.compactMap { dict -> (String, [String: Any])? in |
| 160 | + if |
| 161 | + let primaryKey = arrayOfDictionariesPrimaryKeys[key], |
| 162 | + let type = dict[primaryKey] as? String |
| 163 | + { |
| 164 | + return (type, dict) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | + } |
| 169 | + |
| 170 | + var ps = path |
| 171 | + ps.append(key) |
| 172 | + |
| 173 | + let contents = Dictionary(kvs, uniquingKeysWith: { (l, _) in l }) |
| 174 | + return Struct( |
| 175 | + availables: [], |
| 176 | + comments: [], |
| 177 | + accessModifier: externalAccessLevel, |
| 178 | + type: Type(module: .host, name: SwiftIdentifier(name: key)), |
| 179 | + implements: [], |
| 180 | + typealiasses: [], |
| 181 | + properties: [], |
| 182 | + functions: [], |
| 183 | + structs: structsFromInfoPlist(contents: contents, path: ps, at: externalAccessLevel), |
| 184 | + classes: [], |
| 185 | + os: [] |
| 186 | + ) |
| 187 | + } |
| 188 | +} |
0 commit comments