From 9b99b354369270922a52c9b34e23f1f630ccd97a Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Tue, 17 Dec 2024 19:03:16 -0500 Subject: [PATCH] feat: Uses curl to download toml config to allow for pre-built binaries. --- Package.swift | 1 + .../ConfigurationClient.swift | 20 ++++++++----------- Sources/ConfigurationClient/Constants.swift | 1 + .../ConfigurationClientTests.swift | 10 ++++++++++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Package.swift b/Package.swift index b78ab66..5f97142 100644 --- a/Package.swift +++ b/Package.swift @@ -70,6 +70,7 @@ let package = Package( name: "ConfigurationClient", dependencies: [ "CodersClient", + "CommandClient", "FileClient", .product(name: "Dependencies", package: "swift-dependencies"), .product(name: "DependenciesMacros", package: "swift-dependencies"), diff --git a/Sources/ConfigurationClient/ConfigurationClient.swift b/Sources/ConfigurationClient/ConfigurationClient.swift index e3bc307..37e5589 100644 --- a/Sources/ConfigurationClient/ConfigurationClient.swift +++ b/Sources/ConfigurationClient/ConfigurationClient.swift @@ -1,4 +1,5 @@ import CodersClient +import CommandClient import Dependencies import DependenciesMacros import FileClient @@ -155,6 +156,7 @@ struct LiveConfigurationClient { private let environment: [String: String] @Dependency(\.coders) var coders + @Dependency(\.commandClient) var commandClient @Dependency(\.fileClient) var fileManager @Dependency(\.logger) var logger @@ -245,19 +247,13 @@ struct LiveConfigurationClient { try await fileManager.createDirectory(fileDirectory) } - // TODO: The hpa file needs to be copied somewhere on the system during install and - // not use bundle, as it only works if the tool was built on the users system. if case .toml = file { - // In the case of toml, we copy the internal resource that includes - // usage comments in the file. - guard let resourceFile = Bundle.module.url( - forResource: HPAKey.resourceFileName, - withExtension: HPAKey.resourceFileExtension - ) else { - throw ConfigurationError.resourceNotFound - } - - try await fileManager.copy(resourceFile, fileUrl) + // Copy the file using curl, because when installed as a pre-built binary we + // don't have access to bundled resources. + try await commandClient.run( + quiet: true, + ["curl", HPAKey.tomlConfigUrl, "--output", fileUrl.path] + ) } else { // Json does not allow comments, so we write the mock configuration // to the file path. diff --git a/Sources/ConfigurationClient/Constants.swift b/Sources/ConfigurationClient/Constants.swift index 9030021..dc435c9 100644 --- a/Sources/ConfigurationClient/Constants.swift +++ b/Sources/ConfigurationClient/Constants.swift @@ -15,6 +15,7 @@ public enum HPAKey { public static let resourceFileExtension = "toml" public static let defaultFileName = "config.toml" public static let defaultFileNameWithoutExtension = "config" + public static let tomlConfigUrl = "https://git.housh.dev/michael/swift-hpa/raw/branch/main/Sources/ConfigurationClient/Resources/hpa.toml" } extension [String: String] { diff --git a/Tests/ConfigurationClientTests/ConfigurationClientTests.swift b/Tests/ConfigurationClientTests/ConfigurationClientTests.swift index c998e0e..d3e2e03 100644 --- a/Tests/ConfigurationClientTests/ConfigurationClientTests.swift +++ b/Tests/ConfigurationClientTests/ConfigurationClientTests.swift @@ -18,6 +18,8 @@ struct ConfigurationClientTests: TestCase { @Test(arguments: ["config.toml", "config.json"]) func generateConfigFile(fileName: String) async throws { try await withTestLogger(key: "generateConfigFile") { + $0.asyncShellClient = .liveValue + $0.commandClient = .liveValue $0.coders = .liveValue $0.fileClient = .liveValue } operation: { @@ -42,6 +44,8 @@ struct ConfigurationClientTests: TestCase { @Test(arguments: ["config.toml", "config.json", nil]) func loadConfigFile(fileName: String?) async throws { try await withTestLogger(key: "generateConfigFile") { + $0.asyncShellClient = .liveValue + $0.commandClient = .liveValue $0.coders = .liveValue $0.fileClient = .liveValue } operation: { @@ -65,6 +69,8 @@ struct ConfigurationClientTests: TestCase { @Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"]) func findConfiguration(fileName: String) async throws { try await withTestLogger(key: "findConfiguration") { + $0.asyncShellClient = .liveValue + $0.commandClient = .liveValue $0.fileClient = .liveValue } operation: { @Dependency(\.logger) var logger @@ -94,6 +100,8 @@ struct ConfigurationClientTests: TestCase { @Test(arguments: ["config.toml", "config.json", ".hparc.json", ".hparc.toml"]) func findXdgConfiguration(fileName: String) async throws { try await withTestLogger(key: "findXdgConfiguration") { + $0.asyncShellClient = .liveValue + $0.commandClient = .liveValue $0.fileClient = .liveValue } operation: { @Dependency(\.logger) var logger @@ -133,6 +141,8 @@ struct ConfigurationClientTests: TestCase { @Test func writeCreatesBackupFile() async throws { try await withDependencies { + $0.asyncShellClient = .liveValue + $0.commandClient = .liveValue $0.fileClient = .liveValue } operation: { let client = ConfigurationClient.liveValue