Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add catalog to nib/storyboard image and color parsing
  • Loading branch information
tomlokhorst committed May 8, 2020
commit 05d4d3fdd09c2c5267fa96f06d1f554007dda6df
13 changes: 9 additions & 4 deletions Sources/RswiftCore/Generators/NibStructGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,17 @@ struct NibStructGenerator: StructGenerator {

// Validation
let validateImagesLines = nib.usedImageIdentifiers.uniqueAndSorted()
.map {
"if UIKit.UIImage(named: \"\($0)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Image named '\($0)' is used in nib '\(nib.name)', but couldn't be loaded.\") }"
.map { nameCatalog -> String in
if nameCatalog.isSystemCatalog {
return "if #available(iOS 13.0, *) { if UIKit.UIImage(systemName: \"\(nameCatalog.name)\") == nil { throw Rswift.ValidationError(description: \"[R.swift] System image named '\(nameCatalog.name)' is used in nib '\(nib.name)', but couldn't be loaded.\") } }"
} else {
return "if UIKit.UIImage(named: \"\(nameCatalog.name)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Image named '\(nameCatalog.name)' is used in nib '\(nib.name)', but couldn't be loaded.\") }"
}
}
let validateColorLines = nib.usedColorResources.uniqueAndSorted()
.map {
"if UIKit.UIColor(named: \"\($0)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Color named '\($0)' is used in storyboard '\(nib.name)', but couldn't be loaded.\") }"
.compactMap { nameCatalog -> String? in
if nameCatalog.isSystemCatalog { return nil }
return "if UIKit.UIColor(named: \"\(nameCatalog.name)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Color named '\(nameCatalog.name)' is used in nib '\(nib.name)', but couldn't be loaded.\") }"
}
let validateColorLinesWithAvailableIf = ["if #available(iOS 11.0, tvOS 11.0, *) {"] +
validateColorLines.map { $0.indent(with: " ") } +
Expand Down
13 changes: 9 additions & 4 deletions Sources/RswiftCore/Generators/StoryboardGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ struct StoryboardStructGenerator: StructGenerator {

// Validation
let validateImagesLines = storyboard.usedImageIdentifiers.uniqueAndSorted()
.map {
"if UIKit.UIImage(named: \"\($0)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Image named '\($0)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\") }"
.map { nameCatalog -> String in
if nameCatalog.isSystemCatalog {
return "if #available(iOS 13.0, *) { if UIKit.UIImage(systemName: \"\(nameCatalog.name)\") == nil { throw Rswift.ValidationError(description: \"[R.swift] System image named '\(nameCatalog.name)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\") } }"
} else {
return "if UIKit.UIImage(named: \"\(nameCatalog.name)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Image named '\(nameCatalog.name)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\") }"
}
}
let validateColorLines = storyboard.usedColorResources.uniqueAndSorted()
.map {
"if UIKit.UIColor(named: \"\($0)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Color named '\($0)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\") }"
.compactMap { nameCatalog -> String? in
if nameCatalog.isSystemCatalog { return nil }
return "if UIKit.UIColor(named: \"\(nameCatalog.name)\", in: R.hostingBundle, compatibleWith: nil) == nil { throw Rswift.ValidationError(description: \"[R.swift] Color named '\(nameCatalog.name)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\") }"
}
let validateColorLinesWithAvailableIf = ["if #available(iOS 11.0, tvOS 11.0, *) {"] +
validateColorLines.map { $0.indent(with: " ") } +
Expand Down
23 changes: 23 additions & 0 deletions Sources/RswiftCore/ResourceTypes/NameCatalog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NameCatalog.swift
// RswiftCore
//
// Created by Tom Lokhorst on 2020-05-08.
//

import Foundation

struct NameCatalog: Hashable, Comparable {
let name: String
let catalog: String?

var isSystemCatalog: Bool {
return
catalog == "System" // for colors
|| catalog == "system" // for images
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

}

static func < (lhs: NameCatalog, rhs: NameCatalog) -> Bool {
lhs.name < lhs.name
}
}
12 changes: 6 additions & 6 deletions Sources/RswiftCore/ResourceTypes/Nib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct Nib: WhiteListedExtensionsResourceType, ReusableContainer {
let name: String
let rootViews: [Type]
let reusables: [Reusable]
let usedImageIdentifiers: [String]
let usedColorResources: [String]
let usedImageIdentifiers: [NameCatalog]
let usedColorResources: [NameCatalog]
let usedAccessibilityIdentifiers: [String]

init(url: URL) throws {
Expand Down Expand Up @@ -58,8 +58,8 @@ internal class NibParserDelegate: NSObject, XMLParserDelegate {
let ignoredRootViewElements = ["placeholder"]
var rootViews: [Type] = []
var reusables: [Reusable] = []
var usedImageIdentifiers: [String] = []
var usedColorReferences: [String] = []
var usedImageIdentifiers: [NameCatalog] = []
var usedColorReferences: [NameCatalog] = []
var usedAccessibilityIdentifiers: [String] = []

// State
Expand All @@ -77,12 +77,12 @@ internal class NibParserDelegate: NSObject, XMLParserDelegate {
switch elementName {
case "image":
if let imageIdentifier = attributeDict["name"] {
usedImageIdentifiers.append(imageIdentifier)
usedImageIdentifiers.append(NameCatalog(name: imageIdentifier, catalog: attributeDict["catalog"]))
}

case "color":
if let colorName = attributeDict["name"] {
usedColorReferences.append(colorName)
usedColorReferences.append(NameCatalog(name: colorName, catalog: attributeDict["catalog"]))
}

case "accessibility":
Expand Down
12 changes: 6 additions & 6 deletions Sources/RswiftCore/ResourceTypes/Storyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct Storyboard: WhiteListedExtensionsResourceType, ReusableContainer {
let viewControllers: [ViewController]
let viewControllerPlaceholders: [ViewControllerPlaceholder]
let usedAccessibilityIdentifiers: [String]
let usedImageIdentifiers: [String]
let usedColorResources: [String]
let usedImageIdentifiers: [NameCatalog]
let usedColorResources: [NameCatalog]
let reusables: [Reusable]

var initialViewController: ViewController? {
Expand Down Expand Up @@ -128,8 +128,8 @@ private class StoryboardParserDelegate: NSObject, XMLParserDelegate {
var initialViewControllerIdentifier: String?
var viewControllers: [Storyboard.ViewController] = []
var viewControllerPlaceholders: [Storyboard.ViewControllerPlaceholder] = []
var usedImageIdentifiers: [String] = []
var usedColorReferences: [String] = []
var usedImageIdentifiers: [NameCatalog] = []
var usedColorReferences: [NameCatalog] = []
var usedAccessibilityIdentifiers: [String] = []
var reusables: [Reusable] = []

Expand Down Expand Up @@ -167,12 +167,12 @@ private class StoryboardParserDelegate: NSObject, XMLParserDelegate {

case "image":
if let imageIdentifier = attributeDict["name"] {
usedImageIdentifiers.append(imageIdentifier)
usedImageIdentifiers.append(NameCatalog(name: imageIdentifier, catalog: attributeDict["catalog"]))
}

case "color":
if let colorName = attributeDict["name"] {
usedColorReferences.append(colorName)
usedColorReferences.append(NameCatalog(name: colorName, catalog: attributeDict["catalog"]))
}

case "accessibility":
Expand Down