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:
@ -62,7 +62,8 @@ struct Colors: ParsableCommand {
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: extensionName,
|
||||
extensionFilePath: extensionFilePath,
|
||||
isSwiftUI: true
|
||||
isSwiftUI: true,
|
||||
visibility: options.extensionVisibility
|
||||
)
|
||||
}
|
||||
|
||||
@ -74,7 +75,8 @@ struct Colors: ParsableCommand {
|
||||
staticVar: options.staticMembers,
|
||||
extensionName: extensionNameUIKit,
|
||||
extensionFilePath: extensionFilePathUIKit,
|
||||
isSwiftUI: false
|
||||
isSwiftUI: false,
|
||||
visibility: options.extensionVisibility
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -7,27 +7,47 @@
|
||||
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
import ToolCore
|
||||
|
||||
// swiftlint:disable no_grouping_extension
|
||||
|
||||
struct ColorsToolOptions: 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 colors ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
@Argument(
|
||||
help: "Input files where colors ared defined.",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
)
|
||||
var inputFile: String
|
||||
|
||||
@Option(help: "Color style to generate: light for light colors only, or all for dark and light colors")
|
||||
var style: ColorStyle
|
||||
|
||||
@Option(help: "Path of xcassets where to generate colors", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
@Option(
|
||||
help: "Path of xcassets where to generate colors",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
)
|
||||
var xcassetsPath: String
|
||||
|
||||
@Option(help: "Tell if it will generate static properties or not")
|
||||
var staticMembers: Bool = false
|
||||
|
||||
@Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
|
||||
@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
|
||||
|
||||
@Option(
|
||||
help: "Path where to generate the extension.",
|
||||
transform: { $0.replaceTiltWithHomeDirectoryPath() }
|
||||
)
|
||||
var extensionOutputPath: String?
|
||||
|
||||
@Option(help: "SwiftUI extension name. If not specified, no extension will be generated.")
|
||||
|
@ -20,14 +20,16 @@ struct ColorExtensionGenerator {
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
extensionFilePath: String,
|
||||
isSwiftUI: Bool
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) {
|
||||
// Create extension content
|
||||
let extensionContent = Self.getExtensionContent(
|
||||
colors: colors,
|
||||
staticVar: staticVar,
|
||||
extensionName: extensionName,
|
||||
isSwiftUI: isSwiftUI
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
)
|
||||
|
||||
// Write content
|
||||
@ -45,11 +47,20 @@ struct ColorExtensionGenerator {
|
||||
colors: [ParsedColor],
|
||||
staticVar: Bool,
|
||||
extensionName: String,
|
||||
isSwiftUI: Bool
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
[
|
||||
Self.getHeader(extensionClassname: extensionName, isSwiftUI: isSwiftUI),
|
||||
Self.getProperties(for: colors, withStaticVar: staticVar, isSwiftUI: isSwiftUI),
|
||||
Self.getHeader(
|
||||
extensionClassname: extensionName,
|
||||
isSwiftUI: isSwiftUI
|
||||
),
|
||||
Self.getProperties(
|
||||
for: colors,
|
||||
withStaticVar: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
),
|
||||
Self.getFooter()
|
||||
]
|
||||
.joined(separator: "\n")
|
||||
@ -75,10 +86,15 @@ struct ColorExtensionGenerator {
|
||||
private static func getProperties(
|
||||
for colors: [ParsedColor],
|
||||
withStaticVar staticVar: Bool,
|
||||
isSwiftUI: Bool
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
colors.map {
|
||||
$0.getColorProperty(isStatic: staticVar, isSwiftUI: isSwiftUI)
|
||||
$0.getColorProperty(
|
||||
isStatic: staticVar,
|
||||
isSwiftUI: isSwiftUI,
|
||||
visibility: visibility
|
||||
)
|
||||
}
|
||||
.joined(separator: "\n\n")
|
||||
}
|
||||
|
@ -6,14 +6,19 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ToolCore
|
||||
|
||||
struct ParsedColor {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
let name: String
|
||||
let light: String
|
||||
let dark: String
|
||||
|
||||
// Generate Contents.json content
|
||||
// MARK: - Contents.json
|
||||
|
||||
/// Generate Contents.json content
|
||||
func contentsJSON() -> String {
|
||||
let lightARGB = light.colorComponent()
|
||||
let darkARGB = dark.colorComponent()
|
||||
@ -73,20 +78,24 @@ struct ParsedColor {
|
||||
"""
|
||||
}
|
||||
|
||||
// MARK: - UIKit
|
||||
// MARK: - Property
|
||||
|
||||
func getColorProperty(isStatic: Bool, isSwiftUI: Bool) -> String {
|
||||
func getColorProperty(
|
||||
isStatic: Bool,
|
||||
isSwiftUI: Bool,
|
||||
visibility: ExtensionVisibility
|
||||
) -> String {
|
||||
if isSwiftUI {
|
||||
return """
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
\(isStatic ? "static " : "")var \(name): Color {
|
||||
\(visibility) \(isStatic ? "static " : "")var \(name): Color {
|
||||
Color("\(name)")
|
||||
}
|
||||
"""
|
||||
}
|
||||
return """
|
||||
/// Color \(name) is \(light) (light) or \(dark) (dark)"
|
||||
\(isStatic ? "static " : "@objc ")var \(name): UIColor {
|
||||
\(isStatic ? "" : "@objc ")\(visibility) \(isStatic ? "static " : "")var \(name): UIColor {
|
||||
UIColor(named: "\(name)")!
|
||||
}
|
||||
"""
|
||||
|
Reference in New Issue
Block a user