From 1f6b55014a37b66dbd775967552f7ce5374a13b4 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 30 Dec 2020 12:53:40 +0100 Subject: [PATCH] Configure + run SwiftFormat --- .swift-version | 1 + .swiftformat | 14 +++++++++++++ Package.swift | 11 ++++++---- .../Generators/AndroidGenerator.swift | 6 +++--- .../Generators/AssetCatalogGenerator.swift | 9 ++++----- .../Generators/HTMLGenerator.swift | 2 +- Sources/LibMakeColors/MakeColors.swift | 9 ++++++--- Sources/LibMakeColors/Model/Color.swift | 7 +++---- Sources/LibMakeColors/Model/Parser.swift | 20 +++++++++---------- Tests/MakeColorsTests/ColorParserTest.swift | 12 +++++------ 10 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 .swift-version create mode 100644 .swiftformat diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..d346e2a --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +5.3 diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 0000000..67c42e8 --- /dev/null +++ b/.swiftformat @@ -0,0 +1,14 @@ +--header strip +--wraparguments before-first +--wrapcollections before-first +--wrapconditions before-first +--wrapparameters before-first +--semicolons never +--ranges no-space +--exclude Pods,Generated + +--disable anyObjectProtocol,redundantRawValues +--enable wrapEnumCases,isEmpty + +--maxwidth 120 + diff --git a/Package.swift b/Package.swift index 475bbaf..ea9aaaa 100644 --- a/Package.swift +++ b/Package.swift @@ -15,15 +15,18 @@ let package = Package( .target( name: "MakeColors", dependencies: [ - "LibMakeColors" - ]), + "LibMakeColors", + ] + ), .target( name: "LibMakeColors", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), - ]), + ] + ), .testTarget( name: "MakeColorsTests", - dependencies: ["LibMakeColors"]), + dependencies: ["LibMakeColors"] + ), ] ) diff --git a/Sources/LibMakeColors/Generators/AndroidGenerator.swift b/Sources/LibMakeColors/Generators/AndroidGenerator.swift index ecd225d..e9c309f 100644 --- a/Sources/LibMakeColors/Generators/AndroidGenerator.swift +++ b/Sources/LibMakeColors/Generators/AndroidGenerator.swift @@ -9,7 +9,7 @@ final class AndroidGenerator: Generator { self.context = context } - func generate(data: [String : ColorDef]) throws -> FileWrapper { + func generate(data: [String: ColorDef]) throws -> FileWrapper { var xml = """ @@ -23,8 +23,8 @@ final class AndroidGenerator: Generator { let value: String switch color { - case .color(let colorValue): value = colorValue.description - case .reference(let ref): value = "@color/\(prefix)\(ref.camelCasePathToSnakeCase())" + case let .color(colorValue): value = colorValue.description + case let .reference(ref): value = "@color/\(prefix)\(ref.camelCasePathToSnakeCase())" } xml += """ diff --git a/Sources/LibMakeColors/Generators/AssetCatalogGenerator.swift b/Sources/LibMakeColors/Generators/AssetCatalogGenerator.swift index f17623b..ee277ce 100644 --- a/Sources/LibMakeColors/Generators/AssetCatalogGenerator.swift +++ b/Sources/LibMakeColors/Generators/AssetCatalogGenerator.swift @@ -10,8 +10,8 @@ final class AssetCatalogGenerator: Generator { self.context = context } - func generate(data: [String : ColorDef]) throws -> FileWrapper { - let root = FileWrapper(directoryWithFileWrappers: ["Contents.json" : FileWrapper(catalog)]) + func generate(data: [String: ColorDef]) throws -> FileWrapper { + let root = FileWrapper(directoryWithFileWrappers: ["Contents.json": FileWrapper(catalog)]) let colorRoot: FileWrapper if let prefix = context.prefix?.insertCamelCaseSeparators() { @@ -53,7 +53,7 @@ final class AssetCatalogGenerator: Generator { private extension Color { func json() -> String { - return """ + """ { "colors" : [ { @@ -79,12 +79,11 @@ private extension Color { func fileWrapper() -> FileWrapper { FileWrapper(directoryWithFileWrappers: [ - "Contents.json": FileWrapper(json()) + "Contents.json": FileWrapper(json()), ]) } } - private let group = """ { "info" : { diff --git a/Sources/LibMakeColors/Generators/HTMLGenerator.swift b/Sources/LibMakeColors/Generators/HTMLGenerator.swift index 43d0d08..9900f3e 100644 --- a/Sources/LibMakeColors/Generators/HTMLGenerator.swift +++ b/Sources/LibMakeColors/Generators/HTMLGenerator.swift @@ -8,7 +8,7 @@ final class HTMLGenerator: Generator { self.context = context } - func generate(data: [String : ColorDef]) throws -> FileWrapper { + func generate(data: [String: ColorDef]) throws -> FileWrapper { var html = """ diff --git a/Sources/LibMakeColors/MakeColors.swift b/Sources/LibMakeColors/MakeColors.swift index 79e68d3..00bceb1 100644 --- a/Sources/LibMakeColors/MakeColors.swift +++ b/Sources/LibMakeColors/MakeColors.swift @@ -11,7 +11,7 @@ private struct GeneratorOption: EnumerableFlag, CustomStringConvertible { static let allCases: [GeneratorOption] = [ .init(type: AssetCatalogGenerator.self), .init(type: AndroidGenerator.self), - .init(type: HTMLGenerator.self) + .init(type: HTMLGenerator.self), ] static func == (lhs: GeneratorOption, rhs: GeneratorOption) -> Bool { @@ -54,7 +54,11 @@ public final class MakeColors: ParsableCommand, Context { let resolved = try data.resolve(key) switch color { case .color: print(key.insertCamelCaseSeparators(), resolved, separator: ": ") - case .reference(let r): print("\(key.insertCamelCaseSeparators()) (@\(r.insertCamelCaseSeparators()))", resolved, separator: ": ") + case let .reference(r): print( + "\(key.insertCamelCaseSeparators()) (@\(r.insertCamelCaseSeparators()))", + resolved, + separator: ": " + ) } } @@ -73,4 +77,3 @@ public final class MakeColors: ParsableCommand, Context { } } } - diff --git a/Sources/LibMakeColors/Model/Color.swift b/Sources/LibMakeColors/Model/Color.swift index 6c5c641..ee08acf 100644 --- a/Sources/LibMakeColors/Model/Color.swift +++ b/Sources/LibMakeColors/Model/Color.swift @@ -17,7 +17,7 @@ struct Color: CustomStringConvertible, Equatable { } var description: String { - return a != 0xFF ? String(format: "#%02X%02X%02X%02X", r, g, b, a): String(format: "#%02X%02X%02X", r, g, b) + a != 0xFF ? String(format: "#%02X%02X%02X%02X", r, g, b, a) : String(format: "#%02X%02X%02X", r, g, b) } } @@ -37,10 +37,10 @@ extension Dictionary where Key == String, Value == ColorDef { case nil: throw Errors.missingReference(name) - case .color(let color): + case let .color(color): return color - case .reference(let referenced): + case let .reference(referenced): return try resolve(referenced, visited: visited) } } @@ -56,5 +56,4 @@ extension Dictionary where Key == String, Value == ColorDef { case let ((left, _), (right, _)): return left.localizedStandardCompare(right) == .orderedAscending } } - } diff --git a/Sources/LibMakeColors/Model/Parser.swift b/Sources/LibMakeColors/Model/Parser.swift index 390e88c..ae1e86e 100644 --- a/Sources/LibMakeColors/Model/Parser.swift +++ b/Sources/LibMakeColors/Model/Parser.swift @@ -2,7 +2,7 @@ import Foundation private extension CharacterSet { static let hex = CharacterSet(charactersIn: "0123456789abcdef") - static let name = alphanumerics.union(CharacterSet.init(charactersIn: "_/")) + static let name = alphanumerics.union(CharacterSet(charactersIn: "_/")) } private extension Collection { @@ -16,22 +16,21 @@ private extension Collection { } } - extension Scanner { func string(_ s: String) -> Bool { - return scanString(s) != nil + scanString(s) != nil } func color() -> Color? { if string("#"), let digits = scanCharacters(from: .hex) { switch digits.count { - case 3, 4: //rgb(a) + case 3, 4: // rgb(a) let digits = digits.chunks(size: 1) .compactMap { UInt8($0, radix: 16) } .map { $0 << 4 | $0 } return Color(digits) - case 6, 8: //rrggbb(aa) + case 6, 8: // rrggbb(aa) let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) } return Color(digits) @@ -76,9 +75,11 @@ extension Scanner { } func colorLine() -> (String, ColorDef)? { - guard let name = self.name(), - let def = colorDef(), - endOfLine() else { + guard + let name = self.name(), + let def = colorDef(), + endOfLine() + else { return nil } return (name, def) @@ -92,7 +93,6 @@ extension Scanner { return true } - func colorList() throws -> [String: ColorDef] { var result: [String: ColorDef] = [:] while !isAtEnd { @@ -121,5 +121,3 @@ extension Scanner { return result } } - - diff --git a/Tests/MakeColorsTests/ColorParserTest.swift b/Tests/MakeColorsTests/ColorParserTest.swift index 2aa39f9..c072432 100644 --- a/Tests/MakeColorsTests/ColorParserTest.swift +++ b/Tests/MakeColorsTests/ColorParserTest.swift @@ -1,30 +1,29 @@ -import XCTest @testable import LibMakeColors +import XCTest final class ColorParserTest: XCTestCase { func testScanningThreeDigitColor() throws { let scanner = Scanner(string: "#abc") let color = scanner.color() - XCTAssertEqual(Color(r: 0xaa, g: 0xbb, b: 0xcc), color) + XCTAssertEqual(Color(r: 0xAA, g: 0xBB, b: 0xCC), color) } func testScanningFourDigitColor() throws { let scanner = Scanner(string: "#abcd") let color = scanner.color() - XCTAssertEqual(Color(r: 0xaa, g: 0xbb, b: 0xcc, a: 0xDD), color) + XCTAssertEqual(Color(r: 0xAA, g: 0xBB, b: 0xCC, a: 0xDD), color) } - func testScanningSixDigitColor() throws { let scanner = Scanner(string: "#abcdef") let color = scanner.color() - XCTAssertEqual(Color(r: 0xab, g: 0xcd, b: 0xef), color) + XCTAssertEqual(Color(r: 0xAB, g: 0xCD, b: 0xEF), color) } func testScanningEightDigitColor() throws { let scanner = Scanner(string: "#abcdef17") let color = scanner.color() - XCTAssertEqual(Color(r: 0xab, g: 0xcd, b: 0xef, a: 0x17), color) + XCTAssertEqual(Color(r: 0xAB, g: 0xCD, b: 0xEF, a: 0x17), color) } func testScanningRGBColor() throws { @@ -38,5 +37,4 @@ final class ColorParserTest: XCTestCase { let color = scanner.color() XCTAssertEqual(Color(r: 1, g: 2, b: 3, a: 4), color) } - }