Skip to content

Add --use flag to install #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Sources/Swiftly/Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct Install: SwiftlyCommand {
))
var version: String

@Flag(name: .shortAndLong, help: "Mark the newly installed toolchain as in-use.")
var use: Bool = false

@Option(help: ArgumentHelp(
"A GitHub authentiation token to use for any GitHub API requests.",
discussion: """
Expand All @@ -56,21 +59,22 @@ struct Install: SwiftlyCommand {
public var httpClient = SwiftlyHTTPClient()

private enum CodingKeys: String, CodingKey {
case version, token
case version, token, use
}

mutating func run() async throws {
let selector = try ToolchainSelector(parsing: self.version)
self.httpClient.githubToken = self.token
let toolchainVersion = try await self.resolve(selector: selector)
var config = try Config.load()
try await Self.execute(version: toolchainVersion, &config, self.httpClient)
try await Self.execute(version: toolchainVersion, &config, self.httpClient, useInstalledToolchain: self.use)
}

internal static func execute(
version: ToolchainVersion,
_ config: inout Config,
_ httpClient: SwiftlyHTTPClient
_ httpClient: SwiftlyHTTPClient,
useInstalledToolchain: Bool
) async throws {
guard !config.installedToolchains.contains(version) else {
SwiftlyCore.print("\(version) is already installed, exiting.")
Expand Down Expand Up @@ -168,8 +172,9 @@ struct Install: SwiftlyCommand {
config.installedToolchains.insert(version)
try config.save()

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

Expand Down
11 changes: 6 additions & 5 deletions Sources/Swiftly/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ struct Update: SwiftlyCommand {
}
}

try await Install.execute(version: newToolchain, &config, self.httpClient)

if config.inUse == parameters.oldToolchain {
try await Use.execute(newToolchain, &config)
}
try await Install.execute(
version: newToolchain,
&config,
self.httpClient,
useInstalledToolchain: config.inUse == parameters.oldToolchain
)

try await Uninstall.execute(parameters.oldToolchain, &config)
SwiftlyCore.print("Successfully updated \(parameters.oldToolchain) ⟶ \(newToolchain)")
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftlyTests/InstallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,16 @@ final class InstallTests: SwiftlyTests {
try await self.validateInUse(expected: ToolchainVersion(major: 5, minor: 7, patch: 0))
}
}

/// Verify that the installed toolchain will be marked as in-use if the --use flag is specified.
func testInstallUseFlag() async throws {
try await self.withTestHome {
try await self.installMockedToolchain(toolchain: Self.oldStable)
var use = try self.parseCommand(Use.self, ["use", Self.oldStable.name])
try await use.run()
try await validateInUse(expected: Self.oldStable)
try await self.installMockedToolchain(selector: Self.newStable.name, args: ["--use"])
try await self.validateInUse(expected: Self.newStable)
}
}
}
4 changes: 2 additions & 2 deletions Tests/SwiftlyTests/SwiftlyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ class SwiftlyTests: XCTestCase {
/// in its bin directory.
///
/// When executed, the mocked executables will simply print the toolchain version and return.
func installMockedToolchain(selector: String, executables: [String]? = nil) async throws {
var install = try self.parseCommand(Install.self, ["install", "\(selector)"])
func installMockedToolchain(selector: String, args: [String] = [], executables: [String]? = nil) async throws {
var install = try self.parseCommand(Install.self, ["install", "\(selector)"] + args)
install.httpClient = SwiftlyHTTPClient(toolchainDownloader: MockToolchainDownloader(executables: executables))
try await install.run()
}
Expand Down