Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Show a global key a profile forces as the profile forces it

Țugui Dragoș26c80b18c44c
diff --git a/Packages/PeelCore/Sources/PeelCore/Tweaks/TweakStore.swift b/Packages/PeelCore/Sources/PeelCore/Tweaks/TweakStore.swift
index a93c30e9..8e7c0467 100644
--- a/Packages/PeelCore/Sources/PeelCore/Tweaks/TweakStore.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Tweaks/TweakStore.swift
@@ -44,8 +44,8 @@ public struct TweakStore: Sendable, TweakStoring {
let isManaged = Self.isForced(key, in: tweak.domain)
let layers = layers(of: tweak)
- // A managed value comes before every layer, and only the "App" functions read it.
- guard let current = isManaged ? Self.copy(key, from: tweak.domain) : layers.inEffect else {
+ // A managed value comes before every layer.
+ guard let current = isManaged ? Self.forcedValue(key, in: tweak.domain) : layers.inEffect else {
return TweakState(isOn: false, isManaged: isManaged, text: nil)
}
@@ -194,15 +194,33 @@ public struct TweakStore: Sendable, TweakStoring {
/// is an "App" function, so for the global domain this reads the managed preferences files directly.
static func isForced(_ key: CFString, in name: String) -> Bool {
guard name == globalDomain else { return CFPreferencesAppValueIsForced(key, name as CFString) }
- let user = NSUserName()
- return ["/Library/Managed Preferences/\(user)/.GlobalPreferences.plist", "/Library/Managed Preferences/.GlobalPreferences.plist"]
- .contains { path in
- guard
- let data = FileManager.default.contents(atPath: path),
- let plist = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
- else { return false }
- return plist[key as String] != nil
- }
+ return forcedGlobalValue(key, in: managedGlobalFiles) != nil
+ }
+
+ /// The value a profile forces. The function that reads the person's own global layer doesn't search the managed
+ /// one (`CFPreferences.h`), so for the global domain it comes from the managed files.
+ private static func forcedValue(_ key: CFString, in name: String) -> CFPropertyList? {
+ name == globalDomain
+ ? forcedGlobalValue(key, in: managedGlobalFiles)
+ : CFPreferencesCopyAppValue(key, name as CFString)
+ }
+
+ private static var managedGlobalFiles: [String] {
+ [
+ "/Library/Managed Preferences/\(NSUserName())/.GlobalPreferences.plist",
+ "/Library/Managed Preferences/.GlobalPreferences.plist",
+ ]
+ }
+
+ static func forcedGlobalValue(_ key: CFString, in paths: [String]) -> CFPropertyList? {
+ for path in paths {
+ guard let data = FileManager.default.contents(atPath: path),
+ let plist = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
+ let value = plist[key as String]
+ else { continue }
+ return value as CFPropertyList
+ }
+ return nil
}
static func property(_ value: Tweak.Value) -> CFPropertyList {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TweakTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TweakTests.swift
index 87c023cb..d4341622 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TweakTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TweakTests.swift
@@ -20,6 +20,22 @@ struct TweakTests {
/// The row says so rather than asking for a log out, which would work but is not needed.
/// Apple documents only a few of these keys; the rest are known from use alone, so a release can rename or ignore
/// one without a word. Each tweak says which it is, and a documented one names Apple's page.
+ @Test func aGlobalKeyAProfileForcesReadsAsWhatTheProfileForces() throws {
+ let directory = try TemporaryDirectory()
+ let forced = directory.url.appending(path: "forced.plist")
+ let list: [String: Any] = ["NSAutomaticSpellingCorrectionEnabled": false, "AppleShowScrollBars": "WhenScrolling"]
+ try PropertyListSerialization.data(fromPropertyList: list, format: .xml, options: 0).write(to: forced)
+ let paths = ["/org.example/none.plist", forced.path(percentEncoded: false)]
+
+ let spelling = TweakStore.forcedGlobalValue("NSAutomaticSpellingCorrectionEnabled" as CFString, in: paths)
+ let scrollBars = TweakStore.forcedGlobalValue("AppleShowScrollBars" as CFString, in: paths)
+ let other = TweakStore.forcedGlobalValue("org.example.other" as CFString, in: paths)
+
+ #expect(spelling.map { TweakStore.matches($0, .boolean(false)) } == true)
+ #expect(scrollBars as? String == "WhenScrolling")
+ #expect(other == nil)
+ }
+
@Test func everyTweakSaysWhereAppleDocumentsIt() {
var documented: Set<String> = []
for tweak in TweakCatalog.all {