1212//
1313//===----------------------------------------------------------------------===//
1414
15- import PackagePlugin
1615import Foundation
16+ import PackagePlugin
1717
1818@main
1919@available ( macOS 15 . 0 , * )
2020struct AWSLambdaPackager : CommandPlugin {
2121 func performCommand( context: PackagePlugin . PluginContext , arguments: [ String ] ) async throws {
2222 let configuration = try Configuration ( context: context, arguments: arguments)
23+
24+ if configuration. help {
25+ self . displayHelpMessage ( )
26+ return
27+ }
28+
2329 guard !configuration. products. isEmpty else {
2430 throw Errors . unknownProduct ( " no appropriate products found to package " )
2531 }
@@ -112,7 +118,9 @@ struct AWSLambdaPackager: CommandPlugin {
112118 guard let buildPathOutput = dockerBuildOutputPath. split ( separator: " \n " ) . last else {
113119 throw Errors . failedParsingDockerOutput ( dockerBuildOutputPath)
114120 }
115- let buildOutputPath = URL ( string: buildPathOutput. replacingOccurrences ( of: " /workspace/ " , with: packageDirectory. description) ) !
121+ let buildOutputPath = URL (
122+ string: buildPathOutput. replacingOccurrences ( of: " /workspace/ " , with: packageDirectory. description)
123+ ) !
116124
117125 // build the products
118126 var builtProducts = [ LambdaProduct: URL] ( )
@@ -126,7 +134,7 @@ struct AWSLambdaPackager: CommandPlugin {
126134 // just like Package.swift's examples assume ../.., we assume we are two levels below the root project
127135 let slice = packageDirectory. pathComponents. suffix ( 2 )
128136 let beforeLastComponent = packageDirectory. pathComponents [ slice. startIndex]
129- let lastComponent = packageDirectory. pathComponents [ slice. endIndex- 1 ]
137+ let lastComponent = packageDirectory. pathComponents [ slice. endIndex - 1 ]
130138 try Utils . execute (
131139 executable: dockerToolPath,
132140 arguments: [
@@ -270,10 +278,47 @@ struct AWSLambdaPackager: CommandPlugin {
270278 return false
271279 }
272280 }
281+
282+ private func displayHelpMessage( ) {
283+ print (
284+ """
285+ OVERVIEW: A SwiftPM plugin to build and package your lambda function.
286+
287+ REQUIREMENTS: To use this plugin, you must have docker installed and started.
288+
289+ USAGE: swift package --disable-sandbox archive [--help] [--verbose]
290+ [--output-directory <path>]
291+ [--products <list of products>]
292+ [--configuration debug | release]
293+ [--swift-version <version>]
294+ [--base-docker-image <docker_image_name>]
295+ [--disable-docker-image-update]
296+
297+
298+ OPTIONS:
299+ --verbose Produce verbose output for debugging.
300+ --output-directory <path> The path of the binary package.
301+ (default is `.build/plugins/AWSLambdaPackager/outputs/...`)
302+ --products <list> The list of executable targets to build.
303+ (default is taken from Package.swift)
304+ --configuration <name> The build configuration (debug or release)
305+ (default is release)
306+ --swift-version The swift version to use for building.
307+ (default is latest)
308+ This parameter cannot be used when --base-docker-image is specified.
309+ --base-docker-image <name> The name of the base docker image to use for the build.
310+ (default : swift-<version>:amazonlinux2)
311+ This parameter cannot be used when --swift-version is specified.
312+ --disable-docker-image-update Do not attempt to update the docker image
313+ --help Show help information.
314+ """
315+ )
316+ }
273317}
274318
275319@available ( macOS 15 . 0 , * )
276320private struct Configuration : CustomStringConvertible {
321+ public let help : Bool
277322 public let outputDirectory : URL
278323 public let products : [ Product ]
279324 public let explicitProducts : Bool
@@ -294,15 +339,20 @@ private struct Configuration: CustomStringConvertible {
294339 let swiftVersionArgument = argumentExtractor. extractOption ( named: " swift-version " )
295340 let baseDockerImageArgument = argumentExtractor. extractOption ( named: " base-docker-image " )
296341 let disableDockerImageUpdateArgument = argumentExtractor. extractFlag ( named: " disable-docker-image-update " ) > 0
342+ let helpArgument = argumentExtractor. extractFlag ( named: " help " ) > 0
297343
344+ // help required ?
345+ self . help = helpArgument
346+
347+ // verbose logging required ?
298348 self . verboseLogging = verboseArgument
299349
300350 if let outputPath = outputPathArgument. first {
301351 #if os(Linux)
302352 var isDirectory : Bool = false
303353 #else
304354 var isDirectory : ObjCBool = false
305- #endif
355+ #endif
306356 guard FileManager . default. fileExists ( atPath: outputPath, isDirectory: & isDirectory)
307357 else {
308358 throw Errors . invalidArgument ( " invalid output directory ' \( outputPath) ' " )
@@ -447,7 +497,9 @@ private struct LambdaProduct: Hashable {
447497extension PackageManager . BuildResult {
448498 // find the executable produced by the build
449499 func executableArtifact( for product: Product ) -> PackageManager . BuildResult . BuiltArtifact ? {
450- let executables = self . builtArtifacts. filter { $0. kind == . executable && $0. url. lastPathComponent == product. name }
500+ let executables = self . builtArtifacts. filter {
501+ $0. kind == . executable && $0. url. lastPathComponent == product. name
502+ }
451503 guard !executables. isEmpty else {
452504 return nil
453505 }
0 commit comments