Skip to content

Commit 9b32f69

Browse files
committed
Convert calculate-output-groups to Swift
Signed-off-by: Matt Pennig <mpennig@slack-corp.com>
1 parent 79eedcb commit 9b32f69

File tree

18 files changed

+81
-488
lines changed

18 files changed

+81
-488
lines changed

distribution/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pkg_tar(
5959
remap_paths = dicts.add(
6060
{
6161
"MODULE.release.bazel": "MODULE.bazel",
62+
"tools/calculate_output_groups/BUILD.release.bazel": (
63+
"tools/calculate_output_groups/BUILD"
64+
),
6265
"tools/import_indexstores/BUILD.release.bazel": (
6366
"tools/import_indexstores/BUILD"
6467
),

examples/integration/test/fixtures/bwb.xcodeproj/project.pbxproj

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/integration/test/fixtures/bwx.xcodeproj/project.pbxproj

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rules_ios/test/fixtures/bwb.xcodeproj/project.pbxproj

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/calculate_output_groups/CalculateOutputGroups.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ struct CalculateOutputGroups: AsyncParsableCommand {
1414
@OptionGroup var arguments: OutputGroupsCalculator.Arguments
1515

1616
func run() async throws {
17+
var output = StdoutOutputStream()
1718
let logger = DefaultLogger(
1819
standardError: StderrOutputStream(),
19-
standardOutput: StdoutOutputStream(),
20+
standardOutput: output,
2021
colorize: colorDiagnostics
2122
)
2223

23-
let calculator = OutputGroupsCalculator()
24+
let calculator = OutputGroupsCalculator(logger: logger)
2425

2526
do {
26-
try await calculator.calculateOutputGroups(arguments: arguments)
27+
let groups = try await calculator.calculateOutputGroups(arguments: arguments)
28+
print(groups, to: &output)
2729
} catch {
2830
logger.logError(error.localizedDescription)
2931
Darwin.exit(1)

tools/calculate_output_groups/Errors.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ToolCommon
33
extension UsageError {
44
static func buildMarker(_ path: String) -> Self {
55
.init(message: """
6-
error: Build marker (\(path)) doesn't exist. If you manually cleared Derived \
6+
Build marker (\(path)) doesn't exist. If you manually cleared Derived \
77
Data, you need to close and re-open the project for the file to be created \
88
again. Using the "Clean Build Folder" command instead (⇧ ⌘ K) won't trigger \
99
this error. If this error still happens after re-opening the project, please \
@@ -14,7 +14,7 @@ https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bu
1414

1515
static func pifCache(_ path: String) -> Self {
1616
.init(message: """
17-
error: PIFCache (\(path)) doesn't exist. If you manually cleared Derived \
17+
PIFCache (\(path)) doesn't exist. If you manually cleared Derived \
1818
Data, you need to close and re-open the project for the PIFCache to be created \
1919
again. Using the "Clean Build Folder" command instead (⇧ ⌘ K) won't trigger \
2020
this error. If this error still happens after re-opening the project, please \
@@ -25,7 +25,7 @@ https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bu
2525

2626
static func buildRequest(_ path: String) -> Self {
2727
.init(message: """
28-
error: Couldn't find a build-request.json file inside \(path)". Please file a bug \
28+
Couldn't find latest build-request.json file after 30 seconds. Please file a bug \
2929
report here: https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bug.md
3030
""")
3131
}

tools/calculate_output_groups/OutputGroupsCalculator.swift

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import ToolCommon
33
import ZippyJSON
44

55
struct OutputGroupsCalculator {
6-
func calculateOutputGroups(arguments: Arguments) async throws {
6+
let logger: Logger
7+
8+
func calculateOutputGroups(arguments: Arguments) async throws -> String {
79
let pifCache = arguments.baseObjRoot.appendingPathComponent("XCBuildData/PIFCache")
810
let projectCache = pifCache.appendingPathComponent("project")
911
let targetCache = pifCache.appendingPathComponent("target")
@@ -19,7 +21,6 @@ struct OutputGroupsCalculator {
1921
else {
2022
throw UsageError.pifCache(pifCache.path)
2123
}
22-
2324
async let buildRequest = loadBuildRequestFile(
2425
inPath: arguments.baseObjRoot.appendingPathComponent("XCBuildData"),
2526
since: markerDate
@@ -30,46 +31,65 @@ struct OutputGroupsCalculator {
3031
targetCache: targetCache
3132
)
3233

33-
let output = try await outputGroups(
34+
return try await outputGroups(
3435
buildRequest: buildRequest,
3536
targets: targetMap,
3637
prefixes: arguments.outputGroupPrefixes
3738
)
38-
print(output)
3939
}
4040

4141
private func loadBuildRequestFile(inPath path: URL, since: Date) async throws -> BuildRequest {
4242
@Sendable func findBuildRequestURL() -> URL? {
43-
path.newestDescendent(recursive: true, matching: { url in
43+
guard let xcbuilddata = path.newestDescendent(matching: { url in
4444
guard
45-
url.path.hasSuffix(".xcbuilddata/build-request.json"),
45+
url.path.hasSuffix(".xcbuilddata"),
4646
let date = url.modificationDate
4747
else { return false }
4848
return date >= since
49-
})
49+
}) else {
50+
return nil
51+
}
52+
53+
let buildRequest = xcbuilddata.appendingPathComponent("build-request.json")
54+
if FileManager.default.fileExists(atPath: buildRequest.path) {
55+
return buildRequest
56+
} else {
57+
return nil
58+
}
5059
}
5160

5261
if let url = findBuildRequestURL() {
5362
return try url.decode(BuildRequest.self)
5463
}
5564

5665
// If the file was not immediately found, kick off a process to wait for the file to be created (or time out).
57-
let findTask = Task {
58-
while true {
59-
try Task.checkCancellation()
60-
try await Task.sleep(for: .seconds(1))
61-
if let buildRequestURL = findBuildRequestURL() {
62-
return buildRequestURL
66+
do {
67+
let findTask = Task {
68+
logger.logWarning("The latest build-request.json file has not been updated yet. Waiting…")
69+
while true {
70+
try Task.checkCancellation()
71+
try await Task.sleep(for: .seconds(1))
72+
if let buildRequestURL = findBuildRequestURL() {
73+
return buildRequestURL
74+
}
6375
}
6476
}
65-
}
66-
let timeoutTask = Task {
67-
try await Task.sleep(for: .seconds(10))
68-
findTask.cancel()
69-
}
77+
let waitingTask = Task {
78+
try await Task.sleep(for: .seconds(10))
79+
try Task.checkCancellation()
80+
logger.logWarning("""
81+
The latest build-request.json file has still not been updated after 10 seconds. If this happens frequently, please file a bug report here:
82+
https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bug.md
83+
""")
84+
}
85+
let timeoutTask = Task {
86+
try await Task.sleep(for: .seconds(30))
87+
guard !Task.isCancelled else { return }
88+
findTask.cancel()
89+
}
7090

71-
do {
7291
let result = try await findTask.value
92+
waitingTask.cancel()
7393
timeoutTask.cancel()
7494
return try result.decode(BuildRequest.self)
7595
} catch {

tools/generators/legacy/src/Generator/AddBazelDependenciesTarget.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ extension Generator {
3535

3636
var buildSettings: BuildSettings = [
3737
"BAZEL_PACKAGE_BIN_DIR": "rules_xcodeproj",
38-
"CALCULATE_OUTPUT_GROUPS_SCRIPT": """
39-
$(BAZEL_INTEGRATION_DIR)/calculate_output_groups.py
40-
""",
4138
"INDEXING_SUPPORTED_PLATFORMS__": """
4239
$(INDEXING_SUPPORTED_PLATFORMS__NO)
4340
""",

tools/generators/legacy/src/Generator/SetTargetConfigurations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ $(BAZEL_OUT)\#(linkParams.path.string.dropFirst(9))
283283
buildSettings.set("TARGET_NAME", to: target.name)
284284

285285
if !target.product.isResourceBundle {
286-
// This is used in `calculate_output_groups.py`. We only want to set
286+
// This is used in `calculate_output_groups`. We only want to set
287287
// it on buildable targets
288288
buildSettings.set("BAZEL_LABEL", to: target.label.description)
289289
}

tools/generators/pbxnativetargets/src/Generator/CalculateSharedBuildSettings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extension Generator.CalculateSharedBuildSettings {
114114
}
115115

116116
if productType != .resourceBundle {
117-
// This is used in `calculate_output_groups.py`. We only want to set
117+
// This is used in `calculate_output_groups`. We only want to set
118118
// it on buildable targets.
119119
buildSettings.append(
120120
.init(

0 commit comments

Comments
 (0)