@@ -21,42 +21,48 @@ import Foundation
2121struct Initializer {
2222
2323 private let destFileName = " Sources/main.swift "
24-
24+
2525 func initialize( arguments: [ String ] ) async throws {
2626
2727 let configuration = try InitializerConfiguration ( arguments: arguments)
28-
28+
2929 if configuration. help {
3030 self . displayHelpMessage ( )
3131 return
3232 }
33-
33+
3434 let destFileURL = configuration. destinationDir. appendingPathComponent ( destFileName)
3535 do {
36- try functionWithUrlTemplate. write ( to: destFileURL, atomically: true , encoding: . utf8)
37-
36+
37+ let template = TemplateType . template ( for: configuration. templateType)
38+ try template. write ( to: destFileURL, atomically: true , encoding: . utf8)
39+
3840 if configuration. verboseLogging {
3941 print ( " File created at: \( destFileURL) " )
4042 }
41-
43+
4244 print ( " ✅ Lambda function written to \( destFileName) " )
43- print ( " 📦 You can now package with: 'swift package archive ' " )
45+ print ( " 📦 You can now package with: 'swift package lambda-build ' " )
4446 } catch {
4547 print ( " 🛑Failed to create the Lambda function file: \( error) " )
4648 }
4749 }
48-
49-
50+
51+
5052 private func displayHelpMessage( ) {
5153 print (
5254 """
5355 OVERVIEW: A SwiftPM plugin to scaffold a HelloWorld Lambda function.
54-
56+ By default, it creates a Lambda function that receives a JSON
57+ document and responds with another JSON document.
58+
5559 USAGE: swift package lambda-init
5660 [--help] [--verbose]
61+ [--with-url]
5762 [--allow-writing-to-package-directory]
58-
63+
5964 OPTIONS:
65+ --with-url Create a Lambda function exposed with an URL
6066 --allow-writing-to-package-directory Don't ask for permissions to write files.
6167 --verbose Produce verbose output for debugging.
6268 --help Show help information.
@@ -65,32 +71,50 @@ struct Initializer {
6571 }
6672}
6773
74+ private enum TemplateType {
75+ case `default`
76+ case url
77+
78+ static func template( for type: TemplateType ) -> String {
79+ switch type {
80+ case . default: return functionWithJSONTemplate
81+ case . url: return functionWithUrlTemplate
82+ }
83+ }
84+ }
85+
6886private struct InitializerConfiguration : CustomStringConvertible {
6987 public let help : Bool
7088 public let verboseLogging : Bool
7189 public let destinationDir : URL
72-
90+ public let templateType : TemplateType
91+
7392 public init ( arguments: [ String ] ) throws {
7493 var argumentExtractor = ArgumentExtractor ( arguments)
7594 let verboseArgument = argumentExtractor. extractFlag ( named: " verbose " ) > 0
7695 let helpArgument = argumentExtractor. extractFlag ( named: " help " ) > 0
7796 let destDirArgument = argumentExtractor. extractOption ( named: " dest-dir " )
78-
97+ let templateURLArgument = argumentExtractor. extractFlag ( named: " with-url " ) > 0
98+
7999 // help required ?
80100 self . help = helpArgument
81-
101+
82102 // verbose logging required ?
83103 self . verboseLogging = verboseArgument
84104
85105 // dest dir
86106 self . destinationDir = URL ( fileURLWithPath: destDirArgument [ 0 ] )
107+
108+ // template type. Default is the JSON one
109+ self . templateType = templateURLArgument ? . url : . default
87110 }
88-
111+
89112 var description : String {
90113 """
91114 {
92115 verboseLogging: \( self . verboseLogging)
93116 destinationDir: \( self . destinationDir)
117+ templateType: \( self . templateType)
94118 }
95119 """
96120 }
0 commit comments