diff --git a/Examples/ResourcePackaging/Lambda.swift b/Examples/ResourcePackaging/Lambda.swift new file mode 100644 index 00000000..5e71a688 --- /dev/null +++ b/Examples/ResourcePackaging/Lambda.swift @@ -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) + } +} diff --git a/Examples/ResourcePackaging/Package.swift b/Examples/ResourcePackaging/Package.swift new file mode 100644 index 00000000..cfb4db28 --- /dev/null +++ b/Examples/ResourcePackaging/Package.swift @@ -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: "../.."), + ] +} diff --git a/Examples/ResourcePackaging/hello.txt b/Examples/ResourcePackaging/hello.txt new file mode 100644 index 00000000..b45ef6fe --- /dev/null +++ b/Examples/ResourcePackaging/hello.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index 25c5823e..33cf00e3 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -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 )