🗑️♻️ Replace external trash command with FileManager.trashItem

This commit is contained in:
Ben Chatelain 2019-01-20 21:13:53 -07:00
parent 664ec56ab7
commit 1925144366
3 changed files with 6 additions and 41 deletions

View file

@ -22,12 +22,8 @@ public class MasAppLibrary: AppLibrary {
return products
}()
private var trashCommand: ExternalCommand
/// Designated initializer
public init(trashCommand: ExternalCommand = TrashCommand()) {
self.trashCommand = trashCommand
}
public init() {}
/// Finds an app using a bundle identifier.
///
@ -42,15 +38,13 @@ public class MasAppLibrary: AppLibrary {
/// - Parameter app: App to be removed.
/// - Throws: Error if there is a problem.
public func uninstallApp(app: SoftwareProduct) throws {
let fileManager = FileManager()
let appUrl = URL(fileURLWithPath: app.bundlePath)
do {
try trashCommand.run(arguments: app.bundlePath)
try fileManager.trashItem(at: appUrl, resultingItemURL: nil)
} catch {
printError("Unable to launch trash command")
throw MASError.uninstallFailed
}
if trashCommand.failed {
let reason = trashCommand.process.terminationReason
printError("Uninstall failed: (\(reason)) \(trashCommand.stderr)")
printError("Unable to move app to trash.")
throw MASError.uninstallFailed
}
}

View file

@ -1,25 +0,0 @@
//
// TrashCommand.swift
// MasKit
//
// Created by Ben Chatelain on 1/1/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
/// Wrapper for the external trash command. Relies on the "trash" command
/// from Homebrew. Trash requires el_capitan or higher for core bottles:
/// https://github.com/Homebrew/homebrew-core/blob/master/Formula/trash.rb
public struct TrashCommand: ExternalCommand {
public var binaryPath: String
public let process = Process()
public let stdoutPipe = Pipe()
public let stderrPipe = Pipe()
public init(
binaryPath: String = "/usr/local/bin/trash"
) {
self.binaryPath = binaryPath
}
}

View file

@ -45,7 +45,6 @@
B576FE3521E98AAE0016B39D /* StoreSearchSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B576FE3421E98AAE0016B39D /* StoreSearchSpec.swift */; };
B5793E29219BDD4800135B39 /* JSON in Resources */ = {isa = PBXBuildFile; fileRef = B5793E28219BDD4800135B39 /* JSON */; };
B588CE0221DC89490047D305 /* ExternalCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = B588CE0121DC89490047D305 /* ExternalCommand.swift */; };
B588CE0421DC8AFB0047D305 /* TrashCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = B588CE0321DC8AFB0047D305 /* TrashCommand.swift */; };
B594B12021D53A8200F3AC59 /* Uninstall.swift in Sources */ = {isa = PBXBuildFile; fileRef = B594B11F21D53A8200F3AC59 /* Uninstall.swift */; };
B594B12221D5416100F3AC59 /* ListCommandSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B594B12121D5416100F3AC59 /* ListCommandSpec.swift */; };
B594B12521D580BB00F3AC59 /* UninstallCommandSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B594B12421D580BB00F3AC59 /* UninstallCommandSpec.swift */; };
@ -264,7 +263,6 @@
B576FE3421E98AAE0016B39D /* StoreSearchSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreSearchSpec.swift; sourceTree = "<group>"; };
B5793E28219BDD4800135B39 /* JSON */ = {isa = PBXFileReference; lastKnownFileType = folder; path = JSON; sourceTree = "<group>"; };
B588CE0121DC89490047D305 /* ExternalCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalCommand.swift; sourceTree = "<group>"; };
B588CE0321DC8AFB0047D305 /* TrashCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrashCommand.swift; sourceTree = "<group>"; };
B594B11F21D53A8200F3AC59 /* Uninstall.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Uninstall.swift; sourceTree = "<group>"; };
B594B12121D5416100F3AC59 /* ListCommandSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListCommandSpec.swift; sourceTree = "<group>"; };
B594B12421D580BB00F3AC59 /* UninstallCommandSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UninstallCommandSpec.swift; sourceTree = "<group>"; };
@ -586,7 +584,6 @@
children = (
B588CE0121DC89490047D305 /* ExternalCommand.swift */,
B576FDF621E107AA0016B39D /* OpenSystemCommand.swift */,
B588CE0321DC8AFB0047D305 /* TrashCommand.swift */,
);
path = ExternalCommands;
sourceTree = "<group>";
@ -1026,7 +1023,6 @@
F8FB716E20F2B4DD00F56FDC /* StoreAccount.swift in Sources */,
B594B15021D8998000F3AC59 /* StoreSearch.swift in Sources */,
B576FE0E21E1D6310016B39D /* String+PercentEncoding.swift in Sources */,
B588CE0421DC8AFB0047D305 /* TrashCommand.swift in Sources */,
B594B12021D53A8200F3AC59 /* Uninstall.swift in Sources */,
F8FB717920F2B4DD00F56FDC /* Upgrade.swift in Sources */,
B576FE0221E1139E0016B39D /* URLSession+NetworkSession.swift in Sources */,