Skip to content

Commit

Permalink
fix: image_resources rule support custom symbol image
Browse files Browse the repository at this point in the history
  • Loading branch information
r-plus committed Jan 31, 2020
1 parent df983e6 commit d8e1d59
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 10 deletions.
19 changes: 13 additions & 6 deletions Sources/IBLinterKit/Entity/AssetsCatalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ extension AssetsCatalog {
}) ?? []
}

func entryValues(for item: Item) -> [String] {
return entries.flatMap { $0.values(for: item) }
func entryValues(for items: Item...) -> [String] {
return entries.flatMap { $0.values(for: items) }
}

private enum Constants {
Expand All @@ -40,12 +40,14 @@ extension AssetsCatalog {
enum Item: String {
case colorSet = "colorset"
case imageSet = "imageset"
case symbolSet = "symbolset"
}

enum Entry: Equatable {
case group(name: String, items: [Entry])
case color(name: String, value: String)
case image(name: String, value: String)
case symbol(name: String, value: String)

init?(path: String, withPrefix prefix: String) {
guard FileManager.default.isDirectory(path) else { return nil }
Expand All @@ -58,6 +60,9 @@ extension AssetsCatalog {
case .imageSet?:
let name = path.lastComponentWithoutExtension
self = .image(name: name, value: "\(prefix)\(name)")
case .symbolSet?:
let name = path.lastComponentWithoutExtension
self = .symbol(name: name, value: "\(prefix)\(name)")
case nil:
guard type.isEmpty else { return nil }
let filename = path.lastComponent
Expand Down Expand Up @@ -92,14 +97,16 @@ extension AssetsCatalog {
return (json as? [String: Any]) ?? [:]
}

fileprivate func values(for item: Item) -> [String] {
fileprivate func values(for targets: [Item]) -> [String] {
switch self {
case .group(_, let items):
return items.flatMap { $0.values(for: item) }
return items.flatMap { $0.values(for: targets) }
case .color(_, let value):
return item == .colorSet ? [value] : []
return targets.contains(.colorSet) ? [value] : []
case .image(_, let value):
return item == .imageSet ? [value] : []
return targets.contains(.imageSet) ? [value] : []
case .symbol(_, let value):
return targets.contains(.symbolSet) ? [value] : []
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/IBLinterKit/Rules/ImageResourcesRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension Rules {
}

private func validate<T: InterfaceBuilderFile>(for images: [Image], imageViews: [ImageView], states: [Button.State], file: T) -> [Violation] {
let catalogAssetNames = assetsCatalogs.flatMap { $0.entryValues(for: .imageSet) }
let catalogAssetNames = assetsCatalogs.flatMap { $0.entryValues(for: .imageSet, .symbolSet) }
let xcodeprojAssetNames = xcodeproj.flatMap {
$0.pbxproj.fileReferences.compactMap {
$0.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"symbols" : [
{
"idiom" : "universal",
"filename" : "mySymbol.svg"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"symbols" : [
{
"idiom" : "universal",
"filename" : "pencil.svg"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15509"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15706"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -47,6 +47,10 @@
<rect key="frame" x="130" y="37" width="115" height="81"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="Namespace/NamespaceNestedSymbol" translatesAutoresizingMaskIntoConstraints="NO" id="tvc-W1-7sK">
<rect key="frame" x="244" y="215" width="33" height="42"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="grid" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="ExE-Si-hwZ">
<rect key="frame" x="44" y="232" width="115" height="81"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
Expand All @@ -61,6 +65,10 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button" image="bag" catalog="system"/>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="Namespace/NestedNamespace/NestedNamespaceSymbolTypo" translatesAutoresizingMaskIntoConstraints="NO" id="DYi-wq-Oow">
<rect key="frame" x="285" y="235" width="22" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<viewLayoutGuide key="safeArea" id="hhG-XG-10b"/>
Expand All @@ -77,6 +85,8 @@
<image name="Folder/AppleNonNamespaced" width="16" height="16"/>
<image name="Namespace/AppleNamespaced" width="16" height="16"/>
<image name="Namespace/AppleNonNamespaced" width="16" height="16"/>
<image name="Namespace/NamespaceNestedSymbol" width="15" height="19"/>
<image name="Namespace/NestedNamespace/NestedNamespaceSymbolTypo" width="16" height="16"/>
<image name="TypoImage" width="16" height="16"/>
<image name="bag" catalog="system" width="64" height="64"/>
<image name="grid" catalog="system" width="64" height="56"/>
Expand Down
2 changes: 1 addition & 1 deletion Tests/IBLinterKitTest/Rules/ImageResourcesRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class ImageResourcesRuleTests: XCTestCase {
let assetURL = fixture.path("Resources/Rules/ImageResourcesRule/Media.xcassets")
let rule = Rules.ImageResourcesRule(catalogs: [.init(path: assetURL.path)])
let violations = try! rule.validate(storyboard: StoryboardFile(url: url))
XCTAssertEqual(violations.count, 3)
XCTAssertEqual(violations.count, 4)
}
}
2 changes: 2 additions & 0 deletions Tests/IBLinterKitTest/Utils/AssetsCatalogTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class AssetsCatalogTest: XCTestCase {
name: "Nested",
items: [
.image(name: "EmptyNested", value: "Namespace/EmptyNested"),
.symbol(name: "NamespaceNestedSymbol", value: "Namespace/NamespaceNestedSymbol"),
]
),
.group(
name: "NestedNamespace",
items: [
.image(name: "EmptyNamespaced", value: "Namespace/NestedNamespace/EmptyNamespaced"),
.symbol(name: "NestedNamespaceSymbol", value: "Namespace/NestedNamespace/NestedNamespaceSymbol"),
]
),
]
Expand Down

0 comments on commit d8e1d59

Please sign in to comment.