Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f99e7b5ae7 | ||
|
|
9553968d66 | ||
|
|
3541c6ec8d | ||
|
|
b4c92f86dc |
32
.travis.yml
32
.travis.yml
@@ -1,14 +1,38 @@
|
||||
# only run for: merge commits, releases and pull-requests
|
||||
if: type != push OR branch = master OR branch =~ /^\d+\.\d+(\.\d+)?(-\S*)?$/
|
||||
|
||||
os: osx
|
||||
language: swift
|
||||
osx_image: xcode10.1
|
||||
xcode_project: Path.swift.xcodeproj
|
||||
xcode_scheme: Path.swift-Package
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- os: osx
|
||||
language: swift
|
||||
osx_image: xcode10.1
|
||||
script: swift test
|
||||
- script: swift test
|
||||
name: macOS
|
||||
|
||||
- &xcodebuild
|
||||
before_install: swift package generate-xcodeproj
|
||||
xcode_destination: platform=iOS Simulator,OS=latest,name=iPhone XS
|
||||
name: iOS
|
||||
- <<: *xcodebuild
|
||||
xcode_destination: platform=tvOS Simulator,OS=latest,name=Apple TV
|
||||
name: tvOS
|
||||
- <<: *xcodebuild
|
||||
name: watchOS
|
||||
script: |
|
||||
set -o pipefail
|
||||
xcodebuild \
|
||||
-project Path.swift.xcodeproj \
|
||||
-scheme Path.swift-Package \
|
||||
-destination 'platform=watchOS Simulator,OS=latest,name=Apple Watch Series 4 - 40mm' \
|
||||
build | xcpretty
|
||||
|
||||
|
||||
- env: SWIFT_VERSION=4.2.1
|
||||
os: linux
|
||||
name: Linux
|
||||
language: generic
|
||||
dist: trusty
|
||||
sudo: false
|
||||
|
||||
20
README.md
20
README.md
@@ -1,9 +1,11 @@
|
||||
# Path.swift
|
||||
# Path.swift ![badge-platforms] ![badge-languages] [](https://travis-ci.com/mxcl/Path.swift)
|
||||
|
||||
A file-system pathing library focused on developer experience and robust
|
||||
end‐results.
|
||||
|
||||
```swift
|
||||
import Path
|
||||
|
||||
// convenient static members
|
||||
let home = Path.home
|
||||
|
||||
@@ -26,7 +28,7 @@ try Path.root.join("foo").copy(into: Path.root.mkdir("bar"))
|
||||
// were meant to be directory destinations
|
||||
```
|
||||
|
||||
Paths are just string representations, there *may not* be a real file there.
|
||||
Paths are just string representations, there *might not* be a real file there.
|
||||
|
||||
# Support mxcl
|
||||
|
||||
@@ -112,16 +114,16 @@ We provide `ls()`, called because it behaves like the Terminal `ls` function,
|
||||
the name thus implies its behavior, ie. that it is not recursive.
|
||||
|
||||
```swift
|
||||
for path in Path.home.ls() {
|
||||
print(path.path)
|
||||
print(path.kind) // .directory or .file
|
||||
for entry in Path.home.ls() {
|
||||
print(entry.path)
|
||||
print(entry.kind) // .directory or .file
|
||||
}
|
||||
|
||||
for path in Path.home.ls() where path.kind == .file {
|
||||
for entry in Path.home.ls() where entry.kind == .file {
|
||||
//…
|
||||
}
|
||||
|
||||
for path in Path.home.ls() where path.mtime > yesterday {
|
||||
for entry in Path.home.ls() where entry.path.mtime > yesterday {
|
||||
//…
|
||||
}
|
||||
|
||||
@@ -143,3 +145,7 @@ package.append(.package(url: "https://github.com/mxcl/Path.swift", from: "0.0.0"
|
||||
### Get push notifications for new releases
|
||||
|
||||
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-languages]: https://img.shields.io/badge/swift-4.2-orange.svg
|
||||
|
||||
@@ -47,7 +47,7 @@ public extension Data {
|
||||
func write(to: Path, atomically: Bool = false) throws -> Path {
|
||||
let opts: NSData.WritingOptions
|
||||
if atomically {
|
||||
#if os(macOS)
|
||||
#if !os(Linux)
|
||||
opts = .atomicWrite
|
||||
#else
|
||||
opts = .atomic
|
||||
|
||||
@@ -73,24 +73,37 @@ public extension Path {
|
||||
return try "".write(to: self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public func mkdir() throws -> Path {
|
||||
private func _foo(go: () throws -> Void) throws {
|
||||
#if !os(Linux)
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
|
||||
try go()
|
||||
} catch CocoaError.Code.fileWriteFileExists {
|
||||
// noop
|
||||
}
|
||||
#else
|
||||
do {
|
||||
try go()
|
||||
} catch {
|
||||
let error = error as NSError
|
||||
guard error.domain == NSCocoaErrorDomain, error.code == CocoaError.Code.fileWriteFileExists.rawValue else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func mkdir() throws -> Path {
|
||||
try _foo {
|
||||
try FileManager.default.createDirectory(at: self.url, withIntermediateDirectories: false, attributes: nil)
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public func mkpath() throws -> Path {
|
||||
do {
|
||||
try _foo {
|
||||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
|
||||
} catch CocoaError.Code.fileWriteFileExists {
|
||||
// noop
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
@@ -4,17 +4,40 @@ public class TemporaryDirectory {
|
||||
public let url: URL
|
||||
public var path: Path { return Path(string: url.path) }
|
||||
|
||||
public init() throws {
|
||||
#if os(macOS)
|
||||
url = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), create: true)
|
||||
#else
|
||||
/**
|
||||
Creates a new temporary directory.
|
||||
|
||||
The directory is recursively deleted when this object deallocates.
|
||||
|
||||
If you need a temporary directory on a specific volume use the `appropriateFor`
|
||||
parameter.
|
||||
|
||||
- Important: If you are moving a file, ensure to use the `appropriateFor`
|
||||
parameter, since it is volume aware and moving the file across volumes will take
|
||||
exponentially longer!
|
||||
- Important: The `appropriateFor` parameter does not work on Linux.
|
||||
- Parameter appropriateFor: The temporary directory will be located on this
|
||||
volume.
|
||||
*/
|
||||
public init(appropriateFor: URL? = nil) throws {
|
||||
#if !os(Linux)
|
||||
let appropriate: URL
|
||||
if let appropriateFor = appropriateFor {
|
||||
appropriate = appropriateFor
|
||||
} else if #available(OSX 10.12, iOS 10, tvOS 10, watchOS 3, *) {
|
||||
appropriate = FileManager.default.temporaryDirectory
|
||||
} else {
|
||||
appropriate = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
}
|
||||
url = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: appropriate, create: true)
|
||||
#else
|
||||
let envs = ProcessInfo.processInfo.environment
|
||||
let env = envs["TMPDIR"] ?? envs["TEMP"] ?? envs["TMP"] ?? "/tmp"
|
||||
let dir = Path.root/env/"swift-sh.XXXXXX"
|
||||
var template = [UInt8](dir.string.utf8).map({ Int8($0) }) + [Int8(0)]
|
||||
guard mkdtemp(&template) != nil else { throw CocoaError.error(.featureUnsupported) }
|
||||
url = URL(fileURLWithPath: String(cString: template))
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
||||
@@ -37,12 +37,12 @@ class PathTests: XCTestCase {
|
||||
|
||||
func testExists() {
|
||||
XCTAssert(Path.root.exists)
|
||||
XCTAssert((Path.root/"Users").exists)
|
||||
XCTAssert((Path.root/"bin").exists)
|
||||
}
|
||||
|
||||
func testIsDirectory() {
|
||||
XCTAssert(Path.root.isDirectory)
|
||||
XCTAssert((Path.root/"Users").isDirectory)
|
||||
XCTAssert((Path.root/"bin").isDirectory)
|
||||
}
|
||||
|
||||
func testMktemp() throws {
|
||||
|
||||
@@ -15,7 +15,7 @@ extension PathTests {
|
||||
]
|
||||
}
|
||||
|
||||
#if !os(macOS)
|
||||
#if os(Linux)
|
||||
public func __allTests() -> [XCTestCaseEntry] {
|
||||
return [
|
||||
testCase(PathTests.__allTests),
|
||||
|
||||
Reference in New Issue
Block a user