-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmain.swift
116 lines (105 loc) · 3.62 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
import SwiftSyntax
import SwiftSyntaxBuilder
// FIXME: Instead of depending on Foundation, adopt API provided by SwiftPM in rdar://111523616
// Note that SwiftPM already depends on Foundation, and this dependency does not
// introduce a dependency in the testing library, only in this helper tool.
import Foundation
// Resolve arguments to the tool.
let repoPath = CommandLine.arguments[1]
let generatedSourceURL = URL(fileURLWithPath: CommandLine.arguments[2], isDirectory: false)
/// Run the `git` tool and process the output it writes to standard output.
///
/// - Parameters:
/// - arguments: The arguments to pass to `git`.
/// - maxOutputCount: The maximum amount of output to read and return.
///
/// - Returns: A string containing the `git` command's output, up to
/// `maxOutputCount` UTF-8-encoded bytes, or `nil` if the command failed or
/// the output could not be read.
func _runGit(passing arguments: String..., readingUpToCount maxOutputCount: Int) -> String? {
#if os(macOS) || os(Linux) || os(Windows)
let path: String
var arguments = ["-C", repoPath] + arguments
#if os(Windows)
path = "C:\\Program Files\\Git\\cmd\\git.exe"
#else
path = "/usr/bin/env"
arguments = CollectionOfOne("git") + arguments
#endif
let process = Process()
process.executableURL = URL(fileURLWithPath: path, isDirectory: false)
process.arguments = arguments
let stdoutPipe = Pipe()
process.standardOutput = stdoutPipe
process.standardError = nil
do {
try process.run()
} catch {
return nil
}
defer {
process.terminate()
}
guard let output = try? stdoutPipe.fileHandleForReading.read(upToCount: maxOutputCount) else {
return nil
}
return String(data: output, encoding: .utf8)
#else
return nil
#endif
}
// The current Git tag, if available.
let currentGitTag = _runGit(passing: "describe", "--exact-match", "--tags", readingUpToCount: 40)?
.split(whereSeparator: \.isNewline)
.first
.map(String.init)
// The current Git commit hash, if available.
let currentGitCommitHash = _runGit(passing: "rev-parse", "HEAD", readingUpToCount: 40)?
.split(whereSeparator: \.isNewline)
.first
.map(String.init)
// Whether or not the Git repository has uncommitted changes, if available.
let gitHasUncommittedChanges = _runGit(passing: "status", "-s", readingUpToCount: 1)
.map { !$0.isEmpty } ?? false
// Figure out what value to emit for the testing library version. If the
// repository is sitting at a tag with no uncommitted changes, use the tag.
// Otherwise, use the commit hash (with a "there are changes" marker if needed.)
// Finally, fall back to nil if nothing else is available.
let sourceCode: DeclSyntax = if !gitHasUncommittedChanges, let currentGitTag {
"""
var _testingLibraryVersion: String? {
\(literal: currentGitTag)
}
"""
} else if let currentGitCommitHash {
if gitHasUncommittedChanges {
"""
var _testingLibraryVersion: String? {
\(literal: currentGitCommitHash) + " (modified)"
}
"""
} else {
"""
var _testingLibraryVersion: String? {
\(literal: currentGitCommitHash)
}
"""
}
} else {
"""
var _testingLibraryVersion: String? {
nil
}
"""
}
// Write the generated Swift file to the specified destination path.
try String(describing: sourceCode).write(to: generatedSourceURL, atomically: false, encoding: .utf8)