Skip to content

Commit 718b73f

Browse files
committed
Refactor validateLinked, fix typo
1 parent e076b0f commit 718b73f

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
@@ -83,7 +83,7 @@ struct Install: SwiftlyCommand {
8383

8484
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
8585
try await validateConfig(ctx)
86-
try await valitateLinked(ctx)
86+
try await validateLinked(ctx)
8787

8888
var config = try await Config.load(ctx)
8989
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
@@ -45,28 +45,56 @@ struct Unlink: SwiftlyCommand {
4545
await ctx.print(Messages.refreshShell)
4646
}
4747
}
48-
}
4948

50-
func symlinkedProxies(_ ctx: SwiftlyCoreContext) async throws -> [String] {
51-
if let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) {
52-
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
53-
let swiftlyBinDirContents = (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
54-
var proxies = [String]()
55-
for bin in swiftlyBinDirContents {
56-
let linkTarget = try? await fs.readlink(atPath: swiftlyBinDir / bin)
57-
if linkTarget == proxyTo {
58-
proxies.append(bin)
49+
func symlinkedProxies(_ ctx: SwiftlyCoreContext) async throws -> [String] {
50+
if let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) {
51+
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
52+
let swiftlyBinDirContents = (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
53+
var proxies = [String]()
54+
for file in swiftlyBinDirContents {
55+
let linkTarget = try? await fs.readlink(atPath: swiftlyBinDir / file)
56+
if linkTarget == proxyTo {
57+
proxies.append(file)
58+
}
5959
}
60+
return proxies
6061
}
61-
return proxies
62+
return []
6263
}
63-
return []
6464
}
6565

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

Sources/Swiftly/Update.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct Update: SwiftlyCommand {
8484

8585
public mutating func run(_ ctx: SwiftlyCoreContext) async throws {
8686
var config = try await validatedConfig(ctx)
87-
try await valitateLinked(ctx)
87+
try await validateLinked(ctx)
8888

8989
guard let parameters = try await self.resolveUpdateParameters(ctx, &config) else {
9090
if let toolchain = self.toolchain {

Sources/Swiftly/Use.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct Use: SwiftlyCommand {
6262
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
6363
var config = try await validatedConfig(ctx)
6464

65-
try await valitateLinked(ctx)
65+
try await validateLinked(ctx)
6666

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

0 commit comments

Comments
 (0)