Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dac007e907 | ||
|
|
b6b4a74a26 | ||
|
|
b76db41ca4 | ||
|
|
8d5d67b81b | ||
|
|
21ddc7dc3a | ||
|
|
f324b4a562 | ||
|
|
0e061f9cc8 | ||
|
|
7e774b6cf5 | ||
|
|
8b371fa5d2 | ||
|
|
02fd579f19 | ||
|
|
e915bc0cfb | ||
|
|
f4c2c75aa1 | ||
|
|
dc7affa28c | ||
|
|
476cdc1461 | ||
|
|
a644208c62 | ||
|
|
d7a9819350 | ||
|
|
24a54c2ee0 | ||
|
|
3735ed4476 | ||
|
|
2880aa556b | ||
|
|
a125a871f5 | ||
|
|
d79844cf2b | ||
|
|
d0648411ea | ||
|
|
e74cc63271 | ||
|
|
28f84d3961 | ||
|
|
db184a13a3 | ||
|
|
b65d167937 | ||
|
|
9a770ca576 | ||
|
|
b7c189e6af | ||
|
|
2758f0f698 | ||
|
|
e68ad25cc0 | ||
|
|
c9d300a7b6 | ||
|
|
ed4b773870 | ||
|
|
097e020735 |
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
|
||||||
95
.travis.yml
95
.travis.yml
@@ -12,20 +12,29 @@ language: swift
|
|||||||
osx_image: xcode10.1
|
osx_image: xcode10.1
|
||||||
xcode_project: Path.swift.xcodeproj
|
xcode_project: Path.swift.xcodeproj
|
||||||
xcode_scheme: Path.swift-Package
|
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: |
|
||||||
@@ -46,41 +55,25 @@ jobs:
|
|||||||
sudo: false
|
sudo: false
|
||||||
install: eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
|
install: eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
|
||||||
script: swift test --parallel
|
script: swift test --parallel
|
||||||
|
|
||||||
- <<: *linux
|
- <<: *linux
|
||||||
env: SWIFT_VERSION='5.0-DEVELOPMENT-SNAPSHOT-2019-01-22-a'
|
env: SWIFT_VERSION='5.0'
|
||||||
name: Linux / Swift 5.0.0-dev (2019-01-22)
|
name: Linux / Swift 5.0.0
|
||||||
|
|
||||||
- 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,11 @@ 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: |
|
||||||
before_script: |
|
brew install mxcl/made/swift-sh
|
||||||
export DESCRIPTION=$(swift - <<\ \ EOF
|
curl -O https://raw.githubusercontent.com/mxcl/ops/master/deploy
|
||||||
import Foundation
|
chmod u+x deploy
|
||||||
struct Response: Decodable { let description: String }
|
before_script: ./deploy generate-podspec
|
||||||
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: ./deploy publish-release
|
||||||
|
|||||||
@@ -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
|
|
||||||
]
|
|
||||||
22
README.md
22
README.md
@@ -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
|
||||||
|
|
||||||
@@ -181,7 +181,7 @@ with your work without worries.
|
|||||||
There is also some magic going on in Foundation’s filesystem APIs, which we look
|
There is also some magic going on in Foundation’s filesystem APIs, which we look
|
||||||
for and ensure our API is deterministic, eg. [this test].
|
for and ensure our API is deterministic, eg. [this test].
|
||||||
|
|
||||||
[this test]: TODO
|
[this test]: https://github.com/mxcl/Path.swift/blob/master/Tests/PathTests/PathTests.swift#L539-L554
|
||||||
|
|
||||||
# `Path.swift` is properly cross-platform
|
# `Path.swift` is properly cross-platform
|
||||||
|
|
||||||
@@ -258,9 +258,15 @@ for that as the check was deemed too expensive to be worthwhile.
|
|||||||
equality check is required.
|
equality check is required.
|
||||||
* There are several symlink paths on Mac that are typically automatically
|
* There are several symlink paths on Mac that are typically automatically
|
||||||
resolved by Foundation, eg. `/private`, we attempt to do the same for
|
resolved by Foundation, eg. `/private`, we attempt to do the same for
|
||||||
functions that you would expect it (notably `realpath`), but we do *not* for
|
functions that you would expect it (notably `realpath`), we *do* the same for
|
||||||
`Path.init`, *nor* if you are joining a path that ends up being one of these
|
`Path.init`, but *do not* if you are joining a path that ends up being one of
|
||||||
paths, (eg. `Path.root.join("var/private')`).
|
these paths, (eg. `Path.root.join("var/private')`).
|
||||||
|
|
||||||
|
If a `Path` is a symlink but the destination of the link does not exist `exists`
|
||||||
|
returns `false`. This seems to be the correct thing to do since symlinks are
|
||||||
|
meant to be an abstraction for filesystems. To instead verify that there is
|
||||||
|
no filesystem entry there at all check if `kind` is `nil`.
|
||||||
|
|
||||||
|
|
||||||
## We do not provide change directory functionality
|
## We do not provide change directory functionality
|
||||||
|
|
||||||
@@ -281,7 +287,7 @@ Therefore, if you are not using this feature you are fine. If you have URLs the
|
|||||||
way to get a `Path` is:
|
way to get a `Path` is:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
if let path = Path(url) {
|
if let path = Path(url: url) {
|
||||||
/*…*/
|
/*…*/
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -316,7 +322,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 +334,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`
|
||||||
|
|||||||
@@ -83,4 +83,22 @@ public extension Path {
|
|||||||
#endif
|
#endif
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Kind {
|
||||||
|
case file, symlink, directory
|
||||||
|
}
|
||||||
|
|
||||||
|
var kind: Kind? {
|
||||||
|
var buf = stat()
|
||||||
|
guard lstat(string, &buf) == 0 else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if buf.st_mode & S_IFMT == S_IFLNK {
|
||||||
|
return .symlink
|
||||||
|
} else if buf.st_mode & S_IFMT == S_IFDIR {
|
||||||
|
return .directory
|
||||||
|
} else {
|
||||||
|
return .file
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,11 +25,11 @@ public extension Path {
|
|||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func copy(to: Path, overwrite: Bool = false) throws -> Path {
|
func copy(to: Path, overwrite: Bool = false) throws -> Path {
|
||||||
if overwrite, to.isFile, isFile {
|
if overwrite, let tokind = to.kind, tokind != .directory, kind != .directory {
|
||||||
try FileManager.default.removeItem(at: to.url)
|
try FileManager.default.removeItem(at: to.url)
|
||||||
}
|
}
|
||||||
#if os(Linux) && !swift(>=5.1) // check if fixed
|
#if os(Linux) && !swift(>=5.1) // check if fixed
|
||||||
if !overwrite, to.isFile {
|
if !overwrite, to.kind != nil {
|
||||||
throw CocoaError.error(.fileWriteFileExists)
|
throw CocoaError.error(.fileWriteFileExists)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -61,15 +61,15 @@ public extension Path {
|
|||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func copy(into: Path, overwrite: Bool = false) throws -> Path {
|
func copy(into: Path, overwrite: Bool = false) throws -> Path {
|
||||||
if !into.exists {
|
if into.kind == nil {
|
||||||
try into.mkdir(.p)
|
try into.mkdir(.p)
|
||||||
}
|
}
|
||||||
let rv = into/basename()
|
let rv = into/basename()
|
||||||
if overwrite, rv.isFile {
|
if overwrite, let kind = rv.kind, kind != .directory {
|
||||||
try rv.delete()
|
try FileManager.default.removeItem(at: rv.url)
|
||||||
}
|
}
|
||||||
#if os(Linux) && !swift(>=5.1) // check if fixed
|
#if os(Linux) && !swift(>=5.1) // check if fixed
|
||||||
if !overwrite, rv.isFile {
|
if !overwrite, rv.kind != nil {
|
||||||
throw CocoaError.error(.fileWriteFileExists)
|
throw CocoaError.error(.fileWriteFileExists)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -95,7 +95,7 @@ public extension Path {
|
|||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func move(to: Path, overwrite: Bool = false) throws -> Path {
|
func move(to: Path, overwrite: Bool = false) throws -> Path {
|
||||||
if overwrite, to.isFile {
|
if overwrite, let kind = to.kind, kind != .directory {
|
||||||
try FileManager.default.removeItem(at: to.url)
|
try FileManager.default.removeItem(at: to.url)
|
||||||
}
|
}
|
||||||
try FileManager.default.moveItem(at: url, to: to.url)
|
try FileManager.default.moveItem(at: url, to: to.url)
|
||||||
@@ -119,17 +119,21 @@ public extension Path {
|
|||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func move(into: Path, overwrite: Bool = false) throws -> Path {
|
func move(into: Path, overwrite: Bool = false) throws -> Path {
|
||||||
if !into.exists {
|
switch into.kind {
|
||||||
|
case nil:
|
||||||
try into.mkdir(.p)
|
try into.mkdir(.p)
|
||||||
} else if !into.isDirectory {
|
fallthrough
|
||||||
|
case .directory?:
|
||||||
|
let rv = into/basename()
|
||||||
|
if overwrite, let rvkind = rv.kind, rvkind != .directory {
|
||||||
|
try FileManager.default.removeItem(at: rv.url)
|
||||||
|
}
|
||||||
|
try FileManager.default.moveItem(at: url, to: rv.url)
|
||||||
|
return rv
|
||||||
|
case .file?, .symlink?:
|
||||||
throw CocoaError.error(.fileWriteFileExists)
|
throw CocoaError.error(.fileWriteFileExists)
|
||||||
}
|
}
|
||||||
let rv = into/basename()
|
|
||||||
if overwrite, rv.isFile {
|
|
||||||
try FileManager.default.removeItem(at: rv.url)
|
|
||||||
}
|
|
||||||
try FileManager.default.moveItem(at: url, to: rv.url)
|
|
||||||
return rv
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,11 +142,12 @@ public extension Path {
|
|||||||
∵ *Path.swift* doesn’t error if desired end result preexists.
|
∵ *Path.swift* doesn’t error if desired end result preexists.
|
||||||
- Note: On UNIX will this function will succeed if the parent directory is writable and the current user has permission.
|
- Note: On UNIX will this function will succeed if the parent directory is writable and the current user has permission.
|
||||||
- Note: This function will fail if the file or directory is “locked”
|
- Note: This function will fail if the file or directory is “locked”
|
||||||
|
- Note: If entry is a symlink, deletes the symlink.
|
||||||
- SeeAlso: `lock()`
|
- SeeAlso: `lock()`
|
||||||
*/
|
*/
|
||||||
@inlinable
|
@inlinable
|
||||||
func delete() throws {
|
func delete() throws {
|
||||||
if exists {
|
if kind != nil {
|
||||||
try FileManager.default.removeItem(at: url)
|
try FileManager.default.removeItem(at: url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,7 +159,7 @@ public extension Path {
|
|||||||
@inlinable
|
@inlinable
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func touch() throws -> Path {
|
func touch() throws -> Path {
|
||||||
if !exists {
|
if kind == nil {
|
||||||
guard FileManager.default.createFile(atPath: string, contents: nil) else {
|
guard FileManager.default.createFile(atPath: string, contents: nil) else {
|
||||||
throw CocoaError.error(.fileWriteUnknown)
|
throw CocoaError.error(.fileWriteUnknown)
|
||||||
}
|
}
|
||||||
@@ -228,14 +233,17 @@ public extension Path {
|
|||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func symlink(into dir: Path) throws -> Path {
|
func symlink(into dir: Path) throws -> Path {
|
||||||
if !dir.exists {
|
switch dir.kind {
|
||||||
|
case nil, .symlink?:
|
||||||
try dir.mkdir(.p)
|
try dir.mkdir(.p)
|
||||||
} else if !dir.isDirectory {
|
fallthrough
|
||||||
|
case .directory?:
|
||||||
|
let dst = dir/basename()
|
||||||
|
try FileManager.default.createSymbolicLink(atPath: dst.string, withDestinationPath: string)
|
||||||
|
return dst
|
||||||
|
case .file?:
|
||||||
throw CocoaError.error(.fileWriteFileExists)
|
throw CocoaError.error(.fileWriteFileExists)
|
||||||
}
|
}
|
||||||
let dst = dir/basename()
|
|
||||||
try FileManager.default.createSymbolicLink(atPath: dst.string, withDestinationPath: string)
|
|
||||||
return dst
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ import Darwin
|
|||||||
public extension Path {
|
public extension Path {
|
||||||
//MARK: Filesystem Properties
|
//MARK: Filesystem Properties
|
||||||
|
|
||||||
/// Returns true if the path represents an actual filesystem entry.
|
/**
|
||||||
|
- Returns: `true` if the path represents an actual filesystem entry.
|
||||||
|
- Note: If `self` is a symlink the return value represents the destination.
|
||||||
|
*/
|
||||||
var exists: Bool {
|
var exists: Bool {
|
||||||
return FileManager.default.fileExists(atPath: string)
|
return FileManager.default.fileExists(atPath: string)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -78,43 +92,33 @@ public struct Path: Equatable, Hashable, Comparable {
|
|||||||
tilded = dir
|
tilded = dir
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if username != NSUserName() {
|
return nil // there are no usernames on iOS, etc.
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
tilded = NSHomeDirectory()
|
|
||||||
}
|
|
||||||
#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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public init?(_ url: URL) {
|
/**
|
||||||
|
Creates a new absolute, standardized path from the provided file-scheme URL.
|
||||||
|
- Note: If the URL is not a file URL, returns `nil`.
|
||||||
|
*/
|
||||||
|
public init?(url: URL) {
|
||||||
guard url.scheme == "file" else { return nil }
|
guard url.scheme == "file" else { return nil }
|
||||||
self.init(string: url.path)
|
self.init(url.path)
|
||||||
//NOTE: URL cannot be a file-reference url, unlike NSURL, so this always works
|
//NOTE: URL cannot be a file-reference url, unlike NSURL, so this always works
|
||||||
}
|
}
|
||||||
|
|
||||||
public init?(_ url: NSURL) {
|
/**
|
||||||
|
Creates a new absolute, standardized path from the provided file-scheme URL.
|
||||||
|
- Note: If the URL is not a file URL, returns `nil`.
|
||||||
|
- Note: If the URL is a file reference URL, converts it to a POSIX path first.
|
||||||
|
*/
|
||||||
|
public init?(url: NSURL) {
|
||||||
guard url.scheme == "file", let path = url.path else { return nil }
|
guard url.scheme == "file", let path = url.path else { return nil }
|
||||||
self.init(string: path)
|
self.init(string: path)
|
||||||
// ^^ works even if the url is a file-reference url
|
// ^^ works even if the url is a file-reference url
|
||||||
@@ -195,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
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -69,9 +69,18 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual((Path.root/"tmp/foo/bar").relative(to: .root/"tmp/baz"), "../foo/bar")
|
XCTAssertEqual((Path.root/"tmp/foo/bar").relative(to: .root/"tmp/baz"), "../foo/bar")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testExists() {
|
func testExists() throws {
|
||||||
XCTAssert(Path.root.exists)
|
XCTAssert(Path.root.exists)
|
||||||
XCTAssert((Path.root/"bin").exists)
|
XCTAssert((Path.root/"bin").exists)
|
||||||
|
|
||||||
|
try Path.mktemp { tmpdir in
|
||||||
|
XCTAssertTrue(tmpdir.exists)
|
||||||
|
XCTAssertFalse(try tmpdir.bar.symlink(as: tmpdir.foo).exists)
|
||||||
|
XCTAssertTrue(tmpdir.foo.kind == .symlink)
|
||||||
|
XCTAssertTrue(try tmpdir.bar.touch().symlink(as: tmpdir.baz).exists)
|
||||||
|
XCTAssertTrue(tmpdir.bar.kind == .file)
|
||||||
|
XCTAssertTrue(tmpdir.kind == .directory)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIsDirectory() {
|
func testIsDirectory() {
|
||||||
@@ -379,6 +388,21 @@ class PathTests: XCTestCase {
|
|||||||
#if !os(Linux)
|
#if !os(Linux)
|
||||||
XCTAssertThrowsError(try tmpdir.bar3.touch().lock().delete())
|
XCTAssertThrowsError(try tmpdir.bar3.touch().lock().delete())
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// regression test: can delete a symlink that points to a non-existent file
|
||||||
|
let bar5 = try tmpdir.bar4.symlink(as: tmpdir.bar5)
|
||||||
|
XCTAssertEqual(bar5.kind, .symlink)
|
||||||
|
XCTAssertFalse(bar5.exists)
|
||||||
|
XCTAssertNoThrow(try bar5.delete())
|
||||||
|
XCTAssertEqual(bar5.kind, nil)
|
||||||
|
|
||||||
|
// test that deleting a symlink *only* deletes the symlink
|
||||||
|
let bar7 = try tmpdir.bar6.touch().symlink(as: tmpdir.bar7)
|
||||||
|
XCTAssertEqual(bar7.kind, .symlink)
|
||||||
|
XCTAssertTrue(bar7.exists)
|
||||||
|
XCTAssertNoThrow(try bar7.delete())
|
||||||
|
XCTAssertEqual(bar7.kind, nil)
|
||||||
|
XCTAssertEqual(tmpdir.bar6.kind, .file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,6 +427,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)
|
||||||
@@ -577,10 +602,10 @@ class PathTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testURLInitializer() throws {
|
func testURLInitializer() throws {
|
||||||
XCTAssertEqual(Path(Path.home.url), Path.home)
|
XCTAssertEqual(Path(url: Path.home.url), Path.home)
|
||||||
XCTAssertEqual(Path.home.fileReferenceURL.flatMap(Path.init), Path.home)
|
XCTAssertEqual(Path.home.fileReferenceURL.flatMap(Path.init), Path.home)
|
||||||
XCTAssertNil(Path(URL(string: "https://foo.com")!))
|
XCTAssertNil(Path(url: URL(string: "https://foo.com")!))
|
||||||
XCTAssertNil(Path(NSURL(string: "https://foo.com")!))
|
XCTAssertNil(Path(url: NSURL(string: "https://foo.com")!))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInitializerForRelativePath() throws {
|
func testInitializerForRelativePath() throws {
|
||||||
@@ -588,4 +613,29 @@ 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testKind() throws {
|
||||||
|
try Path.mktemp { tmpdir in
|
||||||
|
let foo = try tmpdir.foo.touch()
|
||||||
|
let bar = try foo.symlink(as: tmpdir.bar)
|
||||||
|
XCTAssertEqual(tmpdir.kind, .directory)
|
||||||
|
XCTAssertEqual(foo.kind, .file)
|
||||||
|
XCTAssertEqual(bar.kind, .symlink)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,15 +23,18 @@ 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),
|
||||||
|
("testKind", testKind),
|
||||||
("testLock", testLock),
|
("testLock", testLock),
|
||||||
("testMkpathIfExists", testMkpathIfExists),
|
("testMkpathIfExists", testMkpathIfExists),
|
||||||
("testMktemp", testMktemp),
|
("testMktemp", testMktemp),
|
||||||
("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 +54,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