Import colors from Figma library #4
4 changed files with 45 additions and 26 deletions
5
Sources/LibMakeColors/Importers/Importer.swift
Normal file
5
Sources/LibMakeColors/Importers/Importer.swift
Normal file
|
@ -0,0 +1,5 @@
|
|||
protocol Importer {
|
||||
init(source: String) throws
|
||||
|
||||
func read() throws -> [String: ColorDef]
|
||||
}
|
36
Sources/LibMakeColors/Importers/List/ListImporter.swift
Normal file
36
Sources/LibMakeColors/Importers/List/ListImporter.swift
Normal 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
|
||||
}
|
||||
}
|
|
@ -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)
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue