colibri/Library/Sources/Public/Tasks/BuildArtifactTask.swift
Javier Cicchelli f8a14e46ed Improved the Build subcommand to support the building of Docker images (#11)
This PR contains the work done to support the building of Docker images in the `Build` subcommand in the executable target. So, for this purpose, the following task have been done:

- added a basic boilerplate of the `docker-compose.yml` file;
- fixed some issues found in the boilerplate of the `Dockerfile` file;
- defined the `Randomable` protocol;
- defined the `Artifact` enumeration;
- updated the `BuildProjectTask` task to support building Docker images if required;
- renamed the `BuildProjectTask` task as `BuildArtifactTask`.

Reviewed-on: #11
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-22 00:33:52 +00:00

41 lines
1.1 KiB
Swift

import Foundation
public struct BuildArtifactTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(_ artifact: Artifact, at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL: URL = switch artifact {
case .executable: .init(at: "/usr/bin/swift")
case .image: .init(at: "/usr/local/bin/docker")
}
var arguments: [String] = switch artifact {
case .executable: ["build"]
case .image: ["compose", "build"]
}
if let location {
if case .executable = artifact {
arguments.append(contentsOf: ["--package-path", location.pathString])
} else if case .image = artifact {
arguments.insert(contentsOf: ["--project-directory", location.pathString], at: 1)
}
}
try await terminalService.run(executableURL, arguments: arguments)
}
}