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 #67 from SwiftGen/feature/swift4-templates
Browse files Browse the repository at this point in the history
Swift 4 templates
  • Loading branch information
djbe authored Aug 13, 2017
2 parents f94c2ab + 8a524ca commit 42d343c
Show file tree
Hide file tree
Showing 52 changed files with 2,777 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ _None_

### New Features

_None_
* Swift 4 templates.
[David Jennes](https://github.com/djbe)
[#67](https://github.com/SwiftGen/templates/pull/67)

### Internal Changes

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

| Name | Description |
| --------- | ----------------- |
| File name | colors/swift4.stencil |
| Invocation example | `swiftgen colors -t 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

## 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. |

## Generated Code

**Extract:**

```swift
struct ColorName {
let rgbaValue: UInt32
var color: Color { return Color(named: self) }

/// <span style="display:block;width:3em;height:2em;border:1px solid black;background:#339666"></span>
/// Alpha: 100% <br/> (0x339666ff)
static let articleBody = ColorName(rgbaValue: 0x339666ff)
/// <span style="display:block;width:3em;height:2em;border:1px solid black;background:#ff66cc"></span>
/// Alpha: 100% <br/> (0xff66ccff)
static let articleFootnote = ColorName(rgbaValue: 0xff66ccff)
}
```

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

## Usage example

```swift
// You can create colors with the convenience constructor like this:
let title = UIColor(named: .articleBody)
let footnote = UIColor(named: .articleFootnote)

// Or as an alternative, you can refer to enum instance and call .color on it:
let sameTitle = ColorName.articleBody.color
let sameFootnote = ColorName.articleFootnote.color
```
52 changes: 52 additions & 0 deletions Documentation/fonts/swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Template Information

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

## When to use it

- When you need to generate *Swift 4* code

## 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` | `FontFamily` | Allows you to change the name of the generated `enum` containing all font families. |

## Generated Code

**Extract:**

```swift
enum FontFamily {
enum SFNSDisplay {
static let black = FontConvertible(name: ".SFNSDisplay-Black", family: ".SF NS Display", path: "SFNSDisplay-Black.otf")
static let bold = FontConvertible(name: ".SFNSDisplay-Bold", family: ".SF NS Display", path: "SFNSDisplay-Bold.otf")
static let heavy = FontConvertible(name: ".SFNSDisplay-Heavy", family: ".SF NS Display", path: "SFNSDisplay-Heavy.otf")
static let regular = FontConvertible(name: ".SFNSDisplay-Regular", family: ".SF NS Display", path: "SFNSDisplay-Regular.otf")
}
enum ZapfDingbats {
static let regular = FontConvertible(name: "ZapfDingbatsITC", family: "Zapf Dingbats", path: "ZapfDingbats.ttf")
}
}
```

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

## Usage example

```swift
// You can create fonts with the convenience constructor like this:
let displayRegular = UIFont(font: FontFamily.SFNSDisplay.regular, size: 20.0)
let dingbats = UIFont(font: FontFamily.ZapfDingbats.regular, size: 20.0)

// Or as an alternative, you can refer to enum instance and call .font on it:
let sameDisplayRegular = FontFamily.SFNSDisplay.regular.font(size: 20.0)
let sameDingbats = FontFamily.ZapfDingbats.regular.font(size: 20.0)
```
72 changes: 72 additions & 0 deletions Documentation/storyboards/swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## Template Information

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

## When to use it

- When you need to generate *Swift 4* code
- The generated code supports both UIKit platforms (iOS, tvOS and watchOS) and AppKit platform (macOS)

## 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 |
| -------------- | ------------- | ----------- |
| `sceneEnumName` | `StoryboardScene` | Allows you to change the name of the generated `enum` containing all storyboard scenes. |
| `segueEnumName` | `StoryboardSegue` | Allows you to change the name of the generated `enum` containing all storyboard segues. |
| `module` | N/A | By default, the template will import the needed modules for custom classes, but won’t import the target’s module to avoid an import warning — using the `PRODUCT_MODULE_NAME` environment variable to detect it. Should you need to ignore an additional module, you can provide it here. |

## Generated Code

Note: the generated code may look differently depending on the platform the storyboard file is targeting.

**Extract:**

```swift
enum StoryboardScene {
enum Dependency: StoryboardType {
static let storyboardName = "Dependency"

static let dependent = SceneType<UIViewController>(storyboard: Dependency.self, identifier: "Dependent")
}
enum Message: StoryboardType {
static let storyboardName = "Message"

static let messagesList = SceneType<UITableViewController>(storyboard: Message.self, identifier: "MessagesList")
}
}
enum StoryboardSegue {
enum Message: String, SegueType {
case embed
case nonCustom
}
}
```

[Full generated code](https://github.com/SwiftGen/templates/blob/master/Tests/Expected/Storyboards-iOS/swift4-context-all.swift)

## Usage example

```swift
// You can instantiate scenes using the `instantiate` method:
let vc = StoryboardScene.Dependency.dependent.instantiate()

// You can perform segues using:
vc.perform(segue: StoryboardSegue.Message.embed)

// or match them (in prepareForSegue):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch StoryboardSegue.Message(rawValue: segue.identifier!)! {
case .embed:
// Prepare for your custom segue transition
case .nonCustom:
// Pass in information to the destination View Controller
}
}
```
53 changes: 53 additions & 0 deletions Documentation/strings/flat-swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Template Information

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

## When to use it

- When you need to generate *Swift 4* code
- If you use unstructured key names for your strings, or a structure that we don't support (yet). If you use "dot-syntax" keys, please check out the [dot-syntax](dot-syntax.md) template.

## 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` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |

## Generated Code

**Extract:**

```swift
enum L10n {
/// Some alert body there
case alertMessage
/// Title of the alert
case alertTitle
/// You have %d apples
case applesCount(Int)
/// Those %d bananas belong to %@.
case bananasOwner(Int, String)
}
```

[Full generated code](https://github.com/SwiftGen/templates/blob/master/Tests/Expected/Strings/flat-swift4-context-localizable.swift)

## Usage example

```swift
// Simple strings
let message = L10n.alertMessage
let title = L10n.alertTitle

// with parameters, note that each argument needs to be of the correct type
let apples = L10n.applesCount(3)
let bananas = L10n.bananasOwner(5, "Olivier")
```
69 changes: 69 additions & 0 deletions Documentation/strings/structured-swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Template Information

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

## When to use it

- When you need to generate *Swift 4* code
- If you use "structured" keys for your strings, that is components separated by the `.` character, for example:

```
"some.deep.structure"
"some.deep.something"
"hello.world"
```

## 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` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |

## Generated Code

**Extract:**

```swift
enum L10n {
/// Some alert body there
static let alertMessage = L10n.tr("alert_message")
/// Title of the alert
static let alertTitle = L10n.tr("alert_title")

enum Apples {
/// You have %d apples
static func count(_ p1: Int) -> String {
return L10n.tr("apples.count", p1)
}
}

enum Bananas {
/// Those %d bananas belong to %@.
static func owner(_ p1: Int, _ p2: String) -> String {
return L10n.tr("bananas.owner", p1, p2)
}
}
}
```

[Full generated code](https://github.com/SwiftGen/templates/blob/master/Tests/Expected/Strings/structured-swift4-context-localizable.swift)

## Usage example

```swift
// Simple strings
let message = L10n.alertMessage
let title = L10n.alertTitle

// with parameters, note that each argument needs to be of the correct type
let apples = L10n.Apples.count(3)
let bananas = L10n.Bananas.owner(5, "Olivier")
```
51 changes: 51 additions & 0 deletions Documentation/xcassets/swift4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## Template Information

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

## When to use it

- When you need to generate *Swift 4* code

It also takes into account any namespacing folder in your Assets Catalogs (i.e. if you create a folder in your Assets Catalog, select it, and check the "Provides Namespace" checkbox on the Attributes Inspector panel on the right)

## 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` | `Asset` | Allows you to change the name of the generated `enum` containing all assets. |
| `noAllValues` | N/A | Setting this parameter will disable generation of the `allValues` constant. |

## Generated Code

**Extract:**

```swift
enum Asset {
enum Exotic {
static let banana: AssetType = "Exotic/Banana"
static let mango: AssetType = "Exotic/Mango"
}
static let `private`: AssetType = "private"
}
```

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

## Usage example

```swift
// You can create new images with the convenience constructor like this:
let bananaImage = UIImage(asset: Asset.Exotic.banana)
let privateImage = UIImage(asset: Asset.private)

// Or as an alternative, you can refer to enum instance and call .image on it:
let sameBananaImage = Asset.Exotic.banana.image
let samePrivateImage = Asset.private.image
```
Empty file.
Loading

0 comments on commit 42d343c

Please sign in to comment.