Skip to content

Commit 3a71f95

Browse files
committed
Readme: fix examples, add summary of why this library exists.
1 parent dccc8f2 commit 3a71f95

File tree

2 files changed

+7
-89
lines changed

2 files changed

+7
-89
lines changed

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
A strongly typed Swift library for working with local files and directories.
66

7-
Because when working with paths you almost always know which are for directories and which are for files, so why not let the compiler in on it too?
7+
It differentiates between file paths and directory paths, and between paths and actual files and directories, because the programmer knows which are which and when the compiler knows it too it can be much more helpful.
88

9-
Still a work in progress and has not been properly tested, so use at your own risk. This readme is also clearly far from finished.
10-
11-
[API documentation](https://kareman.github.io/FileSmith/)
9+
See also:
10+
- [API documentation](https://kareman.github.io/FileSmith/)
1211

1312
## Terms
1413

@@ -69,7 +68,7 @@ var dir2 = try Directory(create: "dir/dir2", ifExists: .throwError)
6968
var dir3 = try dir2.create(directory: "dir3", ifExists: .open) // dir/dir2/dir3
7069

7170
var file1_edit = try filepath.create(ifExists: .open)
72-
let file2_edit = try EditableFile(create: "file2.txt", ifExists: .open)
71+
let file2_edit = try WritableFile(create: "file2.txt", ifExists: .open)
7372
let file3_edit = try dir1.create(file: "file3.txt", ifExists: .open) // dir/dir1/file3
7473
```
7574

@@ -81,7 +80,7 @@ dir2 = try Directory(open: "dir/dir2")
8180
dir3 = try dir2.open(directory: "dir3")
8281

8382
let file1 = try filepath.open()
84-
let file2 = try File(open: "file2.txt")
83+
let file2 = try ReadableFile(open: "file2.txt")
8584
let file3 = try dir1.open(file: "file3.txt")
8685
```
8786

@@ -114,8 +113,8 @@ Directory.current.directories(recursive: true) // [dir, dir/dir1, dir/dir2, dir/
114113
```swift
115114
let dir1_link = try Directory(createSymbolicLink: "dir1_link", to: dir1, ifExists: .open)
116115
let dir2_link = try dir1.create(symbolicLink: "dir2_link", to: dir2, ifExists: .open)
117-
let file1_link = try File(createSymbolicLink: "file1_link", to: file1, ifExists: .open)
118-
let file2_link = try dir2.create(symbolicLink: "file2_link", to: file2, ifExists: .open)
116+
let file1_link = try ReadableFile(createSymbolicLink: "file1_link", to: file1, ifExists: .open)
117+
let file2_link = try dir2.create(symbolicLink: "file2_link", to: file2, ifExists: .open) as ReadableFile
119118
```
120119

121120
#### Misc

Tests/FileSmithTests/DirectoryTests.swift

-81
Original file line numberDiff line numberDiff line change
@@ -163,86 +163,6 @@ class DirectoryTests: XCTestCase {
163163
XCTFail(String(describing: error))
164164
}
165165
}
166-
167-
168-
/// The code examples under "Usage" in the readme.
169-
func testReadme() {
170-
do {
171-
// #### Change current working directory
172-
DirectoryPath.current = "/tmp"
173-
Directory.current = Directory.createTempDirectory()
174-
175-
// #### Paths
176-
let dirpath = DirectoryPath("dir/dir1")
177-
var filepath: FilePath = "file.txt"
178-
filepath = FilePath(base: "dir", relative: "file.txt")
179-
filepath = FilePath("dir/file.txt")
180-
181-
filepath.relativeString
182-
filepath.base?.string
183-
filepath.absoluteString
184-
filepath.string // relativeString ?? absoluteString
185-
filepath.name
186-
filepath.nameWithoutExtension
187-
filepath.extension
188-
189-
190-
dirpath.append(file: "file.txt") // FilePath("dir/dir1/file.txt")
191-
dirpath.append(directory: "dir2") // DirectoryPath("dir/dir1/dir2")
192-
dirpath.isAParentOf(filepath)
193-
194-
// #### Create
195-
var dir1 = try dirpath.create(ifExists: .replace)
196-
var dir2 = try Directory(create: "dir/dir2", ifExists: .throwError)
197-
var dir3 = try dir2.create(directory: "dir3", ifExists: .open) // dir/dir2/dir3
198-
199-
var file1_edit = try filepath.create(ifExists: .open)
200-
let file2_edit = try WritableFile(create: "file2.txt", ifExists: .open)
201-
let file3_edit = try dir1.create(file: "file3.txt", ifExists: .open) // dir/dir1/file3
202-
203-
// #### Open
204-
dir1 = try dirpath.open()
205-
dir2 = try Directory(open: "dir/dir2")
206-
dir3 = try dir2.open(directory: "dir3")
207-
208-
let file1 = try filepath.open()
209-
let file2 = try ReadableFile(open: "file2.txt")
210-
let file3 = try dir1.open(file: "file3.txt")
211-
212-
// #### Read/Write
213-
file1_edit.encoding = .utf16 // .utf8 by default
214-
file1_edit.write("some text...")
215-
file2.write(to: &file1_edit)
216-
217-
let contents: String = file3.read()
218-
for line in file3.lines() { // a lazy sequence
219-
// ...
220-
}
221-
while let text = file3.readSome() {
222-
// read pipes etc. piece by piece, instead of waiting until they are closed.
223-
}
224-
225-
// #### Search/Filter
226-
Directory.current.files(recursive: true) // [file2.txt, dir/file1.txt, dir/dir1/file3.txt]
227-
dir1.files("*3.*", recursive: true) // [file3.txt]
228-
Directory.current.directories(recursive: true) // [dir, dir/dir1, dir/dir2, dir/dir2/dir3]
229-
230-
// #### Symbolic links
231-
let dir1_link = try Directory(createSymbolicLink: "dir1_link", to: dir1, ifExists: .open)
232-
let dir2_link = try dir1.create(symbolicLink: "dir2_link", to: dir2, ifExists: .open)
233-
let file1_link = try ReadableFile(createSymbolicLink: "file1_link", to: file1, ifExists: .open)
234-
let file2_link = try dir2.create(symbolicLink: "file2_link", to: file2, ifExists: .open) as ReadableFile
235-
236-
// #### Misc
237-
try file1_edit.delete()
238-
try dir1.delete()
239-
240-
// Suppress unused variables warning.
241-
_ = [dir3,file2_edit,file3_edit,contents,dir1_link,dir2_link,file1_link,file2_link]
242-
} catch {
243-
XCTFail(String(describing: error))
244-
}
245-
}
246166
}
247167

248168
extension DirectoryTests {
@@ -254,6 +174,5 @@ extension DirectoryTests {
254174
("testOpenDirectory", testOpenDirectory),
255175
("testCreateDirectory", testCreateDirectory),
256176
("testDirectoryInAMultitudeOfWays", testDirectoryInAMultitudeOfWays),
257-
("testReadme", testReadme),
258177
]
259178
}

0 commit comments

Comments
 (0)