Skip to content

Commit

Permalink
Cleanup Color generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlokhorst committed Nov 8, 2020
1 parent 2388051 commit 4076c62
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions Sources/RswiftCore/Generators/ColorStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,6 @@

import Foundation

fileprivate func colorFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
availables: ["tvOS 11.0, *", "iOS 11.0, *"],
comments: ["`UIColor(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: structName,
generics: nil,
parameters: [
Function.Parameter(
name: "compatibleWith",
localName: "traitCollection",
type: Type._UITraitCollection.asOptional(),
defaultValue: "nil"
)
],
doesThrow: false,
returnType: Type._UIColor.asOptional(),
body: "return UIKit.UIColor(resource: \(qualifiedName), compatibleWith: traitCollection)",
os: ["iOS", "tvOS"]
)
}

fileprivate func watchOSColorFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
availables: ["watchOSApplicationExtension 4.0, *"],
comments: ["`UIColor(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: structName,
generics: nil,
parameters: [
Function.Parameter(name: "_", type: Type._Void, defaultValue: "()")
],
doesThrow: false,
returnType: Type._UIColor.asOptional(),
body: "return UIKit.UIColor(named: \(qualifiedName).name)",
os: ["watchOS"]
)
}

struct ColorStructGenerator: ExternalOnlyStructGenerator {
private let assetFolders: [AssetFolder]

Expand Down Expand Up @@ -104,8 +57,8 @@ struct ColorStructGenerator: ExternalOnlyStructGenerator {
implements: [],
typealiasses: [],
properties: colorLets,
functions: groupedColors.uniques.map { [ colorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName),
watchOSColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName)] }.flatMap { $0 },
functions: groupedColors.uniques.map { [ generateColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName),
generateWatchOSColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName)] }.flatMap { $0 },
structs: structs,
classes: [],
os: []
Expand Down Expand Up @@ -157,11 +110,58 @@ private extension NamespacedAssetSubfolder {
implements: [],
typealiasses: [],
properties: colorLets,
functions: groupedFunctions.uniques.map { [ colorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName),
watchOSColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName)] }.flatMap { $0 },
functions: groupedFunctions.uniques.map { [ generateColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName),
generateWatchOSColorFunction(for: $0, at: externalAccessLevel, prefix: qualifiedName)] }.flatMap { $0 },
structs: structs,
classes: [],
os: []
)
}
}

private func generateColorFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
availables: ["tvOS 11.0, *", "iOS 11.0, *"],
comments: ["`UIColor(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: structName,
generics: nil,
parameters: [
Function.Parameter(
name: "compatibleWith",
localName: "traitCollection",
type: Type._UITraitCollection.asOptional(),
defaultValue: "nil"
)
],
doesThrow: false,
returnType: Type._UIColor.asOptional(),
body: "return UIKit.UIColor(resource: \(qualifiedName), compatibleWith: traitCollection)",
os: ["iOS", "tvOS"]
)
}

private func generateWatchOSColorFunction(for name: String, at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Function {
let structName = SwiftIdentifier(name: name)
let qualifiedName = prefix + structName

return Function(
availables: ["watchOSApplicationExtension 4.0, *"],
comments: ["`UIColor(named: \"\(name)\", bundle: ..., traitCollection: ...)`"],
accessModifier: externalAccessLevel,
isStatic: true,
name: structName,
generics: nil,
parameters: [
Function.Parameter(name: "_", type: Type._Void, defaultValue: "()")
],
doesThrow: false,
returnType: Type._UIColor.asOptional(),
body: "return UIKit.UIColor(named: \(qualifiedName).name)",
os: ["watchOS"]
)
}

0 comments on commit 4076c62

Please sign in to comment.