Skip to content

Commit

Permalink
Add Environment manager #284
Browse files Browse the repository at this point in the history
  • Loading branch information
melonamin committed Feb 14, 2022
1 parent a223414 commit 2bc1eaf
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions SwiftBar/Utility/Environment.swift
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
import Foundation

class Environment {
static let shared = Environment()

enum Variables: String {
case swiftBar = "SWIFTBAR"
case swiftBarVersion = "SWIFTBAR_VERSION"
case swiftBarBuild = "SWIFTBAR_BUILD"
case swiftBarPluginsPath = "SWIFTBAR_PLUGINS_PATH"
case swiftBarPluginPath = "SWIFTBAR_PLUGIN_PATH"
case swiftBarPluginCachePath = "SWIFTBAR_PLUGIN_CACHE_PATH"
case swiftBarPluginDataPath = "SWIFTBAR_PLUGIN_DATA_PATH"
case swiftBarLaunchTime = "SWIFTBAR_LAUNCH_TIME"
case osVersionMajor = "OS_VERSION_MAJOR"
case osVersionMinor = "OS_VERSION_MINOR"
case osVersionPatch = "OS_VERSION_PATCH"
case osAppearance = "OS_APPEARANCE"
case osLastSleepTime = "OS_LAST_SLEEP_TIME"
case osLastWakeTime = "OS_LAST_WAKE_TIME"
}

private var dateFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone.current
return formatter
}()

private var systemEnv: [Variables: String] = [
.swiftBar: "1",
.swiftBarVersion: Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
.swiftBarBuild: Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "",
.swiftBarPluginsPath: PreferencesStore.shared.pluginDirectoryPath ?? "",
.osVersionMajor: String(ProcessInfo.processInfo.operatingSystemVersion.majorVersion),
.osVersionMinor: String(ProcessInfo.processInfo.operatingSystemVersion.minorVersion),
.osVersionPatch: String(ProcessInfo.processInfo.operatingSystemVersion.patchVersion),
]

var systemEnvStr: [String: String] {
Dictionary(uniqueKeysWithValues:
systemEnv.map { key, value in (key.rawValue, value) })
}

init() {
systemEnv[.swiftBarLaunchTime] = dateFormatter.string(from: NSDate.now)
}

func updateSleepTime(date: Date) {
systemEnv[.osLastSleepTime] = dateFormatter.string(from: date)
}

func updateWakeTime(date: Date) {
systemEnv[.osLastWakeTime] = dateFormatter.string(from: date)
}
}

0 comments on commit 2bc1eaf

Please sign in to comment.