Skip to content

Commit

Permalink
Add export type (#45)
Browse files Browse the repository at this point in the history
* use latest swift syntax version

* Fix compile error in CsvBuilder

* Refactor.

* Add `exportType` option to CLI

---------

Co-authored-by: infinitepower18 <[email protected]>
  • Loading branch information
fummicc1 and infinitepower18 authored Dec 3, 2023
1 parent d5e6ab2 commit cf8cbf9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/Csv2ImgCmd.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--export-type pdf"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-n https://raw.githubusercontent.com/fummicc1/csv2img/main/Fixtures/sample_1.csv"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "sample.png"
argument = "sample.pdf"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
Expand Down
26 changes: 21 additions & 5 deletions Sources/Csv2ImgCmd/command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import ArgumentParser
import CoreImage
import Csv2Img
import PDFKit


/// Csv resource type
Expand Down Expand Up @@ -67,6 +68,9 @@ public struct Csv2Img: AsyncParsableCommand {
@Flag(help: "Csv file type. Choose either `local` or `network`")
public var inputType: InputType

@Option
public var exportType: Csv.ExportType = .pdf

@Argument(help: "Input. csv absolute-path or url on the internet")
public var input: String

Expand All @@ -87,15 +91,27 @@ public struct Csv2Img: AsyncParsableCommand {
}
csv = try Csv.loadFromNetwork(url)
}
let image = try await csv.generate(fontSize: 12, exportType: .png).base as! CGImage
let data = image.convertToData()
let exportable = try await csv.generate(fontSize: 12, exportType: exportType).base
let outputURL = URL(fileURLWithPath: output)
if !FileManager.default.fileExists(atPath: output) {
FileManager.default.createFile(atPath: output, contents: data)
} else {
FileManager.default.createFile(atPath: output, contents: Data())
}
switch exportable {
case let pdf as PDFDocument:
let isSuccessful = pdf.write(to: outputURL)
if !isSuccessful {
throw PdfMakingError.failedToSavePdf(at: outputURL.absoluteString)
}
print("Succeed generating pdf from csv!")
case let image as CGImage:
let data = image.convertToData()
try data?.write(to: outputURL)
print("Succeed generating image from csv!")
default:
fatalError("unsupported exportable data.")
}
print("Succeed generating image from csv!")
print("Output path: ", outputURL.absoluteString)
}
}

extension Csv.ExportType: ExpressibleByArgument {}

0 comments on commit cf8cbf9

Please sign in to comment.