diff --git a/Sources/FigmaGen/Generators/Tokens/Contexts/ColorToken.swift b/Sources/FigmaGen/Generators/Tokens/Contexts/ColorToken.swift index 3068740..2a6e788 100644 --- a/Sources/FigmaGen/Generators/Tokens/Contexts/ColorToken.swift +++ b/Sources/FigmaGen/Generators/Tokens/Contexts/ColorToken.swift @@ -16,6 +16,7 @@ struct ColorToken: Encodable { let dayTheme: Theme let nightTheme: Theme + let zpDayTheme: Theme let name: String let path: [String] } diff --git a/Sources/FigmaGen/Generators/Tokens/Providers/ColorTokensContext/DefaultColorTokensContextProvider.swift b/Sources/FigmaGen/Generators/Tokens/Providers/ColorTokensContext/DefaultColorTokensContextProvider.swift index 4765b83..fb446de 100644 --- a/Sources/FigmaGen/Generators/Tokens/Providers/ColorTokensContext/DefaultColorTokensContextProvider.swift +++ b/Sources/FigmaGen/Generators/Tokens/Providers/ColorTokensContext/DefaultColorTokensContextProvider.swift @@ -14,48 +14,50 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider { // MARK: - Instance Methods - private func fallbackWarning(tokenName: String) { - logger.warning("Night value for token '\(tokenName)' not found, using day value.") + private func fallbackWarning(warningPrefix: String, tokenName: String) { + logger.warning("\(warningPrefix) value for token '\(tokenName)' not found, using day value.") } - private func resolveNightValue( + private func resolveColorToken( tokenName: String, - fallbackValue: String, - tokenValues: TokenValues - ) throws -> String { - guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else { - fallbackWarning(tokenName: tokenName) - return fallbackValue + fallbackColorToken: ColorToken.Theme, + tokenValues: TokenValues, + theme: Theme + ) throws -> ColorToken.Theme { + // Resolve theme data + let themeData: (tokenValues: [TokenValue], warningPrefix: String) = switch theme { + case .night: + (tokenValues.hhNight, "Night") + + case .zpDay: + (tokenValues.zpDay, "ZpDay") + + case .day, .undefined: + ([], "") + } + + // Resolve token and value + guard let themeToken = themeData.tokenValues.first(where: { $0.name == tokenName }) else { + fallbackWarning(warningPrefix: themeData.warningPrefix, tokenName: tokenName) + return fallbackColorToken } - guard case .color(let nightValue) = nightToken.type else { - fallbackWarning(tokenName: tokenName) - return fallbackValue + guard case .color(let themeValue) = themeToken.type else { + fallbackWarning(warningPrefix: themeData.warningPrefix, tokenName: tokenName) + return fallbackColorToken } - return try tokensResolver.resolveHexColorValue( - nightValue, + // Resolve hex color value + let themeHexColorValue = try tokensResolver.resolveHexColorValue( + themeValue, tokenValues: tokenValues, - theme: .night + theme: theme ) - } - private func resolveNightReference( - tokenName: String, - fallbackRefence: String, - tokenValues: TokenValues - ) throws -> String { - guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else { - fallbackWarning(tokenName: tokenName) - return fallbackRefence - } - - guard case .color(let nightValue) = nightToken.type else { - fallbackWarning(tokenName: tokenName) - return fallbackRefence - } + // Resolve reference + let themeReference = try tokensResolver.resolveBaseReference(themeValue, tokenValues: themeData.tokenValues) - return try tokensResolver.resolveBaseReference(nightValue, tokenValues: tokenValues.hhNight) + return ColorToken.Theme(value: themeHexColorValue, reference: themeReference) } private func makeColorToken( @@ -64,32 +66,35 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider { tokenValues: TokenValues, path: [String] ) throws -> ColorToken { - let dayHexColorValue = try tokensResolver.resolveHexColorValue( - dayValue, - tokenValues: tokenValues, - theme: .day - ) - - let dayReference = try tokensResolver.resolveBaseReference( - dayValue, - tokenValues: tokenValues.hhDay + let dayColorToken = ColorToken.Theme( + value: try tokensResolver.resolveHexColorValue( + dayValue, + tokenValues: tokenValues, + theme: .day + ), + reference: try tokensResolver.resolveBaseReference( + dayValue, + tokenValues: tokenValues.hhDay + ) ) - let nightReference = try resolveNightReference( + let nightColorToken = try resolveColorToken( tokenName: tokenName, - fallbackRefence: dayValue, - tokenValues: tokenValues + fallbackColorToken: dayColorToken, + tokenValues: tokenValues, + theme: .night ) - - let nightHexColorValue = try resolveNightValue( + let zpDayColorToken = try resolveColorToken( tokenName: tokenName, - fallbackValue: dayHexColorValue, - tokenValues: tokenValues + fallbackColorToken: dayColorToken, + tokenValues: tokenValues, + theme: .zpDay ) return ColorToken( - dayTheme: ColorToken.Theme(value: dayHexColorValue, reference: dayReference), - nightTheme: ColorToken.Theme(value: nightHexColorValue, reference: nightReference), + dayTheme: dayColorToken, + nightTheme: nightColorToken, + zpDayTheme: zpDayColorToken, name: tokenName, path: path ) diff --git a/Sources/FigmaGen/Models/Token/Theme.swift b/Sources/FigmaGen/Models/Token/Theme.swift index 23e26ff..e8ec3e3 100644 --- a/Sources/FigmaGen/Models/Token/Theme.swift +++ b/Sources/FigmaGen/Models/Token/Theme.swift @@ -3,5 +3,6 @@ import Foundation enum Theme: Codable { case day case night + case zpDay case undefined } diff --git a/Sources/FigmaGen/Models/Token/TokenValues.swift b/Sources/FigmaGen/Models/Token/TokenValues.swift index 39ab92d..b2db6ed 100644 --- a/Sources/FigmaGen/Models/Token/TokenValues.swift +++ b/Sources/FigmaGen/Models/Token/TokenValues.swift @@ -24,8 +24,11 @@ struct TokenValues: Hashable { case .night: return [hhNight, core, semantic, colors, typography].flatMap { $0 } + case .zpDay: + return [zpDay, core, semantic, colors, typography].flatMap { $0 } + case .undefined: - return [core, semantic, colors, typography, hhDay, hhNight].flatMap { $0 } + return [core, semantic, colors, typography, hhDay, hhNight, zpDay].flatMap { $0 } } } }