Add visibility parameters to control scope of generated extension
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
This commit is contained in:
@ -50,8 +50,8 @@ struct Analytics: ParsableCommand {
|
||||
target: options.target,
|
||||
tags: ["ios", "iosonly"],
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: options.extensionName,
|
||||
extensionFilePath: options.extensionFilePath
|
||||
outputFile: options.outputFile,
|
||||
visibility: options.extensionVisibility
|
||||
)
|
||||
|
||||
print("[\(Self.toolName)] Analytics generated")
|
||||
@ -79,7 +79,7 @@ struct Analytics: ParsableCommand {
|
||||
guard GeneratorChecker.shouldGenerate(
|
||||
force: options.forceGeneration,
|
||||
inputFilePath: options.inputFile,
|
||||
extensionFilePath: options.extensionFilePath
|
||||
extensionFilePath: options.outputFile
|
||||
) else {
|
||||
print("[\(Self.toolName)] Analytics are already up to date :) ")
|
||||
return false
|
||||
|
@ -7,45 +7,43 @@
|
||||
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
import ToolCore
|
||||
|
||||
// swiftlint:disable no_grouping_extension
|
||||
|
||||
struct AnalyticsOptions: ParsableArguments {
|
||||
|
||||
@Flag(name: [.customShort("f"), .customShort("F")], help: "Should force generation")
|
||||
@Flag(
|
||||
name: [.customShort("f"), .customShort("F")],
|
||||
help: "Should force generation"
|
||||
)
|
||||
var forceGeneration = false
|
||||
|
||||
@Argument(help: "Input files where tags ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
@Argument(
|
||||
help: "Input files where tags ared defined.",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
)
|
||||
var inputFile: String
|
||||
|
||||
@Option(help: "Target(s) analytics to generate. (\"matomo\" | \"firebase\")")
|
||||
@Option(
|
||||
help: "Target(s) analytics to generate. (\"matomo\" | \"firebase\")",
|
||||
completion: .list(["matotmo", "firebase"])
|
||||
)
|
||||
var target: String
|
||||
|
||||
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
var extensionOutputPath: String
|
||||
@Option(
|
||||
help: "Where to generate the analytics manager (path with filename)",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
)
|
||||
var outputFile: 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 a Analytics extension.")
|
||||
var extensionName: String = Analytics.defaultExtensionName
|
||||
|
||||
@Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+Analytics{extensionSuffix}.swift")
|
||||
var extensionSuffix: String?
|
||||
}
|
||||
|
||||
// MARK: - Computed var
|
||||
|
||||
extension AnalyticsOptions {
|
||||
|
||||
var extensionFileName: String {
|
||||
if let extensionSuffix {
|
||||
return "\(extensionName)+\(extensionSuffix).swift"
|
||||
}
|
||||
return "\(extensionName).swift"
|
||||
}
|
||||
|
||||
var extensionFilePath: String {
|
||||
"\(extensionOutputPath)/\(extensionFileName)"
|
||||
}
|
||||
@Option(
|
||||
name: .customLong("visibility"),
|
||||
help: "Visibility of extension and properties. Possibles values: public, private, package, internal. Default is internal",
|
||||
completion: .list(["public", "private", "package", "internal"])
|
||||
)
|
||||
var extensionVisibility: ExtensionVisibility = .internal
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ enum AnalyticsGenerator {
|
||||
target: String,
|
||||
tags: [String],
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
extensionFilePath: String
|
||||
outputFile: String,
|
||||
visibility: ExtensionVisibility
|
||||
) {
|
||||
// Get target type from enum
|
||||
let targetsString: [String] = target.components(separatedBy: " ")
|
||||
@ -44,15 +44,15 @@ enum AnalyticsGenerator {
|
||||
sections: sections,
|
||||
tags: tags,
|
||||
staticVar: staticVar,
|
||||
extensionName: extensionName
|
||||
visibility: visibility
|
||||
)
|
||||
|
||||
// Write content
|
||||
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
||||
let outputFilePathURL = URL(fileURLWithPath: outputFile)
|
||||
do {
|
||||
try extensionFileContent.write(to: extensionFilePathURL, atomically: false, encoding: .utf8)
|
||||
try extensionFileContent.write(to: outputFilePathURL, atomically: false, encoding: .utf8)
|
||||
} catch {
|
||||
let error = AnalyticsError.writeFile(extensionFilePath, error.localizedDescription)
|
||||
let error = AnalyticsError.writeFile(outputFile, error.localizedDescription)
|
||||
print(error.description)
|
||||
Analytics.exit(withError: error)
|
||||
}
|
||||
@ -65,18 +65,19 @@ enum AnalyticsGenerator {
|
||||
sections: [AnalyticsCategory],
|
||||
tags: [String],
|
||||
staticVar: Bool,
|
||||
extensionName: String
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
[
|
||||
getHeader(
|
||||
targets: targets,
|
||||
extensionClassname: extensionName,
|
||||
staticVar: staticVar
|
||||
staticVar: staticVar,
|
||||
visibility: visibility
|
||||
),
|
||||
getProperties(
|
||||
sections: sections,
|
||||
tags: tags,
|
||||
staticVar: staticVar
|
||||
staticVar: staticVar,
|
||||
visibility: visibility
|
||||
),
|
||||
getFooter()
|
||||
]
|
||||
@ -87,23 +88,23 @@ enum AnalyticsGenerator {
|
||||
|
||||
private static func getHeader(
|
||||
targets: [TrackerType],
|
||||
extensionClassname: String,
|
||||
staticVar: Bool
|
||||
staticVar: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
"""
|
||||
// Generated by ResgenSwift.\(Analytics.toolName) \(ResgenSwiftVersion)
|
||||
|
||||
\(Self.getImport(targets: targets))
|
||||
|
||||
\(Self.getAnalyticsProtocol(targets: targets))
|
||||
\(Self.getAnalyticsProtocol(targets: targets, visibility: visibility))
|
||||
|
||||
\(Self.getTrackerTypeEnum(targets: targets))
|
||||
\(Self.getTrackerTypeEnum(targets: targets, visibility: visibility))
|
||||
|
||||
// MARK: - Manager
|
||||
|
||||
class AnalyticsManager {
|
||||
\(visibility) class AnalyticsManager {
|
||||
|
||||
static var shared = AnalyticsManager()
|
||||
\(visibility) static var shared = AnalyticsManager()
|
||||
|
||||
private init() {}
|
||||
|
||||
@ -111,15 +112,18 @@ enum AnalyticsGenerator {
|
||||
|
||||
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
|
||||
|
||||
\(Self.getEnabledContent())
|
||||
\(Self.getEnabledContent(visibility: visibility))
|
||||
|
||||
\(Self.getAnalyticsProperties(targets: targets))
|
||||
\(Self.getAnalyticsProperties(targets: targets, visibility: visibility))
|
||||
|
||||
\(Self.getPrivateLogFunction())
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getTrackerTypeEnum(targets: [TrackerType]) -> String {
|
||||
private static func getTrackerTypeEnum(
|
||||
targets: [TrackerType],
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
var result: [String] = []
|
||||
targets.forEach { type in
|
||||
result.append(" case \(type)")
|
||||
@ -128,14 +132,16 @@ enum AnalyticsGenerator {
|
||||
return """
|
||||
// MARK: - Traker Type
|
||||
|
||||
enum TrackerType: CaseIterable {
|
||||
\(visibility) enum TrackerType: CaseIterable {
|
||||
|
||||
\(result.joined(separator: "\n"))
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getEnabledContent() -> String {
|
||||
private static func getEnabledContent(
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
"""
|
||||
private var isEnabled: Bool {
|
||||
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
|
||||
@ -157,11 +163,11 @@ enum AnalyticsGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
\(visibility) func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: true, analytics)
|
||||
}
|
||||
|
||||
func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
\(visibility) func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: false, analytics)
|
||||
}
|
||||
"""
|
||||
@ -223,15 +229,18 @@ enum AnalyticsGenerator {
|
||||
"""
|
||||
}
|
||||
|
||||
private static func getAnalyticsProperties(targets: [TrackerType]) -> String {
|
||||
private static func getAnalyticsProperties(
|
||||
targets: [TrackerType],
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
var header = ""
|
||||
var content: [String] = []
|
||||
let footer = " }"
|
||||
|
||||
if targets.contains(TrackerType.matomo) {
|
||||
header = "func configure(siteId: String, url: String) {"
|
||||
header = "\(visibility) func configure(siteId: String, url: String) {"
|
||||
} else if targets.contains(TrackerType.firebase) {
|
||||
header = "func configure() {"
|
||||
header = "\(visibility) func configure() {"
|
||||
}
|
||||
|
||||
if targets.contains(TrackerType.matomo) {
|
||||
@ -255,11 +264,14 @@ enum AnalyticsGenerator {
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
||||
private static func getAnalyticsProtocol(targets: [TrackerType]) -> String {
|
||||
private static func getAnalyticsProtocol(
|
||||
targets: [TrackerType],
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
let proto = """
|
||||
// MARK: - Protocol
|
||||
|
||||
protocol AnalyticsManagerProtocol {
|
||||
\(visibility) protocol AnalyticsManagerProtocol {
|
||||
|
||||
func logScreen(
|
||||
name: String,
|
||||
@ -294,7 +306,8 @@ enum AnalyticsGenerator {
|
||||
private static func getProperties(
|
||||
sections: [AnalyticsCategory],
|
||||
tags: [String],
|
||||
staticVar: Bool
|
||||
staticVar: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
sections
|
||||
.compactMap { section in
|
||||
@ -310,9 +323,9 @@ enum AnalyticsGenerator {
|
||||
}
|
||||
|
||||
if staticVar {
|
||||
res += "\n\n\(definition.getStaticProperty())"
|
||||
res += "\n\n\(definition.getStaticProperty(visibility: visibility))"
|
||||
} else {
|
||||
res += "\n\n\(definition.getProperty())"
|
||||
res += "\n\n\(definition.getProperty(visibility: visibility))"
|
||||
}
|
||||
}
|
||||
return res
|
||||
|
@ -169,19 +169,19 @@ class AnalyticsDefinition {
|
||||
|
||||
// MARK: - Raw strings
|
||||
|
||||
func getProperty() -> String {
|
||||
func getProperty(visibility: ExtensionVisibility) -> String {
|
||||
replaceIn()
|
||||
return """
|
||||
func \(getFuncName())\(getParameters()) {
|
||||
\(visibility) func \(getFuncName())\(getParameters()) {
|
||||
\(getlogFunction())
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
func getStaticProperty() -> String {
|
||||
func getStaticProperty(visibility: ExtensionVisibility) -> String {
|
||||
replaceIn()
|
||||
return """
|
||||
static func \(getFuncName())\(getParameters()) {
|
||||
\(visibility) static func \(getFuncName())\(getParameters()) {
|
||||
AnalyticsManager.shared.\(getlogFunction())
|
||||
}
|
||||
"""
|
||||
|
Reference in New Issue
Block a user