Skip to content

Instantly share code, notes, and snippets.

@AnthonyMastrean
Last active December 12, 2015 12:19
Show Gist options
  • Save AnthonyMastrean/4771515 to your computer and use it in GitHub Desktop.
Save AnthonyMastrean/4771515 to your computer and use it in GitHub Desktop.
Lame implementation of Guard in PowerShell
param(
[string] $path = (Split-Path $MyInvocation.MyCommand.Definition),
[string] $task = 'default'
)
Start-Guard -Path $path -Action { rake $task }
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
}
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
}
}
@AnthonyMastrean
Copy link
Author

ps> .\guard-rake.ps1 -Path "$pwd\source" -Task 'build'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment