Skip to content

Commit 584c338

Browse files
committed
Basics: introduce some native path representation helpers
We end up needing these in a few places, and while we could make it `internal` and use `@testable` imports in the tests, this ends up being needed in `Build` and `PackageLoading`. Ideally, we would give it package visibility, but that functionality would not be backwards compatible. This is a reluctant approach at solving that problem by using `public` visibility.
1 parent aa0670b commit 584c338

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

Sources/Basics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_library(Basics
4242
ImportScanning.swift
4343
JSON+Extensions.swift
4444
JSONDecoder+Extensions.swift
45+
NativePathExtensions.swift
4546
Netrc.swift
4647
Observability.swift
4748
SQLite.swift
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import struct TSCBasic.AbsolutePath
15+
16+
extension String {
17+
public func nativePathString(escaped: Bool) -> Self {
18+
return URL(fileURLWithPath: self).withUnsafeFileSystemRepresentation {
19+
let repr = String(cString: $0!)
20+
if escaped {
21+
return repr.replacingOccurrences(of: "\\", with: "\\\\")
22+
}
23+
return repr
24+
}
25+
}
26+
}
27+
28+
extension AbsolutePath {
29+
public func nativePathString(escaped: Bool) -> String {
30+
return self.pathString.nativePathString(escaped: escaped)
31+
}
32+
}

Sources/Build/BuildPlan.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ extension String {
3030
}
3131
}
3232

33-
extension AbsolutePath {
34-
internal func nativePathString(escaped: Bool) -> String {
35-
return URL(fileURLWithPath: self.pathString).withUnsafeFileSystemRepresentation {
36-
let repr = String(cString: $0!)
37-
if escaped {
38-
return repr.replacingOccurrences(of: "\\", with: "\\\\")
39-
}
40-
return repr
41-
}
42-
}
43-
}
44-
4533
extension BuildParameters {
4634
/// Returns the directory to be used for module cache.
4735
public var moduleCache: AbsolutePath {

0 commit comments

Comments
 (0)