From beb28e652d55ea443bda14a8ec8a2133aa082c37 Mon Sep 17 00:00:00 2001 From: Thibaut Schmitt Date: Wed, 7 Jun 2023 09:52:36 +0200 Subject: [PATCH 1/2] Add availability to get keyPath from generated strings Key enumeration --- .../Generated/String+GenAllScript.swift | 13 ++- SampleFiles/genAllRessources.sh | 86 +++++++++---------- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/SampleFiles/Strings/Generated/String+GenAllScript.swift b/SampleFiles/Strings/Generated/String+GenAllScript.swift index bdccd44..d351b69 100644 --- a/SampleFiles/Strings/Generated/String+GenAllScript.swift +++ b/SampleFiles/Strings/Generated/String+GenAllScript.swift @@ -6,13 +6,24 @@ fileprivate let kStringsFileName = "sampleStrings" extension String { - enum Key: String { + enum KeyGenAllScript: String { case param_lang = "param_lang" case generic_back = "generic_back" case generic_loading_data = "generic_loading_data" case generic_welcome_firstname_format = "generic_welcome_firstname_format" case test_equal_symbol = "test_equal_symbol" case placeholders_test_one = "placeholders_test_one" + + var keyPath: KeyPath { + switch self { + case .param_lang: return \String.param_lang + case .generic_back: return \String.generic_back + case .generic_loading_data: return \String.generic_loading_data + case .generic_welcome_firstname_format: return \String.generic_welcome_firstname_format + case .test_equal_symbol: return \String.test_equal_symbol + case .placeholders_test_one: return \String.placeholders_test_one + } + } } // MARK: - Webservice diff --git a/SampleFiles/genAllRessources.sh b/SampleFiles/genAllRessources.sh index a9d8550..1530efc 100755 --- a/SampleFiles/genAllRessources.sh +++ b/SampleFiles/genAllRessources.sh @@ -2,33 +2,33 @@ FORCE_FLAG="$1" -# Font -swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/sampleFontsAll.txt" \ - --extension-output-path "./Fonts/Generated" \ - --extension-name "UIFontYolo" \ - --extension-name-swift-ui "FontYolo" \ - --extension-suffix "GenAllScript" \ - --info-plist-paths "./Fonts/Generated/test.plist ./Fonts/Generated/test2.plist" - -echo "\n-------------------------\n" - -# Color -swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/sampleColors1.txt" \ - --style all \ - --xcassets-path "./Colors/colors.xcassets" \ - --extension-output-path "./Colors/Generated/" \ - --extension-name "UIColorYolo" \ - --extension-name-swift-ui "ColorYolo" \ - --extension-suffix "GenAllScript" - -echo "\n-------------------------\n" - -# Twine -swift run -c release ResgenSwift strings twine $FORCE_FLAG "./Twine/sampleStrings.txt" \ - --output-path "./Twine/Generated" \ - --langs "fr en en-us" \ - --default-lang "en" \ - --extension-output-path "./Twine/Generated" +## Font +#swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/sampleFontsAll.txt" \ +# --extension-output-path "./Fonts/Generated" \ +# --extension-name "UIFontYolo" \ +# --extension-name-swift-ui "FontYolo" \ +# --extension-suffix "GenAllScript" \ +# --info-plist-paths "./Fonts/Generated/test.plist ./Fonts/Generated/test2.plist" +# +#echo "\n-------------------------\n" +# +## Color +#swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/sampleColors1.txt" \ +# --style all \ +# --xcassets-path "./Colors/colors.xcassets" \ +# --extension-output-path "./Colors/Generated/" \ +# --extension-name "UIColorYolo" \ +# --extension-name-swift-ui "ColorYolo" \ +# --extension-suffix "GenAllScript" +# +#echo "\n-------------------------\n" +# +## Twine +#swift run -c release ResgenSwift strings twine $FORCE_FLAG "./Twine/sampleStrings.txt" \ +# --output-path "./Twine/Generated" \ +# --langs "fr en en-us" \ +# --default-lang "en" \ +# --extension-output-path "./Twine/Generated" echo "\n-------------------------\n" @@ -43,19 +43,19 @@ swift run -c release ResgenSwift strings stringium $FORCE_FLAG "./Strings/sample echo "\n-------------------------\n" -# Tags -swift run -c release ResgenSwift strings tags $FORCE_FLAG "./Tags/sampleTags.txt" \ - --lang "ium" \ - --extension-output-path "./Tags/Generated" \ - --extension-name "Tags" \ - --extension-suffix "GenAllScript" - -echo "\n-------------------------\n" - -# Images -swift run -c release ResgenSwift images $FORCE_FLAG "./Images/sampleImages.txt" \ - --xcassets-path "./Images/imagium.xcassets" \ - --extension-output-path "./Images/Generated" \ - --extension-name "UIImage" \ - --extension-name-swift-ui "ImageYolo" \ - --extension-suffix "GenAllScript" +## Tags +#swift run -c release ResgenSwift strings tags $FORCE_FLAG "./Tags/sampleTags.txt" \ +# --lang "ium" \ +# --extension-output-path "./Tags/Generated" \ +# --extension-name "Tags" \ +# --extension-suffix "GenAllScript" +# +#echo "\n-------------------------\n" +# +## Images +#swift run -c release ResgenSwift images $FORCE_FLAG "./Images/sampleImages.txt" \ +# --xcassets-path "./Images/imagium.xcassets" \ +# --extension-output-path "./Images/Generated" \ +# --extension-name "UIImage" \ +# --extension-name-swift-ui "ImageYolo" \ +# --extension-suffix "GenAllScript" From 844a8aec4578e57962f701625aa4eb288373422a Mon Sep 17 00:00:00 2001 From: Thibaut Schmitt Date: Wed, 7 Jun 2023 09:52:56 +0200 Subject: [PATCH 2/2] Add availability to get keyPath from generated strings Key enumeration --- .../Generator/StringsFileGenerator.swift | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Sources/ResgenSwift/Strings/Generator/StringsFileGenerator.swift b/Sources/ResgenSwift/Strings/Generator/StringsFileGenerator.swift index 358a9bc..c918dc4 100644 --- a/Sources/ResgenSwift/Strings/Generator/StringsFileGenerator.swift +++ b/Sources/ResgenSwift/Strings/Generator/StringsFileGenerator.swift @@ -133,7 +133,7 @@ class StringsFileGenerator { extensionSuffix: String) -> String { [ Self.getHeader(stringsFilename: inputFilename, extensionClassname: extensionName), - Self.getEnumKey(sections: sections, tags: tags, extensionSuffix: extensionSuffix), + Self.getEnumKey(sections: sections, tags: tags, extensionClassname: extensionName, extensionSuffix: extensionSuffix), Self.getProperties(sections: sections, defaultLang: lang, tags: tags, staticVar: staticVar), Self.getFooter() ] @@ -154,9 +154,10 @@ class StringsFileGenerator { """ } - private static func getEnumKey(sections: [Section], tags: [String], extensionSuffix: String) -> String { + private static func getEnumKey(sections: [Section], tags: [String], extensionClassname: String, extensionSuffix: String) -> String { var enumDefinition = "\n enum Key\(extensionSuffix.uppercasedFirst()): String {\n" + // Enum sections.forEach { section in // Check that at least one string will be generated guard section.hasOneOrMoreMatchingTags(tags: tags) else { @@ -172,7 +173,27 @@ class StringsFileGenerator { } } - enumDefinition += " }" + // KeyPath accessors + enumDefinition += "\n" + enumDefinition += " var keyPath: KeyPath<\(extensionClassname), String> {\n" + enumDefinition += " switch self {\n" + sections.forEach { section in + // Check that at least one string will be generated + guard section.hasOneOrMoreMatchingTags(tags: tags) else { + return // Go to next section + } + + section.definitions.forEach { definition in + guard definition.hasOneOrMoreMatchingTags(inputTags: tags) == true else { + return // Go to next definition + } + debugPrint("Found definition") + enumDefinition += " case .\(definition.name): return \\\(extensionClassname).\(definition.name)\n" + } + } + enumDefinition += " }\n" // Switch + enumDefinition += " }\n" // var keyPath + enumDefinition += " }" // Enum return enumDefinition }