Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #73 from SwiftGen/feature/color-literals
Browse files Browse the repository at this point in the history
Color template with literals
  • Loading branch information
djbe authored Aug 20, 2017
2 parents 52c4ff8 + 67074d9 commit 154dd2c
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ _None_
* Fonts: the path to fonts will now default to just the font filename, but you can disable this behaviour by enabling the `preservePath` parameter.
[David Jennes](https://github.com/djbe)
[#71](https://github.com/SwiftGen/templates/pull/71)
* Colors: new template that uses `#colorLiteral`s.
[David Jennes](https://github.com/djbe)
[#72](https://github.com/SwiftGen/templates/pull/72)

### Internal Changes

Expand Down
47 changes: 47 additions & 0 deletions Documentation/colors/literals-swift3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## Template Information

| Name | Description |
| --------- | ----------------- |
| File name | colors/literals-swift3.stencil |
| Invocation example | `swiftgen colors -t literals-swift3 …` |
| Language | Swift 3 |
| Author | Olivier Halligon |

## When to use it

- When you need to generate *Swift 3* code
- Supports _multiple_ color names with the _same_ value
- Uses `#colorLiteral`s for easy preview of the actual color

## Customization

You can customize some elements of this template by overriding the following parameters when invoking `swiftgen` in the command line, using `--param <paramName>=<newValue>`

| Parameter Name | Default Value | Description |
| -------------- | ------------- | ----------- |
| `enumName` | `ColorName` | Allows you to change the name of the generated `enum` containing all colors. |

Note: if you use `--param enumName=UIColor` (or `NSColor` on macOS) then the color constants will be generated as an extension of the `UIColor` (iOS) / `NSColor` (macOS) type directly without creating a separate `enum` type for namespacing those color constants.

## Generated Code

**Extract:**

```swift
extension ColorName {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
}
```

[Full generated code](https://github.com/SwiftGen/templates/blob/master/Tests/Expected/Colors/literals-swift3-context-defaults.swift)

## Usage example

```swift
// To reference a color, simpy reference its static instance by name:
let title = ColorName.articleBody
let footnote = ColorName.articleFootnote
```
47 changes: 47 additions & 0 deletions Documentation/colors/literals-swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## Template Information

| Name | Description |
| --------- | ----------------- |
| File name | colors/literals-swift4.stencil |
| Invocation example | `swiftgen colors -t literals-swift4 …` |
| Language | Swift 4 |
| Author | Olivier Halligon |

## When to use it

- When you need to generate *Swift 4* code
- Supports _multiple_ color names with the _same_ value
- Uses `#colorLiteral`s for easy preview of the actual color

## Customization

You can customize some elements of this template by overriding the following parameters when invoking `swiftgen` in the command line, using `--param <paramName>=<newValue>`

| Parameter Name | Default Value | Description |
| -------------- | ------------- | ----------- |
| `enumName` | `ColorName` | Allows you to change the name of the generated `enum` containing all colors. |

Note: if you use `--param enumName=UIColor` (or `NSColor` on macOS) then the color constants will be generated as an extension of the `UIColor` (iOS) / `NSColor` (macOS) type directly without creating a separate `enum` type for namespacing those color constants.

## Generated Code

**Extract:**

```swift
extension ColorName {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
}
```

[Full generated code](https://github.com/SwiftGen/templates/blob/master/Tests/Expected/Colors/literals-swift4-context-defaults.swift)

## Usage example

```swift
// To reference a color, simpy reference its static instance by name:
let title = ColorName.articleBody
let footnote = ColorName.articleFootnote
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum UIColor { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension UIColor {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
// swiftlint:enable identifier_name line_length type_body_length
26 changes: 26 additions & 0 deletions Tests/Expected/Colors/literals-swift3-context-defaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum ColorName { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
enum ColorName { }
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension ColorName {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
// swiftlint:enable identifier_name line_length type_body_length
3 changes: 3 additions & 0 deletions Tests/Expected/Colors/literals-swift3-context-empty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

// No color found
44 changes: 44 additions & 0 deletions Tests/Expected/Colors/literals-swift3-context-multiple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum ColorName { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
enum ColorName { }
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension ColorName {
enum Colors {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
enum Extra {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let cyanColor = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let namedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let nestedNamedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
}
// swiftlint:enable identifier_name line_length type_body_length
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum UIColor { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension UIColor {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
// swiftlint:enable identifier_name line_length type_body_length
26 changes: 26 additions & 0 deletions Tests/Expected/Colors/literals-swift4-context-defaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum ColorName { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
enum ColorName { }
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension ColorName {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
// swiftlint:enable identifier_name line_length type_body_length
3 changes: 3 additions & 0 deletions Tests/Expected/Colors/literals-swift4-context-empty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

// No color found
44 changes: 44 additions & 0 deletions Tests/Expected/Colors/literals-swift4-context-multiple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen

#if os(OSX)
import AppKit.NSColor
typealias Color = NSColor
enum ColorName { }
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIColor
typealias Color = UIColor
enum ColorName { }
#endif

// swiftlint:disable file_length

// swiftlint:disable identifier_name line_length type_body_length
extension ColorName {
enum Colors {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
enum Extra {
/// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0x33fe66ff (r: 51, g: 254, b: 102, a: 255)
static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0)
/// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
static let cyanColor = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let namedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let nestedNamedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
/// 0xffffffcc (r: 255, g: 255, b: 255, a: 204)
static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)
}
}
// swiftlint:enable identifier_name line_length type_body_length
42 changes: 29 additions & 13 deletions Tests/TemplatesTests/ColorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,52 @@ class ColorsTests: XCTestCase {
}

// generate variations to test customname generation
let variations: VariationGenerator = { name, context in
guard name == "defaults" else { return [(context: context, suffix: "")] }

return [
(context: context,
suffix: ""),
(context: try StencilContext.enrich(context: context,
parameters: ["enumName=XCTColors"]),
suffix: "-customname")
]
func variations(customName: String) -> VariationGenerator {
return { name, context in
guard name == "defaults" else { return [(context: context, suffix: "")] }

return [
(context: context,
suffix: ""),
(context: try StencilContext.enrich(context: context,
parameters: ["enumName=\(customName)"]),
suffix: "-customname")
]
}
}

func testSwift2() {
test(template: "swift2",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations)
contextVariations: variations(customName: "XCTColors"))
}

func testSwift3() {
test(template: "swift3",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations)
contextVariations: variations(customName: "XCTColors"))
}

func testSwift4() {
test(template: "swift4",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations)
contextVariations: variations(customName: "XCTColors"))
}

func testLiteralsSwift3() {
test(template: "literals-swift3",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations(customName: "UIColor"))
}

func testLiteralsSwift4() {
test(template: "literals-swift4",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations(customName: "UIColor"))
}
}
Loading

0 comments on commit 154dd2c

Please sign in to comment.