Skip to content
This repository was archived by the owner on Sep 6, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sources/Parsers/StoryboardParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ public final class StoryboardParser {
let customModule: String?
}

struct Cell {
let identifier: String
let customClass: String?
let customModule: String?
}

var initialScenes = [String: InitialScene]()
var storyboardsScenes = [String: Set<Scene>]()
var storyboardsSegues = [String: Set<Segue>]()
var storyboardsCells = [String: Set<Cell>]()
var modules = Set<String>()

public init() {}
Expand All @@ -40,10 +47,13 @@ public final class StoryboardParser {
var initialScene: InitialScene?
var scenes = Set<Scene>()
var segues = Set<Segue>()
var cells = Set<Cell>()
var inScene = false
var readyForFirstObject = false
var readyForConnections = false
var readyForTableView = false

// swiftlint:disable cyclomatic_complexity
@objc func parser(_ parser: XMLParser, didStartElement elementName: String,
namespaceURI: String?, qualifiedName qName: String?,
attributes attributeDict: [String: String]) {
Expand Down Expand Up @@ -71,6 +81,14 @@ public final class StoryboardParser {
customModule: customModule))
}
readyForFirstObject = false
case "tableView":
readyForTableView = true
case "tableViewCell" where readyForTableView:
if let reuseIdentifier = attributeDict["reuseIdentifier"] {
let customClass = attributeDict["customClass"]
let customModule = attributeDict["customModule"]
cells.insert(Cell(identifier: reuseIdentifier, customClass: customClass, customModule: customModule))
}
case "connections":
readyForConnections = true
case "segue" where readyForConnections:
Expand All @@ -83,6 +101,7 @@ public final class StoryboardParser {
break
}
}
// swiftlint:enable cyclomatic_complexity

@objc func parser(_ parser: XMLParser, didEndElement elementName: String,
namespaceURI: String?, qualifiedName qName: String?) {
Expand All @@ -91,6 +110,8 @@ public final class StoryboardParser {
inScene = false
case "objects" where inScene:
readyForFirstObject = false
case "tableView":
readyForTableView = false
case "connections":
readyForConnections = false
default:
Expand All @@ -110,6 +131,7 @@ public final class StoryboardParser {
initialScenes[storyboardName] = delegate.initialScene
storyboardsScenes[storyboardName] = delegate.scenes
storyboardsSegues[storyboardName] = delegate.segues
storyboardsCells[storyboardName] = delegate.cells

modules.formUnion(collectModules(initial: delegate.initialScene, scenes: delegate.scenes, segues: delegate.segues))
}
Expand Down Expand Up @@ -163,3 +185,18 @@ extension StoryboardParser.Segue: Hashable {
return identifier.hashValue ^ (customModule?.hashValue ?? 0) ^ (customClass?.hashValue ?? 0)
}
}

extension StoryboardParser.Cell: Equatable { }
func == (lhs: StoryboardParser.Cell, rhs: StoryboardParser.Cell) -> Bool {
return lhs.identifier == rhs.identifier &&
lhs.customClass == rhs.customClass &&
lhs.customModule == rhs.customModule
}

extension StoryboardParser.Cell: Hashable {
var hashValue: Int {
// swiftlint:disable line_length
return identifier.hashValue ^ (customModule?.hashValue ?? 0) ^ (customClass?.hashValue ?? 0) ^ (customClass?.hashValue ?? 0)
// swiftlint:enable line_length
}
}
29 changes: 22 additions & 7 deletions Sources/Stencil/StoryboardsContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ private func uppercaseFirst(_ string: String) -> String {
- `class`: `String` (absent if generic UIStoryboardSegue)
*/
extension StoryboardParser {
// swiftlint:disable function_body_length
public func stencilContext(sceneEnumName: String = "StoryboardScene",
segueEnumName: String = "StoryboardSegue") -> [String: Any] {
segueEnumName: String = "StoryboardSegue",
cellEnumName: String = "StoryboardCells") -> [String: Any] {
let storyboards = Set(storyboardsScenes.keys).union(storyboardsSegues.keys).sorted(by: <)
let storyboardsMap = storyboards.map { (storyboardName: String) -> [String:Any] in
var sbMap: [String:Any] = ["name": storyboardName]
Expand All @@ -60,7 +62,7 @@ extension StoryboardParser {
if let scenes = storyboardsScenes[storyboardName] {
sbMap["scenes"] = scenes
.sorted(by: {$0.storyboardID < $1.storyboardID})
.map { (scene: Scene) -> [String:Any] in
.map { (scene: Scene) -> [String: Any] in
if let customClass = scene.customClass {
return [
"identifier": scene.storyboardID,
Expand All @@ -81,22 +83,35 @@ extension StoryboardParser {
}
// All Segues
if let segues = storyboardsSegues[storyboardName] {
sbMap["segues"] = segues
.sorted(by: {$0.identifier < $1.identifier})
.map { (segue: Segue) -> [String:String] in
["identifier": segue.identifier, "customClass": segue.customClass ?? ""]
}
sbMap["segues"] = segues
.sorted(by: {$0.identifier < $1.identifier})
.map { (segue: Segue) -> [String: String] in
["identifier": segue.identifier, "customClass": segue.customClass ?? ""]
}
}

// All Segues
if let cells = storyboardsCells[storyboardName] {
sbMap["cells"] = cells
.sorted(by: {$0.identifier < $1.identifier})
.map { (cell: Cell) -> [String: String] in
["identifier": cell.identifier, "customClass": cell.customClass ?? ""]
}
}

return sbMap
}

return [
"sceneEnumName": sceneEnumName,
"segueEnumName": segueEnumName,
"cellEnumName": cellEnumName,
"storyboards": storyboardsMap,
"modules": modules.sorted(),

// NOTE: This is a deprecated variable
"extraImports": modules.sorted()
]
}
// swiftlint:enable function_body_length
}
2 changes: 1 addition & 1 deletion Tests/Resources
Submodule Resources updated 124 files