Skip to content

Commit 971c026

Browse files
committed
CommandLine support for Swift
1 parent 4b49f4f commit 971c026

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

CommandLine/CommandLine.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ extension SwiftDraw.CommandLine {
6161
}
6262

6363
static func process(with config: Configuration) throws -> Data {
64-
guard
65-
let svg = SwiftDraw.Image(fileURL: config.input),
66-
let data = processImage(svg, with: config) else {
64+
guard let data = processImage(config: config) else {
6765
throw Error.invalid
6866
}
69-
67+
7068
return data
7169
}
72-
70+
71+
static func processImage(config: Configuration) -> Data? {
72+
switch config.format {
73+
case .swift:
74+
let code = Image.cgCodeText(fileURL: config.input)
75+
return code?.data(using: .utf8)
76+
case .jpeg, .pdf, .png:
77+
return SwiftDraw.Image(fileURL: config.input).flatMap { processImage($0, with: config) }
78+
}
79+
}
80+
7381
static func processImage(_ image: SwiftDraw.Image, with config: Configuration) -> Data? {
7482
switch config.format {
7583
case .jpeg:
@@ -78,6 +86,8 @@ extension SwiftDraw.CommandLine {
7886
return try? Image.pdfData(fileURL: config.input, size: config.size.cgValue)
7987
case .png:
8088
return image.pngData(size: config.size.cgValue, scale: config.scale.cgValue)
89+
case .swift:
90+
preconditionFailure()
8191
}
8292
}
8393

@@ -87,11 +97,11 @@ extension SwiftDraw.CommandLine {
8797
swiftdraw, version 0.7.6
8898
copyright (c) 2021 Simon Whitty
8999
90-
usage: swiftdraw <file.svg> [--format png | pdf | jpeg] [--size wxh] [--scale 1x | 2x | 3x]
100+
usage: swiftdraw <file.svg> [--format png | pdf | jpeg | swift] [--size wxh] [--scale 1x | 2x | 3x]
91101
92102
<file> svg file to be processed
93103
94-
--format format to output image with png | pdf | jpeg
104+
--format format to output image with png | pdf | jpeg | swift
95105
--size size of output image e.g. 100x200
96106
--scale scale of output image with 1x | 2x | 3x
97107
""")

SwiftDraw/CommandLine.Configuration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extension CommandLine {
4545
case jpeg
4646
case pdf
4747
case png
48+
case swift
4849
}
4950

5051
public enum Size: Equatable {
@@ -158,6 +159,8 @@ private extension CommandLine.Format {
158159
return "pdf"
159160
case .png:
160161
return "png"
162+
case .swift:
163+
return "swift"
161164
}
162165
}
163166
}

SwiftDraw/Image+CGText.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ import Foundation
3434
public extension Image {
3535

3636
static func cgCodeText(named name: String, in bundle: Bundle = Bundle.main) -> String? {
37-
guard
38-
let url = bundle.url(forResource: name, withExtension: nil),
39-
let svg = try? DOM.SVG.parse(fileURL: url) else {
40-
return nil
41-
}
37+
guard let url = bundle.url(forResource: name, withExtension: nil) else { return nil }
38+
return cgCodeText(fileURL: url)
39+
}
4240

43-
return cgCodeText(url: url, svg: svg)
41+
static func cgCodeText(fileURL: URL) -> String? {
42+
guard let svg = try? DOM.SVG.parse(fileURL: fileURL) else {
43+
return nil
44+
}
45+
return cgCodeText(url: fileURL, svg: svg)
4446
}
45-
47+
4648
private static func cgCodeText(url: URL, svg: DOM.SVG) -> String {
4749
let layer = LayerTree.Builder(svg: svg).makeLayer()
4850
let size = LayerTree.Size(svg.width, svg.height)

0 commit comments

Comments
 (0)