Optional generation of extension (R.xx)

Optional generation of Colors/Fonts/Images/Stringium extension

Fix swiftlint warning
This commit is contained in:
2025-07-17 09:39:43 +02:00
parent c3b8ebfb37
commit 3092376b65
34 changed files with 973 additions and 688 deletions

View File

@ -2,13 +2,13 @@
ResgenSwift is a package, fully written in Swift, to help you automatize ressource update and generation.
> 🧐 For all commands, see samples files in `SampleFiles`
> 🧐 For all commands, see samples files in `SampleFiles` and use `resgen-swift help` and `resgen-swift help <subcommand>` for detailed help.
## Fonts
Font generator generates an extension of `UIFont` and `Font` (or custom classes). It also prints content of `UIAppFonts` from your project `.plist`. If project `.plist` is specified, it will update `UIAppFonts` content of all `.plist`.
iOS required to use the **real name** of the font, this name can be different from its filename. To get the **real name**, it uses `fc-scan`. So, be sure that the `$PATH` contains path of `fc-scan`.
iOS required to use the **real name** of the font, this name can be different from its filename. To get the **real name**, it uses `fc-scan`. So, be sure that your `$PATH` variable contains path of `fc-scan`.
**Example**
@ -36,7 +36,7 @@ swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/fonts.txt" \
## Colors
Colors generator generates an extension of `UIColor` (or a custom class) along with colorsets in specified xcassets.
Colors generator generates colorsets in specified xcassets and an extension of `Color` (or a custom class) associated to those colorsets. If the extension name is not specified, no extension will be generated.
```sh
swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \
@ -54,12 +54,13 @@ swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \
1. `-f`: force generation
2. Input colors file
3. `--style` can be `all` or `light`
4. `--extension-output-path`: path where to generate generated extension
4. `--extension-output-path` *(optional)* : path where to generate generated extension
5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters
6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters
7. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppColor+GreatApp.swift`)
8. `--static-members` *(optional)*: generate static properties or not
> ⚠️ Passing a `extensionOutputPath` without any `extensionName` does not generate extension. **But** passing an `extensionName` without `extensionOutputPath` will result in a error.
## Strings
@ -266,7 +267,7 @@ events:
## Images
Images generator will generate images assets along with extensions to access those images easily.
Images generator will generate images assets along with extensions to access those images easily. If the extension name is not specified, no extension will be generated.
```sh
swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \
@ -283,12 +284,13 @@ swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \
1. `-f`: force generation
2. Input images definitions file
3. `--xcassets-path`: xcasset path where to generate imagesets
4. `--extension-output-path`: path where to generate generated extension
4. `--extension-output-path` *(optional)* : path where to generate generated extension
5. `--extension-name` *(optional)* : name of the class to add SwiftUI getters
6. `--extension-name-ui-kit` *(optional)* : name of the class to add UIKit getters
6. `--extension-suffix` *(optional)* : additional text which is added to filename (ex: `AppImage+GreatApp.swift`)
7. `--static-members` *(optional)*: generate static properties or not
> ⚠️ Passing a `extensionOutputPath` without any `extensionName` does not generate extension. **But** passing an `extensionName` without `extensionOutputPath` will result in a error.
> ⚠️ Svg images will be copied in the assets and rendered as "Original", however if those images are not rendered correctly you can force the png generation by adding the key word "png" like this: id arrow_back 15 ? png
## All at once

View File

@ -1,4 +1,4 @@
// Generated by ResgenSwift.Color 1.2
// Generated by ResgenSwift.Color 2.1.0
import UIKit

View File

@ -0,0 +1,150 @@
{
"sourceLanguage" : "en",
"strings" : {
"generic_back" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Back"
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "Back"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Retour"
}
}
}
},
"generic_loading_data" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Loading data..."
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "Loading data..."
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Chargement des données..."
}
}
}
},
"generic_welcome_firstname_format" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Welcome \\\"%@\\\" !"
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "Welcome \\\"%@\\\" !"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Bienvenue \\\"%@\\\" !"
}
}
}
},
"param_lang" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "en"
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "en-us"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "fr"
}
}
}
},
"placeholders_test_one" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "You %%: %2$@ %1$@ Age: %3$d"
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "You %%: %2$@ %1$@ Age: %3$d"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Vous %%: %1$@ %2$@ Age: %3$d"
}
}
}
},
"test_equal_symbol" : {
"comment" : "",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "1€ = 1 point !"
}
},
"en-us" : {
"stringUnit" : {
"state" : "translated",
"value" : "1€ = 1 point !"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "1€ = 1 point !"
}
}
}
}
},
"version" : "1.0"
}

View File

@ -1,11 +1,13 @@
// Generated by ResgenSwift.Analytics 2.1.0
import Foundation
import MatomoTracker
import FirebaseAnalytics
// MARK: - Protocol
protocol AnalyticsManagerProtocol {
func logScreen(
name: String,
path: String,
@ -22,75 +24,12 @@ protocol AnalyticsManagerProtocol {
func setEnable(_ enable: Bool)
}
// MARK: - Matomo
class MatomoAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Properties
private var tracker: MatomoTracker
// MARK: - Init
init(siteId: String, url: String) {
debugPrint("[Matomo service] Server URL: \(url)")
debugPrint("[Matomo service] Site ID: \(siteId)")
tracker = MatomoTracker(
siteId: siteId,
baseURL: URL(string: url)!
)
#if DEBUG
tracker.dispatchInterval = 5
#endif
#if DEBUG
tracker.logger = DefaultLogger(minLevel: .verbose)
#endif
debugPrint("[Matomo service] Configured with content base: \(tracker.contentBase?.absoluteString ?? "-")")
debugPrint("[Matomo service] Opt out: \(tracker.isOptedOut)")
}
// MARK: - Methods
func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
guard let trackerUrl = tracker.contentBase?.absoluteString else { return }
let urlString = URL(string: "\(trackerUrl)" + "/" + "\(path)" + "iOS")
tracker.track(
view: [name],
url: urlString
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
tracker.track(
eventWithCategory: category,
action: action,
name: name,
number: nil,
url: nil
)
}
func setEnable(_ enable: Bool) {
tracker.isOptedOut = !enable
}
}
// MARK: - Firebase
class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Methods
func logScreen(
name: String,
path: String,
@ -157,16 +96,16 @@ class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
parameters: parameters
)
}
func setEnable(_ enable: Bool) {
Analytics.setAnalyticsCollectionEnabled(enable)
}
}
// MARK: - Traker Type
enum TrackerType: CaseIterable {
case matomo
case firebase
}
@ -190,7 +129,7 @@ class AnalyticsManager {
}
}
// MARK: - Methods
// MARK: - Enable Methods
private func setAnalytics(enable: Bool, _ analytics: [TrackerType]) {
managers.forEach { (key, value) in
@ -210,14 +149,12 @@ class AnalyticsManager {
setAnalytics(enable: false, analytics)
}
func configure(siteId: String, url: String) {
managers[TrackerType.matomo] = MatomoAnalyticsManager(
siteId: siteId,
url: url
)
func configure() {
managers[TrackerType.firebase] = FirebaseAnalyticsManager()
}
// MARK: - Private Log Methods
private func logScreen(
name: String,
path: String,
@ -262,12 +199,12 @@ class AnalyticsManager {
)
}
static func logEventS1DefTwo(
func logEventS1DefTwo(
title: String,
count: String,
test2: String = "test"
) {
AnalyticsManager.shared.logEvent(
logEvent(
name: "s1 def two",
action: "test",
category: "test",

View File

@ -54,7 +54,7 @@ echo "\n-------------------------\n"
# Analytics
swift run -c release ResgenSwift analytics $FORCE_FLAG "./Tags/sampleTags.yml" \
--target "firebase matomo" \
--target "firebase" \
--extension-output-path "./Tags/Generated" \
--extension-name "Analytics" \
--extension-suffix "GenAllScript" \

View File

@ -5,11 +5,14 @@
// Created by Loris Perret on 08/12/2023.
//
// CPD-OFF
import CoreVideo
import Foundation
import ToolCore
// Disabled cause it's a pain to handle in generated string
// swiftlint:disable type_body_length
enum AnalyticsGenerator {
@ -94,7 +97,7 @@ enum AnalyticsGenerator {
\(Self.getAnalyticsProtocol(targets: targets))
\(Self.getTrackerTypeEnum())
\(Self.getTrackerTypeEnum(targets: targets))
// MARK: - Manager
@ -116,9 +119,9 @@ enum AnalyticsGenerator {
"""
}
private static func getTrackerTypeEnum() -> String {
private static func getTrackerTypeEnum(targets: [TrackerType]) -> String {
var result: [String] = []
TrackerType.allCases.forEach { type in
targets.forEach { type in
result.append(" case \(type)")
}
@ -126,6 +129,7 @@ enum AnalyticsGenerator {
// MARK: - Traker Type
enum TrackerType: CaseIterable {
\(result.joined(separator: "\n"))
}
"""
@ -141,7 +145,7 @@ enum AnalyticsGenerator {
}
}
// MARK: - Methods
// MARK: - Enable Methods
private func setAnalytics(enable: Bool, _ analytics: [TrackerType]) {
managers.forEach { (key, value) in
@ -171,6 +175,7 @@ enum AnalyticsGenerator {
if targets.contains(TrackerType.matomo) {
result.append("import MatomoTracker")
}
if targets.contains(TrackerType.firebase) {
result.append("import FirebaseAnalytics")
}
@ -180,7 +185,9 @@ enum AnalyticsGenerator {
private static func getPrivateLogFunction() -> String {
"""
private func logScreen(
// MARK: - Private Log Methods
private func logScreen(
name: String,
path: String,
params: [String: Any]?
@ -235,6 +242,7 @@ enum AnalyticsGenerator {
)
""")
}
if targets.contains(TrackerType.firebase) {
content.append(" managers[TrackerType.firebase] = FirebaseAnalyticsManager()")
}
@ -252,6 +260,7 @@ enum AnalyticsGenerator {
// MARK: - Protocol
protocol AnalyticsManagerProtocol {
func logScreen(
name: String,
path: String,
@ -267,7 +276,6 @@ enum AnalyticsGenerator {
func setEnable(_ enable: Bool)
}
"""
var result: [String] = [proto]
@ -280,7 +288,7 @@ enum AnalyticsGenerator {
result.append(FirebaseGenerator.service)
}
return result.joined(separator: "\n")
return result.joined(separator: "\n\n")
}
private static func getProperties(
@ -319,3 +327,5 @@ enum AnalyticsGenerator {
"""
}
}
// CPD-ON

View File

@ -13,11 +13,11 @@ enum FirebaseGenerator {
static var service: String {
[
FirebaseGenerator.header,
FirebaseGenerator.logScreen,
FirebaseGenerator.logEvent,
FirebaseGenerator.enable,
FirebaseGenerator.footer
Self.header,
Self.logScreen,
Self.logEvent,
Self.enable,
Self.footer
]
.joined(separator: "\n")
}
@ -29,6 +29,9 @@ enum FirebaseGenerator {
// MARK: - Firebase
class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Methods
"""
}
@ -105,6 +108,7 @@ enum FirebaseGenerator {
parameters: parameters
)
}
"""
}
@ -119,7 +123,6 @@ enum FirebaseGenerator {
private static var footer: String {
"""
}
"""
}
}

View File

@ -5,18 +5,20 @@
// Created by Loris Perret on 05/12/2023.
//
// CPD-OFF
import Foundation
enum MatomoGenerator {
static var service: String {
[
MatomoGenerator.header,
MatomoGenerator.setup,
MatomoGenerator.logScreen,
MatomoGenerator.logEvent,
MatomoGenerator.enable,
MatomoGenerator.footer
Self.header,
Self.setup,
Self.logScreen,
Self.logEvent,
Self.enable,
Self.footer
]
.joined(separator: "\n")
}
@ -115,7 +117,8 @@ enum MatomoGenerator {
private static var footer: String {
"""
}
"""
}
}
// CPD-ON

View File

@ -60,8 +60,10 @@ class AnalyticsDefinition {
switch parameter.type {
case .bool:
defaultValue = "\(parameter.defaultValue.lowercased())"
case .int, .double:
defaultValue = "\(parameter.defaultValue)"
case .string:
defaultValue = "\"\(parameter.defaultValue)\""
}
@ -90,9 +92,13 @@ class AnalyticsDefinition {
for rep in parameter.replaceIn {
switch rep {
case "name": name = name.replacingFirstOccurrence(of: "_\(parameter.name.uppercased())_", with: "\\(\(parameter.name))")
case "path": path = path.replacingFirstOccurrence(of: "_\(parameter.name.uppercased())_", with: "\\(\(parameter.name))")
case "category": category = category.replacingFirstOccurrence(of: "_\(parameter.name.uppercased())_", with: "\\(\(parameter.name))")
case "action": action = action.replacingFirstOccurrence(of: "_\(parameter.name.uppercased())_", with: "\\(\(parameter.name))")
default:
if let param = parameters.first(where: { $0.name == rep }), param.value.isEmpty == false {
param.value = param.value.replacingFirstOccurrence(of: "_\(parameter.name.uppercased())_", with: "\\(\(parameter.name))")
@ -117,8 +123,10 @@ class AnalyticsDefinition {
switch param.type {
case .bool:
params.append("\"\(param.name)\": \(param.value.lowercased())")
case .int, .double:
params.append("\"\(param.name)\": \(param.value)")
case .string:
params.append("\"\(param.name)\": \"\(param.value)\"")
}

View File

@ -8,6 +8,7 @@
import Foundation
enum ParameterType: String {
case string = "String"
case int = "Int"
case double = "Double"

View File

@ -78,18 +78,21 @@ class AnalyticsFileParser {
print(error.description)
Analytics.exit(withError: error)
}
case .bool:
if Bool(value.lowercased()) == nil {
let error = AnalyticsError.invalidParameter("type of \(value) is not \(type)")
print(error.description)
Analytics.exit(withError: error)
}
case .double:
if Double(value) == nil {
let error = AnalyticsError.invalidParameter("type of \(value) is not \(type)")
print(error.description)
Analytics.exit(withError: error)
}
case .string:
break
}
@ -147,7 +150,7 @@ class AnalyticsFileParser {
}
if let parameters {
definition.parameters = AnalyticsFileParser.getParameters(from: parameters)
definition.parameters = Self.getParameters(from: parameters)
}
return definition

View File

@ -21,8 +21,6 @@ struct Colors: ParsableCommand {
// MARK: - Static
static let toolName = "Color"
static let defaultExtensionName = "Color"
static let defaultExtensionNameUIKit = "UIColor"
static let assetsColorsFolderName = "Colors"
// MARK: - Command options
@ -57,22 +55,28 @@ struct Colors: ParsableCommand {
// -> Time: 3.4505380392074585 seconds
// Generate extension
ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath,
isSwiftUI: true
)
if let extensionName = options.extensionName,
let extensionFilePath = options.extensionFilePath {
ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors,
staticVar: options.staticMembers,
extensionName: extensionName,
extensionFilePath: extensionFilePath,
isSwiftUI: true
)
}
// Generate extension
ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors,
staticVar: options.staticMembers,
extensionName: options.extensionNameUIKit,
extensionFilePath: options.extensionFilePathUIKit,
isSwiftUI: false
)
if let extensionNameUIKit = options.extensionNameUIKit,
let extensionFilePathUIKit = options.extensionFilePathUIKit {
ColorExtensionGenerator.writeExtensionFile(
colors: parsedColors,
staticVar: options.staticMembers,
extensionName: extensionNameUIKit,
extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false
)
}
print("[\(Self.toolName)] Colors generated")
}
@ -96,18 +100,40 @@ struct Colors: ParsableCommand {
Self.exit(withError: error)
}
// Extension for UIKit and SwiftUI should have different name
guard options.extensionName != options.extensionNameUIKit else {
let error = ColorsToolError.extensionNamesCollision(options.extensionName)
print(error.description)
Self.exit(withError: error)
// Extension for UIKit and SwiftUI should have different name (if both are defined)
if let extensionName = options.extensionName,
let extensionNameUIKit = options.extensionNameUIKit {
guard extensionName != extensionNameUIKit else {
let error = ColorsToolError.extensionNamesCollision(extensionName)
print(error.description)
Self.exit(withError: error)
}
}
// If an extension need to be generated, ensure extensionOutputPath is defined
if options.extensionName != nil ||
options.extensionNameUIKit != nil {
guard let extensionOutputPath = options.extensionOutputPath,
extensionOutputPath.isEmpty == false else {
let error = ColorsToolError.missingExtensionPath
print(error.description)
Self.exit(withError: error)
}
}
// Check if needed to regenerate
let fileToCompareToInput: String = {
// If there is no extension file to compare
// Then check the xcassets file instead
if let extensionFilePath = options.extensionFilePath {
return extensionFilePath
}
return options.xcassetsPath
}()
guard GeneratorChecker.shouldGenerate(
force: options.forceGeneration,
inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath
extensionFilePath: fileToCompareToInput
) else {
print("[\(Self.toolName)] Colors are already up to date :) ")
return false

View File

@ -17,11 +17,12 @@ enum ColorsToolError: Error {
case fileNotExists(String)
case badColorDefinition(String, String)
case deleteExistingColors(String)
case missingExtensionPath
var description: String {
switch self {
case .extensionNamesCollision(let extensionName):
return "error: [\(Fonts.toolName)] Error on extension names, extension name and SwiftUI extension name should be different (\(extensionName) is used on both)"
return "error: [\(Colors.toolName)] Error on extension names, extension name and SwiftUI extension name should be different (\(extensionName) is used on both)"
case .badFormat(let info):
return "error: [\(Colors.toolName)] Bad line format: \(info). Accepted format are: colorName=\"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\" \"#RGB/#ARGB\""
@ -43,6 +44,9 @@ enum ColorsToolError: Error {
case .deleteExistingColors(let assetsFolder):
return "error: [\(Colors.toolName)] An error occured while deleting colors folder `\(assetsFolder)`"
case .missingExtensionPath:
return "error: [\(Colors.toolName)] Extension need to be generated but no `extensionOutputPath` is provided"
}
}
}

View File

@ -24,17 +24,17 @@ struct ColorsToolOptions: ParsableArguments {
@Option(help: "Path of xcassets where to generate colors", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var xcassetsPath: String
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@Option(help: "Tell if it will generate static properties or not")
var staticMembers: Bool = false
@Option(help: "Extension name. If not specified, it will generate an Color extension.")
var extensionName: String = Colors.defaultExtensionName
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String?
@Option(help: "SwiftUI Extension name. If not specified, it will generate an UIColor extension.")
var extensionNameUIKit: String = Colors.defaultExtensionNameUIKit
@Option(help: "SwiftUI extension name. If not specified, no extension will be generated.")
var extensionName: String?
@Option(help: "UIKit extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String?
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+ColorsMyApp.swift")
var extensionSuffix: String?
@ -46,27 +46,35 @@ extension ColorsToolOptions {
// MARK: - SwiftUI
var extensionFileName: String {
var extensionFileName: String? {
guard let extensionName else { return nil }
if let extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift"
}
return "\(extensionName).swift"
}
var extensionFilePath: String {
"\(extensionOutputPath)/\(extensionFileName)"
var extensionFilePath: String? {
guard let extensionOutputPath, let extensionFileName else { return nil }
return "\(extensionOutputPath)/\(extensionFileName)"
}
// MARK: - UIKit
var extensionFileNameUIKit: String {
var extensionFileNameUIKit: String? {
guard let extensionNameUIKit else { return nil }
if let extensionSuffix {
return "\(extensionNameUIKit)+\(extensionSuffix).swift"
}
return "\(extensionNameUIKit).swift"
}
var extensionFilePathUIKit: String {
"\(extensionOutputPath)/\(extensionFileNameUIKit)"
var extensionFilePathUIKit: String? {
guard let extensionOutputPath, let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
}
}

View File

@ -18,20 +18,20 @@ struct FontsOptions: ParsableArguments {
@Argument(help: "Input files where fonts ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var inputFile: String
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@Option(help: "Tell if it will generate static properties or methods")
var staticMembers: Bool = false
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@Option(help: "Extension name. If not specified, it will generate an Font extension.")
var extensionName: String = Fonts.defaultExtensionName
@Option(help: "Extension name. If not specified, it will generate an UIFont extension.")
var extensionNameUIKit: String = Fonts.defaultExtensionNameUIKit
@Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String?
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+FontsMyApp.swift")
var extensionSuffix: String = ""
var extensionSuffix: String?
@Option(name: .customLong("info-plist-paths"), help: "Info.plist paths (array). Will be used to update UIAppFonts content")
fileprivate var infoPlistPathsRaw: String = ""
@ -44,7 +44,7 @@ extension FontsOptions {
// MARK: - SwiftUI
var extensionFileName: String {
if extensionSuffix.isEmpty == false {
if let extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift"
}
return "\(extensionName).swift"
@ -56,15 +56,19 @@ extension FontsOptions {
// MARK: - UIKit
var extensionFileNameUIKit: String {
if extensionSuffix.isEmpty == false {
var extensionFileNameUIKit: String? {
guard let extensionNameUIKit else { return nil }
if let extensionSuffix {
return "\(extensionNameUIKit)+\(extensionSuffix).swift"
}
return "\(extensionNameUIKit).swift"
}
var extensionFilePathUIKit: String {
"\(extensionOutputPath)/\(extensionFileNameUIKit)"
var extensionFilePathUIKit: String? {
guard let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
}
// MARK: -

View File

@ -22,7 +22,6 @@ struct Fonts: ParsableCommand {
static let toolName = "Fonts"
static let defaultExtensionName = "Font"
static let defaultExtensionNameUIKit = "UIFont"
// MARK: - Command Options
@ -61,16 +60,25 @@ struct Fonts: ParsableCommand {
isSwiftUI: true
)
FontExtensionGenerator.writeExtensionFile(
fontsNames: fontsNames,
staticVar: options.staticMembers,
extensionName: options.extensionNameUIKit,
extensionFilePath: options.extensionFilePathUIKit,
isSwiftUI: false
)
if let extensionNameUIKit = options.extensionNameUIKit,
let extensionFilePathUIKit = options.extensionFilePathUIKit {
FontExtensionGenerator.writeExtensionFile(
fontsNames: fontsNames,
staticVar: options.staticMembers,
extensionName: extensionNameUIKit,
extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false
)
}
print("Info.plist has been updated with:")
print("\(FontPlistGenerator.generatePlistUIAppsFontContent(for: fontsNames, infoPlistPaths: options.infoPlistPaths))")
if options.infoPlistPaths.isEmpty == false {
let plistUpdateFontsData = FontPlistGenerator.generatePlistUIAppsFontContent(
for: fontsNames,
infoPlistPaths: options.infoPlistPaths
)
print("Info.plist has been updated with:")
print(plistUpdateFontsData)
}
print("[\(Self.toolName)] Fonts generated")
}

View File

@ -14,6 +14,7 @@ enum FontsToolError: Error {
case inputFolderNotFound(String)
case fileNotExists(String)
case writeExtension(String, String)
case missingExtensionPath
var description: String {
switch self {
@ -31,6 +32,9 @@ enum FontsToolError: Error {
case let .writeExtension(filename, info):
return "error: [\(Fonts.toolName)] An error occured while writing extension in \(filename): \(info)"
case .missingExtensionPath:
return "error: [\(Fonts.toolName)] Extension need to be generated but no `extensionOutputPath` is provided"
}
}
}

View File

@ -131,7 +131,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
let inputFile: String
let style: String
let xcassetsPath: String
let extensionOutputPath: String
let extensionOutputPath: String?
let extensionName: String?
let extensionNameUIKit: String?
let extensionSuffix: String?
@ -148,7 +148,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
inputFile: String,
style: String,
xcassetsPath: String,
extensionOutputPath: String,
extensionOutputPath: String?,
extensionName: String?,
extensionNameUIKit: String?,
extensionSuffix: String?,
@ -170,7 +170,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
- Input file: \(inputFile)
- Style: \(style)
- Xcassets path: \(xcassetsPath)
- Extension output path: \(extensionOutputPath)
- Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-")
- Extension name UIKit: \(extensionNameUIKit ?? "-")
- Extension suffix: \(extensionSuffix ?? "-")
@ -181,7 +181,7 @@ struct ColorsConfiguration: Codable, CustomDebugStringConvertible {
struct FontsConfiguration: Codable, CustomDebugStringConvertible {
let inputFile: String
let extensionOutputPath: String
let extensionOutputPath: String?
let extensionName: String?
let extensionNameUIKit: String?
let extensionSuffix: String?
@ -197,7 +197,7 @@ struct FontsConfiguration: Codable, CustomDebugStringConvertible {
internal init(
inputFile: String,
extensionOutputPath: String,
extensionOutputPath: String?,
extensionName: String?,
extensionNameUIKit: String?,
extensionSuffix: String?,
@ -217,7 +217,7 @@ struct FontsConfiguration: Codable, CustomDebugStringConvertible {
"""
Fonts configuration:
- Input file: \(inputFile)
- Extension output path: \(extensionOutputPath)
- Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-")
- Extension name UIKit: \(extensionNameUIKit ?? "-")
- Extension suffix: \(extensionSuffix ?? "-")
@ -230,7 +230,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
let inputFile: String
let xcassetsPath: String
let extensionOutputPath: String
let extensionOutputPath: String?
let extensionName: String?
let extensionNameUIKit: String?
let extensionSuffix: String?
@ -246,7 +246,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
internal init(
inputFile: String,
xcassetsPath: String,
extensionOutputPath: String,
extensionOutputPath: String?,
extensionName: String?,
extensionNameUIKit: String?,
extensionSuffix: String?,
@ -266,7 +266,7 @@ struct ImagesConfiguration: Codable, CustomDebugStringConvertible {
Images configuration:
- Input file: \(inputFile)
- Xcassets path: \(xcassetsPath)
- Extension output path: \(extensionOutputPath)
- Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-")
- Extension name UIKit: \(extensionNameUIKit ?? "-")
- Extension suffix: \(extensionSuffix ?? "-")
@ -280,7 +280,7 @@ struct StringsConfiguration: Codable, CustomDebugStringConvertible {
let outputPath: String
let langs: String
let defaultLang: String
let extensionOutputPath: String
let extensionOutputPath: String?
let extensionName: String?
let extensionSuffix: String?
private let staticMembers: Bool?
@ -305,7 +305,7 @@ struct StringsConfiguration: Codable, CustomDebugStringConvertible {
outputPath: String,
langs: String,
defaultLang: String,
extensionOutputPath: String,
extensionOutputPath: String?,
extensionName: String?,
extensionSuffix: String?,
staticMembers: Bool?,
@ -329,7 +329,7 @@ struct StringsConfiguration: Codable, CustomDebugStringConvertible {
- Output path: \(outputPath)
- Langs: \(langs)
- Default lang: \(defaultLang)
- Extension output path: \(extensionOutputPath)
- Extension output path: \(extensionOutputPath ?? "-")
- Extension name: \(extensionName ?? "-")
- Extension suffix: \(extensionSuffix ?? "-")
"""

View File

@ -27,29 +27,23 @@ extension ColorsConfiguration: Runnable {
style,
"--xcassets-path",
xcassetsPath.prependIfRelativePath(projectDirectory),
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members",
"\(staticMembersOptions)"
]
if let extensionName {
args += [
"--extension-name",
extensionName
]
}
if let extensionNameUIKit {
args += [
"--extension-name-ui-kit",
extensionNameUIKit
]
}
if let extensionSuffix {
args += [
"--extension-suffix",
extensionSuffix
]
// Add optional parameters
[
("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
("--extension-name", extensionName),
("--extension-name-ui-kit", extensionNameUIKit),
("--extension-suffix", extensionSuffix)
].forEach { argumentName, argumentValue in
if let argumentValue {
args += [
argumentName,
argumentValue
]
}
}
return args

View File

@ -23,32 +23,26 @@ extension FontsConfiguration: Runnable {
args += [
inputFile.prependIfRelativePath(projectDirectory),
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members",
"\(staticMembersOptions)"
]
if let extensionName {
args += [
"--extension-name",
extensionName
]
}
if let extensionNameUIKit {
args += [
"--extension-name-ui-kit",
extensionNameUIKit
]
}
if let extensionSuffix {
args += [
"--extension-suffix",
extensionSuffix
]
// Add optional parameters
[
("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
("--extension-name", extensionName),
("--extension-name-ui-kit", extensionNameUIKit),
("--extension-suffix", extensionSuffix)
].forEach { argumentName, argumentValue in
if let argumentValue {
args += [
argumentName,
argumentValue
]
}
}
// Add infoPlist paths
if let infoPlistPaths {
let adjustedPlistPaths = infoPlistPaths
.split(separator: " ")

View File

@ -25,31 +25,23 @@ extension ImagesConfiguration: Runnable {
inputFile.prependIfRelativePath(projectDirectory),
"--xcassets-path",
xcassetsPath.prependIfRelativePath(projectDirectory),
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members",
"\(staticMembersOptions)"
]
if let extensionName {
args += [
"--extension-name",
extensionName
]
}
if let extensionNameUIKit {
args += [
"--extension-name-ui-kit",
extensionNameUIKit
]
}
if let extensionSuffix {
args += [
"--extension-suffix",
extensionSuffix
]
// Add optional parameters
[
("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
("--extension-name", extensionName),
("--extension-name-ui-kit", extensionNameUIKit),
("--extension-suffix", extensionSuffix)
].forEach { argumentName, argumentValue in
if let argumentValue {
args += [
argumentName,
argumentValue
]
}
}
return args

View File

@ -24,26 +24,24 @@ extension StringsConfiguration: Runnable {
langs,
"--default-lang",
defaultLang,
"--extension-output-path",
extensionOutputPath.prependIfRelativePath(projectDirectory),
"--static-members",
"\(staticMembersOptions)",
"--xc-strings",
"\(xcStringsOptions)"
]
if let extensionName {
args += [
"--extension-name",
extensionName
]
}
if let extensionSuffix {
args += [
"--extension-suffix",
extensionSuffix
]
// Add optional parameters
[
("--extension-output-path", extensionOutputPath?.prependIfRelativePath(projectDirectory)),
("--extension-name", extensionName),
("--extension-suffix", extensionSuffix)
].forEach { argumentName, argumentValue in
if let argumentValue {
args += [
argumentName,
argumentValue
]
}
}
Stringium.main(args)

View File

@ -21,8 +21,6 @@ struct Images: ParsableCommand {
// MARK: - Static
static let toolName = "Images"
static let defaultExtensionName = "Image"
static let defaultExtensionNameUIKit = "UIImage"
// MARK: - Command Options
@ -55,23 +53,29 @@ struct Images: ParsableCommand {
)
// Generate extension
ImageExtensionGenerator.generateExtensionFile(
images: imagesToGenerate,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath,
isSwiftUI: true
)
if let extensionName = options.extensionName,
let extensionFilePath = options.extensionFilePath {
ImageExtensionGenerator.generateExtensionFile(
images: imagesToGenerate,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: extensionName,
extensionFilePath: extensionFilePath,
isSwiftUI: true
)
}
ImageExtensionGenerator.generateExtensionFile(
images: imagesToGenerate,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: options.extensionNameUIKit,
extensionFilePath: options.extensionFilePathUIKit,
isSwiftUI: false
)
if let extensionNameUIKit = options.extensionNameUIKit,
let extensionFilePathUIKit = options.extensionFilePathUIKit {
ImageExtensionGenerator.generateExtensionFile(
images: imagesToGenerate,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: extensionNameUIKit,
extensionFilePath: extensionFilePathUIKit,
isSwiftUI: false
)
}
print("[\(Self.toolName)] Images generated")
}
@ -96,17 +100,39 @@ struct Images: ParsableCommand {
_ = Self.getSvgConverterPath()
// Extension for UIKit and SwiftUI should have different name
guard options.extensionName != options.extensionNameUIKit else {
let error = ImagesError.extensionNamesCollision(options.extensionName)
print(error.description)
Self.exit(withError: error)
if let extensionName = options.extensionName,
let extensionNameUIKit = options.extensionNameUIKit {
guard extensionName != extensionNameUIKit else {
let error = ImagesError.extensionNamesCollision(extensionName)
print(error.description)
Self.exit(withError: error)
}
}
// If an extension need to be generated, ensure extensionOutputPath is defined
if options.extensionName != nil ||
options.extensionNameUIKit != nil {
guard let extensionOutputPath = options.extensionOutputPath,
extensionOutputPath.isEmpty == false else {
let error = ImagesError.missingExtensionPath
print(error.description)
Self.exit(withError: error)
}
}
// Check if needed to regenerate
let fileToCompareToInput: String = {
// If there is no extension file to compare
// Then check the xcassets file instead
if let extensionFilePath = options.extensionFilePath {
return extensionFilePath
}
return options.xcassetsPath
}()
guard GeneratorChecker.shouldGenerate(
force: options.forceExecution,
inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath
extensionFilePath: fileToCompareToInput
) else {
print("[\(Self.toolName)] Images are already up to date :) ")
return false

View File

@ -18,12 +18,13 @@ enum ImagesError: Error {
case magickConvertNotFound
case writeFile(String, String)
case createAssetFolder(String)
case missingExtensionPath
case unknown(String)
var description: String {
switch self {
case .extensionNamesCollision(let extensionName):
return "error: [\(Fonts.toolName)] Error on extension names, extension name and SwiftUI extension name should be different (\(extensionName) is used on both)"
return "error: [\(Images.toolName)] Error on extension names, extension name and SwiftUI extension name should be different (\(extensionName) is used on both)"
case .inputFolderNotFound(let inputFolder):
return "error: [\(Images.toolName)] Input folder not found: \(inputFolder)"
@ -47,7 +48,10 @@ enum ImagesError: Error {
return "error: [\(Images.toolName)] An error occured while writing content to \(filename): \(subErrorDescription)"
case .createAssetFolder(let folder):
return "error: [\(Colors.toolName)] An error occured while creating folder `\(folder)`"
return "error: [\(Images.toolName)] An error occured while creating folder `\(folder)`"
case .missingExtensionPath:
return "error: [\(Images.toolName)] Extension need to be generated but no `extensionOutputPath` is provided"
case .unknown(let errorDescription):
return "error: [\(Images.toolName)] Unknown error: \(errorDescription)"

View File

@ -24,17 +24,17 @@ struct ImagesOptions: ParsableArguments {
@Option(help: "Xcassets path where to generate images.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var xcassetsPath: String
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@Option(help: "Tell if it will generate static properties or not")
var staticMembers: Bool = false
@Option(help: "Extension name. If not specified, it will generate an Image extension.")
var extensionName: String = Images.defaultExtensionName
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String?
@Option(help: "Extension name. If not specified, it will generate an UIImage extension.")
var extensionNameUIKit: String = Images.defaultExtensionNameUIKit
@Option(help: "SwiftUI extension name. If not specified, no extension will be generated.")
var extensionName: String?
@Option(help: "UIKit extension name. If not specified, no extension will be generated.")
var extensionNameUIKit: String?
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Image{extensionSuffix}.swift")
var extensionSuffix: String?
@ -46,28 +46,36 @@ extension ImagesOptions {
// MARK: - SwiftUI
var extensionFileName: String {
var extensionFileName: String? {
guard let extensionName else { return nil }
if let extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift"
}
return "\(extensionName).swift"
}
var extensionFilePath: String {
"\(extensionOutputPath)/\(extensionFileName)"
var extensionFilePath: String? {
guard let extensionOutputPath, let extensionFileName else { return nil }
return "\(extensionOutputPath)/\(extensionFileName)"
}
// MARK: - UIKit
var extensionFileNameUIKit: String {
var extensionFileNameUIKit: String? {
guard let extensionNameUIKit else { return nil }
if let extensionSuffix {
return "\(extensionNameUIKit)+\(extensionSuffix).swift"
}
return "\(extensionNameUIKit).swift"
}
var extensionFilePathUIKit: String {
"\(extensionOutputPath)/\(extensionFileNameUIKit)"
var extensionFilePathUIKit: String? {
guard let extensionOutputPath, let extensionFileNameUIKit else { return nil }
return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
}
// MARK: -

View File

@ -19,8 +19,7 @@ enum StringsFileGenerator {
langs: [String],
defaultLang: String,
tags: [String],
outputPath: String,
inputFilenameWithoutExt: String
lprojPathFormat: String
) {
var stringsFilesContent = [String: String]()
@ -37,7 +36,7 @@ enum StringsFileGenerator {
langs.forEach { lang in
guard let fileContent = stringsFilesContent[lang] else { return }
let stringsFilePath = "\(outputPath)/\(lang).lproj/\(inputFilenameWithoutExt).strings"
let stringsFilePath = String(format: lprojPathFormat, lang)
let stringsFilePathURL = URL(fileURLWithPath: stringsFilePath)
do {
try fileContent.write(to: stringsFilePathURL, atomically: false, encoding: .utf8)
@ -54,8 +53,7 @@ enum StringsFileGenerator {
langs: [String],
defaultLang: String,
tags: [String],
outputPath: String,
inputFilenameWithoutExt: String
xcStringsFilePath: String
) {
let fileContent: String = Self.generateXcStringsFileContent(
@ -65,12 +63,11 @@ enum StringsFileGenerator {
sections: sections
)
let stringsFilePath = "\(outputPath)/\(inputFilenameWithoutExt).xcstrings"
let stringsFilePathURL = URL(fileURLWithPath: stringsFilePath)
let stringsFilePathURL = URL(fileURLWithPath: xcStringsFilePath)
do {
try fileContent.write(to: stringsFilePathURL, atomically: false, encoding: .utf8)
} catch {
let error = StringiumError.writeFile(error.localizedDescription, stringsFilePath)
let error = StringiumError.writeFile(error.localizedDescription, xcStringsFilePath)
print(error.description)
Stringium.exit(withError: error)
}

View File

@ -21,7 +21,6 @@ struct Stringium: ParsableCommand {
// MARK: - Static
static let toolName = "Stringium"
static let defaultExtensionName = "String"
static let noTranslationTag: String = "notranslation"
// MARK: - Command options
@ -49,8 +48,7 @@ struct Stringium: ParsableCommand {
langs: options.langs,
defaultLang: options.defaultLang,
tags: options.tags,
outputPath: options.stringsFileOutputPath,
inputFilenameWithoutExt: options.inputFilenameWithoutExt
lprojPathFormat: options.lprojPathFormat
)
} else {
StringsFileGenerator.writeXcStringsFiles(
@ -58,22 +56,25 @@ struct Stringium: ParsableCommand {
langs: options.langs,
defaultLang: options.defaultLang,
tags: options.tags,
outputPath: options.stringsFileOutputPath,
inputFilenameWithoutExt: options.inputFilenameWithoutExt
xcStringsFilePath: options.xcStringsFilePath
)
}
// Generate extension
StringsFileGenerator.writeExtensionFiles(
sections: sections,
defaultLang: options.defaultLang,
tags: options.tags,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: options.extensionName,
extensionFilePath: options.extensionFilePath,
extensionSuffix: options.extensionSuffix
)
if let extensionName = options.extensionName,
let extensionFilePath = options.extensionFilePath {
print("Will generate extensions")
StringsFileGenerator.writeExtensionFiles(
sections: sections,
defaultLang: options.defaultLang,
tags: options.tags,
staticVar: options.staticMembers,
inputFilename: options.inputFilenameWithoutExt,
extensionName: extensionName,
extensionFilePath: extensionFilePath,
extensionSuffix: options.extensionSuffix ?? ""
)
}
print("[\(Self.toolName)] Strings generated")
}
@ -104,10 +105,18 @@ struct Stringium: ParsableCommand {
}
// Check if needed to regenerate
let fileToCompareToInput: String = {
// If there is no extension file to compare
// Then check the xcassets file instead
if options.xcStrings {
return options.xcStringsFilePath
}
return String(format: options.lprojPathFormat, options.defaultLang)
}()
guard GeneratorChecker.shouldGenerate(
force: options.forceGeneration,
inputFilePath: options.inputFile,
extensionFilePath: options.extensionFilePath
extensionFilePath: fileToCompareToInput
) else {
print("[\(Self.toolName)] Strings are already up to date :) ")
return false

View File

@ -15,35 +15,51 @@ struct StringiumOptions: ParsableArguments {
@Flag(name: [.customShort("f"), .customShort("F")], help: "Should force generation")
var forceGeneration = false
@Argument(help: "Input files where strings are defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
@Argument(
help: "Input files where strings are defined.",
transform: { $0.replaceTiltWithHomeDirectoryPath() }
)
var inputFile: String
@Option(name: .customLong("output-path"), help: "Path where to strings file.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
@Option(
name: .customLong("output-path"),
help: "Path where to strings file.",
transform: { $0.replaceTiltWithHomeDirectoryPath() }
)
fileprivate var outputPathRaw: String
@Option(name: .customLong("langs"), help: "Langs to generate.")
@Option(
name: .customLong("langs"),
help: "Langs to generate."
)
fileprivate var langsRaw: String
@Option(help: "Default langs.")
var defaultLang: String
@Option(name: .customLong("tags"), help: "Tags to generate.")
@Option(
name: .customLong("tags"),
help: "Tags to generate."
)
fileprivate var tagsRaw: String = "ios iosonly iosOnly notranslation"
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
var extensionOutputPath: String
@Option(help: "Tell if it will generate static properties or not")
@Option(help: "Generate static properties. False by default")
var staticMembers: Bool = false
@Option(help: "Tell if it will generate xcStrings file or not")
var xcStrings: Bool = false
@Option(help: "Tell if it will generate xcStrings file or lproj file. True by default")
var xcStrings: Bool = true
@Option(help: "Extension name. If not specified, it will generate an String extension.")
var extensionName: String = Stringium.defaultExtensionName
@Option(
help: "Path where to generate the extension.",
transform: { $0.replaceTiltWithHomeDirectoryPath() }
)
var extensionOutputPath: String?
@Option(help: "Extension name. If not specified, no extension will be generated.")
var extensionName: String?
@Option(help: "Extension suffix: {extensionName}+{extensionSuffix}.swift")
var extensionSuffix: String
var extensionSuffix: String?
}
// MARK: - Private var getter
@ -75,12 +91,21 @@ extension StringiumOptions {
extension StringiumOptions {
var extensionFileName: String {
"\(extensionName)+\(extensionSuffix).swift"
private var extensionFileName: String? {
if let extensionName {
if let extensionSuffix {
return "\(extensionName)+\(extensionSuffix).swift"
}
return "\(extensionName).swift"
}
return nil
}
var extensionFilePath: String {
"\(extensionOutputPath)/\(extensionFileName)"
var extensionFilePath: String? {
if let extensionOutputPath, let extensionFileName {
return "\(extensionOutputPath)/\(extensionFileName)"
}
return nil
}
var inputFilenameWithoutExt: String {
@ -88,4 +113,12 @@ extension StringiumOptions {
.deletingPathExtension()
.lastPathComponent
}
var xcStringsFilePath: String {
"\(stringsFileOutputPath)/\(inputFilenameWithoutExt).xcstrings"
}
var lprojPathFormat: String {
"\(stringsFileOutputPath)/%@.lproj/\(inputFilenameWithoutExt).strings"
}
}

View File

@ -62,7 +62,8 @@ final class AnalyticsDefinitionTests: XCTestCase {
func logScreenDefinitionName() {
logScreen(
name: "Ecran un",
path: "ecran_un/"
path: "ecran_un/",
params: nil
)
}
"""
@ -84,7 +85,7 @@ final class AnalyticsDefinitionTests: XCTestCase {
name: "Ecran un",
action: "",
category: "",
params: [:]
params: nil
)
}
"""
@ -103,9 +104,10 @@ final class AnalyticsDefinitionTests: XCTestCase {
// Expect
let expectScreen = """
static func logScreenDefinitionName() {
logScreen(
AnalyticsManager.shared.logScreen(
name: "Ecran un",
path: "ecran_un/"
path: "ecran_un/",
params: nil
)
}
"""
@ -123,11 +125,11 @@ final class AnalyticsDefinitionTests: XCTestCase {
// Expect
let expectEvent = """
static func logEventDefinitionName() {
logEvent(
AnalyticsManager.shared.logEvent(
name: "Ecran un",
action: "",
category: "",
params: [:]
params: nil
)
}
"""

View File

@ -32,6 +32,182 @@ final class AnalyticsGeneratorTests: XCTestCase {
return definition
}
private func protocolString() -> String {
"""
// MARK: - Protocol
protocol AnalyticsManagerProtocol {
func logScreen(
name: String,
path: String,
params: [String: Any]?
)
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
)
func setEnable(_ enable: Bool)
}
"""
}
private func firebaseString() -> String {
"""
// MARK: - Firebase
class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Methods
func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
var parameters = [
AnalyticsParameterScreenName: name as NSObject
]
if path.isEmpty == false {
parameters["path"] = path + "/iOS" as NSObject
}
if let supplementaryParameters = params {
for (newKey, newValue) in supplementaryParameters {
if parameters.contains(where: { (key: String, value: NSObject) in
key == newKey
}) {
continue
}
parameters[newKey] = newValue as? NSObject
}
}
Analytics.logEvent(
AnalyticsEventScreenView,
parameters: parameters
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
var parameters: [String:NSObject] = [
AnalyticsParameterItemName: name.replacingOccurrences(of: " ", with: "_") as NSObject
]
if category.isEmpty == false {
parameters["AnalyticsParameterItemCategory"] = category as NSObject
}
if action.isEmpty == false {
parameters["action"] = action as NSObject
}
if let supplementaryParameters = params {
for (newKey, newValue) in supplementaryParameters {
if parameters.contains(where: { (key: String, value: NSObject) in
key == newKey
}) {
continue
}
parameters[newKey] = newValue as? NSObject
}
}
Analytics.logEvent(
AnalyticsEventSelectContent,
parameters: parameters
)
}
func setEnable(_ enable: Bool) {
Analytics.setAnalyticsCollectionEnabled(enable)
}
}
"""
}
private func matomoString() -> String {
"""
// MARK: - Matomo
class MatomoAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Properties
private var tracker: MatomoTracker
// MARK: - Init
init(siteId: String, url: String) {
debugPrint("[Matomo service] Server URL: \\(url)")
debugPrint("[Matomo service] Site ID: \\(siteId)")
tracker = MatomoTracker(
siteId: siteId,
baseURL: URL(string: url)!
)
#if DEBUG
tracker.dispatchInterval = 5
#endif
#if DEBUG
tracker.logger = DefaultLogger(minLevel: .verbose)
#endif
debugPrint("[Matomo service] Configured with content base: \\(tracker.contentBase?.absoluteString ?? "-")")
debugPrint("[Matomo service] Opt out: \\(tracker.isOptedOut)")
}
// MARK: - Methods
func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
guard let trackerUrl = tracker.contentBase?.absoluteString else { return }
let urlString = URL(string: "\\(trackerUrl)" + "/" + "\\(path)" + "iOS")
tracker.track(
view: [name],
url: urlString
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
tracker.track(
eventWithCategory: category,
action: action,
name: name,
number: nil,
url: nil
)
}
func setEnable(_ enable: Bool) {
tracker.isOptedOut = !enable
}
}
"""
}
func testGeneratedExtensionContentFirebase() {
// Given
let sectionOne = AnalyticsCategory(id: "section_one")
@ -65,64 +241,18 @@ final class AnalyticsGeneratorTests: XCTestCase {
let expect = """
// Generated by ResgenSwift.Analytics \(ResgenSwiftVersion)
import Foundation
import FirebaseAnalytics
// MARK: - Protocol
\(protocolString())
protocol AnalyticsManagerProtocol {
\(firebaseString())
func logScreen(name: String, path: String)
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
)
}
// MARK: - Traker Type
// MARK: - Firebase
enum TrackerType: CaseIterable {
class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
func logScreen(name: String, path: String) {
var parameters = [
AnalyticsParameterScreenName: name as NSObject
]
Analytics.logEvent(
AnalyticsEventScreenView,
parameters: parameters
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
var parameters: [String:NSObject] = [
"action": action as NSObject,
"category": category as NSObject,
]
if let supplementaryParameters = params {
for (newKey, newValue) in supplementaryParameters {
if parameters.contains(where: { (key: String, value: NSObject) in
key == newKey
}) {
continue
}
parameters[newKey] = newValue as? NSObject
}
}
Analytics.logEvent(
name.replacingOccurrences(of: [" "], with: "_"),
parameters: parameters
)
}
case firebase
}
// MARK: - Manager
@ -131,27 +261,59 @@ final class AnalyticsGeneratorTests: XCTestCase {
static var shared = AnalyticsManager()
private init() {}
// MARK: - Properties
var managers: [AnalyticsManagerProtocol] = []
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
private var isEnabled: Bool = true
private var isEnabled: Bool {
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
false
} else {
true
}
}
// MARK: - Methods
// MARK: - Enable Methods
func setAnalyticsEnabled(_ enable: Bool) {
isEnabled = enable
private func setAnalytics(enable: Bool, _ analytics: [TrackerType]) {
managers.forEach { (key, value) in
if analytics.contains(where: { type in
type == key
}) {
value.setEnable(enable)
}
}
}
func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: true, analytics)
}
func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: false, analytics)
}
func configure() {
managers.append(FirebaseAnalyticsManager())
managers[TrackerType.firebase] = FirebaseAnalyticsManager()
}
private func logScreen(name: String, path: String) {
// MARK: - Private Log Methods
private func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
guard isEnabled else { return }
managers.forEach { manager in
manager.logScreen(name: name, path: path)
managers.values.forEach { manager in
manager.logScreen(
name: name,
path: path,
params: params
)
}
}
@ -163,7 +325,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
) {
guard isEnabled else { return }
managers.forEach { manager in
managers.values.forEach { manager in
manager.logEvent(
name: name,
action: action,
@ -178,7 +340,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS1DefOne() {
logScreen(
name: "s1 def one",
path: ""
path: "",
params: nil
)
}
@ -187,7 +350,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
name: "s1 def two",
action: "",
category: "",
params: [:]
params: nil
)
}
@ -196,7 +359,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS2DefOne() {
logScreen(
name: "s2 def one",
path: ""
path: "",
params: nil
)
}
}
@ -241,80 +405,18 @@ final class AnalyticsGeneratorTests: XCTestCase {
let expect = """
// Generated by ResgenSwift.Analytics \(ResgenSwiftVersion)
import Foundation
import MatomoTracker
// MARK: - Protocol
\(protocolString())
protocol AnalyticsManagerProtocol {
\(matomoString())
func logScreen(name: String, path: String)
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
)
}
// MARK: - Traker Type
// MARK: - Matomo
enum TrackerType: CaseIterable {
class MatomoAnalyticsManager: AnalyticsManagerProtocol {
// MARK: - Properties
private var tracker: MatomoTracker
// MARK: - Init
init(siteId: String, url: String) {
debugPrint("[Matomo service] Server URL: \\(url)")
debugPrint("[Matomo service] Site ID: \\(siteId)")
tracker = MatomoTracker(
siteId: siteId,
baseURL: URL(string: url)!
)
#if DEBUG
tracker.dispatchInterval = 5
#endif
#if DEBUG
tracker.logger = DefaultLogger(minLevel: .verbose)
#endif
debugPrint("[Matomo service] Configured with content base: \\(tracker.contentBase?.absoluteString ?? "-")")
debugPrint("[Matomo service] Opt out: \\(tracker.isOptedOut)")
}
// MARK: - Methods
func logScreen(name: String, path: String) {
guard !tracker.isOptedOut else { return }
guard let trackerUrl = tracker.contentBase?.absoluteString else { return }
let urlString = URL(string: "\\(trackerUrl)" + "/" + "\\(path)" + "iOS")
tracker.track(
view: [name],
url: urlString
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
guard !tracker.isOptedOut else { return }
tracker.track(
eventWithCategory: category,
action: action,
name: name,
number: nil,
url: nil
)
}
case matomo
}
// MARK: - Manager
@ -323,32 +425,62 @@ final class AnalyticsGeneratorTests: XCTestCase {
static var shared = AnalyticsManager()
private init() {}
// MARK: - Properties
var managers: [AnalyticsManagerProtocol] = []
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
private var isEnabled: Bool = true
private var isEnabled: Bool {
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
false
} else {
true
}
}
// MARK: - Methods
// MARK: - Enable Methods
func setAnalyticsEnabled(_ enable: Bool) {
isEnabled = enable
private func setAnalytics(enable: Bool, _ analytics: [TrackerType]) {
managers.forEach { (key, value) in
if analytics.contains(where: { type in
type == key
}) {
value.setEnable(enable)
}
}
}
func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: true, analytics)
}
func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: false, analytics)
}
func configure(siteId: String, url: String) {
managers.append(
MatomoAnalyticsManager(
siteId: siteId,
url: url
)
managers[TrackerType.matomo] = MatomoAnalyticsManager(
siteId: siteId,
url: url
)
}
private func logScreen(name: String, path: String) {
// MARK: - Private Log Methods
private func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
guard isEnabled else { return }
managers.forEach { manager in
manager.logScreen(name: name, path: path)
managers.values.forEach { manager in
manager.logScreen(
name: name,
path: path,
params: params
)
}
}
@ -360,7 +492,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
) {
guard isEnabled else { return }
managers.forEach { manager in
managers.values.forEach { manager in
manager.logEvent(
name: name,
action: action,
@ -375,7 +507,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS1DefOne() {
logScreen(
name: "s1 def one",
path: "s1_def_one/"
path: "s1_def_one/",
params: nil
)
}
@ -384,7 +517,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
name: "s1 def two",
action: "test",
category: "test",
params: [:]
params: nil
)
}
@ -393,7 +526,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS2DefOne() {
logScreen(
name: "s2 def one",
path: "s2_def_one/"
path: "s2_def_one/",
params: nil
)
}
}
@ -439,125 +573,22 @@ final class AnalyticsGeneratorTests: XCTestCase {
let expect = """
// Generated by ResgenSwift.Analytics \(ResgenSwiftVersion)
import Foundation
import MatomoTracker
import FirebaseAnalytics
// MARK: - Protocol
\(protocolString())
protocol AnalyticsManagerProtocol {
\(matomoString())
func logScreen(name: String, path: String)
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
)
}
\(firebaseString())
// MARK: - Matomo
// MARK: - Traker Type
class MatomoAnalyticsManager: AnalyticsManagerProtocol {
enum TrackerType: CaseIterable {
// MARK: - Properties
private var tracker: MatomoTracker
// MARK: - Init
init(siteId: String, url: String) {
debugPrint("[Matomo service] Server URL: \\(url)")
debugPrint("[Matomo service] Site ID: \\(siteId)")
tracker = MatomoTracker(
siteId: siteId,
baseURL: URL(string: url)!
)
#if DEBUG
tracker.dispatchInterval = 5
#endif
#if DEBUG
tracker.logger = DefaultLogger(minLevel: .verbose)
#endif
debugPrint("[Matomo service] Configured with content base: \\(tracker.contentBase?.absoluteString ?? "-")")
debugPrint("[Matomo service] Opt out: \\(tracker.isOptedOut)")
}
// MARK: - Methods
func logScreen(name: String, path: String) {
guard !tracker.isOptedOut else { return }
guard let trackerUrl = tracker.contentBase?.absoluteString else { return }
let urlString = URL(string: "\\(trackerUrl)" + "/" + "\\(path)" + "iOS")
tracker.track(
view: [name],
url: urlString
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
guard !tracker.isOptedOut else { return }
tracker.track(
eventWithCategory: category,
action: action,
name: name,
number: nil,
url: nil
)
}
}
// MARK: - Firebase
class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
func logScreen(name: String, path: String) {
var parameters = [
AnalyticsParameterScreenName: name as NSObject
]
Analytics.logEvent(
AnalyticsEventScreenView,
parameters: parameters
)
}
func logEvent(
name: String,
action: String,
category: String,
params: [String: Any]?
) {
var parameters: [String:NSObject] = [
"action": action as NSObject,
"category": category as NSObject,
]
if let supplementaryParameters = params {
for (newKey, newValue) in supplementaryParameters {
if parameters.contains(where: { (key: String, value: NSObject) in
key == newKey
}) {
continue
}
parameters[newKey] = newValue as? NSObject
}
}
Analytics.logEvent(
name.replacingOccurrences(of: [" "], with: "_"),
parameters: parameters
)
}
case matomo
case firebase
}
// MARK: - Manager
@ -566,33 +597,63 @@ final class AnalyticsGeneratorTests: XCTestCase {
static var shared = AnalyticsManager()
private init() {}
// MARK: - Properties
var managers: [AnalyticsManagerProtocol] = []
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
private var isEnabled: Bool = true
private var isEnabled: Bool {
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
false
} else {
true
}
}
// MARK: - Methods
// MARK: - Enable Methods
func setAnalyticsEnabled(_ enable: Bool) {
isEnabled = enable
private func setAnalytics(enable: Bool, _ analytics: [TrackerType]) {
managers.forEach { (key, value) in
if analytics.contains(where: { type in
type == key
}) {
value.setEnable(enable)
}
}
}
func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: true, analytics)
}
func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
setAnalytics(enable: false, analytics)
}
func configure(siteId: String, url: String) {
managers.append(
MatomoAnalyticsManager(
siteId: siteId,
url: url
)
managers[TrackerType.matomo] = MatomoAnalyticsManager(
siteId: siteId,
url: url
)
managers.append(FirebaseAnalyticsManager())
managers[TrackerType.firebase] = FirebaseAnalyticsManager()
}
private func logScreen(name: String, path: String) {
// MARK: - Private Log Methods
private func logScreen(
name: String,
path: String,
params: [String: Any]?
) {
guard isEnabled else { return }
managers.forEach { manager in
manager.logScreen(name: name, path: path)
managers.values.forEach { manager in
manager.logScreen(
name: name,
path: path,
params: params
)
}
}
@ -604,7 +665,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
) {
guard isEnabled else { return }
managers.forEach { manager in
managers.values.forEach { manager in
manager.logEvent(
name: name,
action: action,
@ -619,7 +680,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS1DefOne() {
logScreen(
name: "s1 def one",
path: "s1_def_one/"
path: "s1_def_one/",
params: nil
)
}
@ -628,7 +690,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
name: "s1 def two",
action: "test",
category: "test",
params: [:]
params: nil
)
}
@ -637,7 +699,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
func logScreenS2DefOne() {
logScreen(
name: "s2 def one",
path: "s2_def_one/"
path: "s2_def_one/",
params: nil
)
}
}

View File

@ -19,7 +19,7 @@ final class ColorsConfigurationTests: XCTestCase {
let testingConfiguration = ColorsConfiguration(inputFile: "path/to/colors.txt",
style: ColorStyle.all.rawValue,
xcassetsPath: "path/to/assets.xcassets",
extensionOutputPath: "Colors/Generated",
extensionOutputPath: nil,
extensionName: nil,
extensionNameUIKit: nil,
extensionSuffix: nil,
@ -34,8 +34,6 @@ final class ColorsConfigurationTests: XCTestCase {
"all",
"--xcassets-path",
"projectDirectory/path/to/assets.xcassets",
"--extension-output-path",
"projectDirectory/Colors/Generated",
"--static-members",
"false"
]
@ -64,10 +62,10 @@ final class ColorsConfigurationTests: XCTestCase {
"all",
"--xcassets-path",
"projectDirectory/path/to/assets.xcassets",
"--extension-output-path",
"projectDirectory/Colors/Generated",
"--static-members",
"false",
"--extension-output-path",
"projectDirectory/Colors/Generated",
"--extension-name",
"AppUIColor",
"--extension-name-ui-kit",

View File

@ -17,7 +17,7 @@ final class FontsConfigurationTests: XCTestCase {
func test_argsGeneration_requiredArgs() {
// Given
let testingConfiguration = FontsConfiguration(inputFile: "path/to/fonts.txt",
extensionOutputPath: "Fonts/Generated",
extensionOutputPath: nil,
extensionName: nil,
extensionNameUIKit: nil,
extensionSuffix: nil,
@ -29,8 +29,6 @@ final class FontsConfigurationTests: XCTestCase {
// Expect
let expectedArguments = [
"projectDirectory/path/to/fonts.txt",
"--extension-output-path",
"projectDirectory/Fonts/Generated",
"--static-members",
"false"
]
@ -54,10 +52,10 @@ final class FontsConfigurationTests: XCTestCase {
let expectedArguments = [
"-f",
"projectDirectory/path/to/fonts.txt",
"--extension-output-path",
"projectDirectory/Fonts/Generated",
"--static-members",
"true",
"--extension-output-path",
"projectDirectory/Fonts/Generated",
"--extension-name",
"AppUIFont",
"--extension-name-ui-kit",

View File

@ -18,7 +18,7 @@ final class ImagesConfigurationTests: XCTestCase {
// Given
let testingConfiguration = ImagesConfiguration(inputFile: "path/to/images.txt",
xcassetsPath: "path/to/assets.xcassets",
extensionOutputPath: "Images/Generated",
extensionOutputPath: nil,
extensionName: nil,
extensionNameUIKit: nil,
extensionSuffix: nil,
@ -32,8 +32,6 @@ final class ImagesConfigurationTests: XCTestCase {
"projectDirectory/path/to/images.txt",
"--xcassets-path",
"projectDirectory/path/to/assets.xcassets",
"--extension-output-path",
"projectDirectory/Images/Generated",
"--static-members",
"false"
]
@ -60,10 +58,10 @@ final class ImagesConfigurationTests: XCTestCase {
"projectDirectory/path/to/images.txt",
"--xcassets-path",
"projectDirectory/path/to/assets.xcassets",
"--extension-output-path",
"projectDirectory/Images/Generated",
"--static-members",
"true",
"--extension-output-path",
"projectDirectory/Images/Generated",
"--extension-name",
"AppUIImage",
"--extension-name-ui-kit",