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>
This commit was merged in pull request #11.
This commit is contained in:
2025-02-22 00:33:52 +00:00
committed by Javier Cicchelli
parent 888d00c1e8
commit f8a14e46ed
15 changed files with 181 additions and 81 deletions
@@ -0,0 +1,7 @@
services:
app:
build:
context: .
port:
- 3000:8080
command: ["--hostname", "0.0.0.0", "--port", "8080"]
+2 -2
View File
@@ -1,7 +1,7 @@
# ================================
# Build image
# ================================
FROM swift:6.0-noble as build
FROM swift:6.0.3-noble AS build
# Install OS updates
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
@@ -83,5 +83,5 @@ USER hummingbird:hummingbird
EXPOSE 8080
# Start the Hummingbird service when the image is run, default to listening on 8080 in production environment
ENTRYPOINT ["./App]
ENTRYPOINT ["./App"]
CMD ["--hostname", "0.0.0.0", "--port", "8080"]
@@ -2,6 +2,7 @@ enum File: String {
case appArguments = "AppArguments"
case appBuilder = "AppBuilder"
case appOptions = "AppOptions"
case dockerCompose = "DockerCompose"
case dockerFile = "DockerFile"
case dockerIgnore = "DockerIgnore"
case environment = "Environment"
@@ -24,6 +25,7 @@ extension File {
case .appArguments: "AppArguments.swift"
case .appBuilder: "AppBuilder.swift"
case .appOptions: "AppOptions.swift"
case .dockerCompose: "docker-compose.yml"
case .dockerFile: "Dockerfile"
case .dockerIgnore: ".dockerignore"
case .environment: "Environment+Properties.swift"
@@ -0,0 +1,7 @@
protocol Randomable: CaseIterable {
// MARK: Functions
static func random() -> Self
}
@@ -0,0 +1,16 @@
public enum Artifact: String {
case executable
case image
}
// MARK: - Randomable
extension Artifact: Randomable {
// MARK: Functions
static func random() -> Artifact {
.allCases.randomElement() ?? .executable
}
}
@@ -3,9 +3,9 @@ public enum IDE: String {
case xcode
}
// MARK: - Extension
// MARK: - Randomable
extension IDE {
extension IDE: Randomable {
// MARK: Functions
@@ -14,7 +14,3 @@ extension IDE {
}
}
// MARK: - CaseIterable
extension IDE: CaseIterable {}
@@ -0,0 +1,40 @@
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)
}
}
@@ -1,29 +0,0 @@
import Foundation
public struct BuildProjectTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["build"]
if let location {
arguments.append(contentsOf: ["--package-path", location.pathString])
}
try await terminalService.run(executableURL, arguments: arguments)
}
}