Skip to content

Commit

Permalink
Merge pull request #55 from hhru/type-decoding
Browse files Browse the repository at this point in the history
Исправлен декодинг типов токенов
  • Loading branch information
timbaev committed Sep 26, 2023
2 parents 8184bf5 + ddae183 commit 69b32b7
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Sources/FigmaGen/Commands/GenerateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ final class GenerateCommand: AsyncExecutableCommand {
"""
)

let verbose = Flag(
"--verbose",
description: """
Enable verbose logging for debuging.
By default is disabled.
"""
)

let generator: LibraryGenerator

// MARK: - Initializers
Expand All @@ -29,6 +37,10 @@ final class GenerateCommand: AsyncExecutableCommand {
// MARK: - Instance Methods

func executeAsyncAndExit() throws {
if verbose.value {
setenv(Logger.verboseLogKey, "YES", 1)
}

let configurationPath = self.configurationPath.value ?? .defaultConfigurationPath

firstly {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
// MARK: - Instance Methods

private func fallbackWarning(tokenName: String) {
print("[⚠️] Night value for token '\(tokenName)' not found, using day value.")
logger.warning("Night value for token '\(tokenName)' not found, using day value.")
}

private func resolveNightValue(
Expand Down
10 changes: 10 additions & 0 deletions Sources/FigmaGen/Logger/DefaultLoggerPrinter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

struct DefaultLoggerPrinter: LoggerPrinting {

// MARK: - LoggerPrinting

func print(_ message: String, terminator: String) {
Swift.print(message, terminator: terminator)
}
}
76 changes: 76 additions & 0 deletions Sources/FigmaGen/Logger/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Foundation

struct Logger {

// MARK: - Type Properties

static let verboseLogKey = "VERBOSE_LOG"

// MARK: - Instance Properties

let isSilent: Bool
let printer: LoggerPrinting

var isVerbose: Bool {
ProcessInfo.processInfo.environment[Self.verboseLogKey] != nil
}

// MARK: - Initializers

init(isSilent: Bool = false, printer: LoggerPrinting = DefaultLoggerPrinter()) {
self.isSilent = isSilent
self.printer = printer
}

// MARK: - Instance Methods

private func print(_ message: String, terminator: String = "\n", isVerbose: Bool) {
guard !isSilent else {
return
}

guard !isVerbose || (isVerbose && self.isVerbose) else {
return
}

printer.print(message, terminator: terminator)
}

// MARK: -

func debug(_ items: Any..., separator: String = " ", terminator: String = "\n", isVerbose: Bool = true) {
let message = items.joinedDescription(separator: separator)
print(message, terminator: terminator, isVerbose: isVerbose)
}

func info(_ items: Any..., separator: String = " ", terminator: String = "\n", isVerbose: Bool = false) {
let message = items.joinedDescription(separator: separator)
print(message, terminator: terminator, isVerbose: isVerbose)
}

func warning(_ items: Any..., separator: String = " ", terminator: String = "\n", isVerbose: Bool = false) {
let message = "⚠️ WARNING: \(items.joinedDescription(separator: separator))".yellow
print(message, terminator: terminator, isVerbose: isVerbose)
}

func success(_ items: Any..., separator: String = " ", terminator: String = "\n", isVerbose: Bool = false) {
let message = "\(items.joinedDescription(separator: separator))".green
print(message, terminator: terminator, isVerbose: isVerbose)
}

func error(_ items: Any..., separator: String = " ", terminator: String = "\n", isVerbose: Bool = false) {
let message = "🛑 ERROR: \(items.joinedDescription(separator: separator))".red
print(message, terminator: terminator, isVerbose: isVerbose)
}
}

// MARK: -

extension Sequence {

// MARK: - Instance Methods

fileprivate func joinedDescription(separator: String) -> String {
map { "\($0)" }.joined(separator: separator)
}
}
8 changes: 8 additions & 0 deletions Sources/FigmaGen/Logger/LoggerPrinting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

protocol LoggerPrinting {

// MARK: - Instance Methods

func print(_ message: String, terminator: String)
}
10 changes: 9 additions & 1 deletion Sources/FigmaGen/Models/Token/TokenValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extension TokenValue: Decodable {
case textCase
case textDecoration
case typography
case unknown
}

fileprivate enum CodingKeys: String, CodingKey {
Expand All @@ -51,7 +52,8 @@ extension TokenValue: Decodable {

self.name = try container.decode(forKey: .name)

let rawType = try container.decode(RawType.self, forKey: .type)
let rawTypeValue = try container.decode(String.self, forKey: .type)
let rawType = RawType(rawValue: rawTypeValue) ?? .unknown

switch rawType {
case .a11yScales:
Expand Down Expand Up @@ -113,6 +115,9 @@ extension TokenValue: Decodable {

case .typography:
self.type = .typography(value: try container.decode(forKey: .value))

case .unknown:
self.type = .unknown
}
}
}
Expand Down Expand Up @@ -189,6 +194,9 @@ extension TokenValue: Encodable {

case let .typography(value):
try container.encode(value, forKey: .value)

case .unknown:
try container.encodeNil(forKey: .value)
}
}
}
3 changes: 2 additions & 1 deletion Sources/FigmaGen/Models/Token/TokenValueType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum TokenValueType: Hashable {
case textCase(value: String)
case textDecoration(value: String)
case typography(value: TokenTypographyValue)
case unknown

// MARK: - Instance Properties

Expand All @@ -48,7 +49,7 @@ enum TokenValueType: Hashable {
let .textDecoration(value):
return value

case .animation, .boxShadow, .typography:
case .animation, .boxShadow, .typography, .unknown:
return nil
}
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/FigmaGen/Providers/Tokens/DefaultTokensProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ final class DefaultTokensProvider: TokensProvider {
throw TokensProviderError(code: .failedCreateData)
}

let json = try? JSONSerialization.jsonObject(with: valuesData, options: .mutableContainers)
let jsonData = json.flatMap { try? JSONSerialization.data(withJSONObject: $0, options: .prettyPrinted) }

if let jsonData {
logger.debug(String(decoding: jsonData, as: UTF8.self))
}

return try jsonDecoder.decode(TokenValues.self, from: valuesData)
}

Expand Down
1 change: 1 addition & 0 deletions Sources/FigmaGen/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Path.current = Path(#file).appending("../../../Demo")
#endif

let version = "2.0.0-beta.11"
let logger = Logger()

let figmagen = CLI(
name: "figmagen",
Expand Down

0 comments on commit 69b32b7

Please sign in to comment.