Giter Site home page Giter Site logo

filekit's Introduction

Platform Language: Swift CocoaPods Carthage License

InstallationUsageLicenseDocumentation

FileKit is a Swift framework that allows for simple and expressive file management.

Development happens in the develop branch.

Installation

Compatibility

  • Xcode
    • Version: 7.0
    • Language: Swift 2.0
  • OS X
    • Compatible With: OS X 10.11
    • Deployment Target: OS X 10.9
  • iOS
    • Compatible With: iOS 9.0
    • Deployment Target: iOS 8.0
  • watchOS
    • Compatible With: watchOS 2.0
    • Deployment Target: watchOS 2.0

Install Using CocoaPods

CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.

  1. Add the project to your Podfile.

    use_frameworks!
    
    pod 'FileKit', '~> 1.6.0'
  2. Run pod install and open the .xcworkspace file to launch Xcode.

  3. Import the FileKit framework.

    import FileKit

Install Using Carthage

Carthage is a decentralized dependency manager for Objective-C and Swift.

  1. Add the project to your Cartfile.

    github "nvzqz/FileKit"
    
  2. Run carthage update and follow the additional steps in order to add FileKit to your project.

  3. Import the FileKit framework.

    import FileKit

Usage

Paths

Paths are handled with the FKPath structure.

Initialization

Paths can be created with an initializer or with a string literal.

let home = FKPath("~")
let drive: FKPath = "/Volumes/Macintosh HD"

Operations

New Files

A blank file can be written by calling createFile() on an FKPath.

do {
    try FKPath(".gitignore").createFile()
} catch {
    print("Could not create .gitignore")
}
New Directories

A directory can be created by calling createDirectory() on an FKPath.

do {
    try FKPath("~/Files").createDirectory()
} catch {
    print("Could not create Files")
}
New Symlinks

A symbolic link can be created by calling createSymlinkToPath(_:) on an FKPath.

do {
    let filePath = FKPath.UserDesktop + "text.txt"
    try filePath.createFile()

    let linkPath = FKPath.UserDesktop + "link.txt"
    try filePath.createSymlinkToPath(linkPath)
    print(linkPath.exists)  // true

    let text = "If she weighs the same as a duck, she's made of wood."
    try text |>  FKTextFile(path: filePath)

    let contents = try FKTextFile(path: linkPath).read()
    print(contents == text)  // true
} catch {
    print("Could not create symlink")
}
Finding Paths

You can find all paths with the ".txt" extension five folders deep into the Desktop with:

let textFiles = FKPath.UserDesktop.findPaths(searchDepth: 5) { path in
    path.pathExtension == "txt"
}

Setting searchDepth to a negative value will make it run until every path in self is checked against. If the checked path passes the condition, it'll be added to the returned paths and the next path will be checked. If it doesn't and it's a directory, its children paths will be checked.

+ Operator

Appends two paths and returns the result

// ~/Documents/My Essay.docx
let essay  = FKPath.UserDocuments + "My Essay.docx"
+= Operator

Appends the right path to the left path

var photos = FKPath.UserPictures + "My Photos"  // ~/Pictures/My Photos
photos += "../My Other Photos"                  // ~/Pictures/My Photos/../My Other Photos
Operator (alt+8)

Returns the standardized version of the path.

let path: FKPath = "~/Desktop"
path == path.standardized  // true
^ Operator

Returns the path's parent path.

let path: FKPath = "~/Movies"
path^ == "~"  // true
->> Operator

Moves the file at the left path to the right path.

FKPath counterpart: moveFileToPath(_:)

FKFileType counterpart: moveToPath(_:)

->! Operator

Forcibly moves the file at the left path to the right path by deleting anything at the left path before moving the file.

+>> Operator

Copies the file at the left path to the right path.

FKPath counterpart: copyFileToPath(_:)

FKFileType counterpart: copyToPath(_:)

+>! Operator

Forcibly copies the file at the left path to the right path by deleting anything at the left path before copying the file.

=>> Operator

Creates a symlink of the left path at the right path.

FKPath counterpart: symlinkFileToPath(_:)

FKFileType counterpart: symlinkToPath(_:)

=>! Operator

Forcibly creates a symlink of the left path at the right path by deleting anything at the left path before creating the symlink.

Subscripting

Subscripting an FKPath will return all of its components up to and including the index.

let users = FKPath("/Users/me/Desktop")[1]  // /Users
standardize()

Standardizes the path.

The same as doing:

somePath = somePath.standardized
resolve()

Resolves the path's symlinks.

The same as doing:

somePath = somePath.resolved

Files

FKFileType

Files are represented with the FKFileType protocol.

|> Operator

Writes the data on the left to the file on the right.

do {
    try "My name is Bob." |> FKTextFile(path: FKPath.UserDesktop + "name.txt")
} catch {
    print("I can't write to a desktop file?!")
}

FKFile

A file can be made using FKFile with an FKDataType for its data type.

The generic constraint defines the file's data type.

let textFile = FKFile<String>(path: FKPath.UserDesktop + "sample.txt")

FKTextFile

The FKTextFile class allows for reading and writing strings to a file.

Although it is a subclass of FKFile<String>, FKTextFile offers some functionality that FKFile<String> doesn't.

|>> Operator

Appends the string on the left to the FKTextFile on the right.

let readme = FKTextFile(path: "README.txt")

do {
    try "My Awesome Project" |> readme
    try "This is an awesome project." |>> readme
} catch {
    print("Could not write to \(readme.path)")
}

FKDictionaryFile

The FKDictionaryFile class allows for reading and writing dictionaries to a file.

It is not a subclass of FKFile but still conforms to FKFileType.

do {
    let df = FKDictionaryFile(path: FKPath.UserDesktop + "Sample.plist")
    let someDictionary: NSDictionary = ["FileKit" : true,
                                        "Hello"   : "World"]
    try someDictionary |> df
} catch {
    print("Something went wrong :( ...")
}

License

FileKit and its assets are released under the MIT License. Assets can be found in the assets branch.

filekit's People

Contributors

nvzqz avatar phimage avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.