Skip to content

Commit

Permalink
Files 4.0 (JohnSundell#87)
Browse files Browse the repository at this point in the history
Files 4.0

This change includes a brand new implementation for Files,
that keeps most of the public API intact, while modernizing
the underlying implementation.

Under the hood, Files now uses value types and protocols,
rather than class inheritance - which improves the type safety
of the API, and streamlines the underlying code. Error handling
is also improved to include more underlying info, and the docs
have been reworked to be much more thorough.

The API has also been fine-tuned and modernized, dropping the
`FileSystem` class in favor of more “Swifty” APIs on `Folder`,
introducing more options for creating new files and folders, and
making it possible to easily change properties on file system
sequences.

A list of all changes will be posted as part of this version’s
release notes.
  • Loading branch information
JohnSundell authored Sep 7, 2019
1 parent 71d1b04 commit 8c956bc
Show file tree
Hide file tree
Showing 4 changed files with 1,005 additions and 1,096 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
.build/
.swiftpm/

# CocoaPods
#
Expand Down
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<a href="https://github.com/Carthage/Carthage">
<img src="https://img.shields.io/badge/carthage-compatible-4BC51D.svg?style=flat" alt="Carthage" />
</a>
<a href="https://github.com/JohnSundell/Marathon">
<img src="https://img.shields.io/badge/marathon-compatible-4BC51D.svg?style=flat" alt="Marathon" />
</a>
<a href="https://swift.org/package-manager">
<img src="https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat" alt="Swift Package Manager" />
</a>
Expand All @@ -35,27 +32,31 @@ Welcome to **Files**, a compact library that provides a nicer way to handle *fil
## Examples

Iterate over the files contained in a folder:

```swift
for file in try Folder(path: "MyFolder").files {
print(file.name)
}
```

Rename all files contained in a folder:

```swift
try Folder(path: "MyFolder").files.enumerated().forEach { (index, file) in
try file.rename(to: file.nameWithoutExtension + "\(index)")
}
```

Recursively iterate over all folders in a tree:

```swift
Folder.home.makeSubfolderSequence(recursive: true).forEach { folder in
Folder.home.subfolders.recursive.forEach { folder in
print("Name : \(folder.name), parent: \(folder.parent)")
}
```

Create, write and delete files and folders:

```swift
let folder = try Folder(path: "/users/john/folder")
let file = try folder.createFile(named: "file.json")
Expand All @@ -65,48 +66,48 @@ try folder.delete()
```

Move all files in a folder to another:

```swift
let originFolder = try Folder(path: "/users/john/folderA")
let targetFolder = try Folder(path: "/users/john/folderB")
try originFolder.files.move(to: targetFolder)
```

Easy access to system folders:

```swift
Folder.current
Folder.root
Folder.library
Folder.temporary
Folder.home
Folder.documents
```

## Usage

Files can be easily used in either a Swift script, command line tool or in an app for iOS, macOS, tvOS or Linux.

### In a script
## Installation

- Install [Marathon](https://github.com/johnsundell/marathon).
- Add Files using `$ marathon add https://github.com/johnsundell/files.git`.
- Run your script using `$ marathon run <path-to-your-script>`.
Files can be easily used in either a Swift script, a command line tool, or in an app for iOS, macOS, tvOS or Linux.

### In a command line tool
### Using the Swift Package Manager (preferred)

- Drag the file `Files.swift` into your command line tool's Xcode project.
To install Files for use in a Swift Package Manager-powered tool, script or server-side application, add Files as a dependency to your `Package.swift` file. For more information, please see the [Swift Package Manager documentation](https://github.com/apple/swift-package-manager/tree/master/Documentation).

### In an application
```swift
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0")
```

Either
### Using CocoaPods or Carthage

- Drag the file `Files.swift` into your application's Xcode project.
Please refer to [CocoaPods’](https://cocoapods.org) or [Carthage’s](https://github.com/Carthage/Carthage) own documentation for instructions on how to add dependencies using those tools.

or
### As a file

- Use CocoaPods, Carthage or the Swift Package manager to include Files as a dependency in your project.
Since all of Files is implemented within a single file, you can easily use it in any project by simply dragging the file `Files.swift` into your Xcode project.

## Backstory

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.


So, I made **Files**, 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? :)

## Questions or feedback?
Expand Down
Loading

0 comments on commit 8c956bc

Please sign in to comment.