Compare commits

..

2 Commits

Author SHA1 Message Date
repo-ranger[bot]
ab9a70e947 Merge pull request #25 from mxcl/rename
Refactor rename -> rename(to:)
2019-01-31 15:32:59 +00:00
Max Howell
49ef073e34 Refactor rename -> rename(to:) 2019-01-31 10:15:39 -05:00
3 changed files with 14 additions and 7 deletions

View File

@@ -175,8 +175,16 @@ public extension Path {
return self return self
} }
/**
Renames the file at path.
Path.root.foo.bar.rename(to: "baz") // => /foo/baz
- Parameter to: the new basename for the file
- Returns: The renamed path.
*/
@discardableResult @discardableResult
func rename(_ newname: String) throws -> Path { func rename(to newname: String) throws -> Path {
let newpath = parent/newname let newpath = parent/newname
try FileManager.default.moveItem(atPath: string, toPath: newpath.string) try FileManager.default.moveItem(atPath: string, toPath: newpath.string)
return newpath return newpath

View File

@@ -9,7 +9,7 @@ import Foundation
`Path` supports `Codable`, and can be configured to `Path` supports `Codable`, and can be configured to
[encode paths *relatively*](https://github.com/mxcl/Path.swift/#codable). [encode paths *relatively*](https://github.com/mxcl/Path.swift/#codable).
Sorting a `Sequence` of `Path`s will return the locale-aware sort order, which Sorting a `Sequence` of paths will return the locale-aware sort order, which
will give you the same order as Finder. will give you the same order as Finder.
Converting from a `String` is a common first step, here are the recommended Converting from a `String` is a common first step, here are the recommended
@@ -20,13 +20,12 @@ import Foundation
let p3 = Path.cwd/relativePathString let p3 = Path.cwd/relativePathString
let p4 = Path(userInput) ?? Path.cwd/userInput let p4 = Path(userInput) ?? Path.cwd/userInput
If you are constructing Paths from static-strings we provide support for If you are constructing paths from static-strings we provide support for
dynamic members: dynamic members:
let p1 = Path.root.usr.bin.ls // => /usr/bin/ls let p1 = Path.root.usr.bin.ls // => /usr/bin/ls
- Note: There may not be an actual filesystem entry at the path. The underlying - Note: A `Path` does not necessarily represent an actual filesystem entry.
representation for `Path` is `String`.
*/ */
@dynamicMemberLookup @dynamicMemberLookup

View File

@@ -194,13 +194,13 @@ class PathTests: XCTestCase {
try Path.mktemp { root in try Path.mktemp { root in
do { do {
let file = try root.bar.touch() let file = try root.bar.touch()
let foo = try file.rename("foo") let foo = try file.rename(to: "foo")
XCTAssertFalse(file.exists) XCTAssertFalse(file.exists)
XCTAssertTrue(foo.isFile) XCTAssertTrue(foo.isFile)
} }
do { do {
let file = try root.bar.touch() let file = try root.bar.touch()
XCTAssertThrowsError(try file.rename("foo")) XCTAssertThrowsError(try file.rename(to: "foo"))
} }
} }
} }