Skip to content

Commit a60a675

Browse files
committed
Notify swiftly is unlinked when performing use, install and update
1 parent 1847095 commit a60a675

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

Sources/Swiftly/Install.swift

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

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

Sources/Swiftly/Link.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ struct Link: SwiftlyCommand {
4444

4545
if pathChanged {
4646
await ctx.print("""
47-
Linked swiftly to \(toolchainVersion.name).
47+
Linked swiftly to Swift \(toolchainVersion.name).
4848
4949
\(Messages.refreshShell)
5050
""")
51+
} else {
52+
await ctx.print("""
53+
Swiftly is already linked to Swift \(toolchainVersion.name).
54+
""")
5155
}
5256
}
5357
}

Sources/Swiftly/Unlink.swift

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,14 @@ struct Unlink: SwiftlyCommand {
3232
}
3333

3434
var pathChanged = false
35-
if let proxyTo = try? await Swiftly.currentPlatform.findSwiftlyBin(ctx) {
36-
let swiftlyBinDir = Swiftly.currentPlatform.swiftlyBinDir(ctx)
37-
let swiftlyBinDirContents = (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
38-
39-
var existingProxies = [String]()
40-
for bin in swiftlyBinDirContents {
41-
let linkTarget = try? await fs.readlink(atPath: swiftlyBinDir / bin)
42-
if linkTarget == proxyTo {
43-
existingProxies.append(bin)
44-
}
45-
}
35+
let existingProxies = try await symlinkedProxies(ctx)
4636

47-
for p in existingProxies {
48-
let proxy = Swiftly.currentPlatform.swiftlyBinDir(ctx) / p
37+
for p in existingProxies {
38+
let proxy = Swiftly.currentPlatform.swiftlyBinDir(ctx) / p
4939

50-
if try await fs.exists(atPath: proxy) {
51-
try await fs.remove(atPath: proxy)
52-
pathChanged = true
53-
}
40+
if try await fs.exists(atPath: proxy) {
41+
try await fs.remove(atPath: proxy)
42+
pathChanged = true
5443
}
5544
}
5645

@@ -60,3 +49,27 @@ struct Unlink: SwiftlyCommand {
6049
}
6150
}
6251
}
52+
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)
62+
}
63+
}
64+
return proxies
65+
}
66+
return []
67+
}
68+
69+
extension SwiftlyCommand {
70+
func valitateLinked(_ ctx: SwiftlyCoreContext) async throws {
71+
if try await symlinkedProxies(ctx).isEmpty {
72+
await ctx.print(Messages.currentlyUnlinked)
73+
}
74+
}
75+
}

Sources/Swiftly/Update.swift

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

9192
var config = try await Config.load(ctx)
92-
9393
guard let parameters = try await self.resolveUpdateParameters(ctx, &config) else {
9494
if let toolchain = self.toolchain {
9595
await ctx.print("No installed toolchain matched \"\(toolchain)\"")

Sources/Swiftly/Use.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct Use: SwiftlyCommand {
6767

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

70+
try await valitateLinked(ctx)
71+
7072
// This is the bare use command where we print the selected toolchain version (or the path to it)
7173
guard let toolchain = self.toolchain else {
7274
let (selectedVersion, result) = try await selectToolchain(ctx, config: &config, globalDefault: self.globalDefault)
@@ -113,6 +115,7 @@ struct Use: SwiftlyCommand {
113115
}
114116

115117
try await Self.execute(ctx, toolchain, globalDefault: self.globalDefault, assumeYes: self.root.assumeYes, &config)
118+
116119
}
117120

118121
/// Use a toolchain. This method can modify and save the input config and also create/modify a `.swift-version` file.

Sources/SwiftlyCore/Messages.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@ public enum Messages {
1010
"""
1111

1212
public static let unlinkSuccess = """
13-
Swiftly is now unlinked and will not manage the active toolchain until the following command is run:
13+
Swiftly is now unlinked and will not manage the active toolchain until the following
14+
command is run:
1415
1516
$ swiftly link
1617
18+
19+
"""
20+
21+
public static let currentlyUnlinked = """
22+
Swiftly is currently unlinked and will not manage the active toolchain. You can run
23+
the following command to link swiftly to the active toolchain:
24+
25+
$ swiftly link
26+
27+
1728
"""
1829

1930
public static func postInstall(_ command: String) -> String {

0 commit comments

Comments
 (0)