Skip to content

Commit f19c834

Browse files
committed
Add one script to run them all
Add the script to the swift-syntax dev utils package to allow developers run all necessary actions before submitting a PR
1 parent aac81f1 commit f19c834

File tree

5 files changed

+77
-10
lines changed

5 files changed

+77
-10
lines changed

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/SwiftSyntaxDevUtils.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct SwiftSyntaxDevUtils: ParsableCommand {
2828
Build.self,
2929
Format.self,
3030
GenerateSourceCode.self,
31+
LocalPrPrecheck.self,
3132
Test.self,
3233
VerifySourceCode.self,
3334
]

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Format.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ struct FormatExecutor {
163163
/// Ensure that we have an up-to-date checkout of swift-format in `.swift-format-build`.
164164
private func cloneOrUpdateSwiftFormat() throws {
165165
if FileManager.default.fileExists(atPath: Paths.swiftFormatBuildDir.appendingPathComponent(".git").path) {
166+
logSection("Updating swift-format")
167+
166168
try runGitCommand("checkout", Self.swiftFormatBranch)
167169
try runGitCommand("pull")
168170
} else {
171+
logSection("Cloning swift-format")
172+
169173
try FileManager.default.createDirectory(atPath: Paths.swiftFormatBuildDir.path, withIntermediateDirectories: true)
170174
try runGitCommand("clone", "https://github.com/apple/swift-format.git", ".")
171175
try runGitCommand("checkout", Self.swiftFormatBranch)
@@ -219,6 +223,8 @@ struct FormatExecutor {
219223

220224
/// Format all files in the repo using the locally-built swift-format.
221225
private func formatFilesInRepo() throws {
226+
logSection("Formatting code with swift-format")
227+
222228
let swiftFormatExecutable = try findSwiftFormatExecutable()
223229

224230
let filesToFormat = self.filesToFormat()
@@ -240,6 +246,8 @@ struct FormatExecutor {
240246

241247
/// Lint all files in the repo using the locally-built swift-format.
242248
private func lintFilesInRepo() throws {
249+
logSection("Running swift-format linter")
250+
243251
let swiftFormatExecutable = try findSwiftFormatExecutable()
244252

245253
let filesToFormat = self.filesToFormat()

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/GenerateSourceCode.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct GenerateSourceCode: ParsableCommand {
3232

3333
struct GenerateSourceCodeExecutor {
3434
/// The path to the toolchain that shall be used to build SwiftSyntax.
35-
private let toolchain: URL
35+
private let toolchain: URL?
3636

3737
/// Enable verbose logging.
3838
private let verbose: Bool
@@ -41,7 +41,7 @@ struct GenerateSourceCodeExecutor {
4141
/// - Parameters:
4242
/// - toolchain: The path to the toolchain that shall be used to build SwiftSyntax.
4343
/// - verbose: Enable verbose logging.
44-
init(toolchain: URL, verbose: Bool = false) {
44+
init(toolchain: URL?, verbose: Bool = false) {
4545
self.toolchain = toolchain
4646
self.verbose = verbose
4747
}
@@ -65,8 +65,8 @@ struct GenerateSourceCodeExecutor {
6565
"SWIFTCI_USE_LOCAL_DEPS": nil,
6666
]
6767

68-
let process = ProcessRunner(
69-
executableURL: toolchain.appendingPathComponent("bin").appendingPathComponent("swift"),
68+
let process = try ProcessRunner(
69+
executableURL: toolchain?.appendingPathComponent("bin").appendingPathComponent("swift") ?? Paths.swiftExec,
7070
arguments: args,
7171
additionalEnvironment: additionalEnvironment
7272
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
16+
struct LocalPrPrecheck: ParsableCommand {
17+
static let configuration = CommandConfiguration(
18+
abstract: """
19+
Ensure changes are fully tested, formatted, and validated before pull request submission.
20+
"""
21+
)
22+
23+
@Flag(help: "Enable verbose logging.")
24+
var verbose: Bool = false
25+
26+
func run() throws {
27+
let executor = LocalPrPrecheckExecutor(verbose: verbose)
28+
try executor.run()
29+
}
30+
}
31+
32+
struct LocalPrPrecheckExecutor {
33+
private let formatExecutor: FormatExecutor
34+
private let generateSourceCodeExecutor: GenerateSourceCodeExecutor
35+
private let buildExecutor: BuildExecutor
36+
private let testExecutor: TestExecutor
37+
38+
init(verbose: Bool = false) {
39+
self.formatExecutor = FormatExecutor(update: false)
40+
self.generateSourceCodeExecutor = GenerateSourceCodeExecutor(toolchain: nil, verbose: verbose)
41+
self.buildExecutor = BuildExecutor(swiftPMBuilder: SwiftPMBuilder(toolchain: nil, useLocalDeps: false))
42+
self.testExecutor = TestExecutor(swiftPMBuilder: SwiftPMBuilder(toolchain: nil, useLocalDeps: false))
43+
}
44+
45+
func run() throws {
46+
try formatExecutor.run()
47+
try generateSourceCodeExecutor.run(sourceDir: Paths.sourcesDir)
48+
try buildExecutor.run()
49+
try testExecutor.run()
50+
}
51+
}

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/SwiftPMBuilder.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Foundation
1515
/// Wrapper around `swift build` and `swift test` to build and test SwiftPM packages.
1616
struct SwiftPMBuilder {
1717
/// The path to the toolchain that shall be used to build SwiftSyntax.
18-
let toolchain: URL
18+
let toolchain: URL?
1919

2020
/// The directory in which build products shall be put.
2121
///
@@ -38,19 +38,23 @@ struct SwiftPMBuilder {
3838
/// no round-trip or assertion failures.
3939
let enableTestFuzzing: Bool
4040

41+
/// A flag indicating whether to use local dependencies during the build process.
42+
let useLocalDeps: Bool
43+
4144
/// Treat all warnings as errors.
4245
let warningsAsErrors: Bool
4346

4447
/// Enable verbose logging.
4548
let verbose: Bool
4649

4750
init(
48-
toolchain: URL,
51+
toolchain: URL?,
4952
buildDir: URL? = nil,
5053
multirootDataFile: URL? = nil,
5154
release: Bool = false,
5255
enableRawSyntaxValidation: Bool = false,
5356
enableTestFuzzing: Bool = false,
57+
useLocalDeps: Bool = true,
5458
warningsAsErrors: Bool = false,
5559
verbose: Bool = false
5660
) {
@@ -60,6 +64,7 @@ struct SwiftPMBuilder {
6064
self.release = release
6165
self.enableRawSyntaxValidation = enableRawSyntaxValidation
6266
self.enableTestFuzzing = enableTestFuzzing
67+
self.useLocalDeps = useLocalDeps
6368
self.warningsAsErrors = warningsAsErrors
6469
self.verbose = verbose
6570
}
@@ -110,8 +115,8 @@ struct SwiftPMBuilder {
110115

111116
args += additionalArguments
112117

113-
let processRunner = ProcessRunner(
114-
executableURL: toolchain.appendingPathComponent("bin").appendingPathComponent("swift"),
118+
let processRunner = try ProcessRunner(
119+
executableURL: toolchain?.appendingPathComponent("bin").appendingPathComponent("swift") ?? Paths.swiftExec,
115120
arguments: args,
116121
additionalEnvironment: additionalEnvironment
117122
)
@@ -139,8 +144,10 @@ struct SwiftPMBuilder {
139144
additionalEnvironment["SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION"] = "1"
140145
}
141146

142-
// Tell other projects in the unified build to use local dependencies
143-
additionalEnvironment["SWIFTCI_USE_LOCAL_DEPS"] = "1"
147+
if useLocalDeps {
148+
// Tell other projects in the unified build to use local dependencies
149+
additionalEnvironment["SWIFTCI_USE_LOCAL_DEPS"] = "1"
150+
}
144151

145152
return additionalEnvironment
146153
}

0 commit comments

Comments
 (0)