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(
name: "MakeColors",
dependencies: [
"LibMakeColors"
]),
"LibMakeColors",
]
),
.target(
name: "LibMakeColors",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]),
]
),
.testTarget(
name: "MakeColorsTests",
dependencies: ["LibMakeColors"]),
dependencies: ["LibMakeColors"]
),
]
)

View file

@ -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 = """
<?xml version="1.0" encoding="utf-8"?>
<resources>
@ -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 += """

View file

@ -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" : {

View file

@ -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 = """
<html>
<head>

View file

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

View file

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

View file

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

View file

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