@@ -21,42 +21,48 @@ import Foundation
21
21
struct Initializer {
22
22
23
23
private let destFileName = " Sources/main.swift "
24
-
24
+
25
25
func initialize( arguments: [ String ] ) async throws {
26
26
27
27
let configuration = try InitializerConfiguration ( arguments: arguments)
28
-
28
+
29
29
if configuration. help {
30
30
self . displayHelpMessage ( )
31
31
return
32
32
}
33
-
33
+
34
34
let destFileURL = configuration. destinationDir. appendingPathComponent ( destFileName)
35
35
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
+
38
40
if configuration. verboseLogging {
39
41
print ( " File created at: \( destFileURL) " )
40
42
}
41
-
43
+
42
44
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 ' " )
44
46
} catch {
45
47
print ( " 🛑Failed to create the Lambda function file: \( error) " )
46
48
}
47
49
}
48
-
49
-
50
+
51
+
50
52
private func displayHelpMessage( ) {
51
53
print (
52
54
"""
53
55
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
+
55
59
USAGE: swift package lambda-init
56
60
[--help] [--verbose]
61
+ [--with-url]
57
62
[--allow-writing-to-package-directory]
58
-
63
+
59
64
OPTIONS:
65
+ --with-url Create a Lambda function exposed with an URL
60
66
--allow-writing-to-package-directory Don't ask for permissions to write files.
61
67
--verbose Produce verbose output for debugging.
62
68
--help Show help information.
@@ -65,32 +71,50 @@ struct Initializer {
65
71
}
66
72
}
67
73
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
+
68
86
private struct InitializerConfiguration : CustomStringConvertible {
69
87
public let help : Bool
70
88
public let verboseLogging : Bool
71
89
public let destinationDir : URL
72
-
90
+ public let templateType : TemplateType
91
+
73
92
public init ( arguments: [ String ] ) throws {
74
93
var argumentExtractor = ArgumentExtractor ( arguments)
75
94
let verboseArgument = argumentExtractor. extractFlag ( named: " verbose " ) > 0
76
95
let helpArgument = argumentExtractor. extractFlag ( named: " help " ) > 0
77
96
let destDirArgument = argumentExtractor. extractOption ( named: " dest-dir " )
78
-
97
+ let templateURLArgument = argumentExtractor. extractFlag ( named: " with-url " ) > 0
98
+
79
99
// help required ?
80
100
self . help = helpArgument
81
-
101
+
82
102
// verbose logging required ?
83
103
self . verboseLogging = verboseArgument
84
104
85
105
// dest dir
86
106
self . destinationDir = URL ( fileURLWithPath: destDirArgument [ 0 ] )
107
+
108
+ // template type. Default is the JSON one
109
+ self . templateType = templateURLArgument ? . url : . default
87
110
}
88
-
111
+
89
112
var description : String {
90
113
"""
91
114
{
92
115
verboseLogging: \( self . verboseLogging)
93
116
destinationDir: \( self . destinationDir)
117
+ templateType: \( self . templateType)
94
118
}
95
119
"""
96
120
}
0 commit comments