Skip to content

Commit 639f9d1

Browse files
authored
Fix some stream operator deprecation warnings (#6487)
Addresses some of the deprecations introduced in swiftlang/swift-tools-support-core#413 Also using this as an opportunity to use string interpolation and raw string literals to make this more readable.
1 parent a0c9812 commit 639f9d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1226
-930
lines changed

Sources/Basics/FileSystem/FileSystem+Extensions.swift

-4
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,6 @@ extension FileSystem {
302302
public func writeFileContents(_ path: AbsolutePath, string: String) throws {
303303
return try self.writeFileContents(path, bytes: .init(encodingAsUTF8: string))
304304
}
305-
306-
public func writeFileContents(_ path: AbsolutePath, provider: () -> String) throws {
307-
return try self.writeFileContents(path, body: { stream in stream <<< provider() })
308-
}
309305
}
310306

311307
extension FileSystem {

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

+26-22
Original file line numberDiff line numberDiff line change
@@ -310,20 +310,22 @@ public final class ClangTargetBuildDescription {
310310
let bundleBasename = bundlePath.basename
311311

312312
let implFileStream = BufferedOutputByteStream()
313-
implFileStream <<< """
314-
#import <Foundation/Foundation.h>
313+
implFileStream.send(
314+
"""
315+
#import <Foundation/Foundation.h>
315316
316-
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE() {
317-
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"\(bundleBasename)"];
317+
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE() {
318+
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"\(bundleBasename)"];
318319
319-
NSBundle *preferredBundle = [NSBundle bundleWithURL:bundleURL];
320-
if (preferredBundle == nil) {
321-
return [NSBundle bundleWithPath:@"\(bundlePath.pathString)"];
322-
}
320+
NSBundle *preferredBundle = [NSBundle bundleWithURL:bundleURL];
321+
if (preferredBundle == nil) {
322+
return [NSBundle bundleWithPath:@"\(bundlePath.pathString)"];
323+
}
323324
324-
return preferredBundle;
325-
}
326-
"""
325+
return preferredBundle;
326+
}
327+
"""
328+
)
327329

328330
let implFileSubpath = try RelativePath(validating: "resource_bundle_accessor.m")
329331

@@ -338,21 +340,23 @@ public final class ClangTargetBuildDescription {
338340
)
339341

340342
let headerFileStream = BufferedOutputByteStream()
341-
headerFileStream <<< """
342-
#import <Foundation/Foundation.h>
343+
headerFileStream.send(
344+
"""
345+
#import <Foundation/Foundation.h>
343346
344-
#if __cplusplus
345-
extern "C" {
346-
#endif
347+
#if __cplusplus
348+
extern "C" {
349+
#endif
347350
348-
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE(void);
351+
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE(void);
349352
350-
#define SWIFTPM_MODULE_BUNDLE \(target.c99name)_SWIFTPM_MODULE_BUNDLE()
353+
#define SWIFTPM_MODULE_BUNDLE \(target.c99name)_SWIFTPM_MODULE_BUNDLE()
351354
352-
#if __cplusplus
353-
}
354-
#endif
355-
"""
355+
#if __cplusplus
356+
}
357+
#endif
358+
"""
359+
)
356360
let headerFile = derivedSources.root.appending("resource_bundle_accessor.h")
357361
self.resourceAccessorHeaderFile = headerFile
358362

Sources/Build/BuildDescription/ProductBuildDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
331331
let stream = BufferedOutputByteStream()
332332

333333
for object in self.objects {
334-
stream <<< object.pathString.spm_shellEscaped() <<< "\n"
334+
stream.send("\(object.pathString.spm_shellEscaped())\n")
335335
}
336336

337337
try fs.createDirectory(self.linkFileListPath.parentDirectory, recursive: true)

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

+86-44
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,23 @@ public final class SwiftTargetBuildDescription {
309309
guard needsResourceEmbedding else { return }
310310

311311
let stream = BufferedOutputByteStream()
312-
stream <<< """
313-
struct PackageResources {
312+
stream.send(
313+
"""
314+
struct PackageResources {
314315
315-
"""
316+
"""
317+
)
316318

317319
try resources.forEach {
318320
guard $0.rule == .embedInCode else { return }
319321

320322
let variableName = $0.path.basename.spm_mangledToC99ExtendedIdentifier()
321323
let fileContent = try Data(contentsOf: URL(fileURLWithPath: $0.path.pathString)).map { String($0) }.joined(separator: ",")
322324

323-
stream <<< "static let \(variableName): [UInt8] = [\(fileContent)]\n"
325+
stream.send("static let \(variableName): [UInt8] = [\(fileContent)]\n")
324326
}
325327

326-
stream <<< """
327-
}
328-
"""
328+
stream.send("}")
329329

330330
let subpath = try RelativePath(validating: "embedded_resources.swift")
331331
self.derivedSources.relativePaths.append(subpath)
@@ -355,24 +355,26 @@ public final class SwiftTargetBuildDescription {
355355
}
356356

357357
let stream = BufferedOutputByteStream()
358-
stream <<< """
359-
\(self.toolsVersion < .vNext ? "import" : "@_implementationOnly import") class Foundation.Bundle
358+
stream.send(
359+
"""
360+
\(self.toolsVersion < .vNext ? "import" : "@_implementationOnly import") class Foundation.Bundle
360361
361-
extension Foundation.Bundle {
362-
static let module: Bundle = {
363-
let mainPath = \(mainPathSubstitution)
364-
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"
362+
extension Foundation.Bundle {
363+
static let module: Bundle = {
364+
let mainPath = \(mainPathSubstitution)
365+
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"
365366
366-
let preferredBundle = Bundle(path: mainPath)
367+
let preferredBundle = Bundle(path: mainPath)
367368
368-
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
369-
fatalError("could not load resource bundle: from \\(mainPath) or \\(buildPath)")
370-
}
369+
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
370+
fatalError("could not load resource bundle: from \\(mainPath) or \\(buildPath)")
371+
}
371372
372-
return bundle
373-
}()
374-
}
375-
"""
373+
return bundle
374+
}()
375+
}
376+
"""
377+
)
376378

377379
let subpath = try RelativePath(validating: "resource_bundle_accessor.swift")
378380

@@ -671,21 +673,41 @@ public final class SwiftTargetBuildDescription {
671673
let path = self.tempsPath.appending("output-file-map.json")
672674
let stream = BufferedOutputByteStream()
673675

674-
stream <<< "{\n"
675-
676676
let masterDepsPath = self.tempsPath.appending("master.swiftdeps")
677-
stream <<< " \"\": {\n"
677+
stream.send(
678+
#"""
679+
{
680+
"": {
681+
682+
"""#
683+
)
678684
if self.buildParameters.useWholeModuleOptimization {
679685
let moduleName = self.target.c99name
680-
stream <<< " \"dependencies\": \"" <<< self.tempsPath.appending(component: moduleName + ".d")
681-
.nativePathString(escaped: true) <<< "\",\n"
686+
stream.send(
687+
#"""
688+
"dependencies": "\#(
689+
self.tempsPath.appending(component: moduleName + ".d").nativePathString(escaped: true)
690+
)",
691+
692+
"""#
693+
)
682694
// FIXME: Need to record this deps file for processing it later.
683-
stream <<< " \"object\": \"" <<< self.tempsPath.appending(component: moduleName + ".o")
684-
.nativePathString(escaped: true) <<< "\",\n"
695+
stream.send(
696+
#"""
697+
"object": "\#(
698+
self.tempsPath.appending(component: moduleName + ".o").nativePathString(escaped: true)
699+
)",
700+
701+
"""#
702+
)
685703
}
686-
stream <<< " \"swift-dependencies\": \"" <<< masterDepsPath.nativePathString(escaped: true) <<< "\"\n"
704+
stream.send(
705+
#"""
706+
"swift-dependencies": "\#(masterDepsPath.nativePathString(escaped: true))"
707+
},
687708
688-
stream <<< " },\n"
709+
"""#
710+
)
689711

690712
// Write out the entries for each source file.
691713
let sources = self.target.sources.paths + self.derivedSources.paths + self.pluginDerivedSources.paths
@@ -697,23 +719,39 @@ public final class SwiftTargetBuildDescription {
697719

698720
let swiftDepsPath = objectDir.appending(component: sourceFileName + ".swiftdeps")
699721

700-
stream <<< " \"" <<< source.nativePathString(escaped: true) <<< "\": {\n"
722+
stream.send(
723+
#"""
724+
"\#(source.nativePathString(escaped: true))": {
725+
726+
"""#
727+
)
701728

702729
if !self.buildParameters.useWholeModuleOptimization {
703730
let depsPath = objectDir.appending(component: sourceFileName + ".d")
704-
stream <<< " \"dependencies\": \"" <<< depsPath.nativePathString(escaped: true) <<< "\",\n"
731+
stream.send(
732+
#"""
733+
"dependencies": "\#(depsPath.nativePathString(escaped: true))",
734+
735+
"""#
736+
)
705737
// FIXME: Need to record this deps file for processing it later.
706738
}
707739

708-
stream <<< " \"object\": \"" <<< object.nativePathString(escaped: true) <<< "\",\n"
709740

710741
let partialModulePath = objectDir.appending(component: sourceFileName + "~partial.swiftmodule")
711-
stream <<< " \"swiftmodule\": \"" <<< partialModulePath.nativePathString(escaped: true) <<< "\",\n"
712-
stream <<< " \"swift-dependencies\": \"" <<< swiftDepsPath.nativePathString(escaped: true) <<< "\"\n"
713-
stream <<< " }" <<< ((idx + 1) < sources.count ? "," : "") <<< "\n"
742+
743+
stream.send(
744+
#"""
745+
"object": "\#(object.nativePathString(escaped: true))",
746+
"swiftmodule": "\#(partialModulePath.nativePathString(escaped: true))",
747+
"swift-dependencies": "\#(swiftDepsPath.nativePathString(escaped: true))"
748+
}\#((idx + 1) < sources.count ? "," : "")
749+
750+
"""#
751+
)
714752
}
715753

716-
stream <<< "}\n"
754+
stream.send("}\n")
717755

718756
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
719757
try self.fileSystem.writeFileContents(path, bytes: stream.bytes)
@@ -724,19 +762,23 @@ public final class SwiftTargetBuildDescription {
724762
private func generateModuleMap() throws -> AbsolutePath {
725763
let path = self.tempsPath.appending(component: moduleMapFilename)
726764

727-
let stream = BufferedOutputByteStream()
728-
stream <<< "module \(self.target.c99name) {\n"
729-
stream <<< " header \"" <<< self.objCompatibilityHeaderPath.pathString <<< "\"\n"
730-
stream <<< " requires objc\n"
731-
stream <<< "}\n"
765+
let bytes = ByteString(
766+
#"""
767+
module \#(self.target.c99name) {
768+
header "\#(self.objCompatibilityHeaderPath.pathString)"
769+
requires objc
770+
}
771+
772+
"""#.utf8
773+
)
732774

733775
// Return early if the contents are identical.
734-
if self.fileSystem.isFile(path), try self.fileSystem.readFileContents(path) == stream.bytes {
776+
if self.fileSystem.isFile(path), try self.fileSystem.readFileContents(path) == bytes {
735777
return path
736778
}
737779

738780
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
739-
try self.fileSystem.writeFileContents(path, bytes: stream.bytes)
781+
try self.fileSystem.writeFileContents(path, bytes: bytes)
740782

741783
return path
742784
}

0 commit comments

Comments
 (0)