Cotton code
58 lines · 2.8 KB · SwiftRaw
// The URL scheme, written down ONCE.//// The scheme is WIRE PROTOCOL — `tinmen-cotton` for the real app, `cottondev`// for Cotton Dev (`AppFlavor.urlScheme`, the one place either is written):// `tinmen-cotton://capture` is a// documented entry point, and TWO bundles now register it with the system — the// phone through `project.yml`'s `CFBundleURLTypes`, and the Mac through// `Packaging/Info.plist`. Four places that have to agree, and before this there// was no single place to agree WITH: the extension kit held its own literal and// the two plists held two more.//// This is that place, and it sits in `CottonKit` because `CottonKit` is the one// module every other one can see — the share extension is above it, the Mac// host is above it, and neither can reach the other's code.//// The two PLISTS cannot import Swift, so the agreement between them and this// constant is a guard instead: `scripts/__tests__/swift-mac.test.ts` reads the// Mac's plist and this file and fails when they drift, and// the iOS project spec test reads the phone's spec.//// ONE scheme PER APP, by the bundle's own `CottonFlavor` stamp: each bundle in// the family (the app and its share sheet) carries the// stamp, so every one of them builds and answers links under its own app's// scheme, and a link written by Cotton Dev never opens the real app.//// A BUSINESS FLAVOUR (`project.yml`'s `businessFlavours`) is a third app on// the same phone with its own scheme, so the phone's bundles also carry the// scheme itself as `CottonURLScheme`, and that stamp wins. Only an unstamped// process — a library test, the Mac — falls to the flavor's scheme.import Foundation/// The one scheme every Cotton deep link carries.public enum CottonURLScheme {    /// The Info.plist key the phone's bundles carry their scheme in.    public static let infoKey = "CottonURLScheme"    /// This app's scheme, without the `://` — the bundle's `CottonURLScheme`,    /// else `AppFlavor.current.urlScheme`.    public static let name = stamped(Bundle.main.object(forInfoDictionaryKey: infoKey) as? String)        ?? AppFlavor.current.urlScheme    /// A stamped scheme, or nil when the bundle carries none.    static func stamped(_ value: String?) -> String? {        guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else { return nil }        return value    }    /// Whether a URL is one of ours.    ///    /// A URL handler is registered with the SYSTEM, so what reaches it is    /// whatever the system decided to hand over — and a URL under a scheme this    /// app never claimed is a fact worth saying out loud rather than parsing as    /// if it were a path of ours. Case-insensitive, because a scheme is.    public static func isOurs(_ url: URL) -> Bool {        url.scheme?.lowercased() == name    }}