SwiftLint

This commit is contained in:
Sven Weidauer 2020-12-30 13:35:21 +01:00
parent 1f6b55014a
commit 2b94443b56
9 changed files with 320 additions and 94 deletions

View file

@ -23,8 +23,11 @@ final class AndroidGenerator: Generator {
let value: String
switch color {
case let .color(colorValue): value = colorValue.description
case let .reference(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

@ -60,10 +60,10 @@ private extension Color {
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "\(Float(a) / 256)",
"blue" : "0x\(String(b, radix: 16))",
"green" : "0x\(String(g, radix: 16))",
"red" : "0x\(String(r, radix: 16))"
"alpha" : "\(Float(alpha) / 256)",
"blue" : "0x\(String(blue, radix: 16))",
"green" : "0x\(String(green, radix: 16))",
"red" : "0x\(String(red, radix: 16))"
}
},
"idiom" : "universal"

View file

@ -2,6 +2,45 @@ import Foundation
final class HTMLGenerator: Generator {
static let defaultExtension = "html"
static let head = """
<html>
<head>
<style type="text/css">
.checkered {
padding: 5px;
margin: 5px;
background-image:
linear-gradient(45deg, #000 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #000 75%),
linear-gradient(45deg, transparent 75%, #000 75%),
linear-gradient(45deg, #000 25%, transparent 25%);
background-size:30px 30px;
background-position:0 0, 0 0, -15px -15px, 15px 15px;
}
.swatch {
width:50px;
height:50px;
display:inline-block;
}
</style>
<body>
<table>
<thead>
<tr>
<th>&nbsp;</th>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
"""
let context: Context
init(context: Context) {
@ -9,52 +48,27 @@ final class HTMLGenerator: Generator {
}
func generate(data: [String: ColorDef]) throws -> FileWrapper {
var html = """
<html>
<head>
<style type="text/css">
.checkered {
padding: 5px;
margin: 5px;
background-image:
linear-gradient(45deg, #000 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #000 75%),
linear-gradient(45deg, transparent 75%, #000 75%),
linear-gradient(45deg, #000 25%, transparent 25%);
background-size:30px 30px;
background-position:0 0, 0 0, -15px -15px, 15px 15px;
}
</style>
<body>
<table>
<thead>
<tr>
<th>&nbsp;</th>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
"""
var html = Self.head
for (key, color) in data.sorted() {
let actualColor = try data.resolve(key)
let value: String
switch color {
case let .reference(name): value = """
case let .reference(name):
value = """
<a href="#cref/\(name)">\(name.insertCamelCaseSeparators())</a><br>\(actualColor)
"""
case .color: value = actualColor.description
case .color:
value = actualColor.description
}
html += """
<tr>
<td class="checkered" id="cref/\(key)"><span style="background:\(actualColor); width:50px; height:50px;display:inline-block;">&nbsp;&nbsp;</span></td>
<td class="checkered" id="cref/\(key)">
<span style="background:\(actualColor)" class="swatch">&nbsp;</span>
</td>
<td>\(key.insertCamelCaseSeparators())</td>
<td>\(value)</td>
</tr>

View file

@ -2,18 +2,18 @@ import ArgumentParser
import Foundation
private struct GeneratorOption: EnumerableFlag, CustomStringConvertible {
let type: Generator.Type
var description: String {
type.option
}
static let allCases: [GeneratorOption] = [
.init(type: AssetCatalogGenerator.self),
.init(type: AndroidGenerator.self),
.init(type: HTMLGenerator.self),
]
let type: Generator.Type
var description: String {
type.option
}
static func == (lhs: GeneratorOption, rhs: GeneratorOption) -> Bool {
lhs.type == rhs.type
}
@ -53,9 +53,12 @@ public final class MakeColors: ParsableCommand, Context {
for (key, color) in data.sorted() {
let resolved = try data.resolve(key)
switch color {
case .color: print(key.insertCamelCaseSeparators(), resolved, separator: ": ")
case let .reference(r): print(
"\(key.insertCamelCaseSeparators()) (@\(r.insertCamelCaseSeparators()))",
case .color:
print(key.insertCamelCaseSeparators(), resolved, separator: ": ")
case let .reference(referenced):
print(
"\(key.insertCamelCaseSeparators()) (@\(referenced.insertCamelCaseSeparators()))",
resolved,
separator: ": "
)

View file

@ -1,23 +1,27 @@
struct Color: CustomStringConvertible, Equatable {
let r, g, b, a: UInt8
let red: UInt8
let green: UInt8
let blue: UInt8
let alpha: UInt8
init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 0xFF) {
self.r = r
self.g = g
self.b = b
self.a = a
init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8 = 0xFF) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
init?(_ array: [UInt8]) {
guard array.count >= 3 else { return nil }
r = array[0]
g = array[1]
b = array[2]
a = array.count >= 4 ? array[3] : 0xFF
init(_ array: [UInt8]) {
precondition(array.count == 3 || array.count == 4)
red = array[0]
green = array[1]
blue = array[2]
alpha = array.count == 4 ? array[3] : 0xFF
}
var description: String {
a != 0xFF ? String(format: "#%02X%02X%02X%02X", r, g, b, a) : String(format: "#%02X%02X%02X", r, g, b)
let alphaSuffix = alpha != 0xFF ? String(format: "%02X", alpha) : ""
return String(format: "#%02X%02X%02X%@", red, green, blue, alphaSuffix)
}
}
@ -49,11 +53,16 @@ extension Dictionary where Key == String, Value == ColorDef {
sorted(by: Self.compare)
}
static func compare(_ a: (String, ColorDef), _ b: (String, ColorDef)) -> Bool {
switch (a, b) {
case ((_, .color), (_, .reference)): return true
case ((_, .reference), (_, .color)): return false
case let ((left, _), (right, _)): return left.localizedStandardCompare(right) == .orderedAscending
static func compare(_ lhs: (String, ColorDef), _ rhs: (String, ColorDef)) -> Bool {
switch (lhs, rhs) {
case ((_, .color), (_, .reference)):
return true
case ((_, .reference), (_, .color)):
return false
case let ((left, _), (right, _)):
return left.localizedStandardCompare(right) == .orderedAscending
}
}
}

View file

@ -1,26 +1,6 @@
import Foundation
private extension CharacterSet {
static let hex = CharacterSet(charactersIn: "0123456789abcdef")
static let name = alphanumerics.union(CharacterSet(charactersIn: "_/"))
}
private extension Collection {
func chunks(size: Int) -> UnfoldSequence<Self.SubSequence, Self.Index> {
sequence(state: startIndex) { state -> SubSequence? in
guard state != endIndex else { return nil }
let next = index(state, offsetBy: size, limitedBy: endIndex) ?? endIndex
defer { state = next }
return self[state..<next]
}
}
}
extension Scanner {
func string(_ s: String) -> Bool {
scanString(s) != nil
}
func color() -> Color? {
if string("#"), let digits = scanCharacters(from: .hex) {
switch digits.count {
@ -34,7 +14,8 @@ extension Scanner {
let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) }
return Color(digits)
default: return nil
default:
return nil
}
}
@ -110,6 +91,7 @@ extension Scanner {
return result
}
// swiftlint:disable:next discouraged_optional_collection
func commaSeparated() -> [UInt8]? {
var result: [UInt8] = []
repeat {
@ -121,3 +103,25 @@ extension Scanner {
return result
}
}
private extension Scanner {
func string(_ string: String) -> Bool {
scanString(string) != nil
}
}
private extension CharacterSet {
static let hex = CharacterSet(charactersIn: "0123456789abcdef")
static let name = alphanumerics.union(CharacterSet(charactersIn: "_/"))
}
private extension Collection {
func chunks(size: Int) -> UnfoldSequence<Self.SubSequence, Self.Index> {
sequence(state: startIndex) { state -> SubSequence? in
guard state != endIndex else { return nil }
let next = index(state, offsetBy: size, limitedBy: endIndex) ?? endIndex
defer { state = next }
return self[state..<next]
}
}
}