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

Commit

Permalink
simplify storyboards template code + remove use of `isBaseViewControl…
Browse files Browse the repository at this point in the history
…ler`. Fixes #8
  • Loading branch information
djbe committed Jan 16, 2017
1 parent b35d425 commit 746f555
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 111 deletions.
28 changes: 10 additions & 18 deletions templates/storyboards-default.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}UI{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import UIKit
{% for module in modules %}
Expand Down Expand Up @@ -53,8 +54,8 @@ enum {{sceneEnumName}} {
{% if storyboard.scenes %}
enum {{storyboardName}}: String, StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand All @@ -67,25 +68,16 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier}}{% endset %}

case {{sceneID}}Scene = "{{scene.identifier}}"
{% if not scene.isBaseViewController %}
{% if scene.customClass %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% if scene.baseType != "ViewController" %}
{% set sceneClass %}{% call className scene %}{% endset %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> UI{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? UI{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class UI{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% else %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> UIViewController {
return {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController()
}
Expand All @@ -95,8 +87,8 @@ enum {{sceneEnumName}} {
{% else %}
enum {{storyboardName}}: StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand Down
28 changes: 10 additions & 18 deletions templates/storyboards-lowercase.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}UI{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import UIKit
{% for module in modules %}
Expand Down Expand Up @@ -53,8 +54,8 @@ enum {{sceneEnumName}} {
{% if storyboard.scenes %}
enum {{storyboardName}}: String, StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand All @@ -67,25 +68,16 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier|lowerFirstWord}}{% endset %}

case {{sceneID}} = "{{scene.identifier}}"
{% if not scene.isBaseViewController %}
{% if scene.customClass %}
static func {{sceneID}}ViewController() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}.viewController() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% if scene.baseType != "ViewController" %}
{% set sceneClass %}{% call className scene %}{% endset %}
static func {{sceneID}}ViewController() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}.viewController() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func {{sceneID}}ViewController() -> UI{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}.viewController() as? UI{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class UI{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% else %}
static func {{sceneID|lowerFirstWord}}ViewController() -> UIViewController {
return {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}.viewController()
}
Expand All @@ -95,8 +87,8 @@ enum {{sceneEnumName}} {
{% else %}
enum {{storyboardName}}: StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand Down
18 changes: 5 additions & 13 deletions templates/storyboards-osx-default.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}NS{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import Cocoa
{% for module in modules %}
Expand Down Expand Up @@ -64,23 +65,14 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier}}{% endset %}

case {{sceneID}}Scene = "{{scene.identifier}}"
{% if scene.customClass %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% set sceneClass %}{% call className scene %}{% endset %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> NS{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? NS{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class NS{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% endfor %}
}
{% else %}
Expand Down
18 changes: 5 additions & 13 deletions templates/storyboards-osx-lowercase.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}NS{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import Cocoa
{% for module in modules %}
Expand Down Expand Up @@ -64,23 +65,14 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier|lowerFirstWord}}{% endset %}

case {{sceneID}}Scene = "{{scene.identifier}}"
{% if scene.customClass %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% set sceneClass %}{% call className scene %}{% endset %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> NS{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? NS{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class NS{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% endfor %}
}
{% else %}
Expand Down
18 changes: 5 additions & 13 deletions templates/storyboards-osx-swift3.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}NS{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import Cocoa
{% for module in modules %}
Expand Down Expand Up @@ -64,23 +65,14 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}{% endset %}

case {{sceneID}}Scene = "{{scene.identifier}}"
{% if scene.customClass %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% set sceneClass %}{% call className scene %}{% endset %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func instantiate{{sceneID||snakeToCamelCase}}() -> NS{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.controller() as? NS{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class NS{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% endfor %}
}
{% else %}
Expand Down
28 changes: 10 additions & 18 deletions templates/storyboards-swift3.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

{% macro className scene %}{% if scene.customClass %}{% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}{% else %}UI{{scene.baseType}}{% endif %}{% endmacro %}
import Foundation
import UIKit
{% for module in modules %}
Expand Down Expand Up @@ -53,8 +54,8 @@ enum {{sceneEnumName}} {
{% if storyboard.scenes %}
enum {{storyboardName}}: String, StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand All @@ -67,25 +68,16 @@ enum {{sceneEnumName}} {
{% set sceneID %}{{scene.identifier|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}{% endset %}

case {{sceneID}}Scene = "{{scene.identifier}}"
{% if not scene.isBaseViewController %}
{% if scene.customClass %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}
{% if scene.baseType != "ViewController" %}
{% set sceneClass %}{% call className scene %}{% endset %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> {{sceneClass}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? {{sceneClass}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {% if scene.customModule %}{{scene.customModule}}.{% endif %}{{scene.customClass}}.")
fatalError("ViewController '{{scene.identifier}}' is not of the expected class {{sceneClass}}.")
}
return vc
}
{% else %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> UI{{scene.baseType}} {
guard let vc = {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController() as? UI{{scene.baseType}}
else {
fatalError("ViewController '{{scene.identifier}}' is not of the expected class UI{{scene.baseType}}.")
}
return vc
}
{% endif %}
{% else %}
static func instantiate{{sceneID|snakeToCamelCase}}() -> UIViewController {
return {{sceneEnumName}}.{{storyboardName}}.{{sceneID}}Scene.viewController()
}
Expand All @@ -95,8 +87,8 @@ enum {{sceneEnumName}} {
{% else %}
enum {{storyboardName}}: StoryboardSceneType {
static let storyboardName = "{{storyboard.name}}"
{% if storyboard.initialScene %}{% if not storyboard.initialScene.isBaseViewController %}
{% set initialSceneClass %}{% if storyboard.initialScene.customClass %}{% if storyboard.initialScene.customModule %}{{storyboard.initialScene.customModule}}.{% endif %}{{storyboard.initialScene.customClass}}{% else %}UI{{storyboard.initialScene.baseType}}{% endif %}{% endset %}
{% if storyboard.initialScene %}{% if storyboard.initialScene.baseType != "ViewController" %}
{% set initialSceneClass %}{% call className storyboard.initialScene %}{% endset %}

static func initialViewController() -> {{initialSceneClass}} {
guard let vc = storyboard().instantiateInitialViewController() as? {{initialSceneClass}} else {
Expand Down
Loading

0 comments on commit 746f555

Please # to comment.