@@ -23,7 +23,7 @@ import Glibc
23
23
@main
24
24
struct AWSLambdaPackager : CommandPlugin {
25
25
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)
27
27
guard !configuration. products. isEmpty else {
28
28
throw Errors . unknownProduct ( " no appropriate products found to package " )
29
29
}
@@ -44,7 +44,7 @@ struct AWSLambdaPackager: CommandPlugin {
44
44
products: configuration. products,
45
45
toolsProvider: { name in try context. tool ( named: name) . path } ,
46
46
outputDirectory: configuration. outputDirectory,
47
- baseImage: configuration. baseImage ,
47
+ baseImage: configuration. baseDockerImage ,
48
48
buildConfiguration: configuration. buildConfiguration,
49
49
verboseLogging: configuration. verboseLogging
50
50
)
@@ -101,11 +101,9 @@ struct AWSLambdaPackager: CommandPlugin {
101
101
102
102
var builtProducts = [ LambdaProduct: Path] ( )
103
103
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 ( " ------------------------------------------------------------------------- " )
109
107
//
110
108
let buildCommand = " swift build --product \( product. name) -c \( buildConfiguration. rawValue) --static-swift-stdlib "
111
109
try self . execute (
@@ -126,11 +124,9 @@ struct AWSLambdaPackager: CommandPlugin {
126
124
) throws -> [ LambdaProduct : Path ] {
127
125
var results = [ LambdaProduct: Path] ( )
128
126
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 ( " ------------------------------------------------------------------------- " )
134
130
var parameters = PackageManager . BuildParameters ( )
135
131
parameters. configuration = buildConfiguration
136
132
parameters. otherSwiftcFlags = [ " -static-stdlib " ]
@@ -148,6 +144,7 @@ struct AWSLambdaPackager: CommandPlugin {
148
144
return results
149
145
}
150
146
147
+ #warning("FIXME: use zlib")
151
148
private func package (
152
149
products: [ LambdaProduct : Path ] ,
153
150
toolsProvider: ( String ) throws -> Path ,
@@ -158,11 +155,9 @@ struct AWSLambdaPackager: CommandPlugin {
158
155
159
156
var archives = [ LambdaProduct: Path] ( )
160
157
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 ( " ------------------------------------------------------------------------- " )
166
161
167
162
// prep zipfile location
168
163
let workingDirectory = outputDirectory. appending ( product. name)
@@ -217,10 +212,8 @@ struct AWSLambdaPackager: CommandPlugin {
217
212
}
218
213
sync. enter ( )
219
214
defer { sync. leave ( ) }
220
- if verboseLogging {
221
- print ( _output)
222
- fflush ( stdout)
223
- }
215
+ print ( _output)
216
+ fflush ( stdout)
224
217
output += _output
225
218
}
226
219
@@ -266,28 +259,93 @@ struct AWSLambdaPackager: CommandPlugin {
266
259
}
267
260
}
268
261
269
- private struct Configuration {
262
+ private struct Configuration : CustomStringConvertible {
270
263
public let outputDirectory : Path
271
264
public let products : [ Product ]
272
265
public let buildConfiguration : PackageManager . BuildConfiguration
273
266
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 "
287
344
}
288
345
}
289
346
290
347
private enum Errors : Error {
348
+ case invalidArgument( String )
291
349
case unsupportedPlatform( String )
292
350
case unknownProduct( String )
293
351
case unknownExecutable( String )
0 commit comments