Skip to content
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
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions Sources/BinaryDependencyManager/Models/Dependency.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -24,7 +24,7 @@ struct BinaryDependenciesConfigurationReader {

private let fileManager: FileManagerProtocol

init(fileManager: FileManagerProtocol = FileManager.default) {
public init(fileManager: FileManagerProtocol = FileManager.default) {
self.fileManager = fileManager
}

Expand All @@ -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")
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Injecting tool version here, since BinaryDependencyManager target has no access to the CommandLine target where version will be declared later.

) throws -> BinaryDependenciesConfiguration {

let configurationURL: URL = try resolveConfigurationFileURL(configurationPath)

Expand All @@ -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."
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions Sources/CommandLine/main.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension CLI {
/// Helper for interacting with the GitHub CLI (`gh`)
struct GitHub {
public struct GitHub {
let cliURL: URL
}
}
Expand All @@ -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"))
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

enum CLI {
public enum CLI {
/// Returns path to the provided `cliToolName`.
///
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down