diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index 2e091c74..6bd0fcd0 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -12,14 +12,20 @@ // //===----------------------------------------------------------------------===// -import PackagePlugin import Foundation +import PackagePlugin @main @available(macOS 15.0, *) struct AWSLambdaPackager: CommandPlugin { func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws { let configuration = try Configuration(context: context, arguments: arguments) + + if configuration.help { + self.displayHelpMessage() + return + } + guard !configuration.products.isEmpty else { throw Errors.unknownProduct("no appropriate products found to package") } @@ -112,7 +118,9 @@ struct AWSLambdaPackager: CommandPlugin { guard let buildPathOutput = dockerBuildOutputPath.split(separator: "\n").last else { throw Errors.failedParsingDockerOutput(dockerBuildOutputPath) } - let buildOutputPath = URL(string: buildPathOutput.replacingOccurrences(of: "/workspace/", with: packageDirectory.description))! + let buildOutputPath = URL( + string: buildPathOutput.replacingOccurrences(of: "/workspace/", with: packageDirectory.description) + )! // build the products var builtProducts = [LambdaProduct: URL]() @@ -126,7 +134,7 @@ struct AWSLambdaPackager: CommandPlugin { // just like Package.swift's examples assume ../.., we assume we are two levels below the root project let slice = packageDirectory.pathComponents.suffix(2) let beforeLastComponent = packageDirectory.pathComponents[slice.startIndex] - let lastComponent = packageDirectory.pathComponents[slice.endIndex-1] + let lastComponent = packageDirectory.pathComponents[slice.endIndex - 1] try Utils.execute( executable: dockerToolPath, arguments: [ @@ -270,10 +278,47 @@ struct AWSLambdaPackager: CommandPlugin { return false } } + + private func displayHelpMessage() { + print( + """ + OVERVIEW: A SwiftPM plugin to build and package your lambda function. + + REQUIREMENTS: To use this plugin, you must have docker installed and started. + + USAGE: swift package --disable-sandbox archive [--help] [--verbose] + [--output-directory ] + [--products ] + [--configuration debug | release] + [--swift-version ] + [--base-docker-image ] + [--disable-docker-image-update] + + + OPTIONS: + --verbose Produce verbose output for debugging. + --output-directory The path of the binary package. + (default is `.build/plugins/AWSLambdaPackager/outputs/...`) + --products The list of executable targets to build. + (default is taken from Package.swift) + --configuration The build configuration (debug or release) + (default is release) + --swift-version The swift version to use for building. + (default is latest) + This parameter cannot be used when --base-docker-image is specified. + --base-docker-image The name of the base docker image to use for the build. + (default : swift-:amazonlinux2) + This parameter cannot be used when --swift-version is specified. + --disable-docker-image-update Do not attempt to update the docker image + --help Show help information. + """ + ) + } } @available(macOS 15.0, *) private struct Configuration: CustomStringConvertible { + public let help: Bool public let outputDirectory: URL public let products: [Product] public let explicitProducts: Bool @@ -294,7 +339,12 @@ private struct Configuration: CustomStringConvertible { let swiftVersionArgument = argumentExtractor.extractOption(named: "swift-version") let baseDockerImageArgument = argumentExtractor.extractOption(named: "base-docker-image") let disableDockerImageUpdateArgument = argumentExtractor.extractFlag(named: "disable-docker-image-update") > 0 + let helpArgument = argumentExtractor.extractFlag(named: "help") > 0 + // help required ? + self.help = helpArgument + + // verbose logging required ? self.verboseLogging = verboseArgument if let outputPath = outputPathArgument.first { @@ -302,7 +352,7 @@ private struct Configuration: CustomStringConvertible { var isDirectory: Bool = false #else var isDirectory: ObjCBool = false - #endif + #endif guard FileManager.default.fileExists(atPath: outputPath, isDirectory: &isDirectory) else { throw Errors.invalidArgument("invalid output directory '\(outputPath)'") @@ -447,7 +497,9 @@ private struct LambdaProduct: Hashable { extension PackageManager.BuildResult { // find the executable produced by the build func executableArtifact(for product: Product) -> PackageManager.BuildResult.BuiltArtifact? { - let executables = self.builtArtifacts.filter { $0.kind == .executable && $0.url.lastPathComponent == product.name } + let executables = self.builtArtifacts.filter { + $0.kind == .executable && $0.url.lastPathComponent == product.name + } guard !executables.isEmpty else { return nil }