This repository has been archived by the owner on Sep 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from SwiftGen/feature/split-color-parser
Split colors parser into sub-parsers & support multiple palettes (files)
- Loading branch information
Showing
18 changed files
with
556 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Sources/Parsers/ColorsFileParsers/ColorsCLRFileParser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// SwiftGenKit | ||
// Copyright (c) 2017 SwiftGen | ||
// MIT Licence | ||
// | ||
|
||
import Foundation | ||
import PathKit | ||
|
||
final class ColorsCLRFileParser: ColorsFileTypeParser { | ||
static let extensions = ["clr"] | ||
|
||
private enum Keys { | ||
static let userColors = "UserColors" | ||
} | ||
|
||
func parseFile(at path: Path) throws -> Palette { | ||
if let colorsList = NSColorList(name: Keys.userColors, fromFile: path.string) { | ||
var colors = [String: UInt32]() | ||
|
||
for colorName in colorsList.allKeys { | ||
colors[colorName] = colorsList.color(withKey: colorName)?.hexValue | ||
} | ||
|
||
let name = path.lastComponentWithoutExtension | ||
return Palette(name: name, colors: colors) | ||
} else { | ||
throw ColorsParserError.invalidFile(path: path, reason: "Invalid color list") | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Sources/Parsers/ColorsFileParsers/ColorsJSONFileParser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// SwiftGenKit | ||
// Copyright (c) 2017 SwiftGen | ||
// MIT Licence | ||
// | ||
|
||
import Foundation | ||
import PathKit | ||
|
||
final class ColorsJSONFileParser: ColorsFileTypeParser { | ||
static let extensions = ["json"] | ||
|
||
func parseFile(at path: Path) throws -> Palette { | ||
do { | ||
let json = try JSONSerialization.jsonObject(with: try path.read(), options: []) | ||
guard let dict = json as? [String: String] else { | ||
throw ColorsParserError.invalidFile(path: path, | ||
reason: "Invalid structure, must be an object with string values.") | ||
} | ||
|
||
var colors = [String: UInt32]() | ||
for (key, value) in dict { | ||
colors[key] = try parse(hex: value, key: key, path: path) | ||
} | ||
|
||
let name = path.lastComponentWithoutExtension | ||
return Palette(name: name, colors: colors) | ||
} catch let error as ColorsParserError { | ||
throw error | ||
} catch let error { | ||
throw ColorsParserError.invalidFile(path: path, reason: error.localizedDescription) | ||
} | ||
} | ||
} |
Oops, something went wrong.