Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f99e7b5ae7 | ||
|
|
9553968d66 | ||
|
|
3541c6ec8d | ||
|
|
b4c92f86dc | ||
|
|
cac06d89fb | ||
|
|
4af0ee3983 |
40
.travis.yml
Normal file
40
.travis.yml
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# 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:
|
||||||
|
- 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
|
||||||
|
install: eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
|
||||||
|
script: swift test
|
||||||
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
|
A file-system pathing library focused on developer experience and robust
|
||||||
end‐results.
|
end‐results.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
|
import Path
|
||||||
|
|
||||||
// convenient static members
|
// convenient static members
|
||||||
let home = Path.home
|
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
|
// 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
|
# 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.
|
the name thus implies its behavior, ie. that it is not recursive.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
for path in Path.home.ls() {
|
for entry in Path.home.ls() {
|
||||||
print(path.path)
|
print(entry.path)
|
||||||
print(path.kind) // .directory or .file
|
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
|
### Get push notifications for new releases
|
||||||
|
|
||||||
https://codebasesaga.com/canopy/
|
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
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ public extension Bundle {
|
|||||||
public var resources: Path? {
|
public var resources: Path? {
|
||||||
return resourcePath.flatMap(Path.init)
|
return resourcePath.flatMap(Path.init)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var path: Path {
|
||||||
|
return Path(string: bundlePath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension String {
|
public extension String {
|
||||||
@@ -43,7 +47,11 @@ public extension Data {
|
|||||||
func write(to: Path, atomically: Bool = false) throws -> Path {
|
func write(to: Path, atomically: Bool = false) throws -> Path {
|
||||||
let opts: NSData.WritingOptions
|
let opts: NSData.WritingOptions
|
||||||
if atomically {
|
if atomically {
|
||||||
|
#if !os(Linux)
|
||||||
opts = .atomicWrite
|
opts = .atomicWrite
|
||||||
|
#else
|
||||||
|
opts = .atomic
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
opts = []
|
opts = []
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,15 @@ public extension Path {
|
|||||||
return rv
|
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
|
@discardableResult
|
||||||
public func move(into: Path) throws -> Path {
|
public func move(into: Path) throws -> Path {
|
||||||
if !into.exists {
|
if !into.exists {
|
||||||
@@ -48,8 +57,9 @@ public extension Path {
|
|||||||
} else if !into.isDirectory {
|
} else if !into.isDirectory {
|
||||||
throw CocoaError.error(.fileWriteFileExists)
|
throw CocoaError.error(.fileWriteFileExists)
|
||||||
}
|
}
|
||||||
try FileManager.default.moveItem(at: url, to: into.join(basename()).url)
|
let rv = into/basename()
|
||||||
return self
|
try FileManager.default.moveItem(at: url, to: rv.url)
|
||||||
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
@@ -63,24 +73,37 @@ public extension Path {
|
|||||||
return try "".write(to: self)
|
return try "".write(to: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
private func _foo(go: () throws -> Void) throws {
|
||||||
@discardableResult
|
#if !os(Linux)
|
||||||
public func mkdir() throws -> Path {
|
|
||||||
do {
|
do {
|
||||||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
|
try go()
|
||||||
} catch CocoaError.Code.fileWriteFileExists {
|
} catch CocoaError.Code.fileWriteFileExists {
|
||||||
// noop
|
// 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
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public func mkpath() throws -> Path {
|
public func mkpath() throws -> Path {
|
||||||
do {
|
try _foo {
|
||||||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
|
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
|
||||||
} catch CocoaError.Code.fileWriteFileExists {
|
|
||||||
// noop
|
|
||||||
}
|
}
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ public extension Path {
|
|||||||
return FileManager.default.fileExists(atPath: string, isDirectory: &isDir) && !isDir.boolValue
|
return FileManager.default.fileExists(atPath: string, isDirectory: &isDir) && !isDir.boolValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isExecutable: Bool {
|
||||||
|
return FileManager.default.isExecutableFile(atPath: string)
|
||||||
|
}
|
||||||
|
|
||||||
var exists: Bool {
|
var exists: Bool {
|
||||||
return FileManager.default.fileExists(atPath: string)
|
return FileManager.default.fileExists(atPath: string)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,40 @@ public class TemporaryDirectory {
|
|||||||
public let url: URL
|
public let url: URL
|
||||||
public var path: Path { return Path(string: url.path) }
|
public var path: Path { return Path(string: url.path) }
|
||||||
|
|
||||||
public init() throws {
|
/**
|
||||||
url = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), create: true)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
|
|||||||
@@ -37,12 +37,12 @@ class PathTests: XCTestCase {
|
|||||||
|
|
||||||
func testExists() {
|
func testExists() {
|
||||||
XCTAssert(Path.root.exists)
|
XCTAssert(Path.root.exists)
|
||||||
XCTAssert((Path.root/"Users").exists)
|
XCTAssert((Path.root/"bin").exists)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIsDirectory() {
|
func testIsDirectory() {
|
||||||
XCTAssert(Path.root.isDirectory)
|
XCTAssert(Path.root.isDirectory)
|
||||||
XCTAssert((Path.root/"Users").isDirectory)
|
XCTAssert((Path.root/"bin").isDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMktemp() throws {
|
func testMktemp() throws {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ extension PathTests {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !os(macOS)
|
#if os(Linux)
|
||||||
public func __allTests() -> [XCTestCaseEntry] {
|
public func __allTests() -> [XCTestCaseEntry] {
|
||||||
return [
|
return [
|
||||||
testCase(PathTests.__allTests),
|
testCase(PathTests.__allTests),
|
||||||
|
|||||||
Reference in New Issue
Block a user