From c45f7aba9383a8d4a4933998116256bcb47da1d0 Mon Sep 17 00:00:00 2001 From: Jierong Li Date: Fri, 27 Dec 2024 16:43:30 +0900 Subject: [PATCH] Add an option to modify bundle expression --- .../StringGenerator/Models/SourceFile.swift | 3 ++ ...e_StringExtension_StringsTableStruct.swift | 4 +++ ...gsTableBundleComputedPropertySnippet.swift | 32 +++++++++++-------- .../StringStringsTableStructSnippet.swift | 24 +++++++------- Sources/StringGenerator/StringGenerator.swift | 5 +-- Sources/xcstrings-tool/Generate.swift | 9 +++++- 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Sources/StringGenerator/Models/SourceFile.swift b/Sources/StringGenerator/Models/SourceFile.swift index 50ed52b..09c0735 100644 --- a/Sources/StringGenerator/Models/SourceFile.swift +++ b/Sources/StringGenerator/Models/SourceFile.swift @@ -13,6 +13,9 @@ struct SourceFile { /// The string resources that make up the strings table let resources: [Resource] + + /// The custom bundle expression + let bundleExpression: String? } extension SourceFile { diff --git a/Sources/StringGenerator/Models/SourceFile_StringExtension_StringsTableStruct.swift b/Sources/StringGenerator/Models/SourceFile_StringExtension_StringsTableStruct.swift index 763f342..a20f9c9 100644 --- a/Sources/StringGenerator/Models/SourceFile_StringExtension_StringsTableStruct.swift +++ b/Sources/StringGenerator/Models/SourceFile_StringExtension_StringsTableStruct.swift @@ -32,6 +32,10 @@ extension SourceFile.StringExtension { BundleDescriptionEnum(stringsTable: self) } + var bundleExpression: String? { + sourceFile.bundleExpression + } + // MARK: Properties var storedProperties: [Property] { diff --git a/Sources/StringGenerator/Snippets/String/StringsTable/Bundle/StringStringsTableBundleComputedPropertySnippet.swift b/Sources/StringGenerator/Snippets/String/StringsTable/Bundle/StringStringsTableBundleComputedPropertySnippet.swift index da5b135..64292b8 100644 --- a/Sources/StringGenerator/Snippets/String/StringsTable/Bundle/StringStringsTableBundleComputedPropertySnippet.swift +++ b/Sources/StringGenerator/Snippets/String/StringsTable/Bundle/StringStringsTableBundleComputedPropertySnippet.swift @@ -25,19 +25,23 @@ struct StringStringsTableBundleComputedPropertySnippet: Snippet { @CodeBlockItemListBuilder var body: CodeBlockItemListSyntax { - // #if SWIFT_PACKAGE - // .module - // #else - // Bundle(for: BundleLocator.self) - // #endif - IfConfigDeclSyntax( - reference: "SWIFT_PACKAGE", - elements: .statements([ - CodeBlockItemSyntax(item: .expr(ExprSyntax(".module"))) - ]), - else: .statements([ - CodeBlockItemSyntax(item: .expr(ExprSyntax("Bundle(for: BundleLocator.self)"))) - ]) - ) + if let bundleExpression = stringsTable.bundleExpression { + CodeBlockItemSyntax(item: .expr(ExprSyntax(stringLiteral: bundleExpression))) + } else { + // #if SWIFT_PACKAGE + // .module + // #else + // Bundle(for: BundleLocator.self) + // #endif + IfConfigDeclSyntax( + reference: "SWIFT_PACKAGE", + elements: .statements([ + CodeBlockItemSyntax(item: .expr(ExprSyntax(".module"))) + ]), + else: .statements([ + CodeBlockItemSyntax(item: .expr(ExprSyntax("Bundle(for: BundleLocator.self)"))) + ]) + ) + } } } diff --git a/Sources/StringGenerator/Snippets/String/StringsTable/StringStringsTableStructSnippet.swift b/Sources/StringGenerator/Snippets/String/StringsTable/StringStringsTableStructSnippet.swift index ff269bd..b920f0b 100644 --- a/Sources/StringGenerator/Snippets/String/StringsTable/StringStringsTableStructSnippet.swift +++ b/Sources/StringGenerator/Snippets/String/StringsTable/StringStringsTableStructSnippet.swift @@ -27,17 +27,19 @@ struct StringStringsTableStructSnippet: Snippet { var memberBlock: MemberBlockSyntax { MemberBlockSyntax { - // #if !SWIFT_PACKAGE - // private class BundleLocator { ... } - // #endif - IfConfigDeclSyntax( - prefixOperator: "!", - reference: "SWIFT_PACKAGE", - elements: .decls(MemberBlockItemListSyntax { - StringStringsTableBundleLocatorClassSnippet() - }) - ) - .with(\.trailingTrivia, .newlines(2)) + if stringsTable.bundleExpression == nil { + // #if !SWIFT_PACKAGE + // private class BundleLocator { ... } + // #endif + IfConfigDeclSyntax( + prefixOperator: "!", + reference: "SWIFT_PACKAGE", + elements: .decls(MemberBlockItemListSyntax { + StringStringsTableBundleLocatorClassSnippet() + }) + ) + .with(\.trailingTrivia, .newlines(2)) + } // enum Argument { ... } StringStringsTableArgumentEnumSnippet(argument: stringsTable.argumentEnum) diff --git a/Sources/StringGenerator/StringGenerator.swift b/Sources/StringGenerator/StringGenerator.swift index b82b403..c208cad 100644 --- a/Sources/StringGenerator/StringGenerator.swift +++ b/Sources/StringGenerator/StringGenerator.swift @@ -10,10 +10,11 @@ public struct StringGenerator { public static func generateSource( for resources: [Resource], tableName: String, - accessLevel: AccessLevel + accessLevel: AccessLevel, + bundleExpression: String? ) -> String { generateSource( - for: SourceFile(tableName: tableName, accessLevel: accessLevel, resources: resources) + for: SourceFile(tableName: tableName, accessLevel: accessLevel, resources: resources, bundleExpression: bundleExpression) ) } diff --git a/Sources/xcstrings-tool/Generate.swift b/Sources/xcstrings-tool/Generate.swift index 1f9b3d3..4bbff6b 100644 --- a/Sources/xcstrings-tool/Generate.swift +++ b/Sources/xcstrings-tool/Generate.swift @@ -36,6 +36,12 @@ struct Generate: ParsableCommand { ) var accessLevel: AccessLevel? + @Option( + name: .shortAndLong, + help: "Modify bundle expression for the generated source code" + ) + var bundleExpression: String? + @Option( name: .shortAndLong, help: "The development language (defaultLocalization in Package.swift) used when filtering legacy .strings and .stringsdict files from the input paths" @@ -86,7 +92,8 @@ struct Generate: ParsableCommand { let source = StringGenerator.generateSource( for: resources, tableName: input.tableName, - accessLevel: resolvedAccessLevel + accessLevel: resolvedAccessLevel, + bundleExpression: bundleExpression ) // Write the output and catch errors in a diagnostic format