Cotton code
37 lines · 2.1 KB · SwiftRaw
// A development build's invented screenplay, for looking at the Ink screens// on a simulator: brought in once, by the launch argument `-story-ink-seed// YES`, through the core's own script reader (`story.bring_in`). Never in a store build, and// never anyone's real script.#if os(iOS) && COTTON_DEVimport CottonKitimport Foundation@MainActorenum InkSeed {    static var asked: Bool { UserDefaults.standard.bool(forKey: "story-ink-seed") }    /// `-story-ink-crowd YES` brings in the crowded script instead: sixty invented    /// characters, for the speaker chooser (`story-ink-crowd.json`).    static var crowd: Bool { UserDefaults.standard.bool(forKey: "story-ink-crowd") }    /// The invented screenplay is a bundled file (`Resources/Fixtures/story-ink-seed.json`),    /// so no English a person reads is a Swift literal (as Story Dev's and Vault's seeds).    private struct Invented: Decodable { let name: String; let draft: String; let fountain: String }    static func seed(_ core: any CottonCalling, book: any StoryBook) async throws {        guard asked, try await book.list().stories.isEmpty else { return }        guard let url = Bundle.module.url(forResource: crowd ? "story-ink-crowd" : "story-ink-seed", withExtension: "json", subdirectory: "Fixtures"),              let data = try? Data(contentsOf: url),              let invented = try? JSONDecoder().decode(Invented.self, from: data) else { return }        // Brought in as a picked file is (`story.bring_in`), from a copy of the words.        let file = FileManager.default.temporaryDirectory.appendingPathComponent("story-ink-seed-\(UUID().uuidString).fountain")        try Data(invented.fountain.utf8).write(to: file, options: .atomic)        defer { try? FileManager.default.removeItem(at: file) }        _ = try await StoryShelf(core: core).bringIn(path: file.path(percentEncoded: false), name: invented.name)        // One marked draft, so Drafts has a row to look at.        if let story = try await book.list().stories.first?.id {            try await StoryShelf(core: core).markDraft(story: story, name: invented.draft)        }    }}#endif