WIP : migrate archive plugin to new lambda-build

This commit is contained in:
Sébastien Stormacq
2024-11-17 22:06:26 +01:00
parent 90517adfe0
commit 2bdd645a4b
16 changed files with 1031 additions and 828 deletions
@@ -0,0 +1,97 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
struct Initializer {
private let destFileName = "Sources/main.swift"
func initialize(arguments: [String]) async throws {
let configuration = try InitializerConfiguration(arguments: arguments)
if configuration.help {
self.displayHelpMessage()
return
}
let destFileURL = configuration.destinationDir.appendingPathComponent(destFileName)
do {
try functionWithUrlTemplate.write(to: destFileURL, atomically: true, encoding: .utf8)
if configuration.verboseLogging {
print("File created at: \(destFileURL)")
}
print("✅ Lambda function written to \(destFileName)")
print("📦 You can now package with: 'swift package archive'")
} catch {
print("🛑Failed to create the Lambda function file: \(error)")
}
}
private func displayHelpMessage() {
print(
"""
OVERVIEW: A SwiftPM plugin to scaffold a HelloWorld Lambda function.
USAGE: swift package lambda-init
[--help] [--verbose]
[--allow-writing-to-package-directory]
OPTIONS:
--allow-writing-to-package-directory Don't ask for permissions to write files.
--verbose Produce verbose output for debugging.
--help Show help information.
"""
)
}
}
private struct InitializerConfiguration: CustomStringConvertible {
public let help: Bool
public let verboseLogging: Bool
public let destinationDir: URL
public init(arguments: [String]) throws {
var argumentExtractor = ArgumentExtractor(arguments)
let verboseArgument = argumentExtractor.extractFlag(named: "verbose") > 0
let helpArgument = argumentExtractor.extractFlag(named: "help") > 0
let destDirArgument = argumentExtractor.extractOption(named: "dest-dir")
// help required ?
self.help = helpArgument
// verbose logging required ?
self.verboseLogging = verboseArgument
// dest dir
self.destinationDir = URL(fileURLWithPath: destDirArgument[0])
}
var description: String {
"""
{
verboseLogging: \(self.verboseLogging)
destinationDir: \(self.destinationDir)
}
"""
}
}
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
let functionWithUrlTemplate = #"""
import AWSLambdaRuntime
import AWSLambdaEvents
// in this example we receive a FunctionURLRequest and we return a FunctionURLResponse
// https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
let runtime = LambdaRuntime {
(event: FunctionURLRequest, context: LambdaContext) -> FunctionURLResponse in
guard let name = event.queryStringParameters?["name"] else {
return FunctionURLResponse(statusCode: .badRequest)
}
return FunctionURLResponse(statusCode: .ok, body: #"{ "message" : "Hello \#\#(name)" } "#)
}
try await runtime.run()
"""#