From ac708f4ae2497c0357333cb5f33b84658ee0a5ef Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 8 Oct 2022 12:50:55 +0200 Subject: [PATCH] Let importer determine output file name. --- .../Importers/Figma/FigmaImporter.swift | 2 ++ Sources/MakeColors/Importers/Importer.swift | 2 ++ .../MakeColors/Importers/List/ListImporter.swift | 2 ++ Sources/MakeColors/MakeColors.swift | 15 +++------------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Sources/MakeColors/Importers/Figma/FigmaImporter.swift b/Sources/MakeColors/Importers/Figma/FigmaImporter.swift index 488f4d8..c84339f 100644 --- a/Sources/MakeColors/Importers/Figma/FigmaImporter.swift +++ b/Sources/MakeColors/Importers/Figma/FigmaImporter.swift @@ -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 diff --git a/Sources/MakeColors/Importers/Importer.swift b/Sources/MakeColors/Importers/Importer.swift index 5ba54c2..6c4d5d8 100644 --- a/Sources/MakeColors/Importers/Importer.swift +++ b/Sources/MakeColors/Importers/Importer.swift @@ -3,6 +3,8 @@ protocol Importer { func read() async throws -> [String: ColorDef] + var outputName: String { get } + static var option: String { get } } diff --git a/Sources/MakeColors/Importers/List/ListImporter.swift b/Sources/MakeColors/Importers/List/ListImporter.swift index d0e3d50..ef291b1 100644 --- a/Sources/MakeColors/Importers/List/ListImporter.swift +++ b/Sources/MakeColors/Importers/List/ListImporter.swift @@ -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] { diff --git a/Sources/MakeColors/MakeColors.swift b/Sources/MakeColors/MakeColors.swift index 12a5ff3..8b88849 100644 --- a/Sources/MakeColors/MakeColors.swift +++ b/Sources/MakeColors/MakeColors.swift @@ -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`) - } - } }