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

Commit 50517a6

Browse files
author
Martin Conte Mac Donell
committed
Add XIB Parser and context provider
This will allow SwiftGen to parse XIB files and therefore type-safe view controller creation based on XIB files.
1 parent fcdd1bc commit 50517a6

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Sources/Parsers/XIBParser.swift

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// SwiftGenKit
3+
// Copyright (c) 2017 SwiftGen
4+
// MIT Licence
5+
//
6+
7+
import Foundation
8+
import PathKit
9+
10+
public final class XIBParser {
11+
var xibs: [String: (customClass: String?, module: String?)] = [:]
12+
13+
public init() {}
14+
15+
private class ParserDelegate: NSObject, XMLParserDelegate {
16+
fileprivate var fileOwnerClass: String?
17+
fileprivate var fileOwnerModule: String?
18+
19+
func parser(_ parser: XMLParser, didStartElement elementName: String,
20+
namespaceURI: String?, qualifiedName qName: String?,
21+
attributes attributeDict: [String: String]) {
22+
if elementName == "placeholder" && attributeDict["placeholderIdentifier"] == "IBFilesOwner" {
23+
self.fileOwnerClass = attributeDict["customClass"]
24+
self.fileOwnerModule = attributeDict["customModule"]
25+
}
26+
}
27+
}
28+
29+
public func addXIB(at path: Path) throws {
30+
let parser = XMLParser(data: try path.read())
31+
32+
let delegate = ParserDelegate()
33+
parser.delegate = delegate
34+
parser.parse()
35+
36+
let xibName = path.lastComponentWithoutExtension
37+
self.xibs[xibName] = (delegate.fileOwnerClass, delegate.fileOwnerModule)
38+
}
39+
40+
public func parseDirectory(at path: Path) throws {
41+
let iterator = path.makeIterator()
42+
43+
while let subPath = iterator.next() {
44+
if subPath.extension == "xib" {
45+
try addXIB(at: subPath)
46+
}
47+
}
48+
}
49+
}

Sources/Stencil/XIBContext.swift

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// SwiftGenKit
3+
// Copyright (c) 2017 SwiftGen
4+
// MIT Licence
5+
//
6+
7+
import Foundation
8+
9+
/*
10+
- `xibs`: `Array` — List of xibs
11+
- `name`: `String` — Name of the xib
12+
- `owner`: `String` — The custom class of the scene
13+
- `customModule`: `String` — The custom module of the scene (absent if no custom class)
14+
*/
15+
extension XIBParser {
16+
public func stencilContext() -> [String: Any] {
17+
let xibNames = Set(xibs.keys).sorted(by: <)
18+
let xibsMap = xibNames.map { (xibName: String) -> [String: String] in
19+
guard let (owner, module) = xibs[xibName] else {
20+
return [:]
21+
}
22+
23+
var xibInformation = ["name": xibName]
24+
xibInformation["customOwner"] = owner
25+
xibInformation["customModule"] = module
26+
return xibInformation
27+
}
28+
29+
return [
30+
"xibs": xibsMap,
31+
]
32+
}
33+
}

0 commit comments

Comments
 (0)