Skip to content

Commit d6e7e68

Browse files
committed
Notify swiftly is unlinked when performing use, install and update
1 parent 8e08c3b commit d6e7e68

File tree

8 files changed

+61
-24
lines changed

8 files changed

+61
-24
lines changed

Sources/Swiftly/Install.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ struct Install: SwiftlyCommand {
8282
}
8383

8484
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
85-
try await validatedConfig(ctx)
85+
try await validateConfig(ctx)
86+
try await valitateLinked(ctx)
8687

8788
var config = try await Config.load(ctx)
8889
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
@@ -39,10 +39,14 @@ struct Link: SwiftlyCommand {
3939

4040
if pathChanged {
4141
await ctx.print("""
42-
Linked swiftly to \(toolchainVersion.name).
42+
Linked swiftly to Swift \(toolchainVersion.name).
4343
4444
\(Messages.refreshShell)
4545
""")
46+
} else {
47+
await ctx.print("""
48+
Swiftly is already linked to Swift \(toolchainVersion.name).
49+
""")
4650
}
4751
}
4852
}

Sources/Swiftly/SelfUpdate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct SelfUpdate: SwiftlyCommand {
2121
}
2222

2323
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
24-
try await validatedConfig(ctx)
24+
try await validateConfig(ctx)
2525

2626
let swiftlyBin = Swiftly.currentPlatform.swiftlyBinDir(ctx) / "swiftly"
2727
guard try await fs.exists(atPath: swiftlyBin) else {

Sources/Swiftly/Swiftly.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ extension Data {
9595
}
9696

9797
extension SwiftlyCommand {
98-
@discardableResult
99-
public mutating func validatedConfig(_ ctx: SwiftlyCoreContext) async throws -> Config {
98+
func validatedConfig(_ ctx: SwiftlyCoreContext) async throws -> Config {
10099
for requiredDir in Swiftly.requiredDirectories(ctx) {
101100
guard try await fs.exists(atPath: requiredDir) else {
102101
do {
@@ -111,4 +110,9 @@ extension SwiftlyCommand {
111110
// Verifies that the configuration exists and can be loaded
112111
return try await Config.load(ctx)
113112
}
113+
114+
// Perform the verification done in `validatedConfig` without returning it.
115+
func validateConfig(_ ctx: SwiftlyCoreContext) async throws {
116+
let _ = try await validatedConfig(ctx)
117+
}
114118
}

Sources/Swiftly/Unlink.swift

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,17 @@ struct Unlink: SwiftlyCommand {
2626
}
2727

2828
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
29-
try await validatedConfig(ctx)
29+
try await validateConfig(ctx)
3030

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

44-
for p in existingProxies {
45-
let proxy = Swiftly.currentPlatform.swiftlyBinDir(ctx) / p
34+
for p in existingProxies {
35+
let proxy = Swiftly.currentPlatform.swiftlyBinDir(ctx) / p
4636

47-
if try await fs.exists(atPath: proxy) {
48-
try await fs.remove(atPath: proxy)
49-
pathChanged = true
50-
}
37+
if try await fs.exists(atPath: proxy) {
38+
try await fs.remove(atPath: proxy)
39+
pathChanged = true
5140
}
5241
}
5342

@@ -57,3 +46,27 @@ struct Unlink: SwiftlyCommand {
5746
}
5847
}
5948
}
49+
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)
59+
}
60+
}
61+
return proxies
62+
}
63+
return []
64+
}
65+
66+
extension SwiftlyCommand {
67+
func valitateLinked(_ ctx: SwiftlyCoreContext) async throws {
68+
if try await symlinkedProxies(ctx).isEmpty {
69+
await ctx.print(Messages.currentlyUnlinked)
70+
}
71+
}
72+
}

Sources/Swiftly/Update.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +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)
8788

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

Sources/Swiftly/Use.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct Use: SwiftlyCommand {
6262
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
6363
var config = try await validatedConfig(ctx)
6464

65+
try await valitateLinked(ctx)
66+
6567
// This is the bare use command where we print the selected toolchain version (or the path to it)
6668
guard let toolchain = self.toolchain else {
6769
let (selectedVersion, result) = try await selectToolchain(ctx, config: &config, globalDefault: self.globalDefault)
@@ -108,6 +110,7 @@ struct Use: SwiftlyCommand {
108110
}
109111

110112
try await Self.execute(ctx, toolchain, globalDefault: self.globalDefault, assumeYes: self.root.assumeYes, &config)
113+
111114
}
112115

113116
/// 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)