Skip to content

Commit d698703

Browse files
authored
Add --use flag to install (#76)
1 parent 32c2355 commit d698703

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Sources/Swiftly/Install.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ struct Install: SwiftlyCommand {
4343
))
4444
var version: String
4545

46+
@Flag(name: .shortAndLong, help: "Mark the newly installed toolchain as in-use.")
47+
var use: Bool = false
48+
4649
@Option(help: ArgumentHelp(
4750
"A GitHub authentiation token to use for any GitHub API requests.",
4851
discussion: """
@@ -56,21 +59,22 @@ struct Install: SwiftlyCommand {
5659
public var httpClient = SwiftlyHTTPClient()
5760

5861
private enum CodingKeys: String, CodingKey {
59-
case version, token
62+
case version, token, use
6063
}
6164

6265
mutating func run() async throws {
6366
let selector = try ToolchainSelector(parsing: self.version)
6467
self.httpClient.githubToken = self.token
6568
let toolchainVersion = try await self.resolve(selector: selector)
6669
var config = try Config.load()
67-
try await Self.execute(version: toolchainVersion, &config, self.httpClient)
70+
try await Self.execute(version: toolchainVersion, &config, self.httpClient, useInstalledToolchain: self.use)
6871
}
6972

7073
internal static func execute(
7174
version: ToolchainVersion,
7275
_ config: inout Config,
73-
_ httpClient: SwiftlyHTTPClient
76+
_ httpClient: SwiftlyHTTPClient,
77+
useInstalledToolchain: Bool
7478
) async throws {
7579
guard !config.installedToolchains.contains(version) else {
7680
SwiftlyCore.print("\(version) is already installed, exiting.")
@@ -168,8 +172,9 @@ struct Install: SwiftlyCommand {
168172
config.installedToolchains.insert(version)
169173
try config.save()
170174

171-
// If this is the first installed toolchain, mark it as in-use.
172-
if config.inUse == nil {
175+
// If this is the first installed toolchain, mark it as in-use regardless of whether the
176+
// --use argument was provided.
177+
if useInstalledToolchain || config.inUse == nil {
173178
try await Use.execute(version, &config)
174179
}
175180

Sources/Swiftly/Update.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ struct Update: SwiftlyCommand {
100100
}
101101
}
102102

103-
try await Install.execute(version: newToolchain, &config, self.httpClient)
104-
105-
if config.inUse == parameters.oldToolchain {
106-
try await Use.execute(newToolchain, &config)
107-
}
103+
try await Install.execute(
104+
version: newToolchain,
105+
&config,
106+
self.httpClient,
107+
useInstalledToolchain: config.inUse == parameters.oldToolchain
108+
)
108109

109110
try await Uninstall.execute(parameters.oldToolchain, &config)
110111
SwiftlyCore.print("Successfully updated \(parameters.oldToolchain)\(newToolchain)")

Tests/SwiftlyTests/InstallTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,16 @@ final class InstallTests: SwiftlyTests {
255255
try await self.validateInUse(expected: ToolchainVersion(major: 5, minor: 7, patch: 0))
256256
}
257257
}
258+
259+
/// Verify that the installed toolchain will be marked as in-use if the --use flag is specified.
260+
func testInstallUseFlag() async throws {
261+
try await self.withTestHome {
262+
try await self.installMockedToolchain(toolchain: Self.oldStable)
263+
var use = try self.parseCommand(Use.self, ["use", Self.oldStable.name])
264+
try await use.run()
265+
try await validateInUse(expected: Self.oldStable)
266+
try await self.installMockedToolchain(selector: Self.newStable.name, args: ["--use"])
267+
try await self.validateInUse(expected: Self.newStable)
268+
}
269+
}
258270
}

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ class SwiftlyTests: XCTestCase {
172172
/// in its bin directory.
173173
///
174174
/// When executed, the mocked executables will simply print the toolchain version and return.
175-
func installMockedToolchain(selector: String, executables: [String]? = nil) async throws {
176-
var install = try self.parseCommand(Install.self, ["install", "\(selector)"])
175+
func installMockedToolchain(selector: String, args: [String] = [], executables: [String]? = nil) async throws {
176+
var install = try self.parseCommand(Install.self, ["install", "\(selector)"] + args)
177177
install.httpClient = SwiftlyHTTPClient(toolchainDownloader: MockToolchainDownloader(executables: executables))
178178
try await install.run()
179179
}

0 commit comments

Comments
 (0)