-
Notifications
You must be signed in to change notification settings - Fork 763
Images: dot syntax #206
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
Merged
Merged
Images: dot syntax #206
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1048f06
parse asset catalog into simple structure
djbe 74bac88
images: dot syntax swift3 template
djbe a931933
we still need the “paths” of an asset
djbe 40470f9
unit test
djbe 0a878fc
swift 2.3 stencil and test
djbe 8d3e04b
Merge branch 'master' into feature/structured-images
djbe 960623f
moar pathkit
djbe cb27300
Revert some of the last commit, as it’s moved to another PR
djbe d68fbb7
Just use enum to namespace constants
djbe 28d3ca2
whitespace
djbe 5078d03
Merge branch 'master' into feature/structured-images
djbe 0245083
use enum with associated values
djbe 09cae9e
documentation for catalog parser
djbe 449f094
path.description
djbe 20b8028
Merge branch 'master' into feature/structured-images
djbe 0607ca9
spacing
djbe 55866e8
use enums instead of structs for namespacing
djbe dcb6df6
changelog entry
djbe e9641ff
Merge branch 'master' into feature/structured-images
djbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,81 +8,162 @@ import Foundation | |
| import PathKit | ||
|
|
||
| public final class AssetsCatalogParser { | ||
| var imageNames = [String]() | ||
| var entries = [Entry]() | ||
|
|
||
| public init() {} | ||
|
|
||
| @discardableResult | ||
| public func addImage(named name: String) -> Bool { | ||
| if imageNames.contains(name) { | ||
| return false | ||
| } else { | ||
| imageNames.append(name) | ||
| let found = entries.contains { | ||
| if case let .image(imageName, _) = $0, imageName == name { | ||
| return true | ||
| } else { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| guard !found else { return false } | ||
| entries.append(Entry.image(name: name, value: name)) | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| public func parseCatalog(at path: Path) { | ||
| guard let items = loadAssetCatalog(at: path) else { return } | ||
|
|
||
| // process recursively | ||
| processCatalog(items: items) | ||
| entries = process(items: items) | ||
| } | ||
|
|
||
| enum Entry { | ||
| case image(name: String, value: String) | ||
| case namespace(name: String, items: [Entry]) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Plist processing | ||
|
|
||
| private enum AssetCatalog: String { | ||
| case children = "children" | ||
| case filename = "filename" | ||
| case providesNamespace = "provides-namespace" | ||
| case root = "com.apple.actool.catalog-contents" | ||
| private enum AssetCatalog { | ||
| static let children = "children" | ||
| static let filename = "filename" | ||
| static let providesNamespace = "provides-namespace" | ||
| static let root = "com.apple.actool.catalog-contents" | ||
| } | ||
|
|
||
| extension AssetsCatalogParser { | ||
| static let imageSetExtension = "imageset" | ||
|
|
||
| fileprivate func processCatalog(items: [[String: AnyObject]], withPrefix prefix: String = "") { | ||
| /** | ||
| This method recursively parses a tree of nodes (similar to a directory structure) | ||
| resulting from the `actool` utility. | ||
|
|
||
| Each node in an asset catalog is either (there are more types, but we ignore those): | ||
| - An imageset, which is essentially a group containing a list of files (the latter is ignored). | ||
|
|
||
| <dict> | ||
| <key>children</key> | ||
| <array> | ||
| ...actual file items here (for example the 1x, 2x and 3x images)... | ||
| </array> | ||
| <key>filename</key> | ||
| <string>Tomato.imageset</string> | ||
| </dict> | ||
|
|
||
| - A group, containing sub items such as imagesets or groups. A group can provide a namespaced, | ||
| which means that all the sub items will have to be prefixed with their parent's name. | ||
|
|
||
| <dict> | ||
| <key>children</key> | ||
| <array> | ||
| ...sub items such as groups or imagesets... | ||
| </array> | ||
| <key>filename</key> | ||
| <string>Round</string> | ||
| <key>provides-namespace</key> | ||
| <true/> | ||
| </dict> | ||
|
|
||
| - Parameter items: The array of items to recursively process. | ||
| - Parameter prefix: The prefix to prepend values with (from namespaced groups). | ||
| - Returns: An array of processed Entry items. | ||
| */ | ||
| fileprivate func process(items: [[String: Any]], withPrefix prefix: String = "") -> [Entry] { | ||
| var result = [Entry]() | ||
|
|
||
| for item in items { | ||
| guard let filename = item[AssetCatalog.filename.rawValue] as? String else { continue } | ||
| guard let filename = item[AssetCatalog.filename] as? String else { continue } | ||
| let path = Path(filename) | ||
|
|
||
| if path.extension == AssetsCatalogParser.imageSetExtension { | ||
| // this is a simple imageset | ||
| let imageName = path.lastComponentWithoutExtension | ||
| addImage(named: "\(prefix)\(imageName)") | ||
|
|
||
| result += [.image(name: imageName, value: "\(prefix)\(imageName)")] | ||
| } else { | ||
| // this is a group/folder | ||
| let children = item[AssetCatalog.children.rawValue] as? [[String: AnyObject]] ?? [] | ||
| let children = item[AssetCatalog.children] as? [[String: Any]] ?? [] | ||
|
|
||
| if let providesNamespace = item[AssetCatalog.providesNamespace.rawValue] as? NSNumber, | ||
| providesNamespace.boolValue { | ||
| processCatalog(items: children, withPrefix: "\(prefix)\(filename)/") | ||
| if let providesNamespace = item[AssetCatalog.providesNamespace] as? Bool, | ||
| providesNamespace { | ||
| let processed = process(items: children, withPrefix: "\(prefix)\(filename)/") | ||
| result += [.namespace(name: filename, items: processed)] | ||
| } else { | ||
| processCatalog(items: children, withPrefix: prefix) | ||
| let processed = process(items: children, withPrefix: prefix) | ||
| result += [.namespace(name: filename, items: processed)] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
| } | ||
|
|
||
| // MARK: - ACTool | ||
|
|
||
| extension AssetsCatalogParser { | ||
| fileprivate func loadAssetCatalog(at path: Path) -> [[String: AnyObject]]? { | ||
| let command = Command("xcrun", arguments: "actool", "--print-contents", String(describing: path)) | ||
| /** | ||
| Try to parse an asset catalog using the `actool` utilty. While it supports parsing | ||
| multiple catalogs at once, we only use it to parse one at a time. | ||
|
|
||
| The output of the utility is a Plist and should be similar to this: | ||
| ```xml | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>com.apple.actool.catalog-contents</key> | ||
| <array> | ||
| <dict> | ||
| <key>children</key> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <array> | ||
| ... | ||
| </array> | ||
| <key>filename</key> | ||
| <string>Images.xcassets</string> | ||
| </dict> | ||
| </array> | ||
| </dict> | ||
| </plist> | ||
| ``` | ||
|
|
||
| - Parameter path: The path to the catalog to parse. | ||
| - Returns: An array of dictionaries, representing the tree of nodes in the catalog. | ||
| */ | ||
| fileprivate func loadAssetCatalog(at path: Path) -> [[String: Any]]? { | ||
| let command = Command("xcrun", arguments: "actool", "--print-contents", path.description) | ||
| let output = command.execute() as Data | ||
|
|
||
| // try to parse plist | ||
| guard let plist = try? PropertyListSerialization | ||
| .propertyList(from: output, format: nil) else { return nil } | ||
|
|
||
| // get first parsed catalog | ||
| guard let contents = plist as? [String: AnyObject], | ||
| let catalogs = contents[AssetCatalog.root.rawValue] as? [[String: AnyObject]], | ||
| guard let contents = plist as? [String: Any], | ||
| let catalogs = contents[AssetCatalog.root] as? [[String: Any]], | ||
| let catalog = catalogs.first else { return nil } | ||
|
|
||
| // get list of children | ||
| guard let children = catalog[AssetCatalog.children.rawValue] as? [[String: AnyObject]] else { return nil } | ||
| guard let children = catalog[AssetCatalog.children] as? [[String: Any]] else { return nil } | ||
|
|
||
| return children | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation (from lines 17 to 26) seems wrong 😉