2018-12-27 22:24:20 +00:00
|
|
|
//
|
|
|
|
// MasAppLibrary.swift
|
|
|
|
// MasKit
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 12/27/18.
|
|
|
|
// Copyright © 2018 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CommerceKit
|
|
|
|
|
|
|
|
/// Utility for managing installed apps.
|
|
|
|
public class MasAppLibrary: AppLibrary {
|
|
|
|
/// CommerceKit's singleton manager of installed software.
|
2020-03-29 03:15:07 +00:00
|
|
|
private let softwareMap: SoftwareMap
|
2018-12-27 22:24:20 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Array of installed software products.
|
|
|
|
public lazy var installedApps: [SoftwareProduct] = {
|
2020-03-29 03:15:07 +00:00
|
|
|
return softwareMap.allSoftwareProducts()
|
2018-12-28 22:08:31 +00:00
|
|
|
}()
|
|
|
|
|
2020-03-29 03:15:07 +00:00
|
|
|
/// Internal initializer for providing a mock software map.
|
|
|
|
/// - Parameter softwareMap: SoftwareMap to use
|
|
|
|
init(softwareMap: SoftwareMap = CKSoftwareMap.shared()) {
|
|
|
|
self.softwareMap = softwareMap
|
|
|
|
}
|
2018-12-27 22:24:20 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Finds an app using a bundle identifier.
|
2018-12-27 22:24:20 +00:00
|
|
|
///
|
2018-12-28 22:08:31 +00:00
|
|
|
/// - Parameter bundleId: Bundle identifier of app.
|
2018-12-27 22:24:20 +00:00
|
|
|
/// - Returns: Software Product of app if found; nil otherwise.
|
2018-12-28 22:08:31 +00:00
|
|
|
public func installedApp(forBundleId bundleId: String) -> SoftwareProduct? {
|
2020-03-29 03:15:07 +00:00
|
|
|
return softwareMap.product(for: bundleId)
|
2018-12-27 22:24:20 +00:00
|
|
|
}
|
2018-12-27 23:07:35 +00:00
|
|
|
|
|
|
|
/// Uninstalls an app.
|
|
|
|
///
|
|
|
|
/// - Parameter app: App to be removed.
|
|
|
|
/// - Throws: Error if there is a problem.
|
|
|
|
public func uninstallApp(app: SoftwareProduct) throws {
|
2019-01-21 05:14:11 +00:00
|
|
|
if !userIsRoot() {
|
|
|
|
printWarning("Apps installed from the Mac App Store require root permission to remove.")
|
|
|
|
}
|
|
|
|
|
2019-01-21 04:13:53 +00:00
|
|
|
let fileManager = FileManager()
|
|
|
|
let appUrl = URL(fileURLWithPath: app.bundlePath)
|
|
|
|
|
2019-01-03 07:16:45 +00:00
|
|
|
do {
|
2019-01-21 04:50:15 +00:00
|
|
|
var trashUrl: NSURL?
|
|
|
|
try withUnsafeMutablePointer(to: &trashUrl) { (mutablePointer: UnsafeMutablePointer<NSURL?>) in
|
|
|
|
let pointer = AutoreleasingUnsafeMutablePointer<NSURL?>(mutablePointer)
|
|
|
|
|
|
|
|
// Move item to trash
|
|
|
|
try fileManager.trashItem(at: appUrl, resultingItemURL: pointer)
|
|
|
|
|
|
|
|
if let url = pointer.pointee, let path = url.path {
|
|
|
|
printInfo("App moved to trash: \(path)")
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 07:16:45 +00:00
|
|
|
} catch {
|
2019-01-21 04:13:53 +00:00
|
|
|
printError("Unable to move app to trash.")
|
2019-01-03 07:16:45 +00:00
|
|
|
throw MASError.uninstallFailed
|
2018-12-27 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 05:14:11 +00:00
|
|
|
|
|
|
|
/// Detects whether the current user is root.
|
|
|
|
///
|
|
|
|
/// - Returns: true if the current user is root; false otherwise
|
|
|
|
private func userIsRoot() -> Bool {
|
|
|
|
return NSUserName() == "root"
|
|
|
|
}
|
2018-12-27 22:24:20 +00:00
|
|
|
}
|