Skip to content

add help option on archiver plugin #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions Plugins/AWSLambdaPackager/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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]()
Expand All @@ -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: [
Expand Down Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to get rid of --disable-sandbox in #379.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but this is another subject. I'll have separate PRs to address these two changes.

Can we first merge this one ?

[--output-directory <path>]
[--products <list of products>]
[--configuration debug | release]
[--swift-version <version>]
[--base-docker-image <docker_image_name>]
[--disable-docker-image-update]


OPTIONS:
--verbose Produce verbose output for debugging.
--output-directory <path> The path of the binary package.
(default is `.build/plugins/AWSLambdaPackager/outputs/...`)
--products <list> The list of executable targets to build.
(default is taken from Package.swift)
--configuration <name> 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 <name> The name of the base docker image to use for the build.
(default : swift-<version>: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
Expand All @@ -294,15 +339,20 @@ 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 {
#if os(Linux)
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)'")
Expand Down Expand Up @@ -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
}
Expand Down
Loading