Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not generate duplicate enum cases for segue IDs #43

Closed
wants to merge 7 commits into from
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

---

## Master

#### Fixes

* Now `swiftgen storyboards` doesn't generate duplicate enum cases for identical segues (those having equal identifiers and shared custom class).
[@filwag](https://github.com/filwag), [#43](https://github.com/AliSoftware/SwiftGen/pull/43)

## 0.6.0

### New Features: Templates
Expand Down
49 changes: 40 additions & 9 deletions GenumKit/Parsers/StoryboardParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@
import Foundation

public final class StoryboardParser {
typealias Scene = (storyboardID: String, tag: String, customClass: String?)
typealias Segue = (segueID: String, customClass: String?)
var storyboardsScenes = [String: [Scene]]()
var storyboardsSegues = [String: [Segue]]()
struct Scene {
let storyboardID: String
let tag: String
let customClass: String?
}

struct Segue {
let segueID: String
let customClass: String?
}

var storyboardsScenes = [String: Set<Scene>]()
var storyboardsSegues = [String: Set<Segue>]()

public init() {}

public func addStoryboardAtPath(path: String) {
let parser = NSXMLParser(contentsOfURL: NSURL.fileURLWithPath(path))

class ParserDelegate : NSObject, NSXMLParserDelegate {
var scenes = [Scene]()
var segues = [Segue]()
var scenes = Set<Scene>()
var segues = Set<Segue>()
var inScene = false
var readyForFirstObject = false
var readyForConnections = false
Expand All @@ -37,15 +46,15 @@ public final class StoryboardParser {
case let tag where readyForFirstObject:
if let storyboardID = attributeDict["storyboardIdentifier"] {
let customClass = attributeDict["customClass"]
scenes.append(Scene(storyboardID, tag, customClass))
scenes.insert(Scene(storyboardID: storyboardID, tag: tag, customClass: customClass))
}
readyForFirstObject = false
case "connections":
readyForConnections = true
case "segue" where readyForConnections:
if let segueID = attributeDict["identifier"] {
let customClass = attributeDict["customClass"]
segues.append(Segue(segueID, customClass))
segues.insert(Segue(segueID: segueID, customClass: customClass))
}
default:
break
Expand All @@ -63,7 +72,7 @@ public final class StoryboardParser {
case "connections":
readyForConnections = false
default:
break;
break
}
}
}
Expand All @@ -87,3 +96,25 @@ public final class StoryboardParser {
}
}
}

extension StoryboardParser.Scene: Equatable { }
func ==(lhs: StoryboardParser.Scene, rhs: StoryboardParser.Scene) -> Bool {
return lhs.storyboardID == rhs.storyboardID && lhs.tag == rhs.tag && lhs.customClass == rhs.customClass
}

extension StoryboardParser.Scene: Hashable {
var hashValue: Int {
return "\(storyboardID);\(tag);\(customClass)".hashValue
}
}

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

extension StoryboardParser.Segue: Hashable {
var hashValue: Int {
return "\(segueID);\(customClass)".hashValue
}
}
1 change: 1 addition & 0 deletions UnitTests/expected/Storyboards-All-CustomName.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ extension UIStoryboard {
struct XCTStoryboardsSegue {
enum Message : String {
case CustomBack = "CustomBack"
case Embed = "Embed"
case NonCustom = "NonCustom"
case Show_NavCtrl = "Show-NavCtrl"
}
Expand Down
1 change: 1 addition & 0 deletions UnitTests/expected/Storyboards-All-Defaults.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ extension UIStoryboard {
struct Segue {
enum Message : String {
case CustomBack = "CustomBack"
case Embed = "Embed"
case NonCustom = "NonCustom"
case Show_NavCtrl = "Show-NavCtrl"
}
Expand Down
1 change: 1 addition & 0 deletions UnitTests/expected/Storyboards-Message-Defaults.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extension UIStoryboard {
struct Segue {
enum Message : String {
case CustomBack = "CustomBack"
case Embed = "Embed"
case NonCustom = "NonCustom"
case Show_NavCtrl = "Show-NavCtrl"
}
Expand Down
41 changes: 37 additions & 4 deletions UnitTests/fixtures/Message.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="xKD-v4-X1W">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="xKD-v4-X1W">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
</dependencies>
<scenes>
<!--Home-->
Expand Down Expand Up @@ -58,6 +57,13 @@
<segue destination="xKD-v4-X1W" kind="custom" identifier="CustomBack" customClass="CustomSegueClass2" id="eBb-AQ-abV"/>
</connections>
</button>
<containerView opaque="NO" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="jfg-zf-vVo">
<rect key="frame" x="180" y="472" width="240" height="128"/>
<animations/>
<connections>
<segue destination="TKc-dv-ThZ" kind="embed" identifier="Embed" id="O2Q-vy-SWs"/>
</connections>
</containerView>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
Expand Down Expand Up @@ -100,6 +106,15 @@
<view key="view" contentMode="scaleToFill" id="8dI-WY-DPv">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<containerView opaque="NO" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="T02-7w-iPh">
<rect key="frame" x="180" y="472" width="240" height="128"/>
<animations/>
<connections>
<segue destination="TKc-dv-ThZ" kind="embed" identifier="Embed" id="Ne2-PQ-HKf"/>
</connections>
</containerView>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand All @@ -122,7 +137,7 @@
<rect key="frame" x="0.0" y="92" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="ABY-1J-cxh" id="mGm-Cb-M2O">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<animations/>
</tableViewCellContentView>
Expand Down Expand Up @@ -160,5 +175,23 @@
</objects>
<point key="canvasLocation" x="847" y="75"/>
</scene>
<!--Embedded-->
<scene sceneID="0KJ-B4-kHb">
<objects>
<viewController title="Embedded" id="TKc-dv-ThZ" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="XWV-Fs-B8e">
<rect key="frame" x="0.0" y="0.0" width="240" height="128"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="gSB-Wj-Zgr" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1239" y="1440"/>
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="Ne2-PQ-HKf"/>
</inferredMetricsTieBreakers>
</document>