16

GitHub - bannzai/Ocha: Listen to the file system change notifications and raises...

 5 years ago
source link: https://github.com/bannzai/Ocha
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Ocha

swift4.2 Platform License

Ocha

Ocha can be listen to the file system change notifications and raises events when a directory, or file in a directory, changes.

Ocha means Tea? in Japan.

Usage

To use Ocha, first create a Watcher instance with file paths you want to watch.

let watcher = Watcher(paths: [pathString])

And you can call for Watcher.start(_:) method when it start to watch file events. When file removed, you get callback with removed file path infomations.

watcher.start { (events) in ... }

Example

The following example execute git add << REMOVED_FILE_PATH >> and git commit -m << REMOVED_FILE_PATH >> to watch the file path, when it removed.

import Foundation
import Ocha
import SwiftShell
import PathKit

let path = Path(
    #file.components(separatedBy: "/")
        .dropLast() // main.swift
        .dropLast() // GitCommitExample
        .dropLast() // Sources
        .joined(separator: "/")
)
let pathString = path.absolute().string
main.currentdirectory = pathString

let watcher = Watcher(paths: [pathString])
watcher.start { (events) in
    let removedEventPaths = events
        .filter { $0.flag.contains(.removedFile) }
        .map { $0.path }
    removedEventPaths.forEach { path in
        main.run(bash: "git add \(path)")
        main.run(bash: "git commit -m \"Delete file \(path)\"")
    }
}

RunLoop.current.run()

If you execute this swift code, you can confirm removed file git log. $ git log -1

LICENSE

Ocha is released under the MIT license. See LICENSE for details.

Header logo is released CC BY-NC 4.0 license. Original design by noainoue.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK