Skip to content

Commit e02006c

Browse files
chore: general clean up
1 parent 20ec956 commit e02006c

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

Sources/Swiftly/SelfUninstall.swift

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// SelfUninstall.swift
2+
13
import ArgumentParser
24
import Foundation
35
import SwiftlyCore
46
import SystemPackage
57

68
struct SelfUninstall: SwiftlyCommand {
7-
public static let configuration = CommandConfiguration(
8-
abstract: "Uninstall swiftly itself.",
9+
static let configuration = CommandConfiguration(
10+
abstract: "Uninstall swiftly itself."
911
)
1012

1113
@OptionGroup var root: GlobalOptions
@@ -19,24 +21,22 @@ struct SelfUninstall: SwiftlyCommand {
1921
}
2022

2123
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
22-
let _ = try await validateSwiftly(ctx)
24+
_ = try await validateSwiftly(ctx)
2325
let swiftlyBin = Swiftly.currentPlatform.swiftlyBinDir(ctx)
2426

2527
guard try await fs.exists(atPath: swiftlyBin) else {
2628
throw SwiftlyError(
27-
message:
28-
"Self uninstall doesn't work when swiftly has been installed externally. Please uninstall it from the source where you installed it in the first place."
29+
message: "Self uninstall doesn't work when swiftly has been installed externally. Please uninstall it from the source where you installed it in the first place."
2930
)
3031
}
3132

3233
if !self.root.assumeYes {
3334
await ctx.print("""
34-
You are about to uninstall swiftly.
35-
This will remove the swiftly binary and all the files in the swiftly home directory.
36-
All installed toolchains will not be removed, if you want to remove them, please do so manually with `swiftly uninstall all`.
35+
You are about to uninstall swiftly.
36+
This will remove the swiftly binary and all files in the swiftly home directory.
37+
Installed toolchains will not be removed. To remove them, run `swiftly uninstall all`.
3738
This action is irreversible.
3839
""")
39-
4040
guard await ctx.promptForConfirmation(defaultBehavior: true) else {
4141
throw SwiftlyError(message: "swiftly installation has been cancelled")
4242
}
@@ -45,7 +45,7 @@ struct SelfUninstall: SwiftlyCommand {
4545
try await Self.execute(ctx, verbose: self.root.verbose)
4646
}
4747

48-
public static func execute(_ ctx: SwiftlyCoreContext, verbose _: Bool) async throws {
48+
static func execute(_ ctx: SwiftlyCoreContext, verbose _: Bool) async throws {
4949
await ctx.print("Uninstalling swiftly...")
5050

5151
let userHome = ctx.mockedHomeDir ?? fs.home
@@ -71,32 +71,31 @@ struct SelfUninstall: SwiftlyCommand {
7171
userHome / ".profile",
7272
]
7373

74-
// Handle fish shell config
74+
// Add fish shell config path
7575
if let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
7676
profilePaths.append(FilePath(xdgConfigHome) / "fish/conf.d/swiftly.fish")
7777
} else {
7878
profilePaths.append(userHome / ".config/fish/conf.d/swiftly.fish")
7979
}
8080

81-
await ctx.print("Scanning shell profile files to remove swiftly source line...")
82-
83-
// remove swiftly source line from shell profile files
84-
for path in profilePaths {
85-
if try await fs.exists(atPath: path) {
86-
await ctx.print("Removing swiftly source line from \(path)...")
87-
let isFishProfile = path.extension == "fish"
88-
let sourceLine = isFishProfile ? fishSourceLine : shSourceLine
89-
if case let profileContents = try String(contentsOf: path, encoding: .utf8), profileContents.contains(sourceLine) {
90-
let newContents = profileContents.replacingOccurrences(of: sourceLine, with: "")
91-
try Data(newContents.utf8).write(to: path, options: [.atomic])
92-
}
81+
await ctx.print("Cleaning up shell profile files...")
82+
83+
// Remove swiftly source lines from shell profiles
84+
for path in profilePaths where try await fs.exists(atPath: path) {
85+
await ctx.print("Updating \(path)...")
86+
let isFish = path.extension == "fish"
87+
let sourceLine = isFish ? fishSourceLine : shSourceLine
88+
let contents = try String(contentsOf: path, encoding: .utf8)
89+
if contents.contains(sourceLine) {
90+
let updated = contents.replacingOccurrences(of: sourceLine, with: "")
91+
try Data(updated.utf8).write(to: path, options: [.atomic])
9392
}
9493
}
9594

96-
await ctx.print("Removing swiftly binary from \(swiftlyBin)...")
95+
await ctx.print("Removing swiftly binary at \(swiftlyBin)...")
9796
try await fs.remove(atPath: swiftlyBin)
9897

99-
await ctx.print("Removing swiftly home directory from \(swiftlyHome)...")
98+
await ctx.print("Removing swiftly home directory at \(swiftlyHome)...")
10099
try await fs.remove(atPath: swiftlyHome)
101100

102101
await ctx.print("Swiftly uninstalled successfully.")

0 commit comments

Comments
 (0)