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
28 changes: 26 additions & 2 deletions Sources/AWSLambdaPluginHelper/lambda-build/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ struct Builder {

// Select the build backend: build natively when already on an Amazon Linux host,
// otherwise cross-compile using the backend chosen by --cross-compile.
let backend: BuildBackend
let backend: any BuildBackend
if self.isAmazonLinux(.al2) || self.isAmazonLinux(.al2023) {
backend = NativeBuildBackend()
} else {
backend = try configuration.crossCompileMethod.makeBackend(configuration: configuration)
backend = try configuration.makeCrossCompileBackend()
}

let builtProducts = try backend.build(
Expand Down Expand Up @@ -379,6 +379,30 @@ struct BuilderConfiguration: CustomStringConvertible {
}
}

/// Creates the ``BuildBackend`` that performs a cross-compiled build for the configured method.
///
/// Used when the host is not already an Amazon Linux machine. The configuration already holds
/// everything a backend needs (the resolved tool path, base image, and image-update
/// preference), so the factory lives here rather than on ``CrossCompileMethod``.
func makeCrossCompileBackend() throws -> any BuildBackend {
let cli: any ContainerCLI
switch self.crossCompileMethod {
case .docker:
cli = DockerCLI()
case .container:
cli = AppleContainerCLI()
case .swiftStaticSdk, .customSdk:
throw BuilderErrors.unsupportedCrossCompileMethod(self.crossCompileMethod)
}
return ContainerBuildBackend(
cli: cli,
toolPath: self.crossCompileToolPath,
baseImage: self.baseDockerImage,
disableImageUpdate: self.disableDockerImageUpdate,
method: self.crossCompileMethod
)
}

var description: String {
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import Foundation

/// The cross-compilation method requested via `--cross-compile`.
///
/// This enum is the parsed user choice and a factory for the matching ``BuildBackend``. It holds
/// no execution logic itself: container argument spelling lives in the ``ContainerCLI`` types and
/// the build flow lives in the ``BuildBackend`` types.
/// This enum is purely the parsed user choice. It holds no execution logic: container argument
/// spelling lives in the ``ContainerCLI`` types, the build flow lives in the ``BuildBackend``
/// types, and backend construction lives on ``BuilderConfiguration`` (which holds everything a
/// backend needs).
@available(LambdaSwift 2.0, *)
enum CrossCompileMethod: String, CustomStringConvertible {
case docker
Expand Down Expand Up @@ -56,29 +57,6 @@ enum CrossCompileMethod: String, CustomStringConvertible {
return method
}

/// Creates the ``BuildBackend`` that performs a cross-compiled build for this method.
///
/// Used when the host is not already an Amazon Linux machine. The configuration supplies the
/// resolved tool path, base image, and image-update preference.
func makeBackend(configuration: BuilderConfiguration) throws -> BuildBackend {
let cli: ContainerCLI
switch self {
case .docker:
cli = DockerCLI()
case .container:
cli = AppleContainerCLI()
case .swiftStaticSdk, .customSdk:
throw BuilderErrors.unsupportedCrossCompileMethod(self)
}
return ContainerBuildBackend(
cli: cli,
toolPath: configuration.crossCompileToolPath,
baseImage: configuration.baseDockerImage,
disableImageUpdate: configuration.disableDockerImageUpdate,
method: self
)
}

var description: String {
self.rawValue
}
Expand Down
22 changes: 7 additions & 15 deletions Tests/AWSLambdaPluginHelperTests/BuildBackendTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ struct AppleContainerCLIArgumentTests {
}
}

// MARK: - CrossCompileMethod.makeBackend
// MARK: - BuilderConfiguration.makeCrossCompileBackend

@Suite("CrossCompileMethod backend selection")
struct CrossCompileMethodBackendTests {
@Suite("Cross-compile backend selection")
struct CrossCompileBackendSelectionTests {

@available(LambdaSwift 2.0, *)
static func makeConfiguration() throws -> BuilderConfiguration {
static func makeConfiguration(method: String) throws -> BuilderConfiguration {
try BuilderConfiguration(arguments: [
"--package-id", "test",
"--package-display-name", "Test",
Expand All @@ -166,13 +166,14 @@ struct CrossCompileMethodBackendTests {
"--output-path", "/tmp",
"--products", "MyLambda",
"--configuration", "release",
"--cross-compile", method,
])
}

@available(LambdaSwift 2.0, *)
@Test("docker selects a container backend running the docker CLI")
func dockerBackend() throws {
let backend = try CrossCompileMethod.docker.makeBackend(configuration: Self.makeConfiguration())
let backend = try Self.makeConfiguration(method: "docker").makeCrossCompileBackend()
let container = try #require(backend as? ContainerBuildBackend)
#expect(container.name == "docker")
#expect(container.cli is DockerCLI)
Expand All @@ -181,18 +182,9 @@ struct CrossCompileMethodBackendTests {
@available(LambdaSwift 2.0, *)
@Test("container selects a container backend running the apple container CLI")
func containerBackend() throws {
let backend = try CrossCompileMethod.container.makeBackend(configuration: Self.makeConfiguration())
let backend = try Self.makeConfiguration(method: "container").makeCrossCompileBackend()
let container = try #require(backend as? ContainerBuildBackend)
#expect(container.name == "container")
#expect(container.cli is AppleContainerCLI)
}

@available(LambdaSwift 2.0, *)
@Test("unsupported methods throw", arguments: [CrossCompileMethod.swiftStaticSdk, .customSdk])
func unsupportedThrows(method: CrossCompileMethod) throws {
let config = try Self.makeConfiguration()
#expect(throws: BuilderErrors.self) {
_ = try method.makeBackend(configuration: config)
}
}
}
16 changes: 6 additions & 10 deletions Tests/AWSLambdaRuntimeTests/CollectEverythingLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,12 @@ struct CollectEverythingLogHandler: LogHandler {
self.logStore = logStore
}

func log(
level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String,
file: String,
function: String,
line: UInt
) {
self.logStore.append(level: level, message: message, metadata: self.metadata.merging(metadata ?? [:]) { $1 })
func log(event: LogEvent) {
self.logStore.append(
level: event.level,
message: event.message,
metadata: self.metadata.merging(event.metadata ?? [:]) { $1 }
)
}

subscript(metadataKey key: String) -> Logger.Metadata.Value? {
Expand Down