Let importer determine output file name.

This commit is contained in:
Sven Weidauer 2022-10-08 12:50:55 +02:00
parent 2b1f21dbab
commit ac708f4ae2
4 changed files with 9 additions and 12 deletions

View file

@ -10,6 +10,7 @@ enum FigmaErrors: Error {
class FigmaImporter: Importer {
let key: String
let token: String
let outputName: String
required init(source: String) throws {
// https://www.figma.com/file/:key/:title
@ -23,6 +24,7 @@ class FigmaImporter: Importer {
}
key = url.pathComponents[2]
outputName = url.pathComponents[3]
guard let token = ProcessInfo.processInfo.environment["FIGMA_TOKEN"] else {
throw FigmaErrors.missingToken

View file

@ -3,6 +3,8 @@ protocol Importer {
func read() async throws -> [String: ColorDef]
var outputName: String { get }
static var option: String { get }
}

View file

@ -2,9 +2,11 @@ import Foundation
struct ListImporter: Importer {
let input: String
var outputName: String
init(source: String) {
input = source
outputName = URL(fileURLWithPath: source).deletingPathExtension().lastPathComponent
}
func read() throws -> [String: ColorDef] {

View file

@ -113,7 +113,7 @@ public final class MakeColors: AsyncParsableCommand, Context {
let generator = formatter.type.init(context: self)
let fileWrapper = try generator.generate(data: data)
try writeOutput(fileWrapper)
try writeOutput(fileWrapper, name: output ?? "\(importer.outputName).\(formatter.type.defaultExtension)")
}
func dump(data: [String: ColorDef]) throws {
@ -133,7 +133,7 @@ public final class MakeColors: AsyncParsableCommand, Context {
}
}
func writeOutput(_ wrapper: FileWrapper) throws {
func writeOutput(_ wrapper: FileWrapper, name: String) throws {
if shouldWriteToStdout {
guard wrapper.isRegularFile, let contents = wrapper.regularFileContents else {
throw Errors.cannotWriteWrapperToStdout
@ -141,19 +141,10 @@ public final class MakeColors: AsyncParsableCommand, Context {
FileHandle.standardOutput.write(contents)
} else {
let writeURL = outputURL(extension: formatter.type.defaultExtension)
let writeURL = URL(fileURLWithPath: name)
try wrapper.write(to: writeURL, options: .atomic, originalContentsURL: nil)
}
}
var shouldWriteToStdout: Bool { output == "-" || (input == "-" && output == nil) }
func outputURL(extension: String) -> URL {
if let output = output {
return URL(fileURLWithPath: output)
} else {
let basename = URL(fileURLWithPath: input).deletingPathExtension().lastPathComponent
return URL(fileURLWithPath: basename).appendingPathExtension(`extension`)
}
}
}