Closed charlieMonroe closed 3 years ago
I'm surprised you didn't have more (I had 1,000+ plus, XCode couldn't count them). I was using the 2.2.0 set.
In addition to non-existent Config.json files, I also found Config.json files with empty images arrays, and ones with populated universal arrays, but no "filename" elements.
I have my icons grouped in folders, and found an additional problem, which is that ic_rv_hookup_xxxx icons exist in two folders (notifications and places). I had to delete one of those manually.
Here is my modified version of your code (to process icons grouped in folders, and deal with the additional problems I found):
import Foundation
extension NSURL {
var isDirectory: Bool {
guard let path = path where fileURL else { return false }
var bool: ObjCBool = false
return NSFileManager().fileExistsAtPath(path, isDirectory: &bool) ? bool.boolValue : false
}
var subdirectories: [NSURL] {
guard isDirectory else { return [] }
do {
return try NSFileManager.defaultManager()
.contentsOfDirectoryAtURL(self, includingPropertiesForKeys: nil, options: [])
.filter{ $0.isDirectory }
} catch let error as NSError {
print(error.localizedDescription)
return []
}
}
}
print("Processing Material Design icons")
let rootURL = NSURL(fileURLWithPath: "/Users/blah/MyProject/Images.xcassets/MaterialDesign")
for packageURL in rootURL.subdirectories {
for imageSetURL in packageURL.subdirectories {
print("Image dir: \(imageSetURL)")
if imageSetURL.pathExtension != "imageset" {
continue
}
let configURL = imageSetURL.URLByAppendingPathComponent("Contents.json")
if !configURL.checkResourceIsReachableAndReturnError(nil)
{
// Problem 2: No Config.json for a imageset (observed in 2.2.0)
print("Config.json did not exist for \(configURL)")
}
else if let jsonData = NSData(contentsOfURL: configURL)
{
do
{
if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
{
// print("Got JSON data: \(jsonResult)")
if let images = jsonResult["images"] as? NSArray
{
if images.count == 0
{
// Problem 3: Config.json contained empty "images" array (observed)
print("Empty images array in \(configURL)")
}
else
{
if let image1 = images[0] as? NSDictionary
{
if image1["filename"] != nil
{
//print("Filename: \(image1["filename"])")
continue
}
else
{
// Problem 4: Image array existed, but no filenames in array (observed)
print("No filename for image in \(configURL)")
}
}
else
{
print("Image[0] not a dict (unexpected) in \(configURL)")
}
}
}
}
}
catch
{
print("Error parsing JSON for \(configURL)")
}
}
// Unless we ecountered and existing, well-formed Config.json, we're just going to make our own and hammer it over
// what was (or wasn't) there...
//
let name = imageSetURL.URLByDeletingPathExtension!.lastPathComponent!
let dict = [
"images": [
[
"filename": name + ".png",
"idiom": "universal",
"scale": "1x"
],
[
"filename": name + "_2x.png",
"idiom": "universal",
"scale": "2x"
],
[
"filename": name + "_3x.png",
"idiom": "universal",
"scale": "3x"
]
],
"info" : [
"author": "xcode",
"version": 1
]
]
print("Created/Recreated Config.json for \(name)")
let data = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
data.writeToURL(configURL, atomically: true)
}
}
The assets have been updated since this issue was reported, so I believe this should be fixed. If not, feel free to reopen this issue or create a new one with more information.
I've added all icons into an Xcode assets catalog and got over 500 warnings about unassigned children (see the log below). This is caused by the imageset not having the Device selected. Such icons are likely to be unusable since Xcode won't compile those icons. As suggested in the screenshot, the Universal option should be checked.
[EDIT] - after further investigation, these imagesets are completely missing the Contents.json file. See at the end of a post for a sample Swift-script to fix this automatically.
Attaching log of affected icons (using Xcode 7.2):
Fixer (assumes that the imagesets are in a single folder):