Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor path model #6509

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion Examples/package-info/Sources/package-info/example.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Basics
import TSCBasic
import Workspace

@main
Expand Down
1 change: 0 additions & 1 deletion Sources/Basics/Archiver/Archiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//

import _Concurrency
import TSCBasic

/// The `Archiver` protocol abstracts away the different operations surrounding archives.
public protocol Archiver {
Expand Down
12 changes: 5 additions & 7 deletions Sources/Basics/Archiver/TarArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

import class Dispatch.DispatchQueue
import struct Dispatch.DispatchTime
import struct TSCBasic.AbsolutePath
import protocol TSCBasic.FileSystem
import struct TSCBasic.FileSystemError
import class TSCBasic.Process

Expand Down Expand Up @@ -53,11 +51,11 @@ public struct TarArchiver: Archiver {
) {
do {
guard self.fileSystem.exists(archivePath) else {
throw FileSystemError(.noEntry, archivePath)
throw FileSystemError(.noEntry, archivePath.underlying)
}

guard self.fileSystem.isDirectory(destinationPath) else {
throw FileSystemError(.notDirectory, destinationPath)
throw FileSystemError(.notDirectory, destinationPath.underlying)
}

let process = TSCBasic.Process(
Expand Down Expand Up @@ -90,12 +88,12 @@ public struct TarArchiver: Archiver {
) {
do {
guard self.fileSystem.isDirectory(directory) else {
throw FileSystemError(.notDirectory, directory)
throw FileSystemError(.notDirectory, directory.underlying)
}

let process = TSCBasic.Process(
arguments: [self.tarCommand, "acf", destinationPath.pathString, directory.basename],
workingDirectory: directory.parentDirectory
workingDirectory: directory.parentDirectory.underlying
)

guard let registrationKey = self.cancellator.register(process) else {
Expand All @@ -120,7 +118,7 @@ public struct TarArchiver: Archiver {
public func validate(path: AbsolutePath, completion: @escaping (Result<Bool, Error>) -> Void) {
do {
guard self.fileSystem.exists(path) else {
throw FileSystemError(.noEntry, path)
throw FileSystemError(.noEntry, path.underlying)
}

let process = TSCBasic.Process(arguments: [self.tarCommand, "tf", path.pathString])
Expand Down
3 changes: 0 additions & 3 deletions Sources/Basics/Archiver/UniversalArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
//
//===----------------------------------------------------------------------===//

import struct TSCBasic.AbsolutePath
import protocol TSCBasic.FileSystem

/// An `Archiver` that handles multiple formats by delegating to other existing archivers each dedicated to its own
/// format.
public struct UniversalArchiver: Archiver {
Expand Down
39 changes: 21 additions & 18 deletions Sources/Basics/Archiver/ZipArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
//
//===----------------------------------------------------------------------===//

import TSCBasic
import Dispatch
import struct TSCBasic.FileSystemError
import class TSCBasic.Process

/// An `Archiver` that handles ZIP archives using the command-line `zip` and `unzip` tools.
public struct ZipArchiver: Archiver, Cancellable {
Expand Down Expand Up @@ -40,18 +41,20 @@ public struct ZipArchiver: Archiver, Cancellable {
) {
do {
guard self.fileSystem.exists(archivePath) else {
throw FileSystemError(.noEntry, archivePath)
throw FileSystemError(.noEntry, archivePath.underlying)
}

guard self.fileSystem.isDirectory(destinationPath) else {
throw FileSystemError(.notDirectory, destinationPath)
throw FileSystemError(.notDirectory, destinationPath.underlying)
}

#if os(Windows)
let process = TSCBasic.Process(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
#else
let process = TSCBasic.Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
#endif
#if os(Windows)
let process = TSCBasic
.Process(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
#else
let process = TSCBasic
.Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
#endif
guard let registrationKey = self.cancellator.register(process) else {
throw CancellationError.failedToRegisterProcess(process)
}
Expand All @@ -78,21 +81,21 @@ public struct ZipArchiver: Archiver, Cancellable {
) {
do {
guard self.fileSystem.isDirectory(directory) else {
throw FileSystemError(.notDirectory, directory)
throw FileSystemError(.notDirectory, directory.underlying)
}

#if os(Windows)
#if os(Windows)
let process = TSCBasic.Process(
// FIXME: are these the right arguments?
arguments: ["tar.exe", "-a", "-c", "-f", destinationPath.pathString, directory.basename],
workingDirectory: directory.parentDirectory
workingDirectory: directory.parentDirectory.underlying
)
#else
#else
let process = TSCBasic.Process(
arguments: ["zip", "-r", destinationPath.pathString, directory.basename],
workingDirectory: directory.parentDirectory
workingDirectory: directory.parentDirectory.underlying
)
#endif
#endif

guard let registrationKey = self.cancellator.register(process) else {
throw CancellationError.failedToRegisterProcess(process)
Expand All @@ -116,14 +119,14 @@ public struct ZipArchiver: Archiver, Cancellable {
public func validate(path: AbsolutePath, completion: @escaping (Result<Bool, Error>) -> Void) {
do {
guard self.fileSystem.exists(path) else {
throw FileSystemError(.noEntry, path)
throw FileSystemError(.noEntry, path.underlying)
}

#if os(Windows)
#if os(Windows)
let process = TSCBasic.Process(arguments: ["tar.exe", "tf", path.pathString])
#else
#else
let process = TSCBasic.Process(arguments: ["unzip", "-t", path.pathString])
#endif
#endif
guard let registrationKey = self.cancellator.register(process) else {
throw CancellationError.failedToRegisterProcess(process)
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/Basics/AuthorizationProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import struct Foundation.URL
import Security
#endif

import TSCBasic

public protocol AuthorizationProvider {
@Sendable
func authentication(for url: URL) -> (user: String, password: String)?
Expand Down
5 changes: 3 additions & 2 deletions Sources/Basics/ByteString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//
//===----------------------------------------------------------------------===//

import TSCBasic
import struct TSCBasic.ByteString
import struct TSCBasic.SHA256

extension ByteString {
/// A lowercase, hexadecimal representation of the SHA256 hash
Expand All @@ -20,6 +21,6 @@ extension ByteString {
/// Secure Hashing Algorithm 2 (SHA-2) hashing with a 256-bit digest, when available,
/// falling back on a native implementation in Swift provided by TSCBasic.
public var sha256Checksum: String {
return SHA256().hash(self).hexadecimalRepresentation
SHA256().hash(self).hexadecimalRepresentation
}
}
6 changes: 4 additions & 2 deletions Sources/Basics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ add_library(Basics
DispatchTimeInterval+Extensions.swift
EnvironmentVariables.swift
Errors.swift
FileSystem/AbsolutePath.swift
FileSystem/FileSystem+Extensions.swift
FileSystem/Path+Extensions.swift
FileSystem/RelativePath.swift
FileSystem/TemporaryFile.swift
FileSystem/TSCAdapters.swift
FileSystem/VFSOverlay.swift
HTTPClient/HTTPClient.swift
HTTPClient/HTTPClientConfiguration.swift
Expand All @@ -51,7 +53,7 @@ add_library(Basics
String+Extensions.swift
SwiftVersion.swift
SQLiteBackedCache.swift
Triple.swift
Triple.swift
Version+Extensions.swift
WritableByteStream+Extensions.swift)
target_link_libraries(Basics PUBLIC
Expand Down
4 changes: 2 additions & 2 deletions Sources/Basics/Cancellator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import Dispatch
import Foundation
import TSCBasic

import class TSCBasic.Process
import class TSCBasic.Thread
#if canImport(WinSDK)
import WinSDK
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/Concurrency/SendableBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import struct Foundation.Date

/// A `Sendable` storage that allows access from concurrently running tasks in
/// A `Sendable` storage that allows access from concurrently running tasks in
/// an `async` closure. This type serves as a replacement for `ThreadSafeBox`
/// implemented with Swift Concurrency primitives.
public actor SendableBox<Value: Sendable> {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Basics/Concurrency/ThreadSafeBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ extension ThreadSafeBox where Value == String {
public func append(_ value: String) {
self.mutate { existingValue in
if let existingValue {
return existingValue + value
return existingValue + value
} else {
return value
return value
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions Sources/Basics/Dictionary+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

import TSCBasic
import OrderedCollections

extension Dictionary {
@inlinable
Expand All @@ -37,12 +37,14 @@ extension Dictionary {
}
}

extension OrderedDictionary {
public subscript(key: Key, `default` `default`: Value) -> Value {
set {
self[key] = newValue
} get {
self[key] ?? `default`
}
}
}
/*
extension OrderedDictionary {
public subscript(key: Key, `default` `default`: Value) -> Value {
set {
self[key] = newValue
} get {
self[key] ?? `default`
}
}
}
*/
13 changes: 6 additions & 7 deletions Sources/Basics/DispatchTimeInterval+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ extension DispatchTimeInterval {
case .seconds(let value):
return "\(value)s"
case .milliseconds(let value):
return String(format: "%.2f", Double(value)/Double(1000)) + "s"
return String(format: "%.2f", Double(value) / Double(1000)) + "s"
case .microseconds(let value):
return String(format: "%.2f", Double(value)/Double(1_000_000)) + "s"
return String(format: "%.2f", Double(value) / Double(1_000_000)) + "s"
case .nanoseconds(let value):
return String(format: "%.2f", Double(value)/Double(1_000_000_000)) + "s"
return String(format: "%.2f", Double(value) / Double(1_000_000_000)) + "s"
case .never:
return "n/a"
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
@unknown default:
return "n/a"
#endif
#endif
}
}
}
Expand All @@ -100,9 +100,8 @@ extension DispatchTime {
public func distance(to: DispatchTime) -> DispatchTimeInterval {
let final = to.uptimeNanoseconds
let point = self.uptimeNanoseconds
let duration: Int64 = Int64(bitPattern: final.subtractingReportingOverflow(point).partialValue)
let duration = Int64(bitPattern: final.subtractingReportingOverflow(point).partialValue)
return .nanoseconds(duration >= Int.max ? Int.max : Int(duration))
}
}
#endif

12 changes: 5 additions & 7 deletions Sources/Basics/EnvironmentVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
//
//===----------------------------------------------------------------------===//

import TSCBasic
import Foundation

public typealias EnvironmentVariables = [String: String]

extension EnvironmentVariables {

public static func empty() -> EnvironmentVariables {
return [:]
[:]
}

public static func process() -> EnvironmentVariables {
return ProcessInfo.processInfo.environment
ProcessInfo.processInfo.environment
}

public mutating func prependPath(_ key: String, value: String) {
Expand Down Expand Up @@ -52,11 +50,11 @@ extension EnvironmentVariables {

/// `PATH` variable in the process's environment (`Path` under Windows).
public var path: String? {
#if os(Windows)
#if os(Windows)
let pathArg = "Path"
#else
#else
let pathArg = "PATH"
#endif
#endif
return self[pathArg]
}
}
Loading