Skip to content

Commit eb98dd6

Browse files
committed
Updating CommandLine tool
1 parent 3d8bdd6 commit eb98dd6

File tree

6 files changed

+79
-104
lines changed

6 files changed

+79
-104
lines changed

CommandLine/CommandLine.swift

Lines changed: 24 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@
3232
import Foundation
3333
import SwiftDraw
3434

35-
struct CommandLine {
35+
extension SwiftDraw.CommandLine {
3636

37-
var directory: URL
38-
39-
init(directory: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)) {
40-
self.directory = directory
41-
}
42-
43-
func run(with args: [String] = Swift.CommandLine.arguments) -> ExitCode {
44-
guard let config = try? parseConfiguration(from: args) else {
37+
static func run(with args: [String] = Swift.CommandLine.arguments,
38+
baseDirectory: URL = .currentDirectory) -> ExitCode {
39+
40+
guard let config = try? parseConfiguration(from: args, baseDirectory: baseDirectory) else {
4541
print("Invalid Syntax.")
4642
printHelp()
4743
return .error
@@ -56,34 +52,19 @@ struct CommandLine {
5652

5753
do {
5854
try data.write(to: config.output)
59-
print("Created: \(data.count) \(config.output)")
55+
print("Created: \(config.output.path)")
6056
} catch _ {
61-
print("Failure: \(data.count) \(config.output)")
57+
print("Failure: \(config.output.path)")
6258
}
6359

6460
return .ok
6561
}
6662

67-
func printHelp() {
68-
print("")
69-
print("""
70-
swiftdraw, version 0.2
71-
copyright (c) 2018 Simon Whitty
72-
73-
usage: swiftdraw <file.svg> [--format png | pdf | jpeg] [--output filename] [...]
74-
75-
<file> svg file to be processed
76-
77-
--format format to output image with. png | pdf | jpeg
78-
--output filename to output image to. Optional.
79-
""")
80-
}
81-
82-
func process(with config: Configuration) throws -> Data {
63+
static func process(with config: Configuration) throws -> Data {
8364
guard
84-
let svg = SwiftDraw.Image(fileURL: config.inputSVG),
65+
let svg = SwiftDraw.Image(fileURL: config.input),
8566
let data = CommandLine.processImage(svg, with: config) else {
86-
throw Error.invalid
67+
throw Error.invalid
8768
}
8869

8970
return data
@@ -100,45 +81,23 @@ usage: swiftdraw <file.svg> [--format png | pdf | jpeg] [--output filename] [...
10081
}
10182
}
10283

103-
static func parseURL(file: String, directory: URL) throws -> URL {
104-
guard #available(macOS 10.11, *) else {
105-
throw Error.invalid
106-
}
107-
108-
return URL(fileURLWithPath: file, relativeTo: directory).standardizedFileURL
109-
}
84+
static func printHelp() {
85+
print("")
86+
print("""
87+
swiftdraw, version 0.2
88+
copyright (c) 2018 Simon Whitty
11089
111-
func parseConfiguration(from args: [String]) throws -> Configuration {
112-
guard
113-
args.count >= 3,
114-
let format = Format(rawValue: args[2]) else {
115-
throw Error.invalid
116-
}
90+
usage: swiftdraw <file.svg> [--format png | pdf | jpeg] [--output filename] [...]
11791
118-
let source = try CommandLine.parseURL(file: args[1], directory: directory)
119-
let result = source.newURL(for: format)
92+
<file> svg file to be processed
12093
121-
return Configuration(inputSVG: source, output: result, format: format)
94+
--format format to output image with. png | pdf | jpeg
95+
--output filename to output image to. Optional.
96+
""")
12297
}
12398
}
12499

125-
extension CommandLine {
126-
127-
struct Configuration {
128-
var inputSVG: URL
129-
var output: URL
130-
var format: Format
131-
}
132-
133-
enum Format: String {
134-
case jpeg
135-
case pdf
136-
case png
137-
}
138-
139-
enum Error: Swift.Error {
140-
case invalid
141-
}
100+
extension SwiftDraw.CommandLine {
142101

143102
// Represents the exit codes to the command line. See `man sysexits` for more information.
144103
enum ExitCode: Int32 {
@@ -147,31 +106,9 @@ extension CommandLine {
147106
}
148107
}
149108

150-
extension URL {
151-
152-
var lastPathComponentName: String {
153-
let filename = lastPathComponent
154-
let extensionOffset = pathExtension.isEmpty ? 0 : -pathExtension.count - 1
155-
let index = filename.index(filename.endIndex, offsetBy: extensionOffset)
156-
return String(filename[..<index])
157-
}
158-
159-
func newURL(for format: CommandLine.Format) -> URL {
160-
let newFilename = "\(lastPathComponentName).\(format.pathExtension)"
161-
return deletingLastPathComponent().appendingPathComponent(newFilename).standardizedFileURL
162-
}
163-
}
109+
private extension URL {
164110

165-
private extension CommandLine.Format {
166-
167-
var pathExtension: String {
168-
switch self {
169-
case .jpeg:
170-
return "jpg"
171-
case .pdf:
172-
return "pdf"
173-
case .png:
174-
return "png"
175-
}
111+
static var currentDirectory: URL {
112+
return URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
176113
}
177114
}

CommandLine/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ import CoreGraphics
3737

3838
// SR-9397: Failed to demangle witness for associated type (CF_ENUM)
3939
let _ = [CGLineCap.self, CGLineJoin.self, CGBlendMode.self] as [Any]
40-
exit(CommandLine().run().rawValue)
40+
exit(CommandLine.run().rawValue)

SwiftDraw/CommandLine.Arguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension CommandLine {
6464
return modifiers
6565
}
6666

67-
enum Error: Swift.Error {
67+
public enum Error: Swift.Error {
6868
case invalid
6969
}
7070
}

SwiftDraw/CommandLine.Configuration.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ import Foundation
3333

3434
extension CommandLine {
3535

36-
struct Configuration {
37-
var input: URL
38-
var output: URL
39-
var format: Format
36+
public struct Configuration {
37+
public var input: URL
38+
public var output: URL
39+
public var format: Format
4040
}
4141

42-
enum Format: String {
42+
public enum Format: String {
4343
case jpeg
4444
case pdf
4545
case png
4646
}
4747

48-
static func parseConfiguration(from args: [String], baseDirectory: URL) throws -> Configuration {
48+
public static func parseConfiguration(from args: [String], baseDirectory: URL) throws -> Configuration {
4949
guard args.count > 2 else {
5050
throw Error.invalid
5151
}
5252

53-
let source = try CommandLine.parseSource(file: args[1], baseDirectory: baseDirectory)
53+
let source = try CommandLine.parseFileURL(file: args[1], within: baseDirectory)
5454
let modifiers = try CommandLine.parseModifiers(from: Array(args.dropFirst(2)))
5555
guard
5656
let formatString = modifiers[.format],
@@ -62,16 +62,12 @@ extension CommandLine {
6262
return Configuration(input: source, output: result, format: format)
6363
}
6464

65-
static func parseSource(file: String, baseDirectory: URL) throws -> URL {
66-
return try CommandLine.parseURL(file: file, baseDirectory: baseDirectory)
67-
}
68-
69-
static func parseURL(file: String, baseDirectory: URL) throws -> URL {
65+
static func parseFileURL(file: String, within directory: URL) throws -> URL {
7066
guard #available(macOS 10.11, *) else {
7167
throw Error.invalid
7268
}
7369

74-
return URL(fileURLWithPath: file, relativeTo: baseDirectory).standardizedFileURL
70+
return URL(fileURLWithPath: file, relativeTo: directory).standardizedFileURL
7571
}
7672
}
7773

SwiftDraw/CommandLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131

3232
import Foundation
3333

34-
struct CommandLine {
34+
public struct CommandLine {
3535

3636
}

SwiftDrawTests/CommandLine.ConfigurationTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,50 @@ import XCTest
3434

3535
final class CommandLineConfigurationTests: XCTestCase {
3636

37+
func testParseFileURL() throws {
38+
let url = try CommandLine.parseFileURL(file: "file", within: URL(directory: "/test"))
39+
XCTAssertEqual(url, URL(fileURLWithPath: "/test/file"))
40+
41+
let url1 = try CommandLine.parseFileURL(file: "file", within: URL(directory: "/test/subfolder"))
42+
XCTAssertEqual(url1, URL(fileURLWithPath: "/test/subfolder/file"))
43+
44+
let url2 = try CommandLine.parseFileURL(file: "../file", within: URL(directory: "/test/subfolder"))
45+
XCTAssertEqual(url2, URL(fileURLWithPath: "/test/file"))
46+
}
47+
48+
func testNewURLForFormat() throws {
49+
let svg = URL(fileURLWithPath: "/test/file.svg")
50+
XCTAssertEqual(svg.newURL(for: .jpeg), URL(fileURLWithPath: "/test/file.jpg"))
51+
XCTAssertEqual(svg.newURL(for: .png), URL(fileURLWithPath: "/test/file.png"))
52+
XCTAssertEqual(svg.newURL(for: .pdf), URL(fileURLWithPath: "/test/file.pdf"))
53+
54+
let svgExtension = URL(fileURLWithPath: "/test/file")
55+
XCTAssertEqual(svgExtension.newURL(for: .jpeg), URL(fileURLWithPath: "/test/file.jpg"))
56+
}
57+
3758
func testParseConfiguration() throws {
59+
let config = try CommandLine.parseConfiguration(from: ["swiftdraw", "file.svg", "--format", "pdf"],
60+
baseDirectory: URL(directory: "/"))
61+
62+
XCTAssertEqual(config.input, URL(fileURLWithPath: "/file.svg"))
63+
XCTAssertEqual(config.output, URL(fileURLWithPath: "/file.pdf"))
64+
XCTAssertEqual(config.format, .pdf)
65+
}
66+
67+
func testParseConfigurationThrows() {
68+
XCTAssertThrowsError(try CommandLine.parseConfiguration(from: [],
69+
baseDirectory: URL(directory: "/")))
70+
XCTAssertThrowsError(try CommandLine.parseConfiguration(from: ["swiftdraw", "file.svg"],
71+
baseDirectory: URL(directory: "/")))
72+
XCTAssertThrowsError(try CommandLine.parseConfiguration(from: ["swiftdraw", "file.svg", "--format", "unknown"],
73+
baseDirectory: URL(directory: "/")))
74+
75+
}
76+
}
77+
78+
private extension URL {
3879

39-
80+
init(directory: String) {
81+
self.init(fileURLWithPath: directory, isDirectory: true)
4082
}
4183
}

0 commit comments

Comments
 (0)