Skip to content

Commit

Permalink
now generated files variables are sorted alphabetically, chroma now l…
Browse files Browse the repository at this point in the history
…ooks for existing file and replace it allowing it to run on project root without worrying about where the file is located, updated default generated file name to Colors.swift, clean up package structure & added unit tests
  • Loading branch information
Jota Uribe authored and Jota Uribe committed Jul 12, 2021
1 parent e5c8c79 commit 1d2678d
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 38 deletions.
11 changes: 8 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -10,7 +10,8 @@ let package = Package(
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.executable(name: "Chroma", targets: ["Chroma"])
.executable(name: "Chroma", targets: ["Chroma"]),
.library(name: "ChromaLibrary", targets: ["ChromaLibrary"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -23,12 +24,16 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Chroma",
dependencies: ["ChromaLibrary"]),
.target(
name: "ChromaLibrary",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Files", package: "Files")
]),
.testTarget(
name: "ChromaTests",
dependencies: ["Chroma"]),
dependencies: ["ChromaLibrary"],
resources: [.copy("Resources")]),
]
)
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ A command line tool to generate swift colors definitions from .xcassets files.
```
$ Chroma --help
USAGE: generator [--name <name>] [--output <output>] [--platform <platform>]
USAGE: chroma [--name <name>] [--output <output>] [--platform <platform>]
OPTIONS:
-n, --name <name> Defines the name of the generated file. (default:
Chroma)
Colors)
-o, --output <output> Specifies generated file type.
Supported values: "extension","struct". (default:
extension)
-p, --platform <platform>
Specifies the platform compatibility of the exported
file. (default: iOS)
file.
iOS, macOS, swiftUI (default: iOS)
-h, --help Show help information.
```
### Installation
Expand Down
4 changes: 3 additions & 1 deletion Sources/Chroma/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
// Copyright © 2020 Jota Uribe. All rights reserved.
//

import ChromaLibrary
import Foundation
Generator.main()

Chroma.main()
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
//
// Generator.swift
// Chroma.swift
// Chroma
//
// Created by Jota Uribe on 7/06/20.
// Copyright © 2020 Jota Uribe. All rights reserved.
//

import Foundation
import ArgumentParser
import Files
import Foundation

struct Generator: ParsableCommand {

@Option(name: .shortAndLong, default: "Chroma", help: "Defines the name of the generated file.")
public struct Chroma: ParsableCommand {
@Option(name: .shortAndLong, default: "Colors", help: "Defines the name of the generated file.")
private var name: String

@Option(name: .shortAndLong, default: .extension, help: OutputType.help)
private var output: OutputType

@Option(name: .shortAndLong, default: .iOS, help: "Specifies the platform compatibility of the exported file.")
@Option(name: .shortAndLong, default: .iOS, help: "Specifies the platform compatibility of the exported file.\niOS, macOS, swiftUI")
private var platform: Platform

private var header: String {
Expand All @@ -30,25 +29,27 @@ struct Generator: ParsableCommand {
}
}

func run() throws {
public init() {}

public func run() throws {
generate()
}

}

extension Generator {
extension Chroma {

private func generate() {
let folder = Folder.root
let file = File(named: name, at: folder)
let body = folder.colorDefinitions(for: platform).joined(separator: "\n\n")
let file = folder.files.recursive.first(where: { $0.nameExcludingExtension == name }) ?? File(named: name, at: folder)
let body = folder.colorDefinitions(for: platform).sorted().joined(separator: "\n\n")
let content = platform.fileContent(header: header, body: body)
do {
try file.write(content)
print(
"""
\(file.name) was generated successfully.
Can be found at \(folder.path)
Can be found at \(file.path)
"""
)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ extension File {
}
self = file
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ extension Folder {
return platform.colorVariable(name: colorFolder.nameExcludingExtension)
})
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Output.swift
// OutputType.swift
// Chroma
//
// Created by Jota Uribe on 8/06/20.
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions Tests/ChromaTests/ChromaTests.swift

This file was deleted.

35 changes: 35 additions & 0 deletions Tests/ChromaTests/FolderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import XCTest
import Files
@testable import ChromaLibrary

final class FolderTests: XCTestCase {
func testColorDefinitionsForiOS() throws {
let path = Bundle.module.bundlePath
let folder = try Folder(path: path)
let definitions = folder.colorDefinitions(for: .iOS)
XCTAssertEqual(definitions.count, 2)
XCTAssertEqual(definitions.sorted().first, " static var ExampleColor1: UIColor { return UIColor(named: \"ExampleColor1\")! }")
}

func testColorDefinitionsForMacOS() throws {
let path = Bundle.module.bundlePath
let folder = try Folder(path: path)
let definitions = folder.colorDefinitions(for: .macOS)
XCTAssertEqual(definitions.count, 2)
XCTAssertEqual(definitions.sorted().first, " static var ExampleColor1: NSColor { return NSColor(named: \"ExampleColor1\")! }")
}

func testColorDefinitionsForSwiftUI() throws {
let path = Bundle.module.bundlePath
let folder = try Folder(path: path)
let definitions = folder.colorDefinitions(for: .swiftUI)
XCTAssertEqual(definitions.count, 2)
XCTAssertEqual(definitions.sorted().first, " static var ExampleColor1: Color { return Color(\"ExampleColor1\") }")
}

static var allTests = [
("testColorDefinitionsForiOS", testColorDefinitionsForiOS),
("testColorDefinitionsForMacOS", testColorDefinitionsForMacOS),
("testColorDefinitionsForSwiftUI", testColorDefinitionsForSwiftUI)
]
}
26 changes: 26 additions & 0 deletions Tests/ChromaTests/PlatformTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import XCTest
import Files
@testable import ChromaLibrary

final class PlatformTests: XCTestCase {
func testColorVariableForiOS() throws {
let variable = Platform.iOS.colorVariable(name: "ExampleColor1")
XCTAssertEqual(variable, " static var ExampleColor1: UIColor { return UIColor(named: \"ExampleColor1\")! }")
}

func testColorVariableForMacOS() throws {
let variable = Platform.macOS.colorVariable(name: "ExampleColor1")
XCTAssertEqual(variable, " static var ExampleColor1: NSColor { return NSColor(named: \"ExampleColor1\")! }")
}

func testColorVariableForSwiftUI() throws {
let variable = Platform.swiftUI.colorVariable(name: "ExampleColor1")
XCTAssertEqual(variable, " static var ExampleColor1: Color { return Color(\"ExampleColor1\") }")
}

static var allTests = [
("testColorVariableForiOS", testColorVariableForiOS),
("testColorVariableForMacOS", testColorVariableForMacOS),
("testColorVariableForSwiftUI", testColorVariableForSwiftUI)
]
}
6 changes: 6 additions & 0 deletions Tests/ChromaTests/Resources/Tests.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.000",
"green" : "0.000",
"red" : "0.000"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000",
"red" : "1.000"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.000",
"green" : "0.000",
"red" : "0.000"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000",
"red" : "1.000"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
3 changes: 2 additions & 1 deletion Tests/ChromaTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import XCTest
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(ChromaTests.allTests),
testCase(FolderTests.allTests),
testCase(PlatformTests.allTests)
]
}
#endif

0 comments on commit 1d2678d

Please sign in to comment.