Skip to content

Commit 8f49297

Browse files
authored
Merge pull request #174 from ahoppen/pr/package-config
Add support for a Package-config.json to specify defaults for environment variables
2 parents d2f9fdf + 52ad33e commit 8f49297

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

SourceKitStressTester/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/DerivedData
77
/Package.resolved
88
/.swiftpm
9+
/Package-config.json

SourceKitStressTester/Package.swift

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
// swift-tools-version:5.3
22

33
import PackageDescription
4-
#if os(Linux)
5-
import Glibc
6-
#else
7-
import Darwin.C
8-
#endif
9-
10-
func readEnv(_ name: String) -> String? {
11-
if let pointer = getenv(name) {
12-
return String(cString: pointer)
13-
} else {
4+
import Foundation
5+
6+
/// Return the value of the configuration parameter with the given `name` either
7+
/// from the environment variables or, if that doesn't exist, from a
8+
/// `Package-config.json` file located next to `Package.swift`, like the following:
9+
/// ```
10+
/// {
11+
/// "SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH": "/path/to/lib/with/sourcekitd.framework",
12+
/// "SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH": "/path/to/folder/with/SwiftSyntax.framework"
13+
/// }
14+
/// ```
15+
/// If none of these exist, return `nil`.
16+
func getConfigParam(_ name: String) -> String? {
17+
if let envValue = ProcessInfo.processInfo.environment[name] {
18+
return envValue
19+
}
20+
21+
let configFile = URL(fileURLWithPath: #filePath)
22+
.deletingLastPathComponent()
23+
.appendingPathComponent("Package-config.json")
24+
25+
let configData: Data
26+
do {
27+
configData = try Data(contentsOf: configFile)
28+
} catch {
29+
// Config file not found. That's fine. Return `nil` without complaining.
30+
return nil
31+
}
32+
do {
33+
let config = try JSONDecoder().decode([String: String].self, from: configData)
34+
return config[name]
35+
} catch {
36+
// We couldn't parse the Package-config.json, probably malformatted JSON.
37+
// Print the error, which shows up as a warning in Xcode.
38+
print("Loading Package-config.json failed with error: \(error)")
1439
return nil
1540
}
1641
}
@@ -24,15 +49,15 @@ func readEnv(_ name: String) -> String? {
2449
/// `SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH` enviornment variable in the
2550
/// unified build.
2651
/// The environment variable is only specified once we build the stress tester.
27-
let sourceKitSearchPath: String? = readEnv("SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH")
52+
let sourceKitSearchPath: String? = getConfigParam("SWIFT_STRESS_TESTER_SOURCEKIT_SEARCHPATH")
2853

2954
/// Path to a directory containing SwiftSyntax.framework and SwiftSyntaxParser.framework.
3055
/// Optional. If not specified, SwiftSyntax will be built from source.
31-
let swiftSyntaxSearchPath: String? = readEnv("SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH")
56+
let swiftSyntaxSearchPath: String? = getConfigParam("SWIFT_STRESS_TESTER_SWIFTSYNTAX_SEARCHPATH")
3257

3358
/// If specified expect swift-tools-support-core, swift-argument-parser and swift-syntax
3459
/// to be checked out next to swift-stresss-tester.
35-
let useLocalDependencies = readEnv("SWIFTCI_USE_LOCAL_DEPS") != nil
60+
let useLocalDependencies = getConfigParam("SWIFTCI_USE_LOCAL_DEPS") != nil
3661

3762
// MARK: - Conditional build settings
3863

0 commit comments

Comments
 (0)