|
1 | 1 | # FileManagerKit |
2 | 2 |
|
3 | | -Useful extensions for the [FileManager](https://developer.apple.com/documentation/foundation/filemanager) class. |
| 3 | +Swift extensions and DSLs for filesystem testing, scripting, and inspection. |
4 | 4 |
|
5 | | -## Getting started |
| 5 | +This package contains two products: |
6 | 6 |
|
7 | | -⚠️ This repository is a work in progress, things can break until it reaches v1.0.0. |
| 7 | +- FileManagerKit – high-level extensions for FileManager |
| 8 | +- FileManagerKitBuilder – a DSL for creating filesystem layouts (ideal for tests) |
8 | 9 |
|
9 | | -Use at your own risk. |
| 10 | +Note: This repository is a work in progress. Expect breaking changes before v1.0.0. |
10 | 11 |
|
11 | | -### Adding the dependency |
| 12 | +--- |
12 | 13 |
|
13 | | -To add a dependency on the package, declare it in your `Package.swift`: |
| 14 | +## Installation |
| 15 | + |
| 16 | +Add the package to your `Package.swift`: |
14 | 17 |
|
15 | 18 | ```swift |
16 | 19 | .package(url: "https://github.com/binarybirds/file-manager-kit", .upToNextMinor(from: "0.2.0")), |
17 | 20 | ``` |
18 | 21 |
|
19 | | -and to your application target, add `FileManagerKit` to your dependencies: |
| 22 | +Then declare the product you want to use in your target dependencies: |
20 | 23 |
|
21 | 24 | ```swift |
22 | 25 | .product(name: "FileManagerKit", package: "file-manager-kit") |
| 26 | +.product(name: "FileManagerKitBuilder", package: "file-manager-kit") |
23 | 27 | ``` |
24 | 28 |
|
25 | | -Example `Package.swift` file with `FileManagerKit` as a dependency: |
| 29 | +--- |
| 30 | + |
| 31 | +# FileManagerKit |
| 32 | + |
| 33 | +A set of ergonomic, safe extensions for working with FileManager. |
| 34 | + |
| 35 | +## Common Operations |
| 36 | + |
| 37 | +### Check if File or Directory Exists |
26 | 38 |
|
27 | 39 | ```swift |
28 | | -// swift-tools-version:6.0 |
29 | | -import PackageDescription |
| 40 | +let fileURL = URL(filePath: "/path/to/file") |
| 41 | +if fileManager.exists(at: fileURL) { |
| 42 | + print("Exists!") |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Create a Directory |
| 47 | + |
| 48 | +```swift |
| 49 | +let dirURL = URL(filePath: "/path/to/new-dir") |
| 50 | +try fileManager.createDirectory(at: dirURL) |
| 51 | +``` |
| 52 | + |
| 53 | +### Create a File |
| 54 | + |
| 55 | +```swift |
| 56 | +let fileURL = URL(filePath: "/path/to/file.txt") |
| 57 | +let data = "Hello".data(using: .utf8) |
| 58 | +try fileManager.createFile(at: fileURL, contents: data) |
| 59 | +``` |
30 | 60 |
|
31 | | -let package = Package( |
32 | | - name: "my-application", |
33 | | - dependencies: [ |
34 | | - .package(url: "https://github.com/binarybirds/file-manager-kit", .upToNextMinor(from: "0.2.0")), |
35 | | - ], |
36 | | - targets: [ |
37 | | - .target(name: "MyApplication", dependencies: [ |
38 | | - .product(name: "FileManagerKit", package: "file-manager-kit") |
39 | | - ]), |
40 | | - .testTarget(name: "MyApplicationTests", dependencies: [ |
41 | | - .target(name: "MyApplication"), |
42 | | - ]), |
43 | | - ] |
44 | | -) |
| 61 | +### Delete a File or Directory |
| 62 | + |
| 63 | +```swift |
| 64 | +let targetURL = URL(filePath: "/path/to/delete") |
| 65 | +try fileManager.delete(at: targetURL) |
| 66 | +``` |
| 67 | + |
| 68 | +### List Directory Contents |
| 69 | + |
| 70 | +```swift |
| 71 | +let contents = fileManager.listDirectory(at: URL(filePath: "/path/to/dir")) |
| 72 | +print(contents) |
| 73 | +``` |
| 74 | + |
| 75 | +### Copy / Move |
| 76 | + |
| 77 | +```swift |
| 78 | +try fileManager.copy(from: URL(filePath: "/from"), to: URL(filePath: "/to")) |
| 79 | +try fileManager.move(from: URL(filePath: "/from"), to: URL(filePath: "/to")) |
| 80 | +``` |
| 81 | + |
| 82 | +### Get File Size |
| 83 | + |
| 84 | +```swift |
| 85 | +let size = try fileManager.size(at: URL(filePath: "/path/to/file")) |
| 86 | +print("\(size) bytes") |
45 | 87 | ``` |
46 | 88 |
|
| 89 | +--- |
| 90 | + |
| 91 | +# FileManagerKitBuilder |
| 92 | + |
| 93 | +A Swift DSL to declaratively build, inspect, and tear down file system structures — great for testing. |
| 94 | + |
| 95 | +## Installation |
| 96 | + |
| 97 | +To use FileManagerKitBuilder, add this line to your dependencies: |
| 98 | + |
| 99 | +```swift |
| 100 | +.product(name: "FileManagerKitBuilder", package: "file-manager-kit") |
| 101 | +``` |
| 102 | + |
| 103 | +## Simple Example |
| 104 | + |
| 105 | +Create and clean up a file structure: |
| 106 | + |
| 107 | +```swift |
| 108 | +import FileManagerKitBuilder |
| 109 | + |
| 110 | +let playground = FileManagerPlayground { |
| 111 | + Directory(name: "foo") { |
| 112 | + File(name: "bar.txt", string: "Hello, world!") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +let _ = try playground.build() |
| 117 | +try playground.remove() |
| 118 | +``` |
| 119 | + |
| 120 | +## Custom Type Example |
| 121 | + |
| 122 | +Use a BuildableItem to generate structured files (e.g., JSON). |
| 123 | + |
| 124 | +```swift |
| 125 | +public struct JSON<T: Encodable>: BuildableItem { |
| 126 | + public let name: String |
| 127 | + public let contents: T |
| 128 | + |
| 129 | + public func buildItem() -> FileManagerPlayground.Item { |
| 130 | + let data = try! JSONEncoder().encode(contents) |
| 131 | + let string = String(data: data, encoding: .utf8)! |
| 132 | + return .file(File(name: "\(name).json", string: string)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +struct User: Codable { let name: String } |
| 137 | + |
| 138 | +let playground = FileManagerPlayground { |
| 139 | + Directory(name: "data") { |
| 140 | + JSON(name: "user", contents: User(name: "Deku")) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +try playground.build() |
| 145 | +``` |
| 146 | + |
| 147 | +## Test Example |
| 148 | + |
| 149 | +Use `.test` to run assertions in a temporary sandbox: |
| 150 | + |
| 151 | +```swift |
| 152 | +try FileManagerPlayground { |
| 153 | + Directory(name: "foo") { |
| 154 | + "bar.txt" |
| 155 | + } |
| 156 | +} |
| 157 | +.test { fileManager, rootUrl in |
| 158 | + let fileURL = rootUrl.appendingPathComponent("foo/bar.txt") |
| 159 | + #expect(fileManager.fileExists(at: fileURL)) |
| 160 | +} |
| 161 | +``` |
0 commit comments