vChewing-macOS/Packages/vChewing_Uninstaller/Sources/Uninstaller/Uninstaller.swift

75 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Cocoa
import CocoaExtension
public enum Uninstaller {
// MARK: - Uninstall the input method.
@discardableResult public static func uninstall(
isSudo: Bool = false, selfKill: Bool = true, defaultDataFolderPath: String
) -> Int32 {
let realHomeDir = URL(
fileURLWithFileSystemRepresentation: getpwuid(getuid()).pointee.pw_dir, isDirectory: true, relativeTo: nil
)
// Bundle.main.bundleURL便使 sudo
guard let bundleID = Bundle.main.bundleIdentifier else {
NSLog("Failed to ensure the bundle identifier.")
return -1
}
// v2.3.0 sudo
if isSudo {
print(
"vChewing binary does not support sudo-uninstall since v2.3.0. Please use the bundled uninstall.sh instead.\n\nIf you want to fix the installation (i.e. removing all incorrectly installed files outside of the current user folder), please use the bundled fixinstall.sh instead.\n\nIf you don't know how to proceed, please bring either the uninstall.sh / install.sh or the instruction article https://vchewing.github.io/UNINSTALL.html to Apple Support (support.apple.com) for help. Their senior advisors can understand these uninstall instructions."
)
return -1
}
let kTargetBundle = "/vChewing.app"
let pathLibrary =
isSudo
? "/Library"
: realHomeDir.appendingPathComponent("Library/").path
let pathIMELibrary =
isSudo
? "/Library/Input Methods"
: realHomeDir.appendingPathComponent("Library/Input Methods/").path
let pathUnitKeyboardLayouts = "/Keyboard Layouts"
let arrKeyLayoutFiles = [
"/vChewing ETen.keylayout", "/vChewingKeyLayout.bundle", "/vChewing MiTAC.keylayout",
"/vChewing IBM.keylayout", "/vChewing FakeSeigyou.keylayout",
"/vChewing Dachen.keylayout",
]
//
for objPath in arrKeyLayoutFiles {
let objFullPath = pathLibrary + pathUnitKeyboardLayouts + objPath
if !FileManager.trashTargetIfExists(objFullPath) { return -1 }
}
if CommandLine.arguments.count > 2, CommandLine.arguments[2] == "--all",
CommandLine.arguments[1] == "uninstall"
{
// 使
// 使 symbol link
// symbol link
FileManager.trashTargetIfExists(defaultDataFolderPath)
FileManager.trashTargetIfExists(pathLibrary + "/Preferences/" + bundleID + ".plist") // App
FileManager.trashTargetIfExists(pathLibrary + "/Receipts/org.atelierInmu.vChewing.bom") // pkg
FileManager.trashTargetIfExists(pathLibrary + "/Receipts/org.atelierInmu.vChewing.plist") // pkg
}
if !FileManager.trashTargetIfExists(pathIMELibrary + kTargetBundle) { return -1 } // App
//
if selfKill {
NSApp.terminate(nil)
}
return 0
}
}