Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

feat: CLI interface + universal binary (Intel + Apple Silicon)

moamene2c395f4e9fc
diff --git a/PureMac.xcodeproj/project.pbxproj b/PureMac.xcodeproj/project.pbxproj
index f6fee55..58209e1 100644
--- a/PureMac.xcodeproj/project.pbxproj
+++ b/PureMac.xcodeproj/project.pbxproj
@@ -33,10 +33,12 @@
E60B0A2C5D0A6CAE35BF4DFB /* Locations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77D3D9A9BC52839E6D0A22BC /* Locations.swift */; };
EDEF28CAD23E936FBED1783B /* PureMacApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63581B70F9B10231964E3602 /* PureMacApp.swift */; };
F1F506C146E17D6CEC4A12F3 /* AppInfoFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23486A54A82EE3865B784D1C /* AppInfoFetcher.swift */; };
+ F2FA881A4B2209CC2A6342FB /* CLI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01B2C5F66B6D812572BD4F05 /* CLI.swift */; };
FDA30FE6349CA01C8D859F04 /* OrphanListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B71E8F62DB76D85F41F9A2E9 /* OrphanListView.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 01B2C5F66B6D812572BD4F05 /* CLI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLI.swift; sourceTree = "<group>"; };
02E502E2B5C6AECC76E5CFEF /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Localizable.strings"; sourceTree = "<group>"; };
10B0AF194677EAC1D5568785 /* CategoryDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryDetailView.swift; sourceTree = "<group>"; };
11BA091943263913276F3CCF /* SmartScanView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmartScanView.swift; sourceTree = "<group>"; };
@@ -183,6 +185,7 @@
E05E733EFBC4EFDA12BEC43B /* Utilities */ = {
isa = PBXGroup;
children = (
+ 01B2C5F66B6D812572BD4F05 /* CLI.swift */,
5785762276FB5E3209C6DE4D /* Logger.swift */,
);
path = Utilities;
@@ -308,6 +311,7 @@
75B5F0401D37F2872B1AD85A /* AppListView.swift in Sources */,
9BB5AAA574AFED6C27A3F8E2 /* AppPathFinder.swift in Sources */,
9AA80E035DF7B33F6EE118DF /* AppState.swift in Sources */,
+ F2FA881A4B2209CC2A6342FB /* CLI.swift in Sources */,
B52938BBD11842631314543D /* CategoryDetailView.swift in Sources */,
48D1431A7C99C19EEBDB056B /* CleaningEngine.swift in Sources */,
8947F0CE448791BD50EECF46 /* Conditions.swift in Sources */,
diff --git a/PureMac/Logic/Utilities/CLI.swift b/PureMac/Logic/Utilities/CLI.swift
new file mode 100644
index 0000000..96b4815
--- /dev/null
+++ b/PureMac/Logic/Utilities/CLI.swift
@@ -0,0 +1,167 @@
+import Foundation
+
+struct CLI {
+ static func run() -> Never {
+ let args = Array(CommandLine.arguments.dropFirst())
+
+ guard let command = args.first else {
+ printUsage()
+ exit(0)
+ }
+
+ switch command {
+ case "scan":
+ handleScan(args: Array(args.dropFirst()))
+ case "disk-info":
+ handleDiskInfo()
+ case "list":
+ handleList()
+ case "help", "--help", "-h":
+ printUsage()
+ case "version", "--version", "-v":
+ printVersion()
+ default:
+ printError("Unknown command: \(command)")
+ printUsage()
+ exit(1)
+ }
+ exit(0)
+ }
+
+ // MARK: - Commands
+
+ private static func handleScan(args: [String]) {
+ let json = args.contains("--json")
+ let categoryFilter = extractValue(for: "--category", in: args)
+
+ let engine = ScanEngine()
+ let categories: [CleaningCategory]
+
+ if let filter = categoryFilter {
+ guard let cat = CleaningCategory.scannable.first(where: {
+ $0.rawValue.lowercased().replacingOccurrences(of: " ", with: "") ==
+ filter.lowercased().replacingOccurrences(of: " ", with: "")
+ }) else {
+ printError("Unknown category: \(filter)")
+ print("Available: \(CleaningCategory.scannable.map(\.rawValue).joined(separator: ", "))")
+ exit(1)
+ }
+ categories = [cat]
+ } else {
+ categories = CleaningCategory.scannable
+ }
+
+ var allResults: [(String, Int, Int64)] = []
+
+ let group = DispatchGroup()
+ let queue = DispatchQueue(label: "cli.scan")
+
+ for category in categories {
+ group.enter()
+ Task {
+ let result = await engine.scanCategory(category)
+ queue.sync {
+ allResults.append((category.rawValue, result.itemCount, result.totalSize))
+ }
+ group.leave()
+ }
+ }
+ group.wait()
+
+ if json {
+ printJSON(allResults)
+ } else {
+ printTable(allResults)
+ }
+ }
+
+ private static func handleDiskInfo() {
+ let engine = ScanEngine()
+ var info = DiskInfo()
+ let group = DispatchGroup()
+ group.enter()
+ Task {
+ info = await engine.getDiskInfo()
+ group.leave()
+ }
+ group.wait()
+
+ print("Disk Usage:")
+ print(" Total: \(info.formattedTotal)")
+ print(" Used: \(info.formattedUsed)")
+ print(" Free: \(info.formattedFree)")
+ if info.purgeableSpace > 0 {
+ print(" Purgeable: \(info.formattedPurgeable)")
+ }
+ }
+
+ private static func handleList() {
+ let apps = AppInfoFetcher.shared.fetchInstalledApps()
+ print("Installed Apps (\(apps.count)):")
+ for app in apps {
+ let size = ByteCountFormatter.string(fromByteCount: app.size, countStyle: .file)
+ print(" \(app.appName.padding(toLength: 35, withPad: " ", startingAt: 0)) \(size.padding(toLength: 12, withPad: " ", startingAt: 0)) \(app.bundleIdentifier)")
+ }
+ }
+
+ // MARK: - Output
+
+ private static func printTable(_ results: [(String, Int, Int64)]) {
+ var totalSize: Int64 = 0
+ var totalItems = 0
+
+ print("Category Items Size")
+ print("---------------------- ----- --------")
+ for (name, count, size) in results {
+ let sizeStr = ByteCountFormatter.string(fromByteCount: size, countStyle: .file)
+ print("\(name.padding(toLength: 22, withPad: " ", startingAt: 0)) \(String(count).padding(toLength: 5, withPad: " ", startingAt: 0)) \(sizeStr)")
+ totalSize += size
+ totalItems += count
+ }
+ print("---------------------- ----- --------")
+ let totalStr = ByteCountFormatter.string(fromByteCount: totalSize, countStyle: .file)
+ print("Total \(String(totalItems).padding(toLength: 5, withPad: " ", startingAt: 0)) \(totalStr)")
+ }
+
+ private static func printJSON(_ results: [(String, Int, Int64)]) {
+ var entries: [String] = []
+ for (name, count, size) in results {
+ entries.append(" {\"category\": \"\(name)\", \"items\": \(count), \"bytes\": \(size)}")
+ }
+ print("[\n\(entries.joined(separator: ",\n"))\n]")
+ }
+
+ private static func printUsage() {
+ print("""
+ PureMac CLI
+
+ Usage: puremac <command> [options]
+
+ Commands:
+ scan Scan all categories
+ scan --category <name> Scan a specific category
+ scan --json Output as JSON
+ disk-info Show disk usage
+ list List installed apps
+ version Show version
+ help Show this help
+
+ Categories:
+ \(CleaningCategory.scannable.map(\.rawValue).joined(separator: ", "))
+ """)
+ }
+
+ private static func printVersion() {
+ let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "2.0.0"
+ print("PureMac \(version)")
+ }
+
+ private static func printError(_ message: String) {
+ FileHandle.standardError.write(Data("Error: \(message)\n".utf8))
+ }
+
+ private static func extractValue(for flag: String, in args: [String]) -> String? {
+ guard let index = args.firstIndex(of: flag), index + 1 < args.count else { return nil }
+ return args[index + 1]
+ }
+}
diff --git a/PureMac/PureMacApp.swift b/PureMac/PureMacApp.swift
index 36150a0..68fd00e 100644
--- a/PureMac/PureMacApp.swift
+++ b/PureMac/PureMacApp.swift
@@ -15,6 +15,12 @@ struct PureMacApp: App {
@StateObject private var appState = AppState()
@AppStorage("PureMac.OnboardingComplete") private var onboardingComplete = false
+ init() {
+ if CommandLine.arguments.count > 1 {
+ CLI.run()
+ }
+ }
+
var body: some Scene {
WindowGroup {
if onboardingComplete {