Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e774b6cf5 | ||
|
|
8b371fa5d2 | ||
|
|
02fd579f19 | ||
|
|
e915bc0cfb | ||
|
|
f4c2c75aa1 | ||
|
|
dc7affa28c | ||
|
|
476cdc1461 | ||
|
|
a644208c62 | ||
|
|
d7a9819350 | ||
|
|
24a54c2ee0 | ||
|
|
3735ed4476 | ||
|
|
2880aa556b | ||
|
|
a125a871f5 | ||
|
|
d79844cf2b | ||
|
|
d0648411ea | ||
|
|
e74cc63271 | ||
|
|
28f84d3961 | ||
|
|
db184a13a3 | ||
|
|
b65d167937 | ||
|
|
9a770ca576 | ||
|
|
b7c189e6af | ||
|
|
2758f0f698 | ||
|
|
e68ad25cc0 |
168
.github/deploy
vendored
Executable file
168
.github/deploy
vendored
Executable file
@@ -0,0 +1,168 @@
|
|||||||
|
#!/usr/bin/swift sh
|
||||||
|
import func Darwin.fputs
|
||||||
|
import var Darwin.stderr
|
||||||
|
import PMKFoundation // PromiseKit/Foundation ~> 3.3
|
||||||
|
import LegibleError // @mxcl ~> 1.0
|
||||||
|
import Foundation
|
||||||
|
import PromiseKit // @mxcl ~> 6.8
|
||||||
|
import Path // mxcl/Path.swift ~> 0.15
|
||||||
|
|
||||||
|
let env = ProcessInfo.processInfo.environment
|
||||||
|
let token = env["GITHUB_TOKEN"] ?? env["GITHUB_ACCESS_TOKEN"]!
|
||||||
|
let slug = env["TRAVIS_REPO_SLUG"]!
|
||||||
|
let tag = env["TRAVIS_TAG"]!
|
||||||
|
|
||||||
|
func fatal(message: String) -> Never {
|
||||||
|
fputs("error: \(message)\n", stderr)
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
|
func fatal(error: Error) -> Never {
|
||||||
|
fatal(message: "\(error.legibleLocalizedDescription)\n\n\(error.legibleDescription)")
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Repo: Decodable {
|
||||||
|
let description: String
|
||||||
|
let license: License
|
||||||
|
struct License: Decodable {
|
||||||
|
let spdx_id: String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Package: Decodable {
|
||||||
|
let swiftLanguageVersions: [String]
|
||||||
|
let targets: [Target]
|
||||||
|
struct Target: Decodable {
|
||||||
|
let path: String?
|
||||||
|
let type: Kind
|
||||||
|
enum Kind: String, Decodable {
|
||||||
|
case regular
|
||||||
|
case test
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension URLRequest {
|
||||||
|
init(github path: String) {
|
||||||
|
let url = URL(string: "https://api.github.com\(path)")!
|
||||||
|
self.init(url: url)
|
||||||
|
setValue("token \(token)", forHTTPHeaderField: "Authorization")
|
||||||
|
setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
|
setValue("application/json", forHTTPHeaderField: "Accept")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func description() -> Promise<Repo> {
|
||||||
|
let rq = URLRequest(github: "/repos/\(slug)")
|
||||||
|
return firstly {
|
||||||
|
URLSession.shared.dataTask(.promise, with: rq).validate()
|
||||||
|
}.map { data, _ in
|
||||||
|
try JSONDecoder().decode(Repo.self, from: data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct User: Decodable {
|
||||||
|
let name: String
|
||||||
|
let email: String
|
||||||
|
}
|
||||||
|
|
||||||
|
func email() -> Promise<User> {
|
||||||
|
let rq = URLRequest(github: "/user")
|
||||||
|
return firstly {
|
||||||
|
URLSession.shared.dataTask(.promise, with: rq).validate()
|
||||||
|
}.map { data, _ in
|
||||||
|
try JSONDecoder().decode(User.self, from: data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dumpPackage() -> Promise<Package> {
|
||||||
|
let task = Process()
|
||||||
|
task.launchPath = "/usr/bin/swift"
|
||||||
|
task.arguments = ["package", "dump-package"]
|
||||||
|
return firstly {
|
||||||
|
task.launch(.promise)
|
||||||
|
}.map { out, _ in
|
||||||
|
out.fileHandleForReading.readDataToEndOfFile()
|
||||||
|
}.map { data in
|
||||||
|
try JSONDecoder().decode(Package.self, from: data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultSwiftVersion: String {
|
||||||
|
let task = Process()
|
||||||
|
task.launchPath = "/usr/bin/swift"
|
||||||
|
task.arguments = ["--version"]
|
||||||
|
|
||||||
|
func extract(input: String) -> String {
|
||||||
|
let range = input.range(of: #"Apple Swift version \d+\.\d+"#, options: .regularExpression)!
|
||||||
|
return String(input[range].split(separator: " ").last!)
|
||||||
|
}
|
||||||
|
|
||||||
|
return try! firstly {
|
||||||
|
task.launch(.promise)
|
||||||
|
}.compactMap { out, _ in
|
||||||
|
String(data: out.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
|
||||||
|
}.map { out in
|
||||||
|
extract(input: out)
|
||||||
|
}.wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func podspec(repo: Repo, user: User, pkg: Package) -> (Substring, String) {
|
||||||
|
let (owner, name) = { ($0[0], $0[1]) }(slug.split(separator: "/"))
|
||||||
|
let swiftVersion = pkg.swiftLanguageVersions.min() ?? defaultSwiftVersion
|
||||||
|
let targets = pkg.targets.filter{ $0.type == .regular }
|
||||||
|
guard targets.count == 1 else { fatal(message: "Too many targets for this script!") }
|
||||||
|
guard let sources = targets[0].path else { fatal(message: "Target has no path!") }
|
||||||
|
return (name, """
|
||||||
|
Pod::Spec.new do |s|
|
||||||
|
s.name = '\(name)'
|
||||||
|
s.author = { '\(user.name)': '\(user.email)' }
|
||||||
|
s.source = { git: "https://github.com/\(slug).git", tag: '\(tag)' }
|
||||||
|
s.version = '\(tag)'
|
||||||
|
s.summary = '\(repo.description)'
|
||||||
|
s.license = '\(repo.license.spdx_id)'
|
||||||
|
s.homepage = "https://github.com/\(slug)"
|
||||||
|
s.social_media_url = 'https://twitter.com/\(owner)'
|
||||||
|
s.osx.deployment_target = '10.10'
|
||||||
|
s.ios.deployment_target = '8.0'
|
||||||
|
s.tvos.deployment_target = '9.0'
|
||||||
|
s.watchos.deployment_target = '2.0'
|
||||||
|
s.source_files = '\(sources)/*.swift'
|
||||||
|
s.swift_version = '\(swiftVersion)'
|
||||||
|
end
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
func publishRelease() throws -> Promise<Void> {
|
||||||
|
struct Input: Encodable {
|
||||||
|
let tag_name = tag
|
||||||
|
let name = tag
|
||||||
|
let body = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var rq = URLRequest(github: "/repos/\(slug)/releases")
|
||||||
|
rq.httpMethod = "POST"
|
||||||
|
rq.httpBody = try JSONEncoder().encode(Input())
|
||||||
|
return URLSession.shared.dataTask(.promise, with: rq).validate().asVoid()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch CommandLine.arguments[1] {
|
||||||
|
case "generate-podspec":
|
||||||
|
firstly {
|
||||||
|
when(fulfilled: description(), email(), dumpPackage())
|
||||||
|
}.map(podspec).done { name, podspec in
|
||||||
|
try podspec.write(toFile: "\(name).podspec", atomically: false, encoding: .utf8)
|
||||||
|
exit(0)
|
||||||
|
}.catch {
|
||||||
|
fatal(error: $0)
|
||||||
|
}
|
||||||
|
case "publish-release":
|
||||||
|
try publishRelease().done {
|
||||||
|
exit(0)
|
||||||
|
}.catch {
|
||||||
|
fatal(error: $0)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
fatal(message: "invalid usage")
|
||||||
|
}
|
||||||
|
|
||||||
|
RunLoop.main.run()
|
||||||
13
.github/jazzy.yml
vendored
Normal file
13
.github/jazzy.yml
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module: Path
|
||||||
|
custom_categories:
|
||||||
|
- name: Path
|
||||||
|
children:
|
||||||
|
- Path
|
||||||
|
- /(_:_:)
|
||||||
|
xcodebuild_arguments:
|
||||||
|
- UseModernBuildSystem=NO
|
||||||
|
output:
|
||||||
|
../output
|
||||||
|
# output directory is relative to config file… ugh
|
||||||
|
exclude:
|
||||||
|
- Sources/Path+StringConvertibles.swift
|
||||||
84
.travis.yml
84
.travis.yml
@@ -15,17 +15,26 @@ xcode_scheme: Path.swift-Package
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
include:
|
include:
|
||||||
- script: swift test --parallel
|
- name: macOS / Swift 4.0.3
|
||||||
name: macOS / Swift 4.2.1
|
script: swift test --parallel -Xswiftc -swift-version -Xswiftc 4
|
||||||
|
|
||||||
|
- name: macOS / Swift 4.2.1
|
||||||
|
script: swift test --parallel
|
||||||
|
|
||||||
|
- name: macOS / Swift 5.0
|
||||||
|
osx_image: xcode10.2
|
||||||
|
script: swift test --parallel
|
||||||
|
|
||||||
- &xcodebuild
|
- &xcodebuild
|
||||||
before_install: swift package generate-xcodeproj --enable-code-coverage
|
before_install: swift package generate-xcodeproj --enable-code-coverage
|
||||||
xcode_destination: platform=iOS Simulator,OS=latest,name=iPhone XS
|
xcode_destination: platform=iOS Simulator,OS=latest,name=iPhone XS
|
||||||
name: iOS / Swift 4.2.1
|
name: iOS / Swift 4.2.1
|
||||||
after_success: bash <(curl -s https://codecov.io/bash)
|
after_success: bash <(curl -s https://codecov.io/bash)
|
||||||
|
|
||||||
- <<: *xcodebuild
|
- <<: *xcodebuild
|
||||||
xcode_destination: platform=tvOS Simulator,OS=latest,name=Apple TV
|
xcode_destination: platform=tvOS Simulator,OS=latest,name=Apple TV
|
||||||
name: tvOS / Swift 4.2.1
|
name: tvOS / Swift 4.2.1
|
||||||
|
|
||||||
- <<: *xcodebuild
|
- <<: *xcodebuild
|
||||||
name: watchOS / Swift 4.2.1
|
name: watchOS / Swift 4.2.1
|
||||||
script: |
|
script: |
|
||||||
@@ -49,38 +58,22 @@ jobs:
|
|||||||
|
|
||||||
- <<: *linux
|
- <<: *linux
|
||||||
env: SWIFT_VERSION='5.0-DEVELOPMENT-SNAPSHOT-2019-01-22-a'
|
env: SWIFT_VERSION='5.0-DEVELOPMENT-SNAPSHOT-2019-01-22-a'
|
||||||
name: Linux / Swift 5.0.0-dev (2019-01-22)
|
name: Linux / Swift 5.0.0-dev+2019.01.22
|
||||||
|
|
||||||
- stage: pretest
|
- stage: pretest
|
||||||
name: Check Linux tests are sync’d
|
name: Check Linux tests are sync’d
|
||||||
install: swift test --generate-linuxmain
|
install: swift test --generate-linuxmain
|
||||||
script: git diff --exit-code
|
script: git diff --exit-code
|
||||||
|
osx_image: xcode10.2
|
||||||
|
|
||||||
- stage: deploy
|
- stage: deploy
|
||||||
name: Jazzy
|
name: Jazzy
|
||||||
before_install: |
|
|
||||||
cat <<\ \ EOF> .jazzy.yaml
|
|
||||||
module: Path
|
|
||||||
module_version: TRAVIS_TAG
|
|
||||||
custom_categories:
|
|
||||||
- name: Path
|
|
||||||
children:
|
|
||||||
- Path
|
|
||||||
- /(_:_:)
|
|
||||||
xcodebuild_arguments:
|
|
||||||
- UseModernBuildSystem=NO
|
|
||||||
output: output
|
|
||||||
github_url: https://github.com/mxcl/Path.swift
|
|
||||||
exclude:
|
|
||||||
- Sources/Path+StringConvertibles.swift
|
|
||||||
EOF
|
|
||||||
sed -i '' "s/TRAVIS_TAG/$TRAVIS_TAG/" .jazzy.yaml
|
|
||||||
# ^^ this weirdness because Travis multiline YAML is broken and inserts
|
|
||||||
# two spaces in front of the output which means we need a prefixed
|
|
||||||
# delimiter which also weirdly stops bash from doing variable substitution
|
|
||||||
install: gem install jazzy
|
install: gem install jazzy
|
||||||
before_script: swift package generate-xcodeproj
|
before_script: swift package generate-xcodeproj
|
||||||
script: jazzy
|
script: |
|
||||||
|
jazzy --config .github/jazzy.yml \
|
||||||
|
--module-version $TRAVIS_TAG \
|
||||||
|
--github_url "https://github.com/$TRAVIS_REPO_SLUG"
|
||||||
deploy:
|
deploy:
|
||||||
provider: pages
|
provider: pages
|
||||||
skip-cleanup: true
|
skip-cleanup: true
|
||||||
@@ -90,43 +83,8 @@ jobs:
|
|||||||
tags: true
|
tags: true
|
||||||
|
|
||||||
- name: CocoaPods
|
- name: CocoaPods
|
||||||
before_install: export TRAVIS_REPO_NAME=${TRAVIS_REPO_SLUG#*/}
|
osx_image: xcode10.2
|
||||||
install: gem install cocoapods
|
install: brew install mxcl/made/swift-sh
|
||||||
before_script: |
|
before_script: .github/deploy generate-podspec
|
||||||
export DESCRIPTION=$(swift - <<\ \ EOF
|
|
||||||
import Foundation
|
|
||||||
struct Response: Decodable { let description: String }
|
|
||||||
let token = ProcessInfo.processInfo.environment["GITHUB_TOKEN"]!
|
|
||||||
let slug = ProcessInfo.processInfo.environment["TRAVIS_REPO_SLUG"]!
|
|
||||||
let url = URL(string: "https://api.github.com/repos/\(slug)")!
|
|
||||||
var rq = URLRequest(url: url)
|
|
||||||
rq.setValue("token \(token)", forHTTPHeaderField: "Authorization")
|
|
||||||
let semaphore = DispatchSemaphore(value: 0)
|
|
||||||
var data: Data!
|
|
||||||
URLSession.shared.dataTask(with: rq) { d, _, _ in
|
|
||||||
data = d
|
|
||||||
semaphore.signal()
|
|
||||||
}.resume()
|
|
||||||
semaphore.wait()
|
|
||||||
let rsp = try JSONDecoder().decode(Response.self, from: data)
|
|
||||||
print(rsp.description, terminator: "")
|
|
||||||
EOF)
|
|
||||||
cat <<\ \ EOF> $TRAVIS_REPO_NAME.podspec
|
|
||||||
Pod::Spec.new do |s|
|
|
||||||
s.name = ENV['TRAVIS_REPO_NAME']
|
|
||||||
s.version = ENV['TRAVIS_TAG']
|
|
||||||
s.summary = ENV['DESCRIPTION']
|
|
||||||
s.homepage = "https://github.com/#{ENV['TRAVIS_REPO_SLUG']}"
|
|
||||||
s.license = { type: 'Unlicense', file: 'LICENSE.md' }
|
|
||||||
s.author = { mxcl: 'mxcl@me.com' }
|
|
||||||
s.source = { git: "https://github.com/#{ENV['TRAVIS_REPO_SLUG']}.git", tag: s.version }
|
|
||||||
s.social_media_url = 'https://twitter.com/mxcl'
|
|
||||||
s.osx.deployment_target = '10.10'
|
|
||||||
s.ios.deployment_target = '8.0'
|
|
||||||
s.tvos.deployment_target = '10.0'
|
|
||||||
s.watchos.deployment_target = '3.0'
|
|
||||||
s.source_files = 'Sources/*'
|
|
||||||
s.swift_version = '4.2'
|
|
||||||
end
|
|
||||||
EOF
|
|
||||||
script: pod trunk push
|
script: pod trunk push
|
||||||
|
after_success: .github/deploy publish-release
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// swift-tools-version:4.2
|
// swift-tools-version:4.2
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let pkg = Package(
|
||||||
name: "Path.swift",
|
name: "Path.swift",
|
||||||
products: [
|
products: [
|
||||||
.library(name: "Path", targets: ["Path"]),
|
.library(name: "Path", targets: ["Path"]),
|
||||||
@@ -9,5 +9,6 @@ let package = Package(
|
|||||||
targets: [
|
targets: [
|
||||||
.target(name: "Path", path: "Sources"),
|
.target(name: "Path", path: "Sources"),
|
||||||
.testTarget(name: "PathTests", dependencies: ["Path"]),
|
.testTarget(name: "PathTests", dependencies: ["Path"]),
|
||||||
]
|
],
|
||||||
|
swiftLanguageVersions: [.v4, .v4_2, .version("5")]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
// swift-tools-version:5.0
|
|
||||||
import PackageDescription
|
|
||||||
|
|
||||||
let pkg = Package(
|
|
||||||
name: "Path.swift",
|
|
||||||
products: [
|
|
||||||
.library(name: "Path", targets: ["Path"]),
|
|
||||||
],
|
|
||||||
targets: [
|
|
||||||
.target(name: "Path", path: "Sources"),
|
|
||||||
.testTarget(name: "PathTests", dependencies: ["Path"]),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
pkg.platforms = [
|
|
||||||
.macOS(.v10_10), .iOS(.v8), .tvOS(.v10), .watchOS(.v3)
|
|
||||||
]
|
|
||||||
pkg.swiftLanguageVersions = [
|
|
||||||
.v4_2, .v5
|
|
||||||
]
|
|
||||||
@@ -53,7 +53,7 @@ help me continue my work, I appreciate it x
|
|||||||
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
|
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
[Other donation/tipping options](http://mxcl.github.io/donate/)
|
[Other donation/tipping options](http://mxcl.dev/#donate)
|
||||||
|
|
||||||
# Handbook
|
# Handbook
|
||||||
|
|
||||||
@@ -316,7 +316,7 @@ pursuit of getting it *right*)! We will tag 1.0 as soon as possible.
|
|||||||
|
|
||||||
### Get push notifications for new releases
|
### Get push notifications for new releases
|
||||||
|
|
||||||
https://codebasesaga.com/canopy/
|
https://mxcl.dev/canopy/
|
||||||
|
|
||||||
# Alternatives
|
# Alternatives
|
||||||
|
|
||||||
@@ -328,7 +328,7 @@ https://codebasesaga.com/canopy/
|
|||||||
|
|
||||||
[badge-platforms]: https://img.shields.io/badge/platforms-macOS%20%7C%20Linux%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-lightgrey.svg
|
[badge-platforms]: https://img.shields.io/badge/platforms-macOS%20%7C%20Linux%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-lightgrey.svg
|
||||||
[badge-languages]: https://img.shields.io/badge/swift-4.2%20%7C%205.0-orange.svg
|
[badge-languages]: https://img.shields.io/badge/swift-4.2%20%7C%205.0-orange.svg
|
||||||
[docs]: https://mxcl.github.io/Path.swift/Structs/Path.html
|
[docs]: https://mxcl.dev/Path.swift/Structs/Path.html
|
||||||
[badge-jazzy]: https://raw.githubusercontent.com/mxcl/Path.swift/gh-pages/badge.svg?sanitize=true
|
[badge-jazzy]: https://raw.githubusercontent.com/mxcl/Path.swift/gh-pages/badge.svg?sanitize=true
|
||||||
[badge-codecov]: https://codecov.io/gh/mxcl/Path.swift/branch/master/graph/badge.svg
|
[badge-codecov]: https://codecov.io/gh/mxcl/Path.swift/branch/master/graph/badge.svg
|
||||||
[badge-ci]: https://travis-ci.com/mxcl/Path.swift.svg
|
[badge-ci]: https://travis-ci.com/mxcl/Path.swift.svg
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ public extension Bundle {
|
|||||||
var path: Path {
|
var path: Path {
|
||||||
return Path(string: bundlePath)
|
return Path(string: bundlePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the executable for this bundle, if there is one, not all bundles have one hence `Optional`.
|
||||||
|
var executable: Path? {
|
||||||
|
return executablePath.flatMap(Path.init)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extensions on `String` that work with `Path` rather than `String` or `URL`
|
/// Extensions on `String` that work with `Path` rather than `String` or `URL`
|
||||||
|
|||||||
@@ -53,11 +53,25 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
- Note: On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).
|
- Note: On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).
|
||||||
- Returns: The path or `nil` if fed a relative path or a `~foo` string where there is no user `foo`.
|
- Returns: The path or `nil` if fed a relative path or a `~foo` string where there is no user `foo`.
|
||||||
*/
|
*/
|
||||||
public init?(_ description: String) {
|
public init?<S: StringProtocol>(_ description: S) {
|
||||||
var pathComponents = description.split(separator: "/")
|
var pathComponents = description.split(separator: "/")
|
||||||
switch description.first {
|
switch description.first {
|
||||||
case "/":
|
case "/":
|
||||||
break
|
#if os(macOS)
|
||||||
|
func ifExists(withPrefix prefix: String, removeFirst n: Int) {
|
||||||
|
assert(prefix.split(separator: "/").count == n)
|
||||||
|
|
||||||
|
if description.hasPrefix(prefix), FileManager.default.fileExists(atPath: String(description)) {
|
||||||
|
pathComponents.removeFirst(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ifExists(withPrefix: "/private/var/automount", removeFirst: 3)
|
||||||
|
ifExists(withPrefix: "/var/automount", removeFirst: 2)
|
||||||
|
ifExists(withPrefix: "/private", removeFirst: 1)
|
||||||
|
#endif
|
||||||
|
self.string = join_(prefix: "/", pathComponents: pathComponents)
|
||||||
|
|
||||||
case "~":
|
case "~":
|
||||||
if description == "~" {
|
if description == "~" {
|
||||||
self = Path.home
|
self = Path.home
|
||||||
@@ -82,26 +96,11 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
pathComponents.remove(at: 0)
|
pathComponents.remove(at: 0)
|
||||||
pathComponents.insert(contentsOf: tilded.split(separator: "/"), at: 0)
|
self.string = join_(prefix: tilded, pathComponents: pathComponents)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
|
||||||
func ifExists(withPrefix prefix: String, removeFirst n: Int) {
|
|
||||||
assert(prefix.split(separator: "/").count == n)
|
|
||||||
|
|
||||||
if description.hasPrefix(prefix), FileManager.default.fileExists(atPath: description) {
|
|
||||||
pathComponents.removeFirst(n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ifExists(withPrefix: "/private/var/automount", removeFirst: 3)
|
|
||||||
ifExists(withPrefix: "/var/automount", removeFirst: 2)
|
|
||||||
ifExists(withPrefix: "/private", removeFirst: 1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
self.string = join_(prefix: "/", pathComponents: pathComponents)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -200,6 +199,15 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Splits the string representation on the directory separator.
|
||||||
|
- Important: The first element is always "/" to be consistent with `NSString.pathComponents`.
|
||||||
|
*/
|
||||||
|
@inlinable
|
||||||
|
public var components: [String] {
|
||||||
|
return ["/"] + string.split(separator: "/").map(String.init)
|
||||||
|
}
|
||||||
|
|
||||||
//MARK: Pathing
|
//MARK: Pathing
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -403,6 +403,7 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual(bndl.privateFrameworks, tmpdir.Frameworks)
|
XCTAssertEqual(bndl.privateFrameworks, tmpdir.Frameworks)
|
||||||
XCTAssertEqual(bndl.resources, tmpdir)
|
XCTAssertEqual(bndl.resources, tmpdir)
|
||||||
XCTAssertNil(bndl.path(forResource: "foo", ofType: "bar"))
|
XCTAssertNil(bndl.path(forResource: "foo", ofType: "bar"))
|
||||||
|
XCTAssertNil(bndl.executable)
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Contents.Frameworks)
|
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Contents.Frameworks)
|
||||||
@@ -588,4 +589,19 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertNil(Path("../foo"))
|
XCTAssertNil(Path("../foo"))
|
||||||
XCTAssertNil(Path("./foo"))
|
XCTAssertNil(Path("./foo"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPathComponents() throws {
|
||||||
|
XCTAssertEqual(Path.root.foo.bar.components, ["/", "foo", "bar"])
|
||||||
|
XCTAssertEqual(Path.root.components, ["/"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func testFlatMap() throws {
|
||||||
|
// testing compile works
|
||||||
|
let foo: String? = "/a"
|
||||||
|
_ = foo.flatMap(Path.init)
|
||||||
|
let bar: Substring? = "/a"
|
||||||
|
_ = bar.flatMap(Path.init)
|
||||||
|
let baz: String.SubSequence? = "/a/b:1".split(separator: ":").first
|
||||||
|
_ = baz.flatMap(Path.init)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
|
#if !canImport(ObjectiveC)
|
||||||
import XCTest
|
import XCTest
|
||||||
|
|
||||||
extension PathTests {
|
extension PathTests {
|
||||||
static let __allTests = [
|
// DO NOT MODIFY: This is autogenerated, use:
|
||||||
|
// `swift test --generate-linuxmain`
|
||||||
|
// to regenerate.
|
||||||
|
static let __allTests__PathTests = [
|
||||||
("testBasename", testBasename),
|
("testBasename", testBasename),
|
||||||
("testBundleExtensions", testBundleExtensions),
|
("testBundleExtensions", testBundleExtensions),
|
||||||
("testCodable", testCodable),
|
("testCodable", testCodable),
|
||||||
@@ -19,6 +23,7 @@ extension PathTests {
|
|||||||
("testFileHandleExtensions", testFileHandleExtensions),
|
("testFileHandleExtensions", testFileHandleExtensions),
|
||||||
("testFileReference", testFileReference),
|
("testFileReference", testFileReference),
|
||||||
("testFilesystemAttributes", testFilesystemAttributes),
|
("testFilesystemAttributes", testFilesystemAttributes),
|
||||||
|
("testFlatMap", testFlatMap),
|
||||||
("testInitializerForRelativePath", testInitializerForRelativePath),
|
("testInitializerForRelativePath", testInitializerForRelativePath),
|
||||||
("testIsDirectory", testIsDirectory),
|
("testIsDirectory", testIsDirectory),
|
||||||
("testJoin", testJoin),
|
("testJoin", testJoin),
|
||||||
@@ -28,6 +33,7 @@ extension PathTests {
|
|||||||
("testMoveInto", testMoveInto),
|
("testMoveInto", testMoveInto),
|
||||||
("testMoveTo", testMoveTo),
|
("testMoveTo", testMoveTo),
|
||||||
("testNoUndesiredSymlinkResolution", testNoUndesiredSymlinkResolution),
|
("testNoUndesiredSymlinkResolution", testNoUndesiredSymlinkResolution),
|
||||||
|
("testPathComponents", testPathComponents),
|
||||||
("testReadlinkOnFileReturnsSelf", testReadlinkOnFileReturnsSelf),
|
("testReadlinkOnFileReturnsSelf", testReadlinkOnFileReturnsSelf),
|
||||||
("testReadlinkOnNonExistantFileThrows", testReadlinkOnNonExistantFileThrows),
|
("testReadlinkOnNonExistantFileThrows", testReadlinkOnNonExistantFileThrows),
|
||||||
("testReadlinkOnRelativeSymlink", testReadlinkOnRelativeSymlink),
|
("testReadlinkOnRelativeSymlink", testReadlinkOnRelativeSymlink),
|
||||||
@@ -47,10 +53,9 @@ extension PathTests {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !os(macOS)
|
|
||||||
public func __allTests() -> [XCTestCaseEntry] {
|
public func __allTests() -> [XCTestCaseEntry] {
|
||||||
return [
|
return [
|
||||||
testCase(PathTests.__allTests),
|
testCase(PathTests.__allTests__PathTests),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user