Add support for resources when packaging using the SwiftPM plugin (#333)

* Add support for resources when packaging using the SwiftPM plugin.

* Copy the resources directory to the working directory instead of recreating the directroy structure.

* Add resource packaging example.

* Use the bundle's url function to locate the file url.

* Fix year for soundness check.

---------

Co-authored-by: Sébastien Stormacq <sebastien.stormacq@gmail.com>
This commit is contained in:
Camden Fullmer
2024-08-05 11:51:21 -04:00
committed by GitHub
parent 872183bbe9
commit 35e0919ed2
4 changed files with 86 additions and 2 deletions
+28
View File
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
import AWSLambdaRuntime
import Foundation
// in this example we are reading from a bundled resource and responding with the contents
@main
struct MyLambda: SimpleLambdaHandler {
func handle(_ input: String, context: LambdaContext) async throws -> String {
guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else {
fatalError("no file url")
}
return try String(contentsOf: fileURL)
}
}
+36
View File
@@ -0,0 +1,36 @@
// swift-tools-version:5.7
import class Foundation.ProcessInfo // needed for CI to test the local version of the library
import PackageDescription
let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: ".",
resources: [
.process("hello.txt"),
]
),
]
)
// for CI to test the local version of the library
if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
package.dependencies = [
.package(name: "swift-aws-lambda-runtime", path: "../.."),
]
}
+1
View File
@@ -0,0 +1 @@
Hello, World!
+21 -2
View File
@@ -67,6 +67,7 @@ struct AWSLambdaPackager: CommandPlugin {
// create the archive
let archives = try self.package(
packageName: context.package.displayName,
products: builtProducts,
toolsProvider: { name in try context.tool(named: name).path },
outputDirectory: configuration.outputDirectory,
@@ -183,6 +184,7 @@ struct AWSLambdaPackager: CommandPlugin {
// TODO: explore using ziplib or similar instead of shelling out
private func package(
packageName: String,
products: [LambdaProduct: Path],
toolsProvider: (String) throws -> Path,
outputDirectory: Path,
@@ -210,17 +212,34 @@ struct AWSLambdaPackager: CommandPlugin {
try FileManager.default.copyItem(atPath: artifactPath.string, toPath: relocatedArtifactPath.string)
try FileManager.default.createSymbolicLink(atPath: symbolicLinkPath.string, withDestinationPath: relocatedArtifactPath.lastComponent)
var arguments: [String] = []
#if os(macOS) || os(Linux)
let arguments = ["--junk-paths", "--symlinks", zipfilePath.string, relocatedArtifactPath.string, symbolicLinkPath.string]
arguments = [
"--recurse-paths",
"--symlinks",
zipfilePath.lastComponent,
relocatedArtifactPath.lastComponent,
symbolicLinkPath.lastComponent,
]
#else
let arguments = [String]()
throw Errors.unsupportedPlatform("can't or don't know how to create a zip file on this platform")
#endif
// add resources
let artifactDirectory = artifactPath.removingLastComponent()
let resourcesDirectoryName = "\(packageName)_\(product.name).resources"
let resourcesDirectory = artifactDirectory.appending(resourcesDirectoryName)
let relocatedResourcesDirectory = workingDirectory.appending(resourcesDirectoryName)
if FileManager.default.fileExists(atPath: resourcesDirectory.string) {
try FileManager.default.copyItem(atPath: resourcesDirectory.string, toPath: relocatedResourcesDirectory.string)
arguments.append(resourcesDirectoryName)
}
// run the zip tool
try self.execute(
executable: zipToolPath,
arguments: arguments,
customWorkingDirectory: workingDirectory,
logLevel: verboseLogging ? .debug : .silent
)