-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
865d40c
commit c1801e8
Showing
4 changed files
with
659 additions
and
3 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
App/Resources/SwiftGen/plist-runtime-swift5-revision.stencil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// 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 %} | ||
{%- if metadata.type == "Array" -%} | ||
[{% call typeBlock metadata.element %}] | ||
{%- elif metadata.type == "Dictionary" -%} | ||
[String: Any] | ||
{%- else -%} | ||
{{metadata.type}} | ||
{%- endif -%} | ||
{% endmacro %} | ||
{% macro propertyBlock key metadata %} | ||
{%- set propertyName %}{{key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}{% endset -%} | ||
{%- set propertyType %}{% call typeBlock metadata %}{% endset -%} | ||
static let {{propertyName}}: {{propertyType}} = _document["{{key}}"] | ||
{% endmacro %} | ||
{% macro transformPath path %} | ||
{%- if param.preservePath -%} | ||
{{path}} | ||
{%- else -%} | ||
{{path|basename}} | ||
{%- endif -%} | ||
{% 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," ",true %}{% 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<T>(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) { | ||
{% 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 = NSDictionary(contentsOf: url) as? [String: Any] else { | ||
fatalError("Unable to load PLIST at path: \(path)") | ||
} | ||
self.data = data | ||
} | ||
|
||
subscript<T>(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.main | ||
#else | ||
return Bundle(for: BundleToken.self) | ||
#endif | ||
}() | ||
} | ||
// swiftlint:enable convenience_type | ||
{% endif %} | ||
{% else %} | ||
// No files found | ||
{% endif %} |
103 changes: 103 additions & 0 deletions
103
App/Resources/SwiftGen/strings-structured-swift5-revision.stencil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// swiftlint:disable all | ||
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen | ||
|
||
{% if tables.count > 0 %} | ||
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %} | ||
import Foundation | ||
|
||
// swiftlint:disable superfluous_disable_command file_length implicit_return prefer_self_in_static_references | ||
|
||
// MARK: - Strings | ||
|
||
{% macro parametersBlock types %} | ||
{%- for type in types -%} | ||
{%- if type == "String" -%} | ||
_ p{{forloop.counter}}: Any | ||
{%- else -%} | ||
_ p{{forloop.counter}}: {{type}} | ||
{%- endif -%} | ||
{{ ", " if not forloop.last }} | ||
{%- endfor -%} | ||
{% endmacro %} | ||
{% macro argumentsBlock types %} | ||
{%- for type in types -%} | ||
{%- if type == "String" -%} | ||
String(describing: p{{forloop.counter}}) | ||
{%- elif type == "UnsafeRawPointer" -%} | ||
Int(bitPattern: p{{forloop.counter}}) | ||
{%- else -%} | ||
p{{forloop.counter}} | ||
{%- endif -%} | ||
{{ ", " if not forloop.last }} | ||
{%- endfor -%} | ||
{% endmacro %} | ||
{% macro recursiveBlock table item %} | ||
{% for string in item.strings %} | ||
{% if not param.noComments %} | ||
{% for line in string.comment|default:string.translation|split:"\n" %} | ||
/// {{line}} | ||
{% endfor %} | ||
{% endif %} | ||
{% set translation string.translation|replace:'"','\"'|replace:' ','\t' %} | ||
{% if string.types %} | ||
{{accessModifier}} static func {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String { | ||
return {{enumName}}.tr("{{table}}", "{{string.key}}", {%+ call argumentsBlock string.types %}, fallback: "{{translation}}") | ||
} | ||
{% elif param.lookupFunction %} | ||
{{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String { return {{enumName}}.tr("{{table}}", "{{string.key}}", fallback: "{{translation}}") } | ||
{% else %} | ||
{{accessModifier}} static let {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}", fallback: "{{translation}}") | ||
{% endif %} | ||
{% endfor %} | ||
{% for child in item.children %} | ||
{{accessModifier}} enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | ||
{% filter indent:2," ",true %}{% call recursiveBlock table child %}{% endfilter %} | ||
} | ||
{% endfor %} | ||
{% endmacro %} | ||
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length | ||
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces | ||
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %} | ||
{{accessModifier}} enum {{enumName}} { | ||
{% if tables.count > 1 or param.forceFileNameEnum %} | ||
{% for table in tables %} | ||
{{accessModifier}} enum {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | ||
{% filter indent:2," ",true %}{% call recursiveBlock table.name table.levels %}{% endfilter %} | ||
} | ||
{% endfor %} | ||
{% else %} | ||
{% call recursiveBlock tables.first.name tables.first.levels %} | ||
{% endif %} | ||
} | ||
// swiftlint:enable explicit_type_interface function_parameter_count identifier_name line_length | ||
// swiftlint:enable nesting type_body_length type_name vertical_whitespace_opening_braces | ||
|
||
// MARK: - Implementation Details | ||
|
||
extension {{enumName}} { | ||
private static func tr(_ table: String, _ key: String, _ args: CVarArg..., fallback value: String) -> String { | ||
{% if param.lookupFunction %} | ||
let format = {{ param.lookupFunction }}(key, table, value) | ||
{% else %} | ||
let format = {{param.bundle|default:"BundleToken.bundle"}}.localizedString(forKey: key, value: value, table: table) | ||
{% endif %} | ||
return String(format: format, locale: Locale.current, arguments: args) | ||
} | ||
} | ||
{% 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.main | ||
#else | ||
return Bundle(for: BundleToken.self) | ||
#endif | ||
}() | ||
} | ||
// swiftlint:enable convenience_type | ||
{% endif %} | ||
{% else %} | ||
// No string found | ||
{% endif %} |
Oops, something went wrong.