Skip to content

Commit 5c035d2

Browse files
committed
parse arguments
1 parent 95134c0 commit 5c035d2

File tree

1 file changed

+93
-35
lines changed

1 file changed

+93
-35
lines changed

Plugins/AWSLambdaPackager/Plugin.swift

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Glibc
2323
@main
2424
struct AWSLambdaPackager: CommandPlugin {
2525
func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws {
26-
let configuration = Configuration(context: context, arguments: arguments)
26+
let configuration = try Configuration(context: context, arguments: arguments)
2727
guard !configuration.products.isEmpty else {
2828
throw Errors.unknownProduct("no appropriate products found to package")
2929
}
@@ -44,7 +44,7 @@ struct AWSLambdaPackager: CommandPlugin {
4444
products: configuration.products,
4545
toolsProvider: { name in try context.tool(named: name).path },
4646
outputDirectory: configuration.outputDirectory,
47-
baseImage: configuration.baseImage,
47+
baseImage: configuration.baseDockerImage,
4848
buildConfiguration: configuration.buildConfiguration,
4949
verboseLogging: configuration.verboseLogging
5050
)
@@ -101,11 +101,9 @@ struct AWSLambdaPackager: CommandPlugin {
101101

102102
var builtProducts = [LambdaProduct: Path]()
103103
for product in products {
104-
if verboseLogging {
105-
print("-------------------------------------------------------------------------")
106-
print("building \"\(product.name)\" in docker")
107-
print("-------------------------------------------------------------------------")
108-
}
104+
print("-------------------------------------------------------------------------")
105+
print("building \"\(product.name)\" in docker")
106+
print("-------------------------------------------------------------------------")
109107
//
110108
let buildCommand = "swift build --product \(product.name) -c \(buildConfiguration.rawValue) --static-swift-stdlib"
111109
try self.execute(
@@ -126,11 +124,9 @@ struct AWSLambdaPackager: CommandPlugin {
126124
) throws -> [LambdaProduct: Path] {
127125
var results = [LambdaProduct: Path]()
128126
for product in products {
129-
if verboseLogging {
130-
print("-------------------------------------------------------------------------")
131-
print("building \"\(product.name)\"")
132-
print("-------------------------------------------------------------------------")
133-
}
127+
print("-------------------------------------------------------------------------")
128+
print("building \"\(product.name)\"")
129+
print("-------------------------------------------------------------------------")
134130
var parameters = PackageManager.BuildParameters()
135131
parameters.configuration = buildConfiguration
136132
parameters.otherSwiftcFlags = ["-static-stdlib"]
@@ -148,6 +144,7 @@ struct AWSLambdaPackager: CommandPlugin {
148144
return results
149145
}
150146

147+
#warning("FIXME: use zlib")
151148
private func package(
152149
products: [LambdaProduct: Path],
153150
toolsProvider: (String) throws -> Path,
@@ -158,11 +155,9 @@ struct AWSLambdaPackager: CommandPlugin {
158155

159156
var archives = [LambdaProduct: Path]()
160157
for (product, artifactPath) in products {
161-
if verboseLogging {
162-
print("-------------------------------------------------------------------------")
163-
print("archiving \"\(product.name)\"")
164-
print("-------------------------------------------------------------------------")
165-
}
158+
print("-------------------------------------------------------------------------")
159+
print("archiving \"\(product.name)\"")
160+
print("-------------------------------------------------------------------------")
166161

167162
// prep zipfile location
168163
let workingDirectory = outputDirectory.appending(product.name)
@@ -217,10 +212,8 @@ struct AWSLambdaPackager: CommandPlugin {
217212
}
218213
sync.enter()
219214
defer { sync.leave() }
220-
if verboseLogging {
221-
print(_output)
222-
fflush(stdout)
223-
}
215+
print(_output)
216+
fflush(stdout)
224217
output += _output
225218
}
226219

@@ -266,28 +259,93 @@ struct AWSLambdaPackager: CommandPlugin {
266259
}
267260
}
268261

269-
private struct Configuration {
262+
private struct Configuration: CustomStringConvertible {
270263
public let outputDirectory: Path
271264
public let products: [Product]
272265
public let buildConfiguration: PackageManager.BuildConfiguration
273266
public let verboseLogging: Bool
274-
public let baseImage: String
275-
public let version: String
276-
public let applicationRoot: String
277-
278-
public init(context: PluginContext, arguments: [String]) {
279-
self.outputDirectory = context.pluginWorkDirectory.appending(subpath: "\(AWSLambdaPackager.self)") // FIXME: read argument
280-
self.products = context.package.products.filter { $0 is ExecutableProduct } // FIXME: read argument, filter is ugly
281-
self.buildConfiguration = .release // FIXME: read argument
282-
self.verboseLogging = true // FIXME: read argument
283-
let swiftVersion = "5.6" // FIXME: read dynamically current version
284-
self.baseImage = "swift:\(swiftVersion)-amazonlinux2" // FIXME: read argument
285-
self.version = "1.0.0" // FIXME: where can we get this from? argument?
286-
self.applicationRoot = "/app" // FIXME: read argument
267+
public let baseDockerImage: String
268+
269+
public init(
270+
context: PluginContext,
271+
arguments: [String]
272+
) throws {
273+
var argumentExtractor = ArgumentExtractor(arguments)
274+
let verboseArgument = argumentExtractor.extractFlag(named: "verbose") > 0
275+
let outputPathArgument = argumentExtractor.extractOption(named: "output-path")
276+
let productsArgument = argumentExtractor.extractOption(named: "products")
277+
let configurationArgument = argumentExtractor.extractOption(named: "configuration")
278+
let swiftVersionArgument = argumentExtractor.extractOption(named: "swift-version")
279+
let baseDockerImageArgument = argumentExtractor.extractOption(named: "base-docker-image")
280+
281+
self.verboseLogging = verboseArgument
282+
283+
if let outputPath = outputPathArgument.first {
284+
var isDirectory: ObjCBool = false
285+
guard FileManager.default.fileExists(atPath: outputPath, isDirectory: &isDirectory), isDirectory.boolValue else {
286+
throw Errors.invalidArgument("invalid output directory \(outputPath)")
287+
}
288+
self.outputDirectory = Path(outputPath)
289+
} else {
290+
self.outputDirectory = context.pluginWorkDirectory.appending(subpath: "\(AWSLambdaPackager.self)")
291+
}
292+
293+
if !productsArgument.isEmpty {
294+
let products = try context.package.products(named: productsArgument)
295+
for product in products {
296+
guard product is ExecutableProduct else {
297+
throw Errors.invalidArgument("product named \(product.name) is not an executable product")
298+
}
299+
}
300+
self.products = products
301+
302+
} else {
303+
self.products = context.package.products.filter { $0 is ExecutableProduct }
304+
}
305+
306+
if let buildConfigurationName = configurationArgument.first {
307+
guard let buildConfiguration = PackageManager.BuildConfiguration(rawValue: buildConfigurationName) else {
308+
throw Errors.invalidArgument("invalid build configuration named \(buildConfigurationName)")
309+
}
310+
self.buildConfiguration = buildConfiguration
311+
} else {
312+
self.buildConfiguration = .release
313+
}
314+
315+
guard !(!swiftVersionArgument.isEmpty && !baseDockerImageArgument.isEmpty) else {
316+
throw Errors.invalidArgument("--swift-version and --base-docker-image are mutually exclusive")
317+
}
318+
319+
let swiftVersion = swiftVersionArgument.first ?? Self.getSwiftVersion()
320+
self.baseDockerImage = baseDockerImageArgument.first ?? "swift:\(swiftVersion)-amazonlinux2"
321+
322+
if self.verboseLogging {
323+
print("-------------------------------------------------------------------------")
324+
print("configuration")
325+
print("-------------------------------------------------------------------------")
326+
print(self)
327+
}
328+
}
329+
330+
var description: String {
331+
"""
332+
{
333+
outputDirectory: \(self.outputDirectory)
334+
products: \(self.products.map(\.name))
335+
buildConfiguration: \(self.buildConfiguration)
336+
baseDockerImage: \(self.baseDockerImage)
337+
}
338+
"""
339+
}
340+
341+
#warning("FIXME: read this programmatically")
342+
private static func getSwiftVersion() -> String {
343+
"5.6"
287344
}
288345
}
289346

290347
private enum Errors: Error {
348+
case invalidArgument(String)
291349
case unsupportedPlatform(String)
292350
case unknownProduct(String)
293351
case unknownExecutable(String)

0 commit comments

Comments
 (0)