Skip to content

Commit

Permalink
Add support for resources when packaging using the SwiftPM plugin (#333)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
camdenfullmer and sebsto committed Aug 5, 2024
1 parent 872183b commit 35e0919
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Examples/ResourcePackaging/Lambda.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions Examples/ResourcePackaging/Package.swift
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions Examples/ResourcePackaging/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
23 changes: 21 additions & 2 deletions Plugins/AWSLambdaPackager/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 35e0919

Please sign in to comment.