10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import ArgumentParser
13
14
import Foundation
14
15
import SwiftSyntax
15
16
import SwiftSyntaxBuilder
16
17
17
18
/// SwiftSyntaxBuilder sources to be generated
18
- let sourceTemplates = [
19
+ private let sourceTemplates : [ ( SourceFile , String ) ] = [
19
20
( buildableBaseProtocolsFile, " BuildableBaseProtocols.swift " ) ,
20
21
( buildableCollectionNodesFile, " BuildableCollectionNodes.swift " ) ,
21
22
( buildableNodesFile, " BuildableNodes.swift " ) ,
@@ -24,24 +25,31 @@ let sourceTemplates = [
24
25
( tokenFile, " Token.swift " ) ,
25
26
]
26
27
27
- guard CommandLine . arguments. count > 1 else {
28
- fatalError ( " Please add a destination as the first argument " )
28
+ @main
29
+ struct GenerateSwiftSyntaxBuilder : ParsableCommand {
30
+ @Argument ( help: " The path to the destination directory where the source files are to be generated " )
31
+ var generatedPath : String
32
+
33
+ @Flag ( help: " Enable verbose output " )
34
+ var verbose : Bool = false
35
+
36
+ func run( ) throws {
37
+ let generatedURL = URL ( fileURLWithPath: generatedPath)
38
+ let format = Format ( indentWidth: 2 )
39
+
40
+ try FileManager . default. createDirectory (
41
+ atPath: generatedURL. path,
42
+ withIntermediateDirectories: true ,
43
+ attributes: nil
44
+ )
45
+
46
+ for (sourceFile, name) in sourceTemplates {
47
+ let fileURL = generatedURL. appendingPathComponent ( name)
48
+ if verbose {
49
+ print ( " Generating \( fileURL. path) ... " )
50
+ }
51
+ let syntax = sourceFile. buildSyntax ( format: format)
52
+ try " \( syntax) \n " . write ( to: fileURL, atomically: true , encoding: . utf8)
53
+ }
54
+ }
29
55
}
30
-
31
- let destination = CommandLine . arguments [ 1 ]
32
- let generatedPath = URL ( fileURLWithPath: destination)
33
-
34
- try FileManager . default. createDirectory (
35
- atPath: generatedPath. path,
36
- withIntermediateDirectories: true ,
37
- attributes: nil
38
- )
39
-
40
- let format = Format ( indentWidth: 2 )
41
-
42
- for (sourceFile, name) in sourceTemplates {
43
- let filePath = generatedPath. appendingPathComponent ( name)
44
- let syntax = sourceFile. buildSyntax ( format: format)
45
- try " \( syntax) \n " . write ( to: filePath, atomically: true , encoding: . utf8)
46
- }
47
-
0 commit comments