From 36e80bead792016ac6223847507545c000162458 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Thu, 17 Aug 2017 21:41:32 +0200 Subject: [PATCH 1/7] new templates for swift3/4 + output --- ...s-swift3-context-defaults-customname.swift | 26 +++++++++++ .../literals-swift3-context-defaults.swift | 26 +++++++++++ .../literals-swift3-context-empty.swift | 3 ++ .../literals-swift3-context-multiple.swift | 44 +++++++++++++++++++ ...s-swift4-context-defaults-customname.swift | 26 +++++++++++ .../literals-swift4-context-defaults.swift | 26 +++++++++++ .../literals-swift4-context-empty.swift | 3 ++ .../literals-swift4-context-multiple.swift | 44 +++++++++++++++++++ Tests/TemplatesTests/ColorsTests.swift | 14 ++++++ templates/colors/literals-swift3.stencil | 40 +++++++++++++++++ templates/colors/literals-swift4.stencil | 40 +++++++++++++++++ 11 files changed, 292 insertions(+) create mode 100644 Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift create mode 100644 Tests/Expected/Colors/literals-swift3-context-defaults.swift create mode 100644 Tests/Expected/Colors/literals-swift3-context-empty.swift create mode 100644 Tests/Expected/Colors/literals-swift3-context-multiple.swift create mode 100644 Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift create mode 100644 Tests/Expected/Colors/literals-swift4-context-defaults.swift create mode 100644 Tests/Expected/Colors/literals-swift4-context-empty.swift create mode 100644 Tests/Expected/Colors/literals-swift4-context-multiple.swift create mode 100644 templates/colors/literals-swift3.stencil create mode 100644 templates/colors/literals-swift4.stencil diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift new file mode 100644 index 0000000..0482c58 --- /dev/null +++ b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift @@ -0,0 +1,26 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum XCTColors { } + +// swiftlint:disable identifier_name line_length type_body_length +extension XCTColors { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults.swift b/Tests/Expected/Colors/literals-swift3-context-defaults.swift new file mode 100644 index 0000000..1e15fbd --- /dev/null +++ b/Tests/Expected/Colors/literals-swift3-context-defaults.swift @@ -0,0 +1,26 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum ColorName { } + +// swiftlint:disable identifier_name line_length type_body_length +extension ColorName { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/Expected/Colors/literals-swift3-context-empty.swift b/Tests/Expected/Colors/literals-swift3-context-empty.swift new file mode 100644 index 0000000..129ff35 --- /dev/null +++ b/Tests/Expected/Colors/literals-swift3-context-empty.swift @@ -0,0 +1,3 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +// No color found diff --git a/Tests/Expected/Colors/literals-swift3-context-multiple.swift b/Tests/Expected/Colors/literals-swift3-context-multiple.swift new file mode 100644 index 0000000..9e879ea --- /dev/null +++ b/Tests/Expected/Colors/literals-swift3-context-multiple.swift @@ -0,0 +1,44 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum ColorName { } + +// swiftlint:disable identifier_name line_length type_body_length +extension ColorName { + enum Colors { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + } + enum Extra { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let cyanColor = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let namedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let nestedNamedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift new file mode 100644 index 0000000..0482c58 --- /dev/null +++ b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift @@ -0,0 +1,26 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum XCTColors { } + +// swiftlint:disable identifier_name line_length type_body_length +extension XCTColors { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults.swift b/Tests/Expected/Colors/literals-swift4-context-defaults.swift new file mode 100644 index 0000000..1e15fbd --- /dev/null +++ b/Tests/Expected/Colors/literals-swift4-context-defaults.swift @@ -0,0 +1,26 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum ColorName { } + +// swiftlint:disable identifier_name line_length type_body_length +extension ColorName { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/Expected/Colors/literals-swift4-context-empty.swift b/Tests/Expected/Colors/literals-swift4-context-empty.swift new file mode 100644 index 0000000..129ff35 --- /dev/null +++ b/Tests/Expected/Colors/literals-swift4-context-empty.swift @@ -0,0 +1,3 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +// No color found diff --git a/Tests/Expected/Colors/literals-swift4-context-multiple.swift b/Tests/Expected/Colors/literals-swift4-context-multiple.swift new file mode 100644 index 0000000..9e879ea --- /dev/null +++ b/Tests/Expected/Colors/literals-swift4-context-multiple.swift @@ -0,0 +1,44 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +enum ColorName { } + +// swiftlint:disable identifier_name line_length type_body_length +extension ColorName { + enum Colors { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let `private` = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + } + enum Extra { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0x33fe66ff (r: 20%, g: 99%, b: 40%, a: 100%) + static let articleTitle = #colorLiteral(red: 0.2, green: 0.996078, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + static let cyanColor = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let namedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + static let nestedNamedValue = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8) + /// 0xffffffcc (r: 100%, g: 100%, b: 100%, a: 80%) + 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 diff --git a/Tests/TemplatesTests/ColorsTests.swift b/Tests/TemplatesTests/ColorsTests.swift index 50d97db..f775d83 100644 --- a/Tests/TemplatesTests/ColorsTests.swift +++ b/Tests/TemplatesTests/ColorsTests.swift @@ -45,4 +45,18 @@ class ColorsTests: XCTestCase { directory: .colors, contextVariations: variations) } + + func testLiteralsSwift3() { + test(template: "literals-swift3", + contextNames: Contexts.all, + directory: .colors, + contextVariations: variations) + } + + func testLiteralsSwift4() { + test(template: "literals-swift4", + contextNames: Contexts.all, + directory: .colors, + contextVariations: variations) + } } diff --git a/templates/colors/literals-swift3.stencil b/templates/colors/literals-swift3.stencil new file mode 100644 index 0000000..679c83c --- /dev/null +++ b/templates/colors/literals-swift3.stencil @@ -0,0 +1,40 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +{% if palettes %} +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} +{% if enumName != 'Color' %}enum {{enumName}} { }{% endif %} + +// swiftlint:disable identifier_name line_length type_body_length +extension {{enumName}} { +{% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} +{% macro h2p hex %}{{hex|hexToInt|int255toFloat|percent}}{% endmacro %} +{% macro enumBlock colors sp %} +{{sp}} {% for color in colors %} +{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {% call h2p color.red %}, g: {% call h2p color.green %}, b: {% call h2p color.blue %}, a: {% call h2p color.alpha %}) +{{sp}} static let {{color.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = #colorLiteral(red: {% call h2f color.red %}, green: {% call h2f color.green %}, blue: {% call h2f color.blue %}, alpha: {% call h2f color.alpha %}) +{{sp}} {% endfor %} +{% endmacro %} + {% if palettes.count > 1 %} + {% for palette in palettes %} + enum {{palette.name|swiftIdentifier|titlecase|snakeToCamelCase|escapeReservedKeywords}} { + {% call enumBlock palette.colors " " %} + } + {% endfor %} + {% else %} + {% call enumBlock palettes.first.colors "" %} + {% endif %} +} +// swiftlint:enable identifier_name line_length type_body_length +{% else %} +// No color found +{% endif %} diff --git a/templates/colors/literals-swift4.stencil b/templates/colors/literals-swift4.stencil new file mode 100644 index 0000000..679c83c --- /dev/null +++ b/templates/colors/literals-swift4.stencil @@ -0,0 +1,40 @@ +// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen + +{% if palettes %} +#if os(OSX) + import AppKit.NSColor + typealias Color = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + typealias Color = UIColor +#endif + +// swiftlint:disable file_length + +{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} +{% if enumName != 'Color' %}enum {{enumName}} { }{% endif %} + +// swiftlint:disable identifier_name line_length type_body_length +extension {{enumName}} { +{% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} +{% macro h2p hex %}{{hex|hexToInt|int255toFloat|percent}}{% endmacro %} +{% macro enumBlock colors sp %} +{{sp}} {% for color in colors %} +{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {% call h2p color.red %}, g: {% call h2p color.green %}, b: {% call h2p color.blue %}, a: {% call h2p color.alpha %}) +{{sp}} static let {{color.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = #colorLiteral(red: {% call h2f color.red %}, green: {% call h2f color.green %}, blue: {% call h2f color.blue %}, alpha: {% call h2f color.alpha %}) +{{sp}} {% endfor %} +{% endmacro %} + {% if palettes.count > 1 %} + {% for palette in palettes %} + enum {{palette.name|swiftIdentifier|titlecase|snakeToCamelCase|escapeReservedKeywords}} { + {% call enumBlock palette.colors " " %} + } + {% endfor %} + {% else %} + {% call enumBlock palettes.first.colors "" %} + {% endif %} +} +// swiftlint:enable identifier_name line_length type_body_length +{% else %} +// No color found +{% endif %} From 9740e1f4d7c2cd6fc0718abeb46fb3b841c29b5a Mon Sep 17 00:00:00 2001 From: David Jennes Date: Thu, 17 Aug 2017 21:52:06 +0200 Subject: [PATCH 2/7] Add documentation --- Documentation/colors/literals-swift3.md | 45 +++++++++++++++++++++++++ Documentation/colors/literals-swift4.md | 45 +++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Documentation/colors/literals-swift3.md create mode 100644 Documentation/colors/literals-swift4.md diff --git a/Documentation/colors/literals-swift3.md b/Documentation/colors/literals-swift3.md new file mode 100644 index 0000000..47f6771 --- /dev/null +++ b/Documentation/colors/literals-swift3.md @@ -0,0 +1,45 @@ +## 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 =` + +| Parameter Name | Default Value | Description | +| -------------- | ------------- | ----------- | +| `enumName` | `ColorName` | Allows you to change the name of the generated `enum` containing all colors. | + +## Generated Code + +**Extract:** + +```swift +extension ColorName { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + 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 it's static instance by name: +let title = ColorName.articleBody +let footnote = ColorName.articleFootnote +``` diff --git a/Documentation/colors/literals-swift4.md b/Documentation/colors/literals-swift4.md new file mode 100644 index 0000000..87a677c --- /dev/null +++ b/Documentation/colors/literals-swift4.md @@ -0,0 +1,45 @@ +## 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 =` + +| Parameter Name | Default Value | Description | +| -------------- | ------------- | ----------- | +| `enumName` | `ColorName` | Allows you to change the name of the generated `enum` containing all colors. | + +## Generated Code + +**Extract:** + +```swift +extension ColorName { + /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0) + /// 0xff66ccff (r: 100%, g: 40%, b: 80%, a: 100%) + 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 it's static instance by name: +let title = ColorName.articleBody +let footnote = ColorName.articleFootnote +``` From 6ff27770d6858b0d8b7e0fd578f8805281d64384 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Thu, 17 Aug 2017 21:54:45 +0200 Subject: [PATCH 3/7] Changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20363c0..c63ea45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From bdb5e70af924eab4f43c13957bf528b8ae6396ae Mon Sep 17 00:00:00 2001 From: David Jennes Date: Fri, 18 Aug 2017 02:00:13 +0200 Subject: [PATCH 4/7] color comment: use int instead of percent --- Documentation/colors/literals-swift3.md | 4 ++-- Documentation/colors/literals-swift4.md | 4 ++-- ...s-swift3-context-defaults-customname.swift | 8 +++---- .../literals-swift3-context-defaults.swift | 8 +++---- .../literals-swift3-context-multiple.swift | 22 +++++++++---------- ...s-swift4-context-defaults-customname.swift | 8 +++---- .../literals-swift4-context-defaults.swift | 8 +++---- .../literals-swift4-context-multiple.swift | 22 +++++++++---------- templates/colors/literals-swift3.stencil | 3 +-- templates/colors/literals-swift4.stencil | 3 +-- 10 files changed, 44 insertions(+), 46 deletions(-) diff --git a/Documentation/colors/literals-swift3.md b/Documentation/colors/literals-swift3.md index 47f6771..a3a3d5d 100644 --- a/Documentation/colors/literals-swift3.md +++ b/Documentation/colors/literals-swift3.md @@ -27,9 +27,9 @@ You can customize some elements of this template by overriding the following par ```swift extension ColorName { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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) } ``` diff --git a/Documentation/colors/literals-swift4.md b/Documentation/colors/literals-swift4.md index 87a677c..f6c9dfb 100644 --- a/Documentation/colors/literals-swift4.md +++ b/Documentation/colors/literals-swift4.md @@ -27,9 +27,9 @@ You can customize some elements of this template by overriding the following par ```swift extension ColorName { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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) } ``` diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift index 0482c58..ef882b3 100644 --- a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift @@ -14,13 +14,13 @@ enum XCTColors { } // swiftlint:disable identifier_name line_length type_body_length extension XCTColors { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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 diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults.swift b/Tests/Expected/Colors/literals-swift3-context-defaults.swift index 1e15fbd..4c74614 100644 --- a/Tests/Expected/Colors/literals-swift3-context-defaults.swift +++ b/Tests/Expected/Colors/literals-swift3-context-defaults.swift @@ -14,13 +14,13 @@ enum ColorName { } // swiftlint:disable identifier_name line_length type_body_length extension ColorName { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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 diff --git a/Tests/Expected/Colors/literals-swift3-context-multiple.swift b/Tests/Expected/Colors/literals-swift3-context-multiple.swift index 9e879ea..eb58381 100644 --- a/Tests/Expected/Colors/literals-swift3-context-multiple.swift +++ b/Tests/Expected/Colors/literals-swift3-context-multiple.swift @@ -15,29 +15,29 @@ enum ColorName { } // swiftlint:disable identifier_name line_length type_body_length extension ColorName { enum Colors { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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) } } diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift index 0482c58..ef882b3 100644 --- a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift @@ -14,13 +14,13 @@ enum XCTColors { } // swiftlint:disable identifier_name line_length type_body_length extension XCTColors { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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 diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults.swift b/Tests/Expected/Colors/literals-swift4-context-defaults.swift index 1e15fbd..4c74614 100644 --- a/Tests/Expected/Colors/literals-swift4-context-defaults.swift +++ b/Tests/Expected/Colors/literals-swift4-context-defaults.swift @@ -14,13 +14,13 @@ enum ColorName { } // swiftlint:disable identifier_name line_length type_body_length extension ColorName { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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 diff --git a/Tests/Expected/Colors/literals-swift4-context-multiple.swift b/Tests/Expected/Colors/literals-swift4-context-multiple.swift index 9e879ea..eb58381 100644 --- a/Tests/Expected/Colors/literals-swift4-context-multiple.swift +++ b/Tests/Expected/Colors/literals-swift4-context-multiple.swift @@ -15,29 +15,29 @@ enum ColorName { } // swiftlint:disable identifier_name line_length type_body_length extension ColorName { enum Colors { - /// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 20%, g: 58%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 20%, g: 99%, b: 40%, a: 100%) + /// 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: 100%, g: 40%, b: 80%, a: 100%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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: 100%, g: 100%, b: 100%, a: 80%) + /// 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) } } diff --git a/templates/colors/literals-swift3.stencil b/templates/colors/literals-swift3.stencil index 679c83c..d307a3c 100644 --- a/templates/colors/literals-swift3.stencil +++ b/templates/colors/literals-swift3.stencil @@ -17,10 +17,9 @@ // swiftlint:disable identifier_name line_length type_body_length extension {{enumName}} { {% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} -{% macro h2p hex %}{{hex|hexToInt|int255toFloat|percent}}{% endmacro %} {% macro enumBlock colors sp %} {{sp}} {% for color in colors %} -{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {% call h2p color.red %}, g: {% call h2p color.green %}, b: {% call h2p color.blue %}, a: {% call h2p color.alpha %}) +{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {{color.red|hexToInt}}, g: {{color.green|hexToInt}}, b: {{color.blue|hexToInt}}, a: {{color.alpha|hexToInt}}) {{sp}} static let {{color.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = #colorLiteral(red: {% call h2f color.red %}, green: {% call h2f color.green %}, blue: {% call h2f color.blue %}, alpha: {% call h2f color.alpha %}) {{sp}} {% endfor %} {% endmacro %} diff --git a/templates/colors/literals-swift4.stencil b/templates/colors/literals-swift4.stencil index 679c83c..d307a3c 100644 --- a/templates/colors/literals-swift4.stencil +++ b/templates/colors/literals-swift4.stencil @@ -17,10 +17,9 @@ // swiftlint:disable identifier_name line_length type_body_length extension {{enumName}} { {% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} -{% macro h2p hex %}{{hex|hexToInt|int255toFloat|percent}}{% endmacro %} {% macro enumBlock colors sp %} {{sp}} {% for color in colors %} -{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {% call h2p color.red %}, g: {% call h2p color.green %}, b: {% call h2p color.blue %}, a: {% call h2p color.alpha %}) +{{sp}} /// 0x{{color.red}}{{color.green}}{{color.blue}}{{color.alpha}} (r: {{color.red|hexToInt}}, g: {{color.green|hexToInt}}, b: {{color.blue|hexToInt}}, a: {{color.alpha|hexToInt}}) {{sp}} static let {{color.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = #colorLiteral(red: {% call h2f color.red %}, green: {% call h2f color.green %}, blue: {% call h2f color.blue %}, alpha: {% call h2f color.alpha %}) {{sp}} {% endfor %} {% endmacro %} From 54963d74a4aebb2173374774438c9e62b9d3215e Mon Sep 17 00:00:00 2001 From: David Jennes Date: Fri, 18 Aug 2017 02:00:34 +0200 Subject: [PATCH 5/7] match UIColor/NSColor for enumName --- Documentation/colors/literals-swift3.md | 2 ++ Documentation/colors/literals-swift4.md | 2 ++ .../literals-swift3-context-defaults-customname.swift | 4 ++-- .../Expected/Colors/literals-swift3-context-defaults.swift | 4 ++-- .../Expected/Colors/literals-swift3-context-multiple.swift | 4 ++-- .../literals-swift4-context-defaults-customname.swift | 4 ++-- .../Expected/Colors/literals-swift4-context-defaults.swift | 4 ++-- .../Expected/Colors/literals-swift4-context-multiple.swift | 4 ++-- templates/colors/literals-swift3.stencil | 6 +++--- templates/colors/literals-swift4.stencil | 6 +++--- 10 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Documentation/colors/literals-swift3.md b/Documentation/colors/literals-swift3.md index a3a3d5d..f61ed79 100644 --- a/Documentation/colors/literals-swift3.md +++ b/Documentation/colors/literals-swift3.md @@ -21,6 +21,8 @@ You can customize some elements of this template by overriding the following par | -------------- | ------------- | ----------- | | `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:** diff --git a/Documentation/colors/literals-swift4.md b/Documentation/colors/literals-swift4.md index f6c9dfb..73f3fa4 100644 --- a/Documentation/colors/literals-swift4.md +++ b/Documentation/colors/literals-swift4.md @@ -21,6 +21,8 @@ You can customize some elements of this template by overriding the following par | -------------- | ------------- | ----------- | | `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:** diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift index ef882b3..2608dbe 100644 --- a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift @@ -3,15 +3,15 @@ #if os(OSX) import AppKit.NSColor typealias Color = NSColor + enum XCTColors { } #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor + enum XCTColors { } #endif // swiftlint:disable file_length -enum XCTColors { } - // swiftlint:disable identifier_name line_length type_body_length extension XCTColors { /// 0x339666ff (r: 51, g: 150, b: 102, a: 255) diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults.swift b/Tests/Expected/Colors/literals-swift3-context-defaults.swift index 4c74614..59b7215 100644 --- a/Tests/Expected/Colors/literals-swift3-context-defaults.swift +++ b/Tests/Expected/Colors/literals-swift3-context-defaults.swift @@ -3,15 +3,15 @@ #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 -enum ColorName { } - // swiftlint:disable identifier_name line_length type_body_length extension ColorName { /// 0x339666ff (r: 51, g: 150, b: 102, a: 255) diff --git a/Tests/Expected/Colors/literals-swift3-context-multiple.swift b/Tests/Expected/Colors/literals-swift3-context-multiple.swift index eb58381..43b8715 100644 --- a/Tests/Expected/Colors/literals-swift3-context-multiple.swift +++ b/Tests/Expected/Colors/literals-swift3-context-multiple.swift @@ -3,15 +3,15 @@ #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 -enum ColorName { } - // swiftlint:disable identifier_name line_length type_body_length extension ColorName { enum Colors { diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift index ef882b3..2608dbe 100644 --- a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift @@ -3,15 +3,15 @@ #if os(OSX) import AppKit.NSColor typealias Color = NSColor + enum XCTColors { } #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor + enum XCTColors { } #endif // swiftlint:disable file_length -enum XCTColors { } - // swiftlint:disable identifier_name line_length type_body_length extension XCTColors { /// 0x339666ff (r: 51, g: 150, b: 102, a: 255) diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults.swift b/Tests/Expected/Colors/literals-swift4-context-defaults.swift index 4c74614..59b7215 100644 --- a/Tests/Expected/Colors/literals-swift4-context-defaults.swift +++ b/Tests/Expected/Colors/literals-swift4-context-defaults.swift @@ -3,15 +3,15 @@ #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 -enum ColorName { } - // swiftlint:disable identifier_name line_length type_body_length extension ColorName { /// 0x339666ff (r: 51, g: 150, b: 102, a: 255) diff --git a/Tests/Expected/Colors/literals-swift4-context-multiple.swift b/Tests/Expected/Colors/literals-swift4-context-multiple.swift index eb58381..43b8715 100644 --- a/Tests/Expected/Colors/literals-swift4-context-multiple.swift +++ b/Tests/Expected/Colors/literals-swift4-context-multiple.swift @@ -3,15 +3,15 @@ #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 -enum ColorName { } - // swiftlint:disable identifier_name line_length type_body_length extension ColorName { enum Colors { diff --git a/templates/colors/literals-swift3.stencil b/templates/colors/literals-swift3.stencil index d307a3c..ee1b107 100644 --- a/templates/colors/literals-swift3.stencil +++ b/templates/colors/literals-swift3.stencil @@ -1,19 +1,19 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen {% if palettes %} +{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} #if os(OSX) import AppKit.NSColor typealias Color = NSColor + {% if enumName != 'NSColor' %}enum {{enumName}} { }{% endif %} #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor + {% if enumName != 'UIColor' %}enum {{enumName}} { }{% endif %} #endif // swiftlint:disable file_length -{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} -{% if enumName != 'Color' %}enum {{enumName}} { }{% endif %} - // swiftlint:disable identifier_name line_length type_body_length extension {{enumName}} { {% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} diff --git a/templates/colors/literals-swift4.stencil b/templates/colors/literals-swift4.stencil index d307a3c..ee1b107 100644 --- a/templates/colors/literals-swift4.stencil +++ b/templates/colors/literals-swift4.stencil @@ -1,19 +1,19 @@ // Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen {% if palettes %} +{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} #if os(OSX) import AppKit.NSColor typealias Color = NSColor + {% if enumName != 'NSColor' %}enum {{enumName}} { }{% endif %} #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor + {% if enumName != 'UIColor' %}enum {{enumName}} { }{% endif %} #endif // swiftlint:disable file_length -{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %} -{% if enumName != 'Color' %}enum {{enumName}} { }{% endif %} - // swiftlint:disable identifier_name line_length type_body_length extension {{enumName}} { {% macro h2f hex %}{{hex|hexToInt|int255toFloat}}{% endmacro %} From ccb22df246ab59a1c8ab67018d32a6a815de5272 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sun, 20 Aug 2017 02:38:14 +0200 Subject: [PATCH 6/7] docs typo --- Documentation/colors/literals-swift3.md | 2 +- Documentation/colors/literals-swift4.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/colors/literals-swift3.md b/Documentation/colors/literals-swift3.md index f61ed79..175a8ab 100644 --- a/Documentation/colors/literals-swift3.md +++ b/Documentation/colors/literals-swift3.md @@ -41,7 +41,7 @@ extension ColorName { ## Usage example ```swift -// To reference a color, simpy reference it's static instance by name: +// To reference a color, simpy reference its static instance by name: let title = ColorName.articleBody let footnote = ColorName.articleFootnote ``` diff --git a/Documentation/colors/literals-swift4.md b/Documentation/colors/literals-swift4.md index 73f3fa4..38dab50 100644 --- a/Documentation/colors/literals-swift4.md +++ b/Documentation/colors/literals-swift4.md @@ -41,7 +41,7 @@ extension ColorName { ## Usage example ```swift -// To reference a color, simpy reference it's static instance by name: +// To reference a color, simpy reference its static instance by name: let title = ColorName.articleBody let footnote = ColorName.articleFootnote ``` From 67074d9eb98afc7ddd58e3c10ae0ea7574e33414 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sun, 20 Aug 2017 02:44:44 +0200 Subject: [PATCH 7/7] Test UIColor matching --- ...s-swift3-context-defaults-customname.swift | 5 ++- ...s-swift4-context-defaults-customname.swift | 5 ++- Tests/TemplatesTests/ColorsTests.swift | 32 ++++++++++--------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift index 2608dbe..ef2f48b 100644 --- a/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift3-context-defaults-customname.swift @@ -3,17 +3,16 @@ #if os(OSX) import AppKit.NSColor typealias Color = NSColor - enum XCTColors { } + enum UIColor { } #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor - enum XCTColors { } #endif // swiftlint:disable file_length // swiftlint:disable identifier_name line_length type_body_length -extension XCTColors { +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) diff --git a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift index 2608dbe..ef2f48b 100644 --- a/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift +++ b/Tests/Expected/Colors/literals-swift4-context-defaults-customname.swift @@ -3,17 +3,16 @@ #if os(OSX) import AppKit.NSColor typealias Color = NSColor - enum XCTColors { } + enum UIColor { } #elseif os(iOS) || os(tvOS) || os(watchOS) import UIKit.UIColor typealias Color = UIColor - enum XCTColors { } #endif // swiftlint:disable file_length // swiftlint:disable identifier_name line_length type_body_length -extension XCTColors { +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) diff --git a/Tests/TemplatesTests/ColorsTests.swift b/Tests/TemplatesTests/ColorsTests.swift index f775d83..2daebd5 100644 --- a/Tests/TemplatesTests/ColorsTests.swift +++ b/Tests/TemplatesTests/ColorsTests.swift @@ -13,50 +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) + contextVariations: variations(customName: "UIColor")) } func testLiteralsSwift4() { test(template: "literals-swift4", contextNames: Contexts.all, directory: .colors, - contextVariations: variations) + contextVariations: variations(customName: "UIColor")) } }