diff --git a/README.md b/README.md index add3199..10dc551 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ Use it in CI with [mobai-ci](https://github.com/MobAI-App/mobai-ci). The SwiftUI app bundles the CLI and adds: - Searchable simulator status, disk-size, and live RAM columns. -- Searchable service profiles with per-daemon controls and purpose summaries. +- Searchable service profiles with per-daemon controls and purpose summaries, + exportable as a [profile file](#profile-files). - Read-only disk analysis plus confirmed cleanup of allowlisted data. - Clone, rename, erase, delete, and Finder shortcuts. @@ -209,6 +210,9 @@ arrow keys and space to tick whole features to keep enabled, or press `→` to o a feature and keep individual daemons within it. Point it at a directory to save `.json` there, or omit the path to print to stdout. +In the macOS app, pick the services to keep and click **Export…** to save the +same selection as a profile file. + ### Checking a simulator with doctor Slimming a simulator turns features off on purpose, so a test suite that needs diff --git a/gui/AppModel.swift b/gui/AppModel.swift index a5c9545..6722b9e 100644 --- a/gui/AppModel.swift +++ b/gui/AppModel.swift @@ -286,6 +286,31 @@ final class AppModel: ObservableObject { keptServiceLabels.removeAll() } + /// The profile Apply would use, as a file `simslim on --profile` reads. A daemon + /// a kept category already covers is not listed again in "keep". + func profileFileData() throws -> Data { + let covered = categories.filter { keptCategoryIDs.contains($0.id) } + .reduce(into: Set()) { $0.formUnion($1.labels) } + let except = keptCategoryIDs.sorted() + let keep = keptServiceLabels.subtracting(covered).sorted() + guard !except.isEmpty || !keep.isEmpty else { return Data("{}\n".utf8) } + + let encoder = JSONEncoder() + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] + let profile = SlimProfileFile( + except: except.isEmpty ? nil : except, keep: keep.isEmpty ? nil : keep) + return try encoder.encode(profile) + Data("\n".utf8) + } + + func exportProfile(to url: URL) { + do { + try profileFileData().write(to: url, options: .atomic) + record(.success, "Exported the service profile to \(url.path)") + } catch { + recordFailure("Profile export failed: \(error.localizedDescription)", present: true) + } + } + func setDiskCleanupCategory(_ category: DiskCleanupCategory, selected: Bool) { guard category.canClean else { return } if selected { diff --git a/gui/ContentView.swift b/gui/ContentView.swift index 89d86e8..e266881 100644 --- a/gui/ContentView.swift +++ b/gui/ContentView.swift @@ -741,6 +741,11 @@ private struct ProfileSidebar: View { Text("Services to keep") .font(.headline) Spacer() + Button("Export…") { exportProfile() } + .buttonStyle(.plain) + .foregroundStyle(.tint) + .disabled(model.categories.isEmpty) + .help("Save these services as a profile file for simslim on --profile") Button("Full Slim") { model.resetProfile() } .buttonStyle(.plain) .foregroundStyle(.tint) @@ -872,6 +877,15 @@ private struct ProfileSidebar: View { } } + private func exportProfile() { + let panel = NSSavePanel() + panel.allowedContentTypes = [.json] + panel.nameFieldStringValue = "profile.json" + panel.message = "Apply it with simslim on --profile ." + guard panel.runModal() == .OK, let url = panel.url else { return } + model.exportProfile(to: url) + } + private var stateLegend: some View { VStack(alignment: .leading, spacing: 8) { Text("Service state") diff --git a/gui/Models.swift b/gui/Models.swift index 2bf3aff..28f6d88 100644 --- a/gui/Models.swift +++ b/gui/Models.swift @@ -62,6 +62,12 @@ struct AlwaysEnabledService: Decodable, Identifiable, Equatable { var id: String { label } } +/// A profile file for `simslim on --profile`; mirrors SlimProfile in profile_file.go. +struct SlimProfileFile: Encodable { + let except: [String]? + let keep: [String]? +} + struct SimulatorMeasurement: Decodable, Equatable { let processes: Int let bytes: Int64