From bf7e0831b22eb63e4db50c4aeddece236560fb46 Mon Sep 17 00:00:00 2001 From: Vitalii Budnik Date: Mon, 23 Jun 2025 13:36:46 +0300 Subject: [PATCH] chore: public interface --- .../BinaryDependenciesConfiguration.swift | 15 ++++++----- .../Models/Dependency.swift | 26 +++++++++--------- ...inaryDependenciesConfigurationReader.swift | 27 +++++++++++-------- .../Utils/FileManagerProtocol.swift | 2 +- Sources/CommandLine/main.swift | 1 + .../Utils/CLI/CLI+GitHub.swift | 8 +++--- .../Utils/CLI/CLI.swift | 2 +- .../Utils/String+fileURL.swift | 2 +- .../Utils/Version.swift | 0 9 files changed, 45 insertions(+), 38 deletions(-) rename Sources/{BinaryDependencyManager => }/Utils/CLI/CLI+GitHub.swift (95%) rename Sources/{BinaryDependencyManager => }/Utils/CLI/CLI.swift (98%) rename Sources/{BinaryDependencyManager => }/Utils/String+fileURL.swift (90%) rename Sources/{BinaryDependencyManager => }/Utils/Version.swift (100%) diff --git a/Sources/BinaryDependencyManager/Models/BinaryDependenciesConfiguration.swift b/Sources/BinaryDependencyManager/Models/BinaryDependenciesConfiguration.swift index 280cad7..01f5ab8 100644 --- a/Sources/BinaryDependencyManager/Models/BinaryDependenciesConfiguration.swift +++ b/Sources/BinaryDependencyManager/Models/BinaryDependenciesConfiguration.swift @@ -1,15 +1,16 @@ import Foundation +import Utils /// Binary dependencies configuration. -struct BinaryDependenciesConfiguration: Equatable { +public struct BinaryDependenciesConfiguration: Equatable { /// Minimum version of the `binary-dependencies-manager` CLI. - var minimumVersion: Version? + public var minimumVersion: Version? /// Path to the output directory, where downloaded dependencies will be placed. - var outputDirectory: String? + public var outputDirectory: String? /// Path to the cache directory. - var cacheDirectory: String? + public var cacheDirectory: String? /// Dependencies list. - var dependencies: [Dependency] + public var dependencies: [Dependency] } extension BinaryDependenciesConfiguration: Codable { @@ -21,7 +22,7 @@ extension BinaryDependenciesConfiguration: Codable { case dependencies } - init(from decoder: any Decoder) throws { + public init(from decoder: any Decoder) throws { do { // Decode full schema let container = try decoder.container(keyedBy: CodingKeys.self) @@ -45,7 +46,7 @@ extension BinaryDependenciesConfiguration: Codable { } } - func encode(to encoder: any Encoder) throws { + public func encode(to encoder: any Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(self.minimumVersion?.description, forKey: .minimumVersion) try container.encodeIfPresent(self.outputDirectory, forKey: .outputDirectory) diff --git a/Sources/BinaryDependencyManager/Models/Dependency.swift b/Sources/BinaryDependencyManager/Models/Dependency.swift index 193278c..9678d4d 100644 --- a/Sources/BinaryDependencyManager/Models/Dependency.swift +++ b/Sources/BinaryDependencyManager/Models/Dependency.swift @@ -1,13 +1,13 @@ /// Dependency representation -struct Dependency: Equatable { +public struct Dependency: Equatable { /// Repository in format "owner/repo" i.e. "MacPaw/CMMX-Panther-Engine" - let repo: String + public let repo: String /// Tag in format "0.0.47", Basically a release - let tag: String + public let tag: String /// List of assets to download. - let assets: [Asset] + public let assets: [Asset] } // MARK: Decodable @@ -19,7 +19,7 @@ extension Dependency: Decodable { case assets } - init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.repo = try container.decode(String.self, forKey: .repo) self.tag = try container.decode(String.self, forKey: .tag) @@ -37,7 +37,7 @@ extension Dependency: Decodable { // MARK: Encodable extension Dependency: Encodable { - func encode(to encoder: Encoder) throws { + public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(repo, forKey: .repo) try container.encode(tag, forKey: .tag) @@ -48,16 +48,16 @@ extension Dependency: Encodable { // MARK: - Dependency.Asset extension Dependency { - struct Asset: Equatable { + public struct Asset: Equatable { /// SHA-256 checksum of the zip archive. - let checksum: String + public let checksum: String /// Regex pattern to a specific artifact, if multiple artifacts are added to the the release assets. - let pattern: String? + public let pattern: String? /// If provided, the contents of `contents` directory in the Source Code archive will be copied to output directory. /// - Warning: if provided it takes precedence over the `pattern`. - let contents: String? + public let contents: String? /// Custom output directory for the asset. - let outputDirectory: String? + public let outputDirectory: String? init ( checksum: String, @@ -83,7 +83,7 @@ extension Dependency.Asset: Decodable { case outputDirectory = "output" } - init(from decoder: any Decoder) throws { + public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.checksum = try container.decode(String.self, forKey: .checksum) self.pattern = try container.decodeIfPresent(String.self, forKey: .pattern) @@ -95,7 +95,7 @@ extension Dependency.Asset: Decodable { // MARK: Encodable extension Dependency.Asset: Encodable { - func encode(to encoder: any Encoder) throws { + public func encode(to encoder: any Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(checksum, forKey: .checksum) try container.encodeIfPresent(pattern, forKey: .pattern) diff --git a/Sources/BinaryDependencyManager/Utils/BinaryDependenciesConfigurationReader.swift b/Sources/BinaryDependencyManager/Utils/BinaryDependenciesConfigurationReader.swift index 1546362..f5e5528 100644 --- a/Sources/BinaryDependencyManager/Utils/BinaryDependenciesConfigurationReader.swift +++ b/Sources/BinaryDependencyManager/Utils/BinaryDependenciesConfigurationReader.swift @@ -1,10 +1,10 @@ import Foundation import Yams import ArgumentParser - +import Utils /// A utility for resolving and loading binary dependencies configuration files. -struct BinaryDependenciesConfigurationReader { +public struct BinaryDependenciesConfigurationReader { private static let defaultConfigurationFilenames: [String] = [ ".binary-dependencies.yaml", ".binary-dependencies.yml", @@ -24,7 +24,7 @@ struct BinaryDependenciesConfigurationReader { private let fileManager: FileManagerProtocol - init(fileManager: FileManagerProtocol = FileManager.default) { + public init(fileManager: FileManagerProtocol = FileManager.default) { self.fileManager = fileManager } @@ -50,7 +50,7 @@ struct BinaryDependenciesConfigurationReader { /// - Parameter configurationFilePath: Optional configuration file path to use, or nil to search defaults. /// - Throws: ValidationError if the file does not exist. /// - Returns: The resolved configuration file URL. - func resolveConfigurationFileURL(_ configurationFilePath: String?) throws -> URL { + public func resolveConfigurationFileURL(_ configurationFilePath: String?) throws -> URL { let configurationFileURL = resolveFilePath(configurationFilePath, variations: Self.defaultConfigurationFilenames) guard fileManager.fileExists(atPath: configurationFileURL.path) else { throw ValidationError("No configuration file found") @@ -61,22 +61,27 @@ struct BinaryDependenciesConfigurationReader { /// Resolves the output directory URL using the provided path, or falls back to the default output directories. /// - Parameter outputDirectory: Optional output directory path. /// - Returns: The resolved output directory URL. - func resolveOutputDirectoryURL(_ outputDirectory: String?) -> URL { + public func resolveOutputDirectoryURL(_ outputDirectory: String?) -> URL { resolveFilePath(outputDirectory, variations: Self.defaultOutputDirectoryPaths) } /// Resolves the cache directory URL using the provided path, or falls back to the default cache directories. /// - Parameter cacheDirectory: Optional cache directory path. /// - Returns: The resolved cache directory URL. - func resolveCacheDirectoryURL(_ cacheDirectory: String?) -> URL { + public func resolveCacheDirectoryURL(_ cacheDirectory: String?) -> URL { resolveFilePath(cacheDirectory, variations: Self.defaultCacheDirectoryPaths) } /// Parses and returns a `BinaryDependenciesConfiguration` from the specified configuration file path. - /// - Parameter configurationPath: Optional path to the configuration file. + /// - Parameters: + /// - configurationPath: Optional path to the configuration file. + /// - currentToolVersion: Current binary dependency manager version. /// - Throws: An error if the file cannot be found, decoded, or the version check fails. /// - Returns: The parsed `BinaryDependenciesConfiguration` object. - func readConfiguration(at configurationPath: String?) throws -> BinaryDependenciesConfiguration { + public func readConfiguration( + at configurationPath: String?, + currentToolVersion: Version + ) throws -> BinaryDependenciesConfiguration { let configurationURL: URL = try resolveConfigurationFileURL(configurationPath) @@ -97,10 +102,10 @@ struct BinaryDependenciesConfigurationReader { let configuration = try decoder.decode(BinaryDependenciesConfiguration.self, from: dependenciesData) // Check minimum required version - let minimumRequiredVersion = configuration.minimumVersion ?? BinaryDependenciesManager.version - guard BinaryDependenciesManager.version >= minimumRequiredVersion else { + let minimumRequiredVersion = configuration.minimumVersion ?? currentToolVersion + guard currentToolVersion >= minimumRequiredVersion else { throw ValidationError( - "\(configurationPath ?? configurationURL.lastPathComponent) requires version '\(minimumRequiredVersion)', but current version '\(BinaryDependenciesManager.version)' is lower." + "\(configurationPath ?? configurationURL.lastPathComponent) requires version '\(minimumRequiredVersion)', but current version '\(currentToolVersion)' is lower." ) } diff --git a/Sources/BinaryDependencyManager/Utils/FileManagerProtocol.swift b/Sources/BinaryDependencyManager/Utils/FileManagerProtocol.swift index b0c8bc7..50e2fa6 100644 --- a/Sources/BinaryDependencyManager/Utils/FileManagerProtocol.swift +++ b/Sources/BinaryDependencyManager/Utils/FileManagerProtocol.swift @@ -1,6 +1,6 @@ import Foundation -protocol FileManagerProtocol { +public protocol FileManagerProtocol { /// Returns a Boolean value that indicates whether a file or directory exists at a specified path. /// /// - Parameter path: The path of the file or directory. If path begins with a tilde (~), it must first be expanded with expandingTildeInPath; otherwise, this method returns false. diff --git a/Sources/CommandLine/main.swift b/Sources/CommandLine/main.swift index e1aba2a..7de0ed8 100644 --- a/Sources/CommandLine/main.swift +++ b/Sources/CommandLine/main.swift @@ -1,5 +1,6 @@ import ArgumentParser import Foundation +import Utils // Making sure that output will be shown immediately, as it comes in the script #if os(macOS) diff --git a/Sources/BinaryDependencyManager/Utils/CLI/CLI+GitHub.swift b/Sources/Utils/CLI/CLI+GitHub.swift similarity index 95% rename from Sources/BinaryDependencyManager/Utils/CLI/CLI+GitHub.swift rename to Sources/Utils/CLI/CLI+GitHub.swift index ac2990b..6d43cd0 100644 --- a/Sources/BinaryDependencyManager/Utils/CLI/CLI+GitHub.swift +++ b/Sources/Utils/CLI/CLI+GitHub.swift @@ -2,7 +2,7 @@ import Foundation extension CLI { /// Helper for interacting with the GitHub CLI (`gh`) - struct GitHub { + public struct GitHub { let cliURL: URL } } @@ -11,7 +11,7 @@ extension CLI.GitHub { /// Initializes a `CLI.GitHub` by resolving the path to the `gh` command-line tool. /// /// - Throws: An error if the `gh` CLI cannot be found in PATH. - init() throws { + public init() throws { self.init(cliURL: try CLI.which(cliToolName: "gh")) } } @@ -40,7 +40,7 @@ extension CLI.GitHub { /// - tag: The release tag to download. /// - outputFilePath: The output file path for the downloaded archive. /// - Throws: If the download fails. - func downloadSourceCode( + public func downloadSourceCode( repo: String, tag: String, outputFilePath: String @@ -70,7 +70,7 @@ extension CLI.GitHub { /// - pattern: Optional asset name pattern to select the correct file. /// - outputFilePath: Where to save the downloaded asset. /// - Throws: If the download fails. - func downloadReleaseAsset( + public func downloadReleaseAsset( repo: String, tag: String, pattern: String?, diff --git a/Sources/BinaryDependencyManager/Utils/CLI/CLI.swift b/Sources/Utils/CLI/CLI.swift similarity index 98% rename from Sources/BinaryDependencyManager/Utils/CLI/CLI.swift rename to Sources/Utils/CLI/CLI.swift index c25b4a3..58a92bf 100644 --- a/Sources/BinaryDependencyManager/Utils/CLI/CLI.swift +++ b/Sources/Utils/CLI/CLI.swift @@ -1,6 +1,6 @@ import Foundation -enum CLI { +public enum CLI { /// Returns path to the provided `cliToolName`. /// /// - Parameters: diff --git a/Sources/BinaryDependencyManager/Utils/String+fileURL.swift b/Sources/Utils/String+fileURL.swift similarity index 90% rename from Sources/BinaryDependencyManager/Utils/String+fileURL.swift rename to Sources/Utils/String+fileURL.swift index 22b05de..3c91e89 100644 --- a/Sources/BinaryDependencyManager/Utils/String+fileURL.swift +++ b/Sources/Utils/String+fileURL.swift @@ -2,7 +2,7 @@ import Foundation extension String { /// Absolute file URL with standardized path. - var asFileURL: URL { + public var asFileURL: URL { let url = if #available(macOS 13.0, *) { URL(filePath: self) } else { diff --git a/Sources/BinaryDependencyManager/Utils/Version.swift b/Sources/Utils/Version.swift similarity index 100% rename from Sources/BinaryDependencyManager/Utils/Version.swift rename to Sources/Utils/Version.swift