Cotton code
45 lines · 1.8 KB · SwiftRaw
// The invented pictures a face capture draws its records with: the covers,// posters, stills and sleeves cut from the Codex round 2 references into// `scripts/faces-walk/art/<name>.png`. With `FACES_ART=<that folder>`// (passed as `TEST_RUNNER_FACES_ART`; the simulator reads the Mac's paths) a// capture names its record's picture by the design's name and hands the// faces the file through `facePictures`, as the core would hand the bytes.// Never in the app's bundle: without the variable a capture draws its// records with no picture.#if canImport(UIKit)import CottonValuesimport SwiftUIimport UIKit@testable import CottonUIenum FacesArt {    /// The folder the pictures are in, when the run names one.    static var folder: URL? {        guard let path = ProcessInfo.processInfo.environment["FACES_ART"], !path.isEmpty else { return nil }        return URL(fileURLWithPath: path, isDirectory: true)    }    /// The key a record keeps for the design `name`'s picture, when it is there.    static func key(_ name: String) -> String? {        image(name) == nil ? nil : name    }    /// `fields` with the design `name`'s picture as their `photo`, when it is there.    static func with(_ fields: [String: RecordValue], _ name: String) -> [String: RecordValue] {        guard let key = key(name) else { return fields }        return fields.merging(["photo": .text(key)]) { _, picture in picture }    }    /// The picture under `key`, for `facePictures`.    static func picture(_ key: String) -> Image? {        image(key).map { Image(uiImage: $0) }    }    static func image(_ name: String) -> UIImage? {        guard let file = folder?.appendingPathComponent("\(name).png") else { return nil }        return UIImage(contentsOfFile: file.path)    }}#endif