Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/StringGenerator/Models/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extension SourceFile.StringExtension {
BundleDescriptionEnum(stringsTable: self)
}

var bundleExpression: String? {
sourceFile.bundleExpression
}

// MARK: Properties

var storedProperties: [Property] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")))
])
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions Sources/StringGenerator/StringGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand Down
9 changes: 8 additions & 1 deletion Sources/xcstrings-tool/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down