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