1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
//===--- PathProtocol.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import System
public protocol PathProtocol: Hashable, CustomStringConvertible {
var storage: FilePath { get }
var asAnyPath: AnyPath { get }
init(_ storage: FilePath)
}
public extension PathProtocol {
typealias Component = FilePath.Component
var parentDir: Self? {
// Remove the last component and check to see if it's empty.
var result = storage
guard result.removeLastComponent(), !result.isEmpty else { return nil }
return Self(result)
}
var fileName: String {
storage.lastComponent?.string ?? ""
}
func appending(_ relPath: RelativePath) -> Self {
Self(storage.pushing(relPath.storage))
}
func appending(_ str: String) -> Self {
Self(storage.appending(str))
}
func commonAncestor(with other: Self) -> Self {
precondition(storage.root == other.storage.root)
var result = [Component]()
for (comp, otherComp) in zip(components, other.components) {
guard comp == otherComp else { break }
result.append(comp)
}
return Self(FilePath(root: storage.root, result))
}
/// Attempt to remove `other` as a prefix of `self`, or `nil` if `other` is
/// not a prefix of `self`.
func removingPrefix(_ other: Self) -> RelativePath? {
var result = storage
guard result.removePrefix(other.storage) else { return nil }
return RelativePath(result)
}
func hasExtension(_ ext: FileExtension) -> Bool {
storage.extension == ext.rawValue
}
func hasExtension(_ exts: FileExtension...) -> Bool {
// Note that querying `.extension` involves re-parsing, so only do it
// once here.
let ext = storage.extension
return exts.contains(where: { ext == $0.rawValue })
}
func hasPrefix(_ other: Self) -> Bool {
rawPath.hasPrefix(other.rawPath)
}
var components: FilePath.ComponentView {
storage.components
}
var description: String { storage.string }
init(stringLiteral value: String) {
self.init(value)
}
init(_ rawPath: String) {
self.init(FilePath(rawPath))
}
var rawPath: String {
storage.string
}
func escaped(addQuotesIfNeeded: Bool) -> String {
rawPath.escaped(addQuotesIfNeeded: addQuotesIfNeeded)
}
var escaped: String {
rawPath.escaped
}
}
extension PathProtocol {
/// Whether this is a .swift.gyb file.
var isSwiftGyb: Bool {
hasExtension(.gyb) && rawPath.dropLast(4).hasExtension(.swift)
}
var isHeaderLike: Bool {
if hasExtension(.h, .def, .td, .modulemap) {
return true
}
// Consider all gyb files to be header-like, except .swift.gyb, which
// will be handled separately when creating Swift targets.
if hasExtension(.gyb) && !isSwiftGyb {
return true
}
return false
}
var isCSourceLike: Bool {
hasExtension(.c, .cpp)
}
var isDocLike: Bool {
hasExtension(.md, .rst) || fileName.starts(with: "README")
}
}
extension Collection where Element: PathProtocol {
/// Computes the common parent for a collection of paths. If there is only
/// a single unique path, this returns the parent for that path.
var commonAncestor: Element? {
guard let first = self.first else { return nil }
let result = dropFirst().reduce(first, { $0.commonAncestor(with: $1) })
return result == first ? result.parentDir : result
}
}
|