Skip to content

Commit 1572a30

Browse files
committed
Add lock in uninstall
1 parent 1d67cb7 commit 1572a30

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Sources/Swiftly/Uninstall.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,28 @@ struct Uninstall: SwiftlyCommand {
128128
await ctx.print("\(toolchains.count) toolchain(s) successfully uninstalled")
129129
}
130130

131-
static func execute(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, _ config: inout Config, verbose: Bool) async throws {
131+
static func execute(
132+
_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, _ config: inout Config,
133+
verbose: Bool
134+
) async throws {
132135
await ctx.print("Uninstalling \(toolchain)... ", terminator: "")
133-
config.installedToolchains.remove(toolchain)
134-
// This is here to prevent the inUse from referencing a toolchain that is not installed
135-
if config.inUse == toolchain {
136-
config.inUse = nil
136+
let lockFile = Swiftly.currentPlatform.swiftlyHomeDir(ctx) / "swiftly.lock"
137+
if verbose {
138+
await ctx.print("Attempting to acquire installation lock at \(lockFile) ...")
137139
}
138-
try config.save(ctx)
139140

140-
try await Swiftly.currentPlatform.uninstall(ctx, toolchain, verbose: verbose)
141+
config = try await withLock(lockFile) {
142+
var config = try await Config.load(ctx)
143+
config.installedToolchains.remove(toolchain)
144+
// This is here to prevent the inUse from referencing a toolchain that is not installed
145+
if config.inUse == toolchain {
146+
config.inUse = nil
147+
}
148+
try config.save(ctx)
149+
150+
try await Swiftly.currentPlatform.uninstall(ctx, toolchain, verbose: verbose)
151+
return config
152+
}
141153
await ctx.print("done")
142154
}
143155
}

0 commit comments

Comments
 (0)