From 19a14b45838a21c75500313c481e5a05b607a4a7 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Mon, 6 Jun 2022 15:02:16 +0300 Subject: [PATCH] Fix Info.plist crash on Mac (#72) * #71 Use custom template for Info.plist * Add changelog --- ElementX/Sources/Generated/InfoPlist.swift | 6 +- .../runtime-swift5-element-info.stencil | 109 ++++++++++++++++++ Tools/SwiftGen/swiftgen-config.yml | 2 +- changelog.d/71.bugfix | 1 + 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 Tools/SwiftGen/Templates/Plists/runtime-swift5-element-info.stencil create mode 100644 changelog.d/71.bugfix diff --git a/ElementX/Sources/Generated/InfoPlist.swift b/ElementX/Sources/Generated/InfoPlist.swift index 71a316a80..4c19463b7 100644 --- a/ElementX/Sources/Generated/InfoPlist.swift +++ b/ElementX/Sources/Generated/InfoPlist.swift @@ -39,11 +39,7 @@ private struct PlistDocument { let data: [String: Any] init(path: String) { - guard let url = BundleToken.bundle.url(forResource: path, withExtension: nil), - let data = NSDictionary(contentsOf: url) as? [String: Any] else { - fatalError("Unable to load PLIST at path: \(path)") - } - self.data = data + self.data = BundleToken.bundle.infoDictionary ?? [:] } subscript(key: String) -> T { diff --git a/Tools/SwiftGen/Templates/Plists/runtime-swift5-element-info.stencil b/Tools/SwiftGen/Templates/Plists/runtime-swift5-element-info.stencil new file mode 100644 index 000000000..551d19cb0 --- /dev/null +++ b/Tools/SwiftGen/Templates/Plists/runtime-swift5-element-info.stencil @@ -0,0 +1,109 @@ +// swiftlint:disable all +// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen + +{% if files %} +{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %} +import Foundation + +// swiftlint:disable superfluous_disable_command +// swiftlint:disable file_length + +// MARK: - Plist Files +{% macro fileBlock file %} + {% call documentBlock file file.document %} +{% endmacro %} +{% macro documentBlock file document %} + {% set rootType %}{% call typeBlock document.metadata %}{% endset %} + {% if document.metadata.type == "Array" %} + {{accessModifier}} static let items: {{rootType}} = arrayFromPlist(at: "{% call transformPath file.path %}") + {% elif document.metadata.type == "Dictionary" %} + private static let _document = PlistDocument(path: "{% call transformPath file.path %}") + + {% for key,value in document.metadata.properties %} + {{accessModifier}} {% call propertyBlock key value %} + {% endfor %} + {% else %} + // Unsupported root type `{{rootType}}` + {% endif %} +{% endmacro %} +{% macro typeBlock metadata %}{% filter removeNewlines:"leading" %} + {% if metadata.type == "Array" %} + [{% call typeBlock metadata.element %}] + {% elif metadata.type == "Dictionary" %} + [String: Any] + {% else %} + {{metadata.type}} + {% endif %} +{% endfilter %}{% endmacro %} +{% macro propertyBlock key metadata %}{% filter removeNewlines:"leading" %} + {% set propertyName %}{{key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}{% endset %} + {% set propertyType %}{% call typeBlock metadata %}{% endset %} + static let {{propertyName}}: {{propertyType}} = _document["{{key}}"] +{% endfilter %}{% endmacro %} +{% macro transformPath path %}{% filter removeNewlines %} + {% if param.preservePath %} + {{path}} + {% else %} + {{path|basename}} + {% endif %} +{% endfilter %}{% endmacro %} + +// swiftlint:disable identifier_name line_length type_body_length +{{accessModifier}} enum {{param.enumName|default:"PlistFiles"}} { + {% if files.count > 1 or param.forceFileNameEnum %} + {% for file in files %} + {{accessModifier}} enum {{file.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { + {% filter indent:2 %}{% call fileBlock file %}{% endfilter %} + } + {% endfor %} + {% else %} + {% call fileBlock files.first %} + {% endif %} +} +// swiftlint:enable identifier_name line_length type_body_length + +// MARK: - Implementation Details + +private func arrayFromPlist(at path: String) -> [T] { + {% if param.lookupFunction %} + guard let url = {{param.lookupFunction}}(path), + {% else %} + guard let url = {{param.bundle|default:"BundleToken.bundle"}}.url(forResource: path, withExtension: nil), + {% endif %} + let data = NSArray(contentsOf: url) as? [T] else { + fatalError("Unable to load PLIST at path: \(path)") + } + return data +} + +private struct PlistDocument { + let data: [String: Any] + + init(path: String) { + self.data = {{param.bundle|default:"BundleToken.bundle"}}.infoDictionary ?? [:] + } + + subscript(key: String) -> T { + guard let result = data[key] as? T else { + fatalError("Property '\(key)' is not of type \(T.self)") + } + return result + } +} +{% if not param.bundle and not param.lookupFunction %} + +// swiftlint:disable convenience_type +private final class BundleToken { + static let bundle: Bundle = { + #if SWIFT_PACKAGE + return Bundle.module + #else + return Bundle(for: BundleToken.self) + #endif + }() +} +// swiftlint:enable convenience_type +{% endif %} +{% else %} +// No files found +{% endif %} diff --git a/Tools/SwiftGen/swiftgen-config.yml b/Tools/SwiftGen/swiftgen-config.yml index 2cf933ff8..c138508cd 100755 --- a/Tools/SwiftGen/swiftgen-config.yml +++ b/Tools/SwiftGen/swiftgen-config.yml @@ -27,7 +27,7 @@ strings: plist: inputs: SupportingFiles/Info.plist outputs: - templateName: runtime-swift5 + templatePath: Templates/Plists/runtime-swift5-element-info.stencil output: InfoPlist.swift params: enumName: ElementInfoPlist diff --git a/changelog.d/71.bugfix b/changelog.d/71.bugfix new file mode 100644 index 000000000..96461cc6f --- /dev/null +++ b/changelog.d/71.bugfix @@ -0,0 +1 @@ +ElementInfoPlist: Use custom template for Info.plist.