feat(RES-57): Add visibility to control scope of generated code (#18)
All checks were successful
gitea-openium/resgen.swift/pipeline/head This commit looks good

Add visibility parameter: public, package, internal, private
Impacted command: analytics, colors, fonts, images, strings, tags

Reviewed-on: #18
This commit is contained in:
2025-07-21 16:56:05 +02:00
parent beca2c6b2b
commit 5ad219ae89
62 changed files with 1526 additions and 794 deletions

View File

@@ -6,6 +6,7 @@
//
import Foundation
import ToolCore
// swiftlint:disable force_unwrapping
@@ -99,14 +100,20 @@ class Definition {
return (inputParameters: inputParameters, translationArguments: translationArguments)
}
private func getBaseProperty(lang: String, translation: String, isStatic: Bool, comment: String?) -> String {
private func getBaseProperty(
lang: String,
translation: String,
isStatic: Bool,
comment: String?,
visibility: ExtensionVisibility
) -> String {
"""
/// Translation in \(lang) :
/// \(translation)
///
/// Comment :
/// \(comment?.isEmpty == false ? comment! : "No comment")
\(isStatic ? "static " : "")var \(name): String {
\(visibility) \(isStatic ? "static " : "")var \(name): String {
NSLocalizedString("\(name)", tableName: kStringsFileName, bundle: Bundle.main, value: "\(translation)", comment: "\(comment ?? "")")
}
"""
@@ -118,7 +125,8 @@ class Definition {
isStatic: Bool,
inputParameters: [String],
translationArguments: [String],
comment: String?
comment: String?,
visibility: ExtensionVisibility
) -> String {
"""
@@ -127,13 +135,13 @@ class Definition {
///
/// Comment :
/// \(comment?.isEmpty == false ? comment! : "No comment")
\(isStatic ? "static " : "")func \(name)(\(inputParameters.joined(separator: ", "))) -> String {
\(visibility) \(isStatic ? "static " : "")func \(name)(\(inputParameters.joined(separator: ", "))) -> String {
String(format: \(isStatic ? "Self" : "self").\(name), \(translationArguments.joined(separator: ", ")))
}
"""
}
func getNSLocalizedStringProperty(forLang lang: String) -> String {
func getNSLocalizedStringProperty(forLang lang: String, visibility: ExtensionVisibility) -> String {
guard let translation = translations[lang] else {
let error = StringiumError.langNotDefined(lang, name, reference != nil)
print(error.description)
@@ -145,7 +153,8 @@ class Definition {
lang: lang,
translation: translation,
isStatic: false,
comment: self.comment
comment: self.comment,
visibility: visibility
)
// Generate method
@@ -157,14 +166,15 @@ class Definition {
isStatic: false,
inputParameters: parameters.inputParameters,
translationArguments: parameters.translationArguments,
comment: self.comment
comment: self.comment,
visibility: visibility
)
}
return property + method
}
func getNSLocalizedStringStaticProperty(forLang lang: String) -> String {
func getNSLocalizedStringStaticProperty(forLang lang: String, visibility: ExtensionVisibility) -> String {
guard let translation = translations[lang] else {
let error = StringiumError.langNotDefined(lang, name, reference != nil)
print(error.description)
@@ -176,7 +186,8 @@ class Definition {
lang: lang,
translation: translation,
isStatic: true,
comment: self.comment
comment: self.comment,
visibility: visibility
)
// Generate method
@@ -188,7 +199,8 @@ class Definition {
isStatic: true,
inputParameters: parameters.inputParameters,
translationArguments: parameters.translationArguments,
comment: self.comment
comment: self.comment,
visibility: visibility
)
}
@@ -197,7 +209,7 @@ class Definition {
// MARK: - Raw strings
func getProperty(forLang lang: String) -> String {
func getProperty(forLang lang: String, visibility: ExtensionVisibility) -> String {
guard let translation = translations[lang] else {
let error = StringiumError.langNotDefined(lang, name, reference != nil)
print(error.description)
@@ -210,14 +222,13 @@ class Definition {
///
/// Comment :
/// \(comment?.isEmpty == false ? comment! : "No comment")
var \(name): String {
\(visibility) var \(name): String {
"\(translation)"
}
"""
}
func getStaticProperty(forLang lang: String) -> String {
func getStaticProperty(forLang lang: String, visibility: ExtensionVisibility) -> String {
guard let translation = translations[lang] else {
let error = StringiumError.langNotDefined(lang, name, reference != nil)
print(error.description)
@@ -230,7 +241,7 @@ class Definition {
///
/// Comment :
/// \(comment?.isEmpty == false ? comment! : "No comment")
static var \(name): String {
\(visibility) static var \(name): String {
"\(translation)"
}
"""