/
Asset.swift
36 lines (29 loc) · 1 KB
/
Asset.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// Asset.swift
// Iconizer
// https://github.com/raphaelhanneken/iconizer
//
import Cocoa
protocol Asset {
static var resourcePrefix: String { get }
static func directory(named: String) -> String
func save(_ image: [ImageOrientation: NSImage], aspect: AspectMode?, to url: URL) throws
}
extension Asset {
static func resourceURL(name: String) throws -> URL {
guard let path = Bundle.main.path(forResource: name, ofType: "json") else {
throw AssetCatalogError.missingPlatformJSON
}
return URL(fileURLWithPath: path)
}
}
extension Asset where Self: Decodable {
static func images(forPlatform platform: Platform, orientation: ImageOrientation = .all) throws -> [Self] {
//.json file
let url = try resourceURL(name: resourcePrefix + platform.name(forOrientation: orientation))
let data = try Data(contentsOf: url)
//parse json
let decoder = JSONDecoder()
return try decoder.decode([Self].self, from: data)
}
}