Skip to content

Commit 223c001

Browse files
committed
Refactor validateLinked, fix typo
1 parent 6262ad7 commit 223c001

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

Sources/Swiftly/Install.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct Install: SwiftlyCommand {
8686
defer {
8787
versionUpdateReminder()
8888
}
89-
try await valitateLinked(ctx)
89+
try await validateLinked(ctx)
9090

9191
var config = try await Config.load(ctx)
9292
let toolchainVersion = try await Self.determineToolchainVersion(ctx, version: self.version, config: &config)

Sources/Swiftly/Unlink.swift

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,56 @@ struct Unlink: SwiftlyCommand {
4848
await ctx.print(Messages.refreshShell)
4949
}
5050
}
51-
}
5251

53-
func symlinkedProxies(_ ctx: SwiftlyCoreContext) async throws -> [String] {
54-
if let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) {
55-
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
56-
let swiftlyBinDirContents = (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
57-
var proxies = [String]()
58-
for bin in swiftlyBinDirContents {
59-
let linkTarget = try? await fs.readlink(atPath: swiftlyBinDir / bin)
60-
if linkTarget == proxyTo {
61-
proxies.append(bin)
52+
func symlinkedProxies(_ ctx: SwiftlyCoreContext) async throws -> [String] {
53+
if let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) {
54+
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
55+
let swiftlyBinDirContents = (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
56+
var proxies = [String]()
57+
for file in swiftlyBinDirContents {
58+
let linkTarget = try? await fs.readlink(atPath: swiftlyBinDir / file)
59+
if linkTarget == proxyTo {
60+
proxies.append(file)
61+
}
6262
}
63+
return proxies
6364
}
64-
return proxies
65+
return []
6566
}
66-
return []
6767
}
6868

6969
extension SwiftlyCommand {
70-
func valitateLinked(_ ctx: SwiftlyCoreContext) async throws {
71-
if try await symlinkedProxies(ctx).isEmpty {
70+
/// Checks if swiftly is currently linked to manage the active toolchain.
71+
/// - Parameter ctx: The Swiftly context.
72+
func validateLinked(_ ctx: SwiftlyCoreContext) async throws {
73+
if try await !self.isLinked(ctx) {
7274
await ctx.print(Messages.currentlyUnlinked)
7375
}
7476
}
77+
78+
private func isLinked(_ ctx: SwiftlyCoreContext) async throws -> Bool {
79+
guard let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) else {
80+
return false
81+
}
82+
83+
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
84+
guard let swiftlyBinDirContents = try? await fs.ls(atPath: swiftlyBinDir) else {
85+
return false
86+
}
87+
88+
for file in swiftlyBinDirContents {
89+
// A way to test swiftly locally is to symlink the swiftly executable
90+
// in the bin dir to one being built from their local swiftly repo.
91+
if file == "swiftly" {
92+
continue
93+
}
94+
95+
let potentialProxyPath = swiftlyBinDir / file
96+
if let linkTarget = try? await fs.readlink(atPath: potentialProxyPath), linkTarget == proxyTo {
97+
return true
98+
}
99+
}
100+
101+
return false
102+
}
75103
}

Sources/Swiftly/Update.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct Update: SwiftlyCommand {
8787
defer {
8888
versionUpdateReminder()
8989
}
90-
try await valitateLinked(ctx)
90+
try await validateLinked(ctx)
9191

9292
var config = try await Config.load(ctx)
9393
guard let parameters = try await self.resolveUpdateParameters(ctx, &config) else {

Sources/Swiftly/Use.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct Use: SwiftlyCommand {
6767

6868
var config = try await Config.load(ctx)
6969

70-
try await valitateLinked(ctx)
70+
try await validateLinked(ctx)
7171

7272
// This is the bare use command where we print the selected toolchain version (or the path to it)
7373
guard let toolchain = self.toolchain else {

0 commit comments

Comments
 (0)