From a223414b4b1c7d162dbfea6ebacf1d7ced18992c Mon Sep 17 00:00:00 2001 From: Alex Mazanov Date: Mon, 14 Feb 2022 07:27:29 -0800 Subject: [PATCH] Add new environment variables #284 --- README.md | 9 +++++--- SwiftBar.xcodeproj/project.pbxproj | 12 +++++------ SwiftBar/AppDelegate.swift | 3 +++ SwiftBar/Plugin/Plugin.swift | 8 ++++---- SwiftBar/UI/Debug/DebugView.swift | 3 ++- SwiftBar/Utility/EnvConfig.swift | 30 --------------------------- SwiftBar/Utility/Environment.swift | 1 + SwiftBar/Utility/RunScript.swift | 33 +++--------------------------- 8 files changed, 25 insertions(+), 74 deletions(-) delete mode 100644 SwiftBar/Utility/EnvConfig.swift create mode 100644 SwiftBar/Utility/Environment.swift diff --git a/README.md b/README.md index 346b3f0..2af267b 100644 --- a/README.md +++ b/README.md @@ -168,10 +168,13 @@ When running a plugin, SwiftBar sets the following environment variables: | `SWIFTBAR_PLUGIN_PATH` | The path to the running plugin | | `SWIFTBAR_PLUGIN_CACHE_PATH` | The cache to data folder, individual per plugin | | `SWIFTBAR_PLUGIN_DATA_PATH` | The path to data folder, individual per plugin | +| `SWIFTBAR_LAUNCH_TIME` | SwiftBar launch date and time, ISO8601 | | `OS_APPEARANCE` | Current macOS appearance (`Light` or `Dark`) | -| `OS_VERSION_MAJOR` | The first part of the macOS version (e.g., `11` for macOS 11.0.1). | -| `OS_VERSION_MINOR` | The second part of the macOS version (e.g., `0` for macOS 11.0.1). | -| `OS_VERSION_PATCH` | The third part of the macOS version (e.g., `1` for macOS 11.0.1). | +| `OS_VERSION_MAJOR` | The first part of the macOS version (e.g., `11` for macOS 11.0.1) | +| `OS_VERSION_MINOR` | The second part of the macOS version (e.g., `0` for macOS 11.0.1) | +| `OS_VERSION_PATCH` | The third part of the macOS version (e.g., `1` for macOS 11.0.1) | +| `OS_LAST_SLEEP_TIME` | Last OS sleep date and time, ISO8601. Empty if OS didn't sleep since SwiftBar launch. | +| `OS_LAST_WAKE_TIME` | Last OS wake from sleep date and time, ISO8601. Empty if OS didn't sleep since SwiftBar launch. | ### Script Metadata diff --git a/SwiftBar.xcodeproj/project.pbxproj b/SwiftBar.xcodeproj/project.pbxproj index 67bb7be..ea51b98 100644 --- a/SwiftBar.xcodeproj/project.pbxproj +++ b/SwiftBar.xcodeproj/project.pbxproj @@ -62,8 +62,8 @@ 39224E2125F4344600BABF21 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 393375EB25508B320050AC28 /* Credits.rtf */; }; 39224E2225F4344600BABF21 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3920748125460FD300213DBE /* Assets.xcassets */; }; 39224E3325F4351600BABF21 /* SwiftBarMAS.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 39224E3225F4351300BABF21 /* SwiftBarMAS.xcconfig */; }; - 39224E5C25F530AF00BABF21 /* EnvConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39224E5B25F530AF00BABF21 /* EnvConfig.swift */; }; - 39224E5F25F5317800BABF21 /* EnvConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39224E5B25F530AF00BABF21 /* EnvConfig.swift */; }; + 39224E5C25F530AF00BABF21 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39224E5B25F530AF00BABF21 /* Environment.swift */; }; + 39224E5F25F5317800BABF21 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39224E5B25F530AF00BABF21 /* Environment.swift */; }; 393375E725507ADE0050AC28 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 393375E625507ADE0050AC28 /* Log.swift */; }; 393375EC25508B320050AC28 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 393375EB25508B320050AC28 /* Credits.rtf */; }; 396257262569667000341A19 /* PluginRepositoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 396257252569667000341A19 /* PluginRepositoryView.swift */; }; @@ -165,7 +165,7 @@ 39224E2A25F4344600BABF21 /* SwiftBar MAS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SwiftBar MAS.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 39224E3225F4351300BABF21 /* SwiftBarMAS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = SwiftBarMAS.xcconfig; sourceTree = ""; }; 39224E5625F52F7400BABF21 /* SwiftBar MAS.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = "SwiftBar MAS.entitlements"; sourceTree = ""; }; - 39224E5B25F530AF00BABF21 /* EnvConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvConfig.swift; sourceTree = ""; }; + 39224E5B25F530AF00BABF21 /* Environment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Environment.swift; sourceTree = ""; }; 39294CD526934EDD00C6BB29 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/Localizable.strings; sourceTree = ""; }; 393375E625507ADE0050AC28 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = ""; }; 393375EB25508B320050AC28 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = ""; }; @@ -401,7 +401,7 @@ 39B84A1E257D436200FA012E /* RunScript.swift */, ABF91B7C2589C7920020590A /* NSFont+Offset.swift */, 39224DD425F414F500BABF21 /* Notification+Extension.swift */, - 39224E5B25F530AF00BABF21 /* EnvConfig.swift */, + 39224E5B25F530AF00BABF21 /* Environment.swift */, FA58E39927AD855D0034837F /* SystemNotificationName.swift */, ); path = Utility; @@ -748,7 +748,7 @@ ABF91B7D2589C7920020590A /* NSFont+Offset.swift in Sources */, 398B86CA254DB5D200DEA027 /* DirectoryObserver.swift in Sources */, 39B84A22257D63F700FA012E /* NSMutableAttributedString+SFSymbols.swift in Sources */, - 39224E5C25F530AF00BABF21 /* EnvConfig.swift in Sources */, + 39224E5C25F530AF00BABF21 /* Environment.swift in Sources */, FAA14A112728C6E90052FDB8 /* EnablePluginIntentHandler.swift in Sources */, 39224DD225F3F19B00BABF21 /* AppDelegate+Toolbar.swift in Sources */, 39641EAD254E0E3400713DAF /* ImageLoader.swift in Sources */, @@ -806,7 +806,7 @@ 39224E0725F4344600BABF21 /* NSFont+Offset.swift in Sources */, 39224E0825F4344600BABF21 /* DirectoryObserver.swift in Sources */, 39224E0925F4344600BABF21 /* NSMutableAttributedString+SFSymbols.swift in Sources */, - 39224E5F25F5317800BABF21 /* EnvConfig.swift in Sources */, + 39224E5F25F5317800BABF21 /* Environment.swift in Sources */, FA57F2A6272A308F00817A79 /* EnablePluginIntentHandler.swift in Sources */, 39224E0A25F4344600BABF21 /* AppDelegate+Toolbar.swift in Sources */, FA57F2A9272A309600817A79 /* Intents.intentdefinition in Sources */, diff --git a/SwiftBar/AppDelegate.swift b/SwiftBar/AppDelegate.swift index 904cd0a..40b2ac2 100644 --- a/SwiftBar/AppDelegate.swift +++ b/SwiftBar/AppDelegate.swift @@ -25,6 +25,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegat var pluginManager: PluginManager! let prefs = PreferencesStore.shared + let sharedEnv = Environment.shared #if !MAC_APP_STORE var softwareUpdater: SPUUpdater! #endif @@ -76,6 +77,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegat object: nil, queue: OperationQueue.main) { [weak self] _ in os_log("Mac is going to sleep", log: Log.plugin, type: .info) + self?.sharedEnv.updateSleepTime(date: NSDate.now) self?.pluginManager.terminateAllPlugins() } @@ -83,6 +85,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegat object: nil, queue: OperationQueue.main) { [weak self] _ in os_log("Mac waked up", log: Log.plugin, type: .info) + self?.sharedEnv.updateWakeTime(date: NSDate.now) self?.pluginManager.startAllPlugins() } } diff --git a/SwiftBar/Plugin/Plugin.swift b/SwiftBar/Plugin/Plugin.swift index 846b524..a11a7a4 100644 --- a/SwiftBar/Plugin/Plugin.swift +++ b/SwiftBar/Plugin/Plugin.swift @@ -102,10 +102,10 @@ extension Plugin { var env: [String: String] { var pluginEnv = [ - EnvironmentVariables.swiftBarPluginPath.rawValue: file, - EnvironmentVariables.osAppearance.rawValue: AppShared.isDarkTheme ? "Dark" : "Light", - EnvironmentVariables.swiftBarPluginCachePath.rawValue: cacheDirectoryPath, - EnvironmentVariables.swiftBarPluginDataPath.rawValue: dataDirectoryPath, + Environment.Variables.swiftBarPluginPath.rawValue: file, + Environment.Variables.osAppearance.rawValue: AppShared.isDarkTheme ? "Dark" : "Light", + Environment.Variables.swiftBarPluginCachePath.rawValue: cacheDirectoryPath, + Environment.Variables.swiftBarPluginDataPath.rawValue: dataDirectoryPath, ] metadata?.environment.forEach { k, v in pluginEnv[k] = v diff --git a/SwiftBar/UI/Debug/DebugView.swift b/SwiftBar/UI/Debug/DebugView.swift index 7bbad05..5a6b0ea 100644 --- a/SwiftBar/UI/Debug/DebugView.swift +++ b/SwiftBar/UI/Debug/DebugView.swift @@ -2,6 +2,7 @@ import SwiftUI struct DebugView: View { let plugin: Plugin + let sharedEnv = Environment.shared @ObservedObject var debugInfo: PluginDebugInfo var debugText: String { String(debugInfo.events.sorted(by: { $0.key < $1.key }).map { "\nšŸ• \($0.key) \($0.value.eventString)" } @@ -51,7 +52,7 @@ struct DebugView: View { Button("Print SwiftBar ENV", action: { let envs = plugin.env - let swiftbarEnv = systemEnvStr.merging(envs) { current, _ in current } + let swiftbarEnv = sharedEnv.systemEnvStr.merging(envs) { current, _ in current } let debugString = swiftbarEnv.map { "\($0.key) = \($0.value)" }.sorted().joined(separator: "\n") debugInfo.addEvent(type: .Environment, value: "\n\(debugString)") }) diff --git a/SwiftBar/Utility/EnvConfig.swift b/SwiftBar/Utility/EnvConfig.swift deleted file mode 100644 index f286090..0000000 --- a/SwiftBar/Utility/EnvConfig.swift +++ /dev/null @@ -1,30 +0,0 @@ -import Foundation - -enum Configuration { - enum Error: Swift.Error { - case missingKey, invalidValue - } - - static func value(for key: String) throws -> T where T: LosslessStringConvertible { - guard let object = Bundle.main.object(forInfoDictionaryKey: key) else { - throw Error.missingKey - } - - switch object { - case let value as T: - return value - case let string as String: - guard let value = T(string) else { fallthrough } - return value - default: - throw Error.invalidValue - } - } -} - -enum EnvConfig { - static var isMacAppstore: Bool { - let value = try? Bool(Configuration.value(for: "MAC_APP_STORE")) - return value ?? false - } -} diff --git a/SwiftBar/Utility/Environment.swift b/SwiftBar/Utility/Environment.swift new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/SwiftBar/Utility/Environment.swift @@ -0,0 +1 @@ + diff --git a/SwiftBar/Utility/RunScript.swift b/SwiftBar/Utility/RunScript.swift index 82fd065..7053f40 100644 --- a/SwiftBar/Utility/RunScript.swift +++ b/SwiftBar/Utility/RunScript.swift @@ -2,37 +2,10 @@ import Dispatch import Foundation import os -enum EnvironmentVariables: 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 osVersionMajor = "OS_VERSION_MAJOR" - case osVersionMinor = "OS_VERSION_MINOR" - case osVersionPatch = "OS_VERSION_PATCH" - case osAppearance = "OS_APPEARANCE" -} - -private let systemEnv: [EnvironmentVariables: 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) }) -} +let sharedEnv = Environment.shared func getEnvExportString(env: [String: String]) -> String { - let dict = systemEnvStr.merging(env) { current, _ in current } + let dict = sharedEnv.systemEnvStr.merging(env) { current, _ in current } return "export \(dict.map { "\($0.key)='\($0.value)'" }.joined(separator: " "))" } @@ -44,7 +17,7 @@ func getEnvExportString(env: [String: String]) -> String { streamOutput: Bool = false, onOutputUpdate: @escaping (String?) -> Void = { _ in }) throws -> String { - let swiftbarEnv = systemEnvStr.merging(env) { current, _ in current } + let swiftbarEnv = sharedEnv.systemEnvStr.merging(env) { current, _ in current } process.environment = swiftbarEnv.merging(ProcessInfo.processInfo.environment) { current, _ in current } return try process.launchScript(with: command, args: args, runInBash: runInBash, streamOutput: streamOutput, onOutputUpdate: onOutputUpdate) }