Files
swift-mirror/utils/swift-xcodegen/Sources/SwiftXcodeGen/Path/AnyPath.swift
Hamish Knight 03d8ea5248 Introduce swift-xcodegen
This is a tool specifically designed to generate
Xcode projects for the Swift repo (as well as a
couple of adjacent repos such as LLVM and Clang).
It aims to provide a much more user-friendly experience
than the CMake Xcode generation (`build-script --xcode`).
2024-11-05 22:42:10 +00:00

74 lines
1.7 KiB
Swift

//===--- AnyPath.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 ArgumentParser
import System
public enum AnyPath: PathProtocol, Sendable {
case relative(RelativePath)
case absolute(AbsolutePath)
public init<P: PathProtocol>(_ path: P) {
self = path.asAnyPath
}
public init(_ storage: FilePath) {
if storage.isAbsolute {
self = .absolute(.init(storage))
} else {
self = .relative(.init(storage))
}
}
public var storage: FilePath {
switch self {
case .relative(let r):
r.storage
case .absolute(let a):
a.storage
}
}
public var asAnyPath: AnyPath {
self
}
}
extension AnyPath {
public var absoluteInWorkingDir: AbsolutePath {
switch self {
case .relative(let r):
r.absoluteInWorkingDir
case .absolute(let a):
a
}
}
}
extension AnyPath: Decodable {
public init(from decoder: Decoder) throws {
self.init(try decoder.singleValueContainer().decode(String.self))
}
}
extension AnyPath: ExpressibleByArgument {
public init(argument rawPath: String) {
self.init(rawPath)
}
}
extension StringProtocol {
func hasExtension(_ ext: FileExtension) -> Bool {
FilePath(String(self)).extension == ext.rawValue
}
}