Remove Entry since it is barely worthwhile sugar

This commit is contained in:
Max Howell
2019-07-21 15:49:08 -04:00
parent 621d1b0160
commit 62073d584b
3 changed files with 28 additions and 53 deletions

View File

@@ -151,24 +151,25 @@ the name thus implies its behavior, ie. that it is not recursive and doesnt
list hidden files.
```swift
for entry in Path.home.ls() {
print(entry.path)
print(entry.kind) // .directory or .file
}
for entry in Path.home.ls() where entry.kind == .file {
for path in Path.home.ls() {
//
}
for entry in Path.home.ls() where entry.path.mtime > yesterday {
for path in Path.home.ls() where path.isFile {
//
}
for path in Path.home.ls() where path.mtime > yesterday {
//
}
let dirs = Path.home.ls().directories
// ^^ directories that *exist*
let files = Path.home.ls().files
// ^^ files that both *exist* and are *not* directories
let swiftFiles = Path.home.ls().files(withExtension: "swift")
let swiftFiles = Path.home.ls().files.filter{ $0.extension == "swift" }
```
We provide `find()` for recursive listing:

View File

@@ -1,24 +1,6 @@
import Foundation
public extension Path {
/**
A file entry from a directory listing.
- SeeAlso: `ls()`
*/
struct Entry {
/// The kind of this directory entry.
public enum Kind {
/// The path is a file.
case file
/// The path is a directory.
case directory
}
/// The kind of this entry.
public let kind: Kind
/// The path of this entry.
public let path: Path
}
class Finder {
fileprivate init(path: Path) {
self.path = path
@@ -110,7 +92,7 @@ public extension Pathish {
- Parameter options: Configure the listing.
- Important: On Linux the listing is always `ls -a`
*/
func ls(_ options: ListDirectoryOptions? = nil) -> [Path.Entry] {
func ls(_ options: ListDirectoryOptions? = nil) -> [Path] {
guard let urls = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) else {
fputs("warning: could not list: \(self)\n", stderr)
return []
@@ -119,7 +101,7 @@ public extension Pathish {
guard let path = Path(url.path) else { return nil }
if options != .a, path.basename().hasPrefix(".") { return nil }
// ^^ we dont use the Foundation `skipHiddenFiles` because it considers weird things hidden and we are mirroring `ls`
return .init(kind: path.isDirectory ? .directory : .file, path: path)
return path
}
}
@@ -128,26 +110,20 @@ public extension Pathish {
}
}
/// Convenience functions for the array return value of `Path.ls()`
public extension Array where Element == Path.Entry {
/// Filters the list of entries to be a list of Paths that are directories.
/// Convenience functions for the arraies of `Path`
public extension Array where Element == Path {
/// Filters the list of entries to be a list of Paths that are directories. Symlinks to directories are not returned.
var directories: [Path] {
return compactMap {
$0.kind == .directory ? $0.path : nil
return filter {
$0.isDirectory
}
}
/// Filters the list of entries to be a list of Paths that are files.
/// Filters the list of entries to be a list of Paths that exist and are *not* directories. Thus expect symlinks, etc.
/// - Note: symlinks that point to files that do not exist are *not* returned.
var files: [Path] {
return compactMap {
$0.kind == .file ? $0.path : nil
}
}
/// Filters the list of entries to be a list of Paths that are files with the specified extension.
func files(withExtension ext: String) -> [Path] {
return compactMap {
$0.kind == .file && $0.path.extension == ext ? $0.path : nil
return filter {
$0.exists && !$0.isDirectory
}
}
}

View File

@@ -28,24 +28,23 @@ class PathTests: XCTestCase {
var paths = Set<String>()
let lsrv = tmpdir.ls(.a)
var dirs = 0
for entry in lsrv {
if entry.kind == .directory {
for path in lsrv {
if path.isDirectory {
dirs += 1
}
paths.insert(entry.path.basename())
paths.insert(path.basename())
}
XCTAssertEqual(dirs, 2)
XCTAssertEqual(dirs, lsrv.directories.count)
XCTAssertEqual(["a", ".d"], Set(lsrv.directories.map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(["b.swift", "c"], Set(lsrv.files.map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(["b.swift"], Set(lsrv.files(withExtension: "swift").map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(["c"], Set(lsrv.files(withExtension: "").map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(["b.swift"], Set(lsrv.files.filter{ $0.extension == "swift" }.map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(["c"], Set(lsrv.files.filter{ $0.extension == "" }.map{ $0.relative(to: tmpdir) }))
XCTAssertEqual(paths, ["a", "b.swift", "c", ".d"])
}
func testEnumerationSkippingHiddenFiles() throws {
#if !os(Linux)
let tmpdir_ = try TemporaryDirectory()
let tmpdir = tmpdir_.path
try tmpdir.join("a").mkdir().join("c").touch()
@@ -55,15 +54,14 @@ class PathTests: XCTestCase {
var paths = Set<String>()
var dirs = 0
for entry in tmpdir.ls() {
if entry.kind == .directory {
for path in tmpdir.ls() {
if path.isDirectory {
dirs += 1
}
paths.insert(entry.path.basename())
paths.insert(path.basename())
}
XCTAssertEqual(dirs, 1)
XCTAssertEqual(paths, ["a", "b", "c"])
#endif
}
func testRelativeTo() {