Skip to content

Commit ba38037

Browse files
committed
use opaque type for Data and String return type
1 parent c7a2617 commit ba38037

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

Sources/SourceKitBazelBSP/RequestHandlers/InitializeHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ final class InitializeHandler {
8080
let rootUri = request.rootUri.arbitrarySchemeURL.path
8181
logger.debug("rootUri: \(rootUri)")
8282
let regularOutputBase = URL(
83-
fileURLWithPath: try commandRunner.bazel(baseConfig: baseConfig, rootUri: rootUri, cmd: "info output_base")
83+
fileURLWithPath: try commandRunner.bazel(baseConfig: baseConfig, rootUri: rootUri, cmd: "info output_base").asString
8484
)
8585
logger.debug("regularOutputBase: \(regularOutputBase)")
8686

@@ -97,7 +97,7 @@ final class InitializeHandler {
9797
outputBase: outputBase,
9898
cmd: "info output_path",
9999
rootUri: rootUri
100-
)
100+
).asString
101101
logger.debug("outputPath: \(outputPath)")
102102

103103
// Collecting the rest of the env's details

Sources/SourceKitBazelBSP/RequestHandlers/SKOptions/BazelTargetAquerier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final class BazelTargetAquerier {
6767
}
6868

6969
// Run the aquery on the special index output base since that's where we will build at.
70-
let output = try commandRunner.bazelActionQuery(initializedConfig: config, cmd: cmd)
70+
let output = try commandRunner.bazelIndexAction(initializedConfig: config, cmd: cmd).asData
7171

7272
let parsedOutput = try BazelProtobufBindings.parseActionGraph(data: output)
7373

Sources/SourceKitBazelBSP/SharedUtils/Shell/CommandRunner.swift

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ extension CommandRunner {
3838
// MARK: Bazel-related helpers
3939

4040
extension CommandRunner {
41-
func bazel(baseConfig: BaseServerConfig, rootUri: String, cmd: String) throws -> String {
41+
func bazel(baseConfig: BaseServerConfig, rootUri: String, cmd: String) throws -> some ContentRepresentable {
4242
try run(baseConfig.bazelWrapper + " " + cmd, cwd: rootUri)
4343
}
4444

4545
/// A regular bazel command, but at this BSP's special output base and taking into account the special index flags.
46-
func bazelIndexAction(initializedConfig: InitializedServerConfig, cmd: String) throws -> String {
46+
func bazelIndexAction(initializedConfig: InitializedServerConfig, cmd: String) throws -> some ContentRepresentable {
4747
return try bazelIndexAction(
4848
baseConfig: initializedConfig.baseConfig,
4949
outputBase: initializedConfig.outputBase,
@@ -52,25 +52,13 @@ extension CommandRunner {
5252
)
5353
}
5454

55-
/// A bazel action query command, but at this BSP's special output base and taking into account the special index flags.
56-
func bazelActionQuery(initializedConfig: InitializedServerConfig, cmd: String) throws -> Data {
57-
let baseCommand = [
58-
initializedConfig.baseConfig.bazelWrapper,
59-
"--output_base=\(initializedConfig.outputBase)",
60-
cmd,
61-
]
62-
let additionalFlags = initializedConfig.baseConfig.indexFlags
63-
let command = (baseCommand + additionalFlags).joined(separator: " ")
64-
return try run(command, cwd: initializedConfig.rootUri)
65-
}
66-
6755
/// A regular bazel command, but at this BSP's special output base and taking into account the special index flags.
6856
func bazelIndexAction(
6957
baseConfig: BaseServerConfig,
7058
outputBase: String,
7159
cmd: String,
7260
rootUri: String,
73-
) throws -> String {
61+
) throws -> some ContentRepresentable {
7462
let indexFlags = baseConfig.indexFlags
7563
let additionalFlags: String
7664
if indexFlags.isEmpty {
@@ -82,3 +70,30 @@ extension CommandRunner {
8270
return try bazel(baseConfig: baseConfig, rootUri: rootUri, cmd: cmd)
8371
}
8472
}
73+
74+
// Define a protocol that both String and Data types can conform to
75+
protocol ContentRepresentable {
76+
var asData: Data { get }
77+
var asString: String { get }
78+
}
79+
80+
extension String: ContentRepresentable {
81+
var asData: Data {
82+
Data(self.utf8)
83+
}
84+
85+
var asString: String {
86+
return self
87+
}
88+
}
89+
90+
extension Data: ContentRepresentable {
91+
var asData: Data {
92+
self
93+
}
94+
95+
var asString: String {
96+
let str = String(data: self, encoding: .utf8) ?? ""
97+
return str.trimmingCharacters(in: .whitespacesAndNewlines)
98+
}
99+
}

0 commit comments

Comments
 (0)