@@ -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
6666extension 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}
0 commit comments