From 49ef073e34eceaef3e1f250474c311e4bb9613bd Mon Sep 17 00:00:00 2001 From: Max Howell Date: Thu, 31 Jan 2019 10:15:34 -0500 Subject: [PATCH] Refactor rename -> rename(to:) --- Sources/Path+FileManager.swift | 10 +++++++++- Sources/Path.swift | 7 +++---- Tests/PathTests/PathTests.swift | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Sources/Path+FileManager.swift b/Sources/Path+FileManager.swift index 2e3484a..b464571 100644 --- a/Sources/Path+FileManager.swift +++ b/Sources/Path+FileManager.swift @@ -175,8 +175,16 @@ public extension Path { 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 - func rename(_ newname: String) throws -> Path { + func rename(to newname: String) throws -> Path { let newpath = parent/newname try FileManager.default.moveItem(atPath: string, toPath: newpath.string) return newpath diff --git a/Sources/Path.swift b/Sources/Path.swift index 80bbc36..2c993a2 100644 --- a/Sources/Path.swift +++ b/Sources/Path.swift @@ -9,7 +9,7 @@ import Foundation `Path` supports `Codable`, and can be configured to [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. 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 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: let p1 = Path.root.usr.bin.ls // => /usr/bin/ls - - Note: There may not be an actual filesystem entry at the path. The underlying - representation for `Path` is `String`. + - Note: A `Path` does not necessarily represent an actual filesystem entry. */ @dynamicMemberLookup diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 59784a7..e771880 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -194,13 +194,13 @@ class PathTests: XCTestCase { try Path.mktemp { root in do { let file = try root.bar.touch() - let foo = try file.rename("foo") + let foo = try file.rename(to: "foo") XCTAssertFalse(file.exists) XCTAssertTrue(foo.isFile) } do { let file = try root.bar.touch() - XCTAssertThrowsError(try file.rename("foo")) + XCTAssertThrowsError(try file.rename(to: "foo")) } } }