12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- import PackagePlugin
16
15
import Foundation
16
+ import PackagePlugin
17
17
18
18
@main
19
19
@available ( macOS 15 . 0 , * )
20
20
struct AWSLambdaPackager : CommandPlugin {
21
21
func performCommand( context: PackagePlugin . PluginContext , arguments: [ String ] ) async throws {
22
22
let configuration = try Configuration ( context: context, arguments: arguments)
23
+
24
+ if configuration. help {
25
+ self . displayHelpMessage ( )
26
+ return
27
+ }
28
+
23
29
guard !configuration. products. isEmpty else {
24
30
throw Errors . unknownProduct ( " no appropriate products found to package " )
25
31
}
@@ -112,7 +118,9 @@ struct AWSLambdaPackager: CommandPlugin {
112
118
guard let buildPathOutput = dockerBuildOutputPath. split ( separator: " \n " ) . last else {
113
119
throw Errors . failedParsingDockerOutput ( dockerBuildOutputPath)
114
120
}
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
+ ) !
116
124
117
125
// build the products
118
126
var builtProducts = [ LambdaProduct: URL] ( )
@@ -126,7 +134,7 @@ struct AWSLambdaPackager: CommandPlugin {
126
134
// just like Package.swift's examples assume ../.., we assume we are two levels below the root project
127
135
let slice = packageDirectory. pathComponents. suffix ( 2 )
128
136
let beforeLastComponent = packageDirectory. pathComponents [ slice. startIndex]
129
- let lastComponent = packageDirectory. pathComponents [ slice. endIndex- 1 ]
137
+ let lastComponent = packageDirectory. pathComponents [ slice. endIndex - 1 ]
130
138
try Utils . execute (
131
139
executable: dockerToolPath,
132
140
arguments: [
@@ -270,10 +278,47 @@ struct AWSLambdaPackager: CommandPlugin {
270
278
return false
271
279
}
272
280
}
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
+ }
273
317
}
274
318
275
319
@available ( macOS 15 . 0 , * )
276
320
private struct Configuration : CustomStringConvertible {
321
+ public let help : Bool
277
322
public let outputDirectory : URL
278
323
public let products : [ Product ]
279
324
public let explicitProducts : Bool
@@ -294,15 +339,20 @@ private struct Configuration: CustomStringConvertible {
294
339
let swiftVersionArgument = argumentExtractor. extractOption ( named: " swift-version " )
295
340
let baseDockerImageArgument = argumentExtractor. extractOption ( named: " base-docker-image " )
296
341
let disableDockerImageUpdateArgument = argumentExtractor. extractFlag ( named: " disable-docker-image-update " ) > 0
342
+ let helpArgument = argumentExtractor. extractFlag ( named: " help " ) > 0
297
343
344
+ // help required ?
345
+ self . help = helpArgument
346
+
347
+ // verbose logging required ?
298
348
self . verboseLogging = verboseArgument
299
349
300
350
if let outputPath = outputPathArgument. first {
301
351
#if os(Linux)
302
352
var isDirectory : Bool = false
303
353
#else
304
354
var isDirectory : ObjCBool = false
305
- #endif
355
+ #endif
306
356
guard FileManager . default. fileExists ( atPath: outputPath, isDirectory: & isDirectory)
307
357
else {
308
358
throw Errors . invalidArgument ( " invalid output directory ' \( outputPath) ' " )
@@ -447,7 +497,9 @@ private struct LambdaProduct: Hashable {
447
497
extension PackageManager . BuildResult {
448
498
// find the executable produced by the build
449
499
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
+ }
451
503
guard !executables. isEmpty else {
452
504
return nil
453
505
}
0 commit comments