mirror of
https://github.com/swiftbar/SwiftBar.git
synced 2024-10-27 00:50:16 +03:00
Add new environment variables #284
This commit is contained in:
parent
1b597a208a
commit
a223414b4b
@ -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
|
||||
|
||||
|
@ -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 = "<group>"; };
|
||||
39224E5625F52F7400BABF21 /* SwiftBar MAS.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = "SwiftBar MAS.entitlements"; sourceTree = "<group>"; };
|
||||
39224E5B25F530AF00BABF21 /* EnvConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvConfig.swift; sourceTree = "<group>"; };
|
||||
39224E5B25F530AF00BABF21 /* Environment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Environment.swift; sourceTree = "<group>"; };
|
||||
39294CD526934EDD00C6BB29 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/Localizable.strings; sourceTree = "<group>"; };
|
||||
393375E625507ADE0050AC28 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
|
||||
393375EB25508B320050AC28 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = "<group>"; };
|
||||
@ -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 */,
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)")
|
||||
})
|
||||
|
@ -1,30 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
enum Configuration {
|
||||
enum Error: Swift.Error {
|
||||
case missingKey, invalidValue
|
||||
}
|
||||
|
||||
static func value<T>(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
|
||||
}
|
||||
}
|
1
SwiftBar/Utility/Environment.swift
Normal file
1
SwiftBar/Utility/Environment.swift
Normal file
@ -0,0 +1 @@
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user