Skip to content

Commit 86c555c

Browse files
committed
Share gnerate command between generate-swiftbasicformat and generate-swiftsyntaxbuilder
1 parent 32e8af4 commit 86c555c

File tree

3 files changed

+66
-70
lines changed

3 files changed

+66
-70
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
import SwiftSyntax
16+
import SwiftSyntaxBuilder
17+
18+
open class GenerateCommand: ParsableCommand {
19+
@Argument(help: "The path to the destination directory where the source files are to be generated")
20+
var generatedPath: String
21+
22+
@Flag(help: "Enable verbose output")
23+
var verbose: Bool = false
24+
25+
open var sourceTemplates: [(SourceFile, String)] {
26+
fatalError("Must be overridden by subclasses")
27+
}
28+
29+
public required init() {}
30+
31+
public func run() throws {
32+
let generatedURL = URL(fileURLWithPath: generatedPath)
33+
let format = Format(indentWidth: 2)
34+
35+
try FileManager.default.createDirectory(
36+
atPath: generatedURL.path,
37+
withIntermediateDirectories: true,
38+
attributes: nil
39+
)
40+
41+
for (sourceFile, name) in sourceTemplates {
42+
let fileURL = generatedURL.appendingPathComponent(name)
43+
if verbose {
44+
print("Generating \(fileURL.path)...")
45+
}
46+
let syntax = sourceFile.buildSyntax(format: format)
47+
try "\(syntax)\n".write(to: fileURL, atomically: true, encoding: .utf8)
48+
}
49+
}
50+
}

Code-Generation/Sources/generate-swiftbasicformat/GenerateSwiftBasicFormat.swift

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import ArgumentParser
14-
import Foundation
15-
import SwiftSyntax
13+
import Utils
1614
import SwiftSyntaxBuilder
1715

18-
/// SwiftSyntaxBuilder sources to be generated
19-
private let sourceTemplates: [(SourceFile, String)] = [
20-
(formatFile, "Format.swift"),
21-
]
22-
2316
@main
24-
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
25-
@Argument(help: "The path to the destination directory where the source files are to be generated")
26-
var generatedPath: String
27-
28-
@Flag(help: "Enable verbose output")
29-
var verbose: Bool = false
30-
31-
func run() throws {
32-
let generatedURL = URL(fileURLWithPath: generatedPath)
33-
let format = Format(indentWidth: 2)
34-
35-
try FileManager.default.createDirectory(
36-
atPath: generatedURL.path,
37-
withIntermediateDirectories: true,
38-
attributes: nil
39-
)
40-
41-
for (sourceFile, name) in sourceTemplates {
42-
let fileURL = generatedURL.appendingPathComponent(name)
43-
if verbose {
44-
print("Generating \(fileURL.path)...")
45-
}
46-
let syntax = sourceFile.buildSyntax(format: format)
47-
try "\(syntax)\n".write(to: fileURL, atomically: true, encoding: .utf8)
48-
}
17+
class GenerateSwiftStaticFormat: GenerateCommand {
18+
open override var sourceTemplates: [(SourceFile, String)] {
19+
[
20+
(formatFile, "Format.swift"),
21+
]
4922
}
5023
}

Code-Generation/Sources/generate-swiftsyntaxbuilder/GenerateSwiftSyntaxBuilder.swift

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,18 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import ArgumentParser
14-
import Foundation
15-
import SwiftSyntax
13+
import Utils
1614
import SwiftSyntaxBuilder
1715

18-
/// SwiftSyntaxBuilder sources to be generated
19-
private let sourceTemplates: [(SourceFile, String)] = [
20-
(buildableBaseProtocolsFile, "BuildableBaseProtocols.swift"),
21-
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
22-
(buildableNodesFile, "BuildableNodes.swift"),
23-
(expressibleAsProtocolsFile, "ExpressibleAsProtocols.swift"),
24-
(tokenFile, "Token.swift"),
25-
]
26-
2716
@main
28-
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
29-
@Argument(help: "The path to the destination directory where the source files are to be generated")
30-
var generatedPath: String
31-
32-
@Flag(help: "Enable verbose output")
33-
var verbose: Bool = false
34-
35-
func run() throws {
36-
let generatedURL = URL(fileURLWithPath: generatedPath)
37-
let format = Format(indentWidth: 2)
38-
39-
try FileManager.default.createDirectory(
40-
atPath: generatedURL.path,
41-
withIntermediateDirectories: true,
42-
attributes: nil
43-
)
44-
45-
for (sourceFile, name) in sourceTemplates {
46-
let fileURL = generatedURL.appendingPathComponent(name)
47-
if verbose {
48-
print("Generating \(fileURL.path)...")
49-
}
50-
let syntax = sourceFile.buildSyntax(format: format)
51-
try "\(syntax)\n".write(to: fileURL, atomically: true, encoding: .utf8)
52-
}
17+
class GenerateSwiftSyntaxBuilder: GenerateCommand {
18+
open override var sourceTemplates: [(SourceFile, String)] {
19+
[
20+
(buildableBaseProtocolsFile, "BuildableBaseProtocols.swift"),
21+
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
22+
(buildableNodesFile, "BuildableNodes.swift"),
23+
(expressibleAsProtocolsFile, "ExpressibleAsProtocols.swift"),
24+
(tokenFile, "Token.swift"),
25+
]
5326
}
5427
}

0 commit comments

Comments
 (0)