| 1 |
function Add-Watcher { |
| 2 |
param( |
| 3 |
[String] $Path = ".", |
| 4 |
[String] $FileFilter = "*" |
| 5 |
) |
| 6 |
|
| 7 |
$AttributeFilter = [IO.NotifyFilters]::FileName, |
| 8 |
[IO.NotifyFilters]::LastWrite |
| 9 |
$ChangeTypes = [System.IO.WatcherChangeTypes]::All |
| 10 |
$Timeout = 1000 |
| 11 |
|
| 12 |
try { |
| 13 |
$watcher = New-Object ` |
| 14 |
-TypeName IO.FileSystemWatcher ` |
| 15 |
-ArgumentList $Path, $FileFilter ` |
| 16 |
-Property @{ |
| 17 |
IncludeSubdirectories = $true |
| 18 |
NotifyFilter = $AttributeFilter |
| 19 |
} |
| 20 |
|
| 21 |
do { |
| 22 |
$result = $watcher.WaitForChanged($ChangeTypes, $Timeout) |
| 23 |
if ($result.TimedOut) { continue } |
| 24 |
|
| 25 |
Invoke-Action -Change $result |
| 26 |
} while ($true) |
| 27 |
} finally { |
| 28 |
$watcher.Dispose() |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
function Invoke-Action { |
| 33 |
param ( |
| 34 |
[Parameter(Mandatory)] |
| 35 |
[System.IO.WaitForChangedResult] $ChangeInformation |
| 36 |
) |
| 37 |
|
| 38 |
tox -e linting,py -p auto -o |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
Add-Watcher -FileFilter *.py |