Skip to content

Commit 3d8bdd6

Browse files
committed
Adding command line configuration parser and tests
1 parent 0ef5c29 commit 3d8bdd6

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

SwiftDraw.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@
181181
D9BA810021B9C5E5005159E7 /* CommandLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */; };
182182
D9BA810221B9C624005159E7 /* CommandLine.Arguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */; };
183183
D9BA810521B9C665005159E7 /* CommandLine.ArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */; };
184+
D9BA810721BA577B005159E7 /* CommandLine.Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810621BA577B005159E7 /* CommandLine.Configuration.swift */; };
185+
D9BA810921BA94A4005159E7 /* CommandLine.ConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810821BA94A4005159E7 /* CommandLine.ConfigurationTests.swift */; };
184186
D9C47AC21ED54FA100178D06 /* mask.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47ABF1ED54FA100178D06 /* mask.svg */; };
185187
D9C47AC31ED54FA100178D06 /* mask.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47ABF1ED54FA100178D06 /* mask.svg */; };
186188
D9C47AC41ED54FA100178D06 /* transform.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47AC01ED54FA100178D06 /* transform.svg */; };
@@ -328,6 +330,8 @@
328330
D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.swift; sourceTree = "<group>"; };
329331
D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.Arguments.swift; sourceTree = "<group>"; };
330332
D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.ArgumentsTests.swift; sourceTree = "<group>"; };
333+
D9BA810621BA577B005159E7 /* CommandLine.Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.Configuration.swift; sourceTree = "<group>"; };
334+
D9BA810821BA94A4005159E7 /* CommandLine.ConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.ConfigurationTests.swift; sourceTree = "<group>"; };
331335
D9C47ABF1ED54FA100178D06 /* mask.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = mask.svg; sourceTree = "<group>"; };
332336
D9C47AC01ED54FA100178D06 /* transform.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = transform.svg; sourceTree = "<group>"; };
333337
D9C47AC11ED54FA100178D06 /* viewbox.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = viewbox.svg; sourceTree = "<group>"; };
@@ -626,6 +630,7 @@
626630
children = (
627631
D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */,
628632
D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */,
633+
D9BA810621BA577B005159E7 /* CommandLine.Configuration.swift */,
629634
);
630635
name = CommandLine;
631636
sourceTree = "<group>";
@@ -634,6 +639,7 @@
634639
isa = PBXGroup;
635640
children = (
636641
D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */,
642+
D9BA810821BA94A4005159E7 /* CommandLine.ConfigurationTests.swift */,
637643
);
638644
name = CommandLine;
639645
sourceTree = "<group>";
@@ -967,6 +973,7 @@
967973
D90DB45D219CD27800D374D2 /* Parser.XML.Element.swift in Sources */,
968974
D90DB434219CD1C600D374D2 /* DOM.Path.swift in Sources */,
969975
D90DB405219CD06800D374D2 /* XML.SAXParser.swift in Sources */,
976+
D9BA810721BA577B005159E7 /* CommandLine.Configuration.swift in Sources */,
970977
D90DB43A219CD1C600D374D2 /* DOM.Color.swift in Sources */,
971978
D90DB3C1219CCC4100D374D2 /* LayerTree.Commands.swift in Sources */,
972979
D90DB3C3219CCC4800D374D2 /* Renderer.LayerTree.swift in Sources */,
@@ -1007,6 +1014,7 @@
10071014
D90DB3D6219CCC8A00D374D2 /* LayerTree.ShapeTests.swift in Sources */,
10081015
D925CC6821A4A2F800C8897B /* LayerTree.BuilderTests.swift in Sources */,
10091016
D90DB39B219CCB9E00D374D2 /* Renderer.CoreGraphicsTypesTests.swift in Sources */,
1017+
D9BA810921BA94A4005159E7 /* CommandLine.ConfigurationTests.swift in Sources */,
10101018
D925CC5F21A3E02C00C8897B /* CGPath+SegmentTests.swift in Sources */,
10111019
D90DB48A219CD2C300D374D2 /* Parser.XML.TransformTests.swift in Sources */,
10121020
D90DB3D3219CCC8A00D374D2 /* LayerTree.ImageTests.swift in Sources */,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// CommandLine.Configuration.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 7/12/18.
6+
// Copyright 2018 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import Foundation
33+
34+
extension CommandLine {
35+
36+
struct Configuration {
37+
var input: URL
38+
var output: URL
39+
var format: Format
40+
}
41+
42+
enum Format: String {
43+
case jpeg
44+
case pdf
45+
case png
46+
}
47+
48+
static func parseConfiguration(from args: [String], baseDirectory: URL) throws -> Configuration {
49+
guard args.count > 2 else {
50+
throw Error.invalid
51+
}
52+
53+
let source = try CommandLine.parseSource(file: args[1], baseDirectory: baseDirectory)
54+
let modifiers = try CommandLine.parseModifiers(from: Array(args.dropFirst(2)))
55+
guard
56+
let formatString = modifiers[.format],
57+
let format = Format(rawValue: formatString) else {
58+
throw Error.invalid
59+
}
60+
61+
let result = source.newURL(for: format)
62+
return Configuration(input: source, output: result, format: format)
63+
}
64+
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 {
70+
guard #available(macOS 10.11, *) else {
71+
throw Error.invalid
72+
}
73+
74+
return URL(fileURLWithPath: file, relativeTo: baseDirectory).standardizedFileURL
75+
}
76+
}
77+
78+
79+
extension URL {
80+
81+
var lastPathComponentName: String {
82+
let filename = lastPathComponent
83+
let extensionOffset = pathExtension.isEmpty ? 0 : -pathExtension.count - 1
84+
let index = filename.index(filename.endIndex, offsetBy: extensionOffset)
85+
return String(filename[..<index])
86+
}
87+
88+
func newURL(for format: CommandLine.Format) -> URL {
89+
let newFilename = "\(lastPathComponentName).\(format.pathExtension)"
90+
return deletingLastPathComponent().appendingPathComponent(newFilename).standardizedFileURL
91+
}
92+
}
93+
94+
private extension CommandLine.Format {
95+
96+
var pathExtension: String {
97+
switch self {
98+
case .jpeg:
99+
return "jpg"
100+
case .pdf:
101+
return "pdf"
102+
case .png:
103+
return "png"
104+
}
105+
}
106+
}
107+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// CommandLine.ConfigurationTests.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 7/12/18.
6+
// Copyright 2018 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import XCTest
33+
@testable import SwiftDraw
34+
35+
final class CommandLineConfigurationTests: XCTestCase {
36+
37+
func testParseConfiguration() throws {
38+
39+
40+
}
41+
}

0 commit comments

Comments
 (0)