Last active
December 12, 2015 12:19
-
-
Save AnthonyMastrean/4771515 to your computer and use it in GitHub Desktop.
Lame implementation of Guard in PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string] $path = (Split-Path $MyInvocation.MyCommand.Definition), | |
[string] $task = 'default' | |
) | |
Start-Guard -Path $path -Action { rake $task } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-FileSystemWatcher { | |
param( | |
[ValidateNotNullOrEmpty()] | |
[string] $Path = (Split-Path $MyInvocation.MyCommand.Definition) | |
) | |
$watcher = New-Object System.IO.FileSystemWatcher | |
$watcher.Path = $path | |
$watcher.IncludeSubdirectories = $true | |
$watcher | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Start-Guard { | |
param( | |
[ValidateNotNullOrEmpty()] | |
[string] $Path = (Split-Path $MyInvocation.MyCommand.Definition), | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[scriptblock] $Action | |
) | |
$watcher = New-FileSystemWatcher $path | |
$timeout = 3000 | |
while($true) { | |
$result = $watcher.WaitForChanged('All', $timeout) | |
if($result.TimedOut) { | |
continue | |
} | |
& $action | |
Start-Sleep -Milliseconds $timeout | |
} | |
} |
Author
AnthonyMastrean
commented
Feb 12, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment