Swift开源:Files-优雅、健壮的文件系统管理类库

   <p style="text-align:center"><img src="https://simg.open-open.com/show/097dcd12ea2009fd2c25c3b1a0975275.png"></p>    <p>Welcome to <strong>Files</strong> , a compact library that provides a nicer way to handle <em>files</em> and <em>folders</em> in Swift. It’s primarily aimed at Swift scripting and tooling, but can also be embedded in applications that need to access the file system. It's essentially a thin wrapper around the FileManager APIs that Foundation provides.</p>    <h2>Features</h2>    <ul>     <li>Modern, object-oriented API for accessing, reading and writing files & folders.</li>     <li>Unified, simple do, try, catch error handling.</li>     <li>Easily construct recursive and flat sequences of files and folders.</li>    </ul>    <h2>Examples</h2>    <p>Iterate over the files contained in a folder:</p>    <pre>  <code class="language-swift">for file in try Folder(path: "MyFolder").files {      print(file.name)  }</code></pre>    <p>Rename all files contained in a folder:</p>    <pre>  <code class="language-swift">try Folder(path: "MyFolder").files.enumerated().forEach { (index, file) in      try file.rename(to: file.nameWithoutExtension + "\(index)")  }</code></pre>    <p>Recursively iterate over all folders in a tree:</p>    <pre>  <code class="language-swift">FileSystem().homeFolder.makeSubfolderSequence(recursive: true).forEach { file in      print("Name : \(file.name), parent: \(file.parent)")  }</code></pre>    <p>Create, write and delete files and folders:</p>    <pre>  <code class="language-swift">let folder = try Folder(path: "/users/john/folder")  let file = try folder.createFile(named: "file.json")  try file.write(data: wrap(object))  try file.delete()  try folder.delete()</code></pre>    <p>Move all files in a folder to another:</p>    <pre>  <code class="language-swift">let originFolder = try Folder(path: "/users/john/folderA")  let targetFolder = try Folder(path: "/users/john/folderB")  try originFolder.files.move(to: targetFolder)</code></pre>    <h2>Usage</h2>    <p>Files can be easily used in either a Swift script, command line tool or in an app for iOS, macOS or tvOS.</p>    <h3>In a script</h3>    <ul>     <li>Write a Swift script in your favorite editor.</li>     <li>Concat your script with Files.swift and run it using swift (for example: $ cat Files.swift MyScript.swift | swift - ).</li>    </ul>    <h3>In a command line tool</h3>    <ul>     <li>Drag the file Files.swift into your command line tool's Xcode project.</li>    </ul>    <h3>In an application</h3>    <p>Either</p>    <ul>     <li>Drag the file Files.swift into your application's Xcode project.</li>    </ul>    <p>or</p>    <ul>     <li>Use CocoaPods, Carthage or the Swift Package manager to include Files as a dependency in your project.</li>    </ul>    <h2>Backstory</h2>    <p>So, why was this made? As I've migrated most of my build tools and other scripts from languages like Bash, Ruby and Python to Swift, I've found myself lacking an easy way to deal with the file system. Sure, FileManager has a quite nice API, but it can be quite cumbersome to use because of its string-based nature, which makes simple scripts that move or rename files quickly become quite complex.</p>    <p>So, I made <strong>Files</strong> , to enable me to quickly handle files and folders, in an expressive way. And, since I love open source, I thought - why not package it up and share it with the community? :)</p>    <p> </p>    <p> </p>    <p> </p>