Skip to content

Commit d63c30e

Browse files
committed
Setup proxies on use of a toolchain
1 parent 86cd751 commit d63c30e

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

Sources/Swiftly/Init.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,8 @@ struct Init: SwiftlyCommand {
281281
""")
282282
}
283283

284-
// Fish doesn't have path caching, so this might only be needed for bash/zsh
285-
if pathChanged && !quietShellFollowup && !shell.hasSuffix("fish") {
286-
await ctx.message("""
287-
Your shell caches items on your path for better performance. Swiftly has added
288-
items to your path that may not get picked up right away. You can update your
289-
shell's environment by running
290-
291-
hash -r
292-
293-
or restarting your shell.
294-
295-
""")
284+
if pathChanged {
285+
try await Self.handlePathChange(ctx)
296286
}
297287

298288
if let postInstall {

Sources/Swiftly/Install.swift

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,8 @@ struct Install: SwiftlyCommand {
121121
progressFile: self.progressFile
122122
)
123123

124-
let shell =
125-
if let s = ProcessInfo.processInfo.environment["SHELL"]
126-
{
127-
s
128-
} else {
129-
try await Swiftly.currentPlatform.getShell()
130-
}
131-
132-
// Fish doesn't cache its path, so this instruction is not necessary.
133-
if pathChanged && !shell.hasSuffix("fish") {
134-
await ctx.message(
135-
"""
136-
NOTE: Swiftly has updated some elements in your path and your shell may not yet be
137-
aware of the changes. You can update your shell's environment by running
138-
139-
hash -r
140-
141-
or restarting your shell.
142-
143-
""")
124+
if pathChanged {
125+
try await Self.handlePathChange(ctx)
144126
}
145127

146128
if let postInstallScript {
@@ -410,7 +392,7 @@ struct Install: SwiftlyCommand {
410392
verbose: verbose
411393
)
412394

413-
let pathChanged = try await Self.setupProxies(
395+
var pathChanged = try await Self.setupProxies(
414396
ctx,
415397
version: version,
416398
verbose: verbose,
@@ -424,7 +406,8 @@ struct Install: SwiftlyCommand {
424406
// If this is the first installed toolchain, mark it as in-use regardless of whether the
425407
// --use argument was provided.
426408
if useInstalledToolchain {
427-
try await Use.execute(ctx, version, globalDefault: false, &config)
409+
let pc = try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
410+
pathChanged = pathChanged || pc
428411
}
429412

430413
// We always update the global default toolchain if there is none set. This could

Sources/Swiftly/Swiftly.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,28 @@ extension SwiftlyCommand {
132132
}
133133
}
134134
}
135+
136+
public static func handlePathChange(_ ctx: SwiftlyCoreContext) async throws {
137+
let shell =
138+
if let s = ProcessInfo.processInfo.environment["SHELL"]
139+
{
140+
s
141+
} else {
142+
try await Swiftly.currentPlatform.getShell()
143+
}
144+
145+
// Fish doesn't cache its path, so this instruction is not necessary.
146+
if !shell.hasSuffix("fish") {
147+
await ctx.message(
148+
"""
149+
NOTE: Swiftly has updated some elements in your path and your shell may not yet be
150+
aware of the changes. You can update your shell's environment by running
151+
152+
hash -r
153+
154+
or restarting your shell.
155+
156+
""")
157+
}
158+
}
135159
}

Sources/Swiftly/Uninstall.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ struct Uninstall: SwiftlyCommand {
119119
?? config.listInstalledToolchains(selector: .latest).filter({ !toolchains.contains($0) }).max()
120120
?? config.installedToolchains.filter({ !toolchains.contains($0) }).max()
121121
{
122-
try await Use.execute(ctx, toUse, globalDefault: true, &config)
122+
let pathChanged = try await Use.execute(ctx, toUse, globalDefault: true, verbose: self.root.verbose, &config)
123+
if pathChanged {
124+
try await Self.handlePathChange(ctx)
125+
}
123126
} else {
124127
// If there are no more toolchains installed, just unuse the currently active toolchain.
125128
config.inUse = nil

Sources/Swiftly/Use.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,14 @@ struct Use: SwiftlyCommand {
121121
throw SwiftlyError(message: "No installed toolchains match \"\(toolchain)\"")
122122
}
123123

124-
try await Self.execute(ctx, toolchain, globalDefault: self.globalDefault, assumeYes: self.root.assumeYes, &config)
124+
let pathChanged = try await Self.execute(ctx, toolchain, globalDefault: self.globalDefault, verbose: self.root.verbose, assumeYes: self.root.assumeYes, &config)
125+
if pathChanged {
126+
try await Self.handlePathChange(ctx)
127+
}
125128
}
126129

127130
/// Use a toolchain. This method can modify and save the input config and also create/modify a `.swift-version` file.
128-
static func execute(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, globalDefault: Bool, assumeYes: Bool = true, _ config: inout Config) async throws {
131+
static func execute(_ ctx: SwiftlyCoreContext, _ toolchain: ToolchainVersion, globalDefault: Bool, verbose: Bool, assumeYes: Bool = true, _ config: inout Config) async throws -> Bool {
129132
let (selectedVersion, result) = try await selectToolchain(ctx, config: &config, globalDefault: globalDefault)
130133

131134
let isGlobal: Bool
@@ -142,7 +145,7 @@ struct Use: SwiftlyCommand {
142145

143146
guard await ctx.promptForConfirmation(defaultBehavior: true) else {
144147
await ctx.message("Aborting setting in-use toolchain")
145-
return
148+
return false
146149
}
147150
}
148151

@@ -156,12 +159,21 @@ struct Use: SwiftlyCommand {
156159
configFile = nil
157160
}
158161

162+
let pathChanged = try await Install.setupProxies(
163+
ctx,
164+
version: toolchain,
165+
verbose: verbose,
166+
assumeYes: assumeYes
167+
)
168+
159169
try await ctx.output(ToolchainSetInfo(
160170
version: toolchain,
161171
previousVersion: selectedVersion,
162172
isGlobal: isGlobal,
163173
versionFile: configFile
164174
))
175+
176+
return pathChanged
165177
}
166178

167179
static func findNewVersionFile(_ ctx: SwiftlyCoreContext) async throws -> FilePath? {

0 commit comments

Comments
 (0)