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.

Export List of Apps is one menu in the toolbar and in File

Țugui Dragoș27e59879eef5
diff --git a/Peel/Model/InventoryExport.swift b/Peel/Model/InventoryExport.swift
index c7ea0333..a55408dc 100644
--- a/Peel/Model/InventoryExport.swift
+++ b/Peel/Model/InventoryExport.swift
@@ -1,14 +1,31 @@
import AppKit
import Foundation
import PeelCore
+import SwiftUI
import UniformTypeIdentifiers
/// Writes out what is installed and where it came from, to a file the user picks.
enum InventoryExport {
- /// Asks the user where to save the inventory, then writes it there. Returns nil when the file was written
- /// or the user canceled, and the error message otherwise.
+ /// Whether the list can be written: once the apps are read, and while Homebrew is installed, once its list is
+ /// too, since without it every cask app's source would read "Unknown".
@MainActor
- static func run(format: Inventory.Format, apps: [InstalledApp], homebrew: HomebrewLibrary) async -> String? {
+ static func canExport(library: AppLibrary, homebrew: HomebrewLibrary) -> Bool {
+ library.hasLoaded && !(homebrew.isInstalled && homebrew.packages == nil)
+ }
+
+ /// Asks the user where to save the list, writes it there, and says in an alert when it could not.
+ @MainActor
+ static func save(_ format: Inventory.Format, apps: [InstalledApp], homebrew: HomebrewLibrary) async {
+ guard let failure = await run(format: format, apps: apps, homebrew: homebrew) else { return }
+ let alert = NSAlert()
+ alert.messageText = String(localized: "The list couldn’t be saved.")
+ alert.informativeText = failure
+ alert.runModal()
+ }
+
+ /// Returns nil when the file was written or the user canceled, and the error message otherwise.
+ @MainActor
+ private static func run(format: Inventory.Format, apps: [InstalledApp], homebrew: HomebrewLibrary) async -> String? {
let contents: String
var brewfile: String?
// Homebrew writes the Brewfile, asked only once it has answered, so its definitions are on this Mac.
@@ -58,6 +75,20 @@ enum InventoryExport {
}
}
+/// The formats of Export List of Apps, as the toolbar and the File menu both offer them.
+struct ExportListMenuContent: View {
+ let library: AppLibrary
+ let homebrew: HomebrewLibrary
+
+ var body: some View {
+ ForEach(Inventory.Format.offered(by: homebrew), id: \.self) { format in
+ Button(String(localized: format.title)) {
+ Task { await InventoryExport.save(format, apps: library.apps, homebrew: homebrew) }
+ }
+ }
+ }
+}
+
extension Inventory.Format {
/// What Export List of Apps offers: a Brewfile only while Homebrew is installed and not too old to write one.
@MainActor
diff --git a/Peel/Navigation/PeelCommands.swift b/Peel/Navigation/PeelCommands.swift
index 3a14df28..eedc15f2 100644
--- a/Peel/Navigation/PeelCommands.swift
+++ b/Peel/Navigation/PeelCommands.swift
@@ -104,11 +104,9 @@ struct PeelCommands: Commands {
.disabled(moveToTrash?.isEnabled != true || textEditing.isEditing)
Divider()
Menu("Export List of Apps") {
- ForEach(Inventory.Format.offered(by: homebrew), id: \.self) { format in
- Button(String(localized: format.title)) { export(as: format) }
- }
+ ExportListMenuContent(library: library, homebrew: homebrew)
}
- .disabled(!library.hasLoaded || (homebrew.isInstalled && homebrew.packages == nil))
+ .disabled(!InventoryExport.canExport(library: library, homebrew: homebrew))
}
CommandGroup(before: .textEditing) {
@@ -151,15 +149,3 @@ struct PeelCommands: Commands {
}
}
-extension PeelCommands {
- private func export(as format: Inventory.Format) {
- Task {
- let failure = await InventoryExport.run(format: format, apps: library.apps, homebrew: homebrew)
- guard let failure else { return }
- let alert = NSAlert()
- alert.messageText = String(localized: "The list couldn’t be saved.")
- alert.informativeText = failure
- alert.runModal()
- }
- }
-}
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index 16157ac2..56f75fb4 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -143,31 +143,15 @@ private struct TeamRecordNotice: View {
private struct ExportMenu: View {
@Environment(AppLibrary.self) private var library
@Environment(HomebrewLibrary.self) private var homebrew
- @State private var failure: String?
var body: some View {
Menu {
- // The whole menu waits for Homebrew's list, because without it every cask app's source would read
- // "Unknown".
- ForEach(Inventory.Format.offered(by: homebrew), id: \.self) { format in
- Button(String(localized: format.title)) {
- Task { failure = await InventoryExport.run(format: format, apps: library.apps, homebrew: homebrew) }
- }
- }
+ ExportListMenuContent(library: library, homebrew: homebrew)
} label: {
ToolbarMenuLabel(title: "Export List of Apps", systemImage: "doc.badge.arrow.up")
}
- .disabled(!library.hasLoaded || (homebrew.isInstalled && homebrew.packages == nil))
+ .disabled(!InventoryExport.canExport(library: library, homebrew: homebrew))
.help(Text("Save a list of what is installed and where it came from"))
- .alert("The list couldn’t be saved.", isPresented: isShowingFailure) {
- Button("OK", role: .cancel) {}
- } message: {
- Text(verbatim: failure ?? "")
- }
- }
-
- private var isShowingFailure: Binding<Bool> {
- Binding(get: { failure != nil }, set: { if !$0 { failure = nil } })
}
}