Import colors from Figma library #4

Merged
Sven merged 15 commits from figma into main 2022-10-08 11:14:23 +00:00
4 changed files with 45 additions and 26 deletions
Showing only changes of commit e28bee804f - Show all commits

View file

@ -0,0 +1,5 @@
protocol Importer {
init(source: String) throws
func read() throws -> [String: ColorDef]
}

View file

@ -0,0 +1,36 @@
import Foundation
struct ListImporter: Importer {
let input: String
init(source: String) {
input = source
}
func read() throws -> [String: ColorDef] {
let scanner = Scanner(string: try readInput())
scanner.charactersToBeSkipped = .whitespaces
return try scanner.colorList()
}
func readInput() throws -> String {
if input == "-" {
return try readStdin()
}
let url = URL(fileURLWithPath: input)
return try String(contentsOf: url)
}
func readStdin() throws -> String {
guard
let data = try FileHandle.standardInput.readToEnd(),
let input = String(data: data, encoding: .utf8)
else {
throw Errors.cannotReadStdin
}
return input
}
}

View file

@ -110,7 +110,7 @@ extension Scanner {
func colorLine() -> (String, ColorDef)? {
guard
let name = self.name(),
let name = name(),
let def = colorDef(),
endOfLine()
else {
@ -168,7 +168,7 @@ extension Scanner {
func commaSeparated() -> [UInt8]? {
var result: [UInt8] = []
repeat {
guard let component = self.component() else {
guard let component = component() else {
return nil
}
result.append(component)

View file

@ -66,10 +66,8 @@ public final class MakeColors: ParsableCommand, Context {
public init() {}
public func run() throws {
let scanner = Scanner(string: try readInput())
scanner.charactersToBeSkipped = .whitespaces
let data = try scanner.colorList()
let importer = ListImporter(source: input)
let data = try importer.read()
if dump {
try dump(data: data)
@ -81,26 +79,6 @@ public final class MakeColors: ParsableCommand, Context {
try writeOutput(fileWrapper)
}
func readInput() throws -> String {
if input == "-" {
return try readStdin()
}
let url = URL(fileURLWithPath: input)
return try String(contentsOf: url)
}
func readStdin() throws -> String {
guard
let data = try FileHandle.standardInput.readToEnd(),
let input = String(data: data, encoding: .utf8)
else {
throw Errors.cannotReadStdin
}
return input
}
func dump(data: [String: ColorDef]) throws {
for (key, color) in data.sorted() {
let resolved = try data.resolve(key)