forked from mac-cain13/R.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First working implementation of a11y identifiers
- Loading branch information
1 parent
1753dee
commit dec6bf0
Showing
10 changed files
with
154 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: feb2d125d56abaa9101403aedbf488a7d5445eab | ||
|
||
COCOAPODS: 1.7.1 | ||
COCOAPODS: 1.7.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES"> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES"> | ||
<device id="retina6_1" orientation="portrait"> | ||
<adaptation id="fullscreen"/> | ||
</device> | ||
<dependencies> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/> | ||
</dependencies> | ||
<scenes/> | ||
</document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
Sources/RswiftCore/Generators/AccessibilityIdentifierStructGenerator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// AccessibilityIdentifierStructGenerator.swift | ||
// R.swift | ||
// | ||
// Created by Mathijs Kadijk on 04/06/2019. | ||
// From: https://github.com/mac-cain13/R.swift | ||
// License: MIT License | ||
// | ||
|
||
import Foundation | ||
|
||
private protocol AccessibilityIdentifierContainer { | ||
var name: String { get } | ||
var usedAccessibilityIdentifiers: [String] { get } | ||
} | ||
|
||
extension Nib: AccessibilityIdentifierContainer {} | ||
extension Storyboard: AccessibilityIdentifierContainer {} | ||
|
||
struct AccessibilityIdentifierStructGenerator: ExternalOnlyStructGenerator { | ||
private let accessibilityIdentifierContainers: [AccessibilityIdentifierContainer] | ||
|
||
init(nibs: [Nib], storyboards: [Storyboard]) { | ||
accessibilityIdentifierContainers = nibs + storyboards | ||
} | ||
|
||
func generatedStruct(at externalAccessLevel: AccessLevel, prefix: SwiftIdentifier) -> Struct { | ||
let structName: SwiftIdentifier = "id" | ||
let qualifiedName = prefix + structName | ||
let structsForMergedContainers = accessibilityIdentifierContainers | ||
.grouped(by: { SwiftIdentifier(name: $0.name) }) | ||
.mapValues { | ||
$0.flatMap { $0.usedAccessibilityIdentifiers } | ||
} | ||
.filter { $0.value.count > 0 } | ||
.map { self.structFromContainer(identifier: $0.key, accessibilityIdentifiers: $0.value, at: externalAccessLevel) } | ||
|
||
return Struct( | ||
availables: [], | ||
comments: ["This `\(qualifiedName)` struct is generated, and contains static references to accessibility identifiers."], | ||
accessModifier: externalAccessLevel, | ||
type: Type(module: .host, name: structName), | ||
implements: [], | ||
typealiasses: [], | ||
properties: [], | ||
functions: [], | ||
structs: structsForMergedContainers, | ||
classes: [], | ||
os: [] | ||
) | ||
} | ||
|
||
private func structFromContainer(identifier: SwiftIdentifier, accessibilityIdentifiers: [String], at externalAccessLevel: AccessLevel) -> Struct { | ||
let groupedAccessibilityIdentifiers = Set(accessibilityIdentifiers) | ||
.array() | ||
.grouped(bySwiftIdentifier: { $0 }) | ||
groupedAccessibilityIdentifiers.printWarningsForDuplicatesAndEmpties(source: "accessibility identifier", result: "accessibility identifier") | ||
|
||
let accessibilityIdentifierLets = groupedAccessibilityIdentifiers | ||
.uniques | ||
.map { letFromAccessibilityIdentifier($0, at: externalAccessLevel) } | ||
|
||
return Struct( | ||
availables: [], | ||
comments: [], | ||
accessModifier: externalAccessLevel, | ||
type: Type(module: .host, name: identifier), | ||
implements: [], | ||
typealiasses: [], | ||
properties: accessibilityIdentifierLets, | ||
functions: [], | ||
structs: [], | ||
classes: [], | ||
os: [] | ||
) | ||
} | ||
|
||
private func letFromAccessibilityIdentifier(_ accessibilityIdentifier: String, at externalAccessLevel: AccessLevel) -> Let { | ||
return Let( | ||
comments: ["Accessibility identifier `\(accessibilityIdentifier)`."], | ||
accessModifier: externalAccessLevel, | ||
isStatic: true, | ||
name: SwiftIdentifier(name: accessibilityIdentifier), | ||
typeDefinition: .specified(._String), | ||
value: "\"\(accessibilityIdentifier)\"" | ||
) | ||
} | ||
} | ||
|
||
private extension Sequence { | ||
func groupedWithExactDuplicatesAllowed(bySwiftIdentifier identifierSelector: @escaping (Iterator.Element) -> String) -> SwiftNameGroups<Iterator.Element> { | ||
var groupedBy = grouped { SwiftIdentifier(name: identifierSelector($0)) } | ||
let empty = SwiftIdentifier(name: "") | ||
let empties = groupedBy[empty]?.map { "'\(identifierSelector($0))'" }.sorted() | ||
groupedBy[empty] = nil | ||
|
||
let uniques = Array(groupedBy.values.filter { $0.count == 1 }.joined()) | ||
.sorted { identifierSelector($0) < identifierSelector($1) } | ||
let duplicates = groupedBy | ||
.filter { $0.1.count > 1 } | ||
.map { ($0.0, $0.1.map(identifierSelector).sorted()) } | ||
.sorted { $0.0.description < $1.0.description } | ||
|
||
return SwiftNameGroups(uniques: uniques, duplicates: duplicates, empties: empties ?? []) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters