Skip to content

Commit 35e0919

Browse files
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 <[email protected]>
1 parent 872183b commit 35e0919

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
import Foundation
17+
18+
// in this example we are reading from a bundled resource and responding with the contents
19+
20+
@main
21+
struct MyLambda: SimpleLambdaHandler {
22+
func handle(_ input: String, context: LambdaContext) async throws -> String {
23+
guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else {
24+
fatalError("no file url")
25+
}
26+
return try String(contentsOf: fileURL)
27+
}
28+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version:5.7
2+
3+
import class Foundation.ProcessInfo // needed for CI to test the local version of the library
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "swift-aws-lambda-runtime-example",
8+
platforms: [
9+
.macOS(.v12),
10+
],
11+
products: [
12+
.executable(name: "MyLambda", targets: ["MyLambda"]),
13+
],
14+
dependencies: [
15+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
16+
],
17+
targets: [
18+
.executableTarget(
19+
name: "MyLambda",
20+
dependencies: [
21+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
22+
],
23+
path: ".",
24+
resources: [
25+
.process("hello.txt"),
26+
]
27+
),
28+
]
29+
)
30+
31+
// for CI to test the local version of the library
32+
if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
33+
package.dependencies = [
34+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
35+
]
36+
}

Examples/ResourcePackaging/hello.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, World!

Plugins/AWSLambdaPackager/Plugin.swift

+21-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct AWSLambdaPackager: CommandPlugin {
6767

6868
// create the archive
6969
let archives = try self.package(
70+
packageName: context.package.displayName,
7071
products: builtProducts,
7172
toolsProvider: { name in try context.tool(named: name).path },
7273
outputDirectory: configuration.outputDirectory,
@@ -183,6 +184,7 @@ struct AWSLambdaPackager: CommandPlugin {
183184

184185
// TODO: explore using ziplib or similar instead of shelling out
185186
private func package(
187+
packageName: String,
186188
products: [LambdaProduct: Path],
187189
toolsProvider: (String) throws -> Path,
188190
outputDirectory: Path,
@@ -210,17 +212,34 @@ struct AWSLambdaPackager: CommandPlugin {
210212
try FileManager.default.copyItem(atPath: artifactPath.string, toPath: relocatedArtifactPath.string)
211213
try FileManager.default.createSymbolicLink(atPath: symbolicLinkPath.string, withDestinationPath: relocatedArtifactPath.lastComponent)
212214

215+
var arguments: [String] = []
213216
#if os(macOS) || os(Linux)
214-
let arguments = ["--junk-paths", "--symlinks", zipfilePath.string, relocatedArtifactPath.string, symbolicLinkPath.string]
217+
arguments = [
218+
"--recurse-paths",
219+
"--symlinks",
220+
zipfilePath.lastComponent,
221+
relocatedArtifactPath.lastComponent,
222+
symbolicLinkPath.lastComponent,
223+
]
215224
#else
216-
let arguments = [String]()
217225
throw Errors.unsupportedPlatform("can't or don't know how to create a zip file on this platform")
218226
#endif
219227

228+
// add resources
229+
let artifactDirectory = artifactPath.removingLastComponent()
230+
let resourcesDirectoryName = "\(packageName)_\(product.name).resources"
231+
let resourcesDirectory = artifactDirectory.appending(resourcesDirectoryName)
232+
let relocatedResourcesDirectory = workingDirectory.appending(resourcesDirectoryName)
233+
if FileManager.default.fileExists(atPath: resourcesDirectory.string) {
234+
try FileManager.default.copyItem(atPath: resourcesDirectory.string, toPath: relocatedResourcesDirectory.string)
235+
arguments.append(resourcesDirectoryName)
236+
}
237+
220238
// run the zip tool
221239
try self.execute(
222240
executable: zipToolPath,
223241
arguments: arguments,
242+
customWorkingDirectory: workingDirectory,
224243
logLevel: verboseLogging ? .debug : .silent
225244
)
226245

0 commit comments

Comments
 (0)