3232import Foundation
3333import 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}
0 commit comments