FWatcher 1.0.1

dotnet add package FWatcher --version 1.0.1                
NuGet\Install-Package FWatcher -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FWatcher" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FWatcher --version 1.0.1                
#r "nuget: FWatcher, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FWatcher as a Cake Addin
#addin nuget:?package=FWatcher&version=1.0.1

// Install FWatcher as a Cake Tool
#tool nuget:?package=FWatcher&version=1.0.1                

FWatcher

Watch directories and perform actions when changes are detected.

Installation

Add it as a nuget package

dotnet add package FWatcher

How It Works

We need

  • Path: at least one directory path to 'watch'
  • Pattern: regex pattern of files to keep track of

The Watcher type will hold that data.

type Watcher = { Path: string; Pattern: string }

We make a DirectoryState by hashing the contents inside Watcher.Path that conform to Watcher.Pattern.

The kv pairs are the file paths and their hashed values.

type DirectoryState = Map<string, byte[]>

We get DirectoryChanges when comparing DirectoryState's. If any changes exist, we can trigger an action. (explained further below)

type DirectoryChanges =
  {
    Count: int
    Added: DirectoryState
    Deleted: DirectoryState
    Modified: DirectoryState
  }

To start 'watching' a directory we call the watch function.

let watch (action: Watcher -> DirectoryChanges -> Async<unit>) (compareStateInterval: int) (watcher: Watcher)

watch expects

  • action: what action to take when there are directory changes

  • compareStateInterval: how often the directory will be checked for changes, in milliseconds

  • watcher: directory path and regex pattern to look for

How to Use

module Example

open System
open FWatcher

module SimpleExample =

  let watcher = { Path = "../Example/watching"; Pattern = ".*.txt"}

  let someActionOnChanges watcher directorychanges =
    async {
      printfn "Changes in %s" watcher.Path
      printfn "Added: %A" directorychanges.Added.Keys
      printfn "Deleted: %A" directorychanges.Deleted.Keys
      printfn "Modified: %A" directorychanges.Modified.Keys
      return ()
    }

module ExcelExample =
  open FSharp.Interop.Excel

  type WatcherGrid = ExcelFile<FileName = "../Example/my_grid.xlsx", HasHeaders = true, ForceString = true>

  let myGrid = new WatcherGrid()

  let prettyPrint directoryChanges =
    let prettyishPrint change keys =
      let s = sprintf "\n\n\t%s\n\t\t%s\n" change
      [ for k in keys do yield s k ]
      |> String.concat ""

    match directoryChanges with
    | directoryChanges when directoryChanges.Count > 0 ->
      printfn "\nChanges detected:%s%s%s"
        (prettyishPrint "Added:" directoryChanges.Added.Keys)
        (prettyishPrint "Deleted:" directoryChanges.Deleted.Keys)
        (prettyishPrint "Modified:" directoryChanges.Modified.Keys)
    | o -> ()

  let prettyPrintOnChange watcher directoryChanges =
    async {
      prettyPrint directoryChanges
      return ()
    }

  let startWatchers () =
    async {
      try
        return!
          myGrid.Data
          |> Seq.filter (fun row -> not (String.IsNullOrWhiteSpace row.Path || String.IsNullOrWhiteSpace row.Pattern))
          |> Seq.map (fun row -> { Path = row.Path; Pattern = row.Pattern })
          |> Seq.map (watch prettyPrintOnChange 500)
          |> Async.Parallel
          |> Async.Ignore
      with ex -> printfn $"%s{ex.Message}"; return ()
    }

[<EntryPoint>]
let main _ =
  async {
    // do! FWatcher.watch SimpleExample.someActionOnChanges 1000 SimpleExample.watcher
    do! ExcelExample.startWatchers ()
    return 0
  }
  |> Async.RunSynchronously
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 469 9/6/2022
1.0.0 417 9/5/2022

RELEASE_NOTES.md