Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4c92f86dc | ||
|
|
cac06d89fb | ||
|
|
4af0ee3983 |
16
.travis.yml
Normal file
16
.travis.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
# only run for: merge commits, releases and pull-requests
|
||||
if: type != push OR branch = master OR branch =~ /^\d+\.\d+(\.\d+)?(-\S*)?$/
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- os: osx
|
||||
language: swift
|
||||
osx_image: xcode10.1
|
||||
script: swift test
|
||||
- env: SWIFT_VERSION=4.2.1
|
||||
os: linux
|
||||
language: generic
|
||||
dist: trusty
|
||||
sudo: false
|
||||
install: eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
|
||||
script: swift test
|
||||
@@ -4,6 +4,8 @@ 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
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@ public extension Bundle {
|
||||
public var resources: Path? {
|
||||
return resourcePath.flatMap(Path.init)
|
||||
}
|
||||
|
||||
public var path: Path {
|
||||
return Path(string: bundlePath)
|
||||
}
|
||||
}
|
||||
|
||||
public extension String {
|
||||
@@ -43,7 +47,11 @@ public extension Data {
|
||||
func write(to: Path, atomically: Bool = false) throws -> Path {
|
||||
let opts: NSData.WritingOptions
|
||||
if atomically {
|
||||
#if !os(Linux)
|
||||
opts = .atomicWrite
|
||||
#else
|
||||
opts = .atomic
|
||||
#endif
|
||||
} else {
|
||||
opts = []
|
||||
}
|
||||
|
||||
@@ -41,6 +41,15 @@ public extension Path {
|
||||
return rv
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func move(to: Path, overwrite: Bool = false) throws -> Path {
|
||||
if overwrite, to.exists {
|
||||
try FileManager.default.removeItem(at: to.url)
|
||||
}
|
||||
try FileManager.default.moveItem(at: url, to: to.url)
|
||||
return to
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func move(into: Path) throws -> Path {
|
||||
if !into.exists {
|
||||
@@ -48,8 +57,9 @@ public extension Path {
|
||||
} else if !into.isDirectory {
|
||||
throw CocoaError.error(.fileWriteFileExists)
|
||||
}
|
||||
try FileManager.default.moveItem(at: url, to: into.join(basename()).url)
|
||||
return self
|
||||
let rv = into/basename()
|
||||
try FileManager.default.moveItem(at: url, to: rv.url)
|
||||
return rv
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -63,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
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@ public extension Path {
|
||||
return FileManager.default.fileExists(atPath: string, isDirectory: &isDir) && !isDir.boolValue
|
||||
}
|
||||
|
||||
var isExecutable: Bool {
|
||||
return FileManager.default.isExecutableFile(atPath: string)
|
||||
}
|
||||
|
||||
var exists: Bool {
|
||||
return FileManager.default.fileExists(atPath: string)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,16 @@ public class TemporaryDirectory {
|
||||
public var path: Path { return Path(string: url.path) }
|
||||
|
||||
public init() throws {
|
||||
#if !os(Linux)
|
||||
url = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), 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
|
||||
}
|
||||
|
||||
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