Configure + run SwiftFormat

This commit is contained in:
Sven Weidauer 2020-12-30 12:53:40 +01:00
parent 7d277a0e43
commit 1f6b55014a
10 changed files with 53 additions and 38 deletions

1
.swift-version Normal file
View file

@ -0,0 +1 @@
5.3

14
.swiftformat Normal file
View file

@ -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

View file

@ -15,15 +15,18 @@ let package = Package(
.target( .target(
name: "MakeColors", name: "MakeColors",
dependencies: [ dependencies: [
"LibMakeColors" "LibMakeColors",
]), ]
),
.target( .target(
name: "LibMakeColors", name: "LibMakeColors",
dependencies: [ dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"), .product(name: "ArgumentParser", package: "swift-argument-parser"),
]), ]
),
.testTarget( .testTarget(
name: "MakeColorsTests", name: "MakeColorsTests",
dependencies: ["LibMakeColors"]), dependencies: ["LibMakeColors"]
),
] ]
) )

View file

@ -9,7 +9,7 @@ final class AndroidGenerator: Generator {
self.context = context self.context = context
} }
func generate(data: [String : ColorDef]) throws -> FileWrapper { func generate(data: [String: ColorDef]) throws -> FileWrapper {
var xml = """ var xml = """
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
@ -23,8 +23,8 @@ final class AndroidGenerator: Generator {
let value: String let value: String
switch color { switch color {
case .color(let colorValue): value = colorValue.description case let .color(colorValue): value = colorValue.description
case .reference(let ref): value = "@color/\(prefix)\(ref.camelCasePathToSnakeCase())" case let .reference(ref): value = "@color/\(prefix)\(ref.camelCasePathToSnakeCase())"
} }
xml += """ xml += """

View file

@ -10,8 +10,8 @@ final class AssetCatalogGenerator: Generator {
self.context = context self.context = context
} }
func generate(data: [String : ColorDef]) throws -> FileWrapper { func generate(data: [String: ColorDef]) throws -> FileWrapper {
let root = FileWrapper(directoryWithFileWrappers: ["Contents.json" : FileWrapper(catalog)]) let root = FileWrapper(directoryWithFileWrappers: ["Contents.json": FileWrapper(catalog)])
let colorRoot: FileWrapper let colorRoot: FileWrapper
if let prefix = context.prefix?.insertCamelCaseSeparators() { if let prefix = context.prefix?.insertCamelCaseSeparators() {
@ -53,7 +53,7 @@ final class AssetCatalogGenerator: Generator {
private extension Color { private extension Color {
func json() -> String { func json() -> String {
return """ """
{ {
"colors" : [ "colors" : [
{ {
@ -79,12 +79,11 @@ private extension Color {
func fileWrapper() -> FileWrapper { func fileWrapper() -> FileWrapper {
FileWrapper(directoryWithFileWrappers: [ FileWrapper(directoryWithFileWrappers: [
"Contents.json": FileWrapper(json()) "Contents.json": FileWrapper(json()),
]) ])
} }
} }
private let group = """ private let group = """
{ {
"info" : { "info" : {

View file

@ -8,7 +8,7 @@ final class HTMLGenerator: Generator {
self.context = context self.context = context
} }
func generate(data: [String : ColorDef]) throws -> FileWrapper { func generate(data: [String: ColorDef]) throws -> FileWrapper {
var html = """ var html = """
<html> <html>
<head> <head>

View file

@ -11,7 +11,7 @@ private struct GeneratorOption: EnumerableFlag, CustomStringConvertible {
static let allCases: [GeneratorOption] = [ static let allCases: [GeneratorOption] = [
.init(type: AssetCatalogGenerator.self), .init(type: AssetCatalogGenerator.self),
.init(type: AndroidGenerator.self), .init(type: AndroidGenerator.self),
.init(type: HTMLGenerator.self) .init(type: HTMLGenerator.self),
] ]
static func == (lhs: GeneratorOption, rhs: GeneratorOption) -> Bool { static func == (lhs: GeneratorOption, rhs: GeneratorOption) -> Bool {
@ -54,7 +54,11 @@ public final class MakeColors: ParsableCommand, Context {
let resolved = try data.resolve(key) let resolved = try data.resolve(key)
switch color { switch color {
case .color: print(key.insertCamelCaseSeparators(), resolved, separator: ": ") 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 {
} }
} }
} }

View file

@ -17,7 +17,7 @@ struct Color: CustomStringConvertible, Equatable {
} }
var description: String { 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: case nil:
throw Errors.missingReference(name) throw Errors.missingReference(name)
case .color(let color): case let .color(color):
return color return color
case .reference(let referenced): case let .reference(referenced):
return try resolve(referenced, visited: visited) 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 case let ((left, _), (right, _)): return left.localizedStandardCompare(right) == .orderedAscending
} }
} }
} }

View file

@ -2,7 +2,7 @@ import Foundation
private extension CharacterSet { private extension CharacterSet {
static let hex = CharacterSet(charactersIn: "0123456789abcdef") static let hex = CharacterSet(charactersIn: "0123456789abcdef")
static let name = alphanumerics.union(CharacterSet.init(charactersIn: "_/")) static let name = alphanumerics.union(CharacterSet(charactersIn: "_/"))
} }
private extension Collection { private extension Collection {
@ -16,22 +16,21 @@ private extension Collection {
} }
} }
extension Scanner { extension Scanner {
func string(_ s: String) -> Bool { func string(_ s: String) -> Bool {
return scanString(s) != nil scanString(s) != nil
} }
func color() -> Color? { func color() -> Color? {
if string("#"), let digits = scanCharacters(from: .hex) { if string("#"), let digits = scanCharacters(from: .hex) {
switch digits.count { switch digits.count {
case 3, 4: //rgb(a) case 3, 4: // rgb(a)
let digits = digits.chunks(size: 1) let digits = digits.chunks(size: 1)
.compactMap { UInt8($0, radix: 16) } .compactMap { UInt8($0, radix: 16) }
.map { $0 << 4 | $0 } .map { $0 << 4 | $0 }
return Color(digits) return Color(digits)
case 6, 8: //rrggbb(aa) case 6, 8: // rrggbb(aa)
let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) } let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) }
return Color(digits) return Color(digits)
@ -76,9 +75,11 @@ extension Scanner {
} }
func colorLine() -> (String, ColorDef)? { func colorLine() -> (String, ColorDef)? {
guard let name = self.name(), guard
let def = colorDef(), let name = self.name(),
endOfLine() else { let def = colorDef(),
endOfLine()
else {
return nil return nil
} }
return (name, def) return (name, def)
@ -92,7 +93,6 @@ extension Scanner {
return true return true
} }
func colorList() throws -> [String: ColorDef] { func colorList() throws -> [String: ColorDef] {
var result: [String: ColorDef] = [:] var result: [String: ColorDef] = [:]
while !isAtEnd { while !isAtEnd {
@ -121,5 +121,3 @@ extension Scanner {
return result return result
} }
} }

View file

@ -1,30 +1,29 @@
import XCTest
@testable import LibMakeColors @testable import LibMakeColors
import XCTest
final class ColorParserTest: XCTestCase { final class ColorParserTest: XCTestCase {
func testScanningThreeDigitColor() throws { func testScanningThreeDigitColor() throws {
let scanner = Scanner(string: "#abc") let scanner = Scanner(string: "#abc")
let color = scanner.color() 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 { func testScanningFourDigitColor() throws {
let scanner = Scanner(string: "#abcd") let scanner = Scanner(string: "#abcd")
let color = scanner.color() 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 { func testScanningSixDigitColor() throws {
let scanner = Scanner(string: "#abcdef") let scanner = Scanner(string: "#abcdef")
let color = scanner.color() 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 { func testScanningEightDigitColor() throws {
let scanner = Scanner(string: "#abcdef17") let scanner = Scanner(string: "#abcdef17")
let color = scanner.color() 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 { func testScanningRGBColor() throws {
@ -38,5 +37,4 @@ final class ColorParserTest: XCTestCase {
let color = scanner.color() let color = scanner.color()
XCTAssertEqual(Color(r: 1, g: 2, b: 3, a: 4), color) XCTAssertEqual(Color(r: 1, g: 2, b: 3, a: 4), color)
} }
} }