2018-12-27 22:24:20 +00:00
|
|
|
//
|
|
|
|
// AppLibrary.swift
|
|
|
|
// MasKit
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 12/27/18.
|
|
|
|
// Copyright © 2018 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Utility for managing installed apps.
|
|
|
|
public protocol AppLibrary {
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Entire set of installed apps.
|
|
|
|
var installedApps: [SoftwareProduct] { get }
|
|
|
|
|
|
|
|
/// Map of app name to ID.
|
|
|
|
var appIdsByName: [String: UInt64] { get }
|
|
|
|
|
|
|
|
/// Finds an app by ID.
|
|
|
|
///
|
|
|
|
/// - Parameter forId: MAS ID for app.
|
|
|
|
/// - Returns: Software Product of app if found; nil otherwise.
|
|
|
|
func installedApp(forId: UInt64) -> SoftwareProduct?
|
|
|
|
|
|
|
|
/// Finds an app by it's bundle identifier.
|
|
|
|
///
|
|
|
|
/// - Parameter forBundleId: Bundle identifier of app.
|
|
|
|
/// - Returns: Software Product of app if found; nil otherwise.
|
|
|
|
func installedApp(forBundleId: String) -> SoftwareProduct?
|
|
|
|
|
|
|
|
/// Finds an app by name.
|
2018-12-27 22:24:20 +00:00
|
|
|
///
|
2018-12-28 22:08:31 +00:00
|
|
|
/// - Parameter named: Name 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
|
|
|
func installedApp(named: String) -> SoftwareProduct?
|
2018-12-27 23:07:35 +00:00
|
|
|
|
|
|
|
/// Uninstalls an app.
|
|
|
|
///
|
|
|
|
/// - Parameter app: App to be removed.
|
|
|
|
/// - Throws: Error if there is a problem.
|
|
|
|
func uninstallApp(app: SoftwareProduct) throws
|
2018-12-27 22:24:20 +00:00
|
|
|
}
|
2018-12-28 22:08:31 +00:00
|
|
|
|
|
|
|
/// Common logic
|
|
|
|
extension AppLibrary {
|
|
|
|
/// Map of app name to ID.
|
|
|
|
public var appIdsByName: [String: UInt64] {
|
|
|
|
get {
|
|
|
|
var destMap = [String: UInt64]()
|
|
|
|
for product in installedApps {
|
|
|
|
destMap[product.appName] = product.itemIdentifier.uint64Value
|
|
|
|
}
|
|
|
|
return destMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds an app by name.
|
|
|
|
///
|
|
|
|
/// - Parameter id: MAS ID for app.
|
|
|
|
/// - Returns: Software Product of app if found; nil otherwise.
|
|
|
|
public func installedApp(forId id: UInt64) -> SoftwareProduct? {
|
|
|
|
let appId = NSNumber(value: id)
|
|
|
|
return installedApps.first { $0.itemIdentifier == appId }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds an app by name.
|
|
|
|
///
|
|
|
|
/// - Parameter appName: Full title of an app.
|
|
|
|
/// - Returns: Software Product of app if found; nil otherwise.
|
|
|
|
public func installedApp(named appName: String) -> SoftwareProduct? {
|
|
|
|
return installedApps.first { $0.appName == appName }
|
|
|
|
}
|
|
|
|
}
|