Cotton code
130 lines · 4.6 KB · SwiftRaw
// AppFlavor.swift — which app this build is: CottonDB or Cotton Dev.//// One source tree builds two apps that sit side by side on one iPhone and one// Mac and never touch each other's data: the real CottonDB, whose identity is// frozen, and Cotton Dev, which every local build, test and simulator run// produces. The build stamps `CottonFlavor` into every Info.plist// (`project.yml`'s `COTTON_FLAVOR`, `scripts/build-app.sh --flavor`); this// type reads it and holds every identifier Swift names by flavor. The Rust// core's half — the data folder, the port, the relay-name rule, a server's// flavor — is `crates/cotton-core/src/flavor.rs`, handed the same word at open.//// UNSTAMPED IS DEV. Only the exact word `real` is the real app: a build that// forgot to say which it is lands on the side that cannot reach the real// household. `AppFlavorTests` is the guard that no dev identifier is a real one.import Foundationpublic enum AppFlavor: String, CaseIterable, Sendable {    case real    case dev    /// The Info.plist key every bundle in the family carries.    public static let infoKey = "CottonFlavor"    /// What a bundle declares. Anything but `real` is `.dev`.    public static func of(_ bundle: Bundle) -> AppFlavor {        declared(bundle.object(forInfoDictionaryKey: infoKey) as? String)    }    /// A declared word, read the one way.    public static func declared(_ value: String?) -> AppFlavor {        value?.trimmingCharacters(in: .whitespacesAndNewlines) == "real" ? .real : .dev    }    /// This process's flavor.    public static let current: AppFlavor = of(.main)    // MARK: - The iPhone    /// The phone app's bundle id; the share extension appends    /// `.share-extension` (`project.yml`).    public var phoneAppID: String {        switch self {        case .real: return "com.cottondb.cotton"        case .dev: return "com.cottondb.cotton.dev"        }    }    /// `group.<phone app id>`.    public var phoneAppGroup: String { "group." + phoneAppID }    /// The phone's Keychain service — the device seed, the vault key and every    /// secret `SecretStore` keeps. The phone's access group already differs by    /// bundle id; the service differs too so nothing depends on that alone.    public var phoneKeychainService: String { phoneAppID }    // MARK: - The Mac    /// CottonDB for Mac's bundle id.    public var macBundleID: String {        switch self {        case .real: return "com.decemberfm.cottondb.mac"        case .dev: return "com.decemberfm.cottondb.mac.dev"        }    }    /// The Mac's app group — team-prefixed (see `CottonDefaults.macAppGroup`).    public var macAppGroup: String {        switch self {        case .real: return "M7V3QVXZQH.com.decemberfm.cotton"        case .dev: return "M7V3QVXZQH.com.decemberfm.cotton.dev"        }    }    /// The Mac's Keychain service. The Mac's login keychain is shared by every    /// app the person runs, so this is what keeps the two apps' device keys,    /// relay identities and vault keys apart.    public var macKeychainService: String {        switch self {        case .real: return "com.decemberfm.cotton.mac"        case .dev: return "com.decemberfm.cotton.mac.dev"        }    }    /// The session host's launchd label (and its plist's file name, `.plist`).    public var sessionHostLabel: String {        switch self {        case .real: return "com.decemberfm.cotton.session-host"        case .dev: return "com.decemberfm.cotton.dev.session-host"        }    }    /// The Mac app's name — its bundle's file name without `.app`.    public var macAppName: String {        switch self {        case .real: return "CottonDB"        case .dev: return "Cotton Dev"        }    }    /// The one place the Mac app registers its login item and agent from.    public var installedPath: String { "/Applications/\(macAppName).app" }    /// The Mac's data folder's name under the home folder. The Rust core    /// decides the path (`Flavor::home_data_dir`); this is for sentences.    public var dataFolderName: String {        switch self {        case .real: return "CottonDB"        case .dev: return "CottonDev"        }    }    // MARK: - Both    /// The URL scheme every deep link carries.    public var urlScheme: String {        switch self {        case .real: return "tinmen-cotton"        case .dev: return "cottondev"        }    }    /// The name a person reads on the phone's home screen.    public var displayName: String {        switch self {        case .real: return "Cotton"        case .dev: return "Cotton Dev"        }    }}