Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions image/src/entrypoints/worker/Development.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ param(
$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"
$timeFormat = "HH:mm:ss:fff"
$executable = "C:\\service\\$($env:WORKER_EXECUTABLE_NAME_ENV)"

# Print start message
Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: starting..."
Expand All @@ -23,32 +24,21 @@ if ($useWatchDirectory)
{
$WatchDirectoryParameters = @{ Path = "C:\deploy"; Destination = "C:\service"; }
}

$WatchDirectoryParameters["Executable"] = $executable
Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: '$watchDirectoryJobName' validating..."

# First a trial-run to catch any parameter validation / setup errors
$WatchDirectoryParameters["WhatIf"] = $true
& "C:\tools\scripts\Watch-Directory.ps1" @WatchDirectoryParameters
$WatchDirectoryParameters["WhatIf"] = $false

Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: '$watchDirectoryJobName' starting..."

# Start Watch-Directory.ps1 in background
Start-Job -Name $watchDirectoryJobName -ArgumentList $WatchDirectoryParameters -ScriptBlock {
param([hashtable]$params)

& "C:\tools\scripts\Watch-Directory.ps1" @params

} | Out-Null

Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: '$watchDirectoryJobName' started."

& "C:\tools\scripts\Watch-Directory.ps1" @WatchDirectoryParameters
}
else
{
Write-Host ("$(Get-Date -Format $timeFormat): Development ENTRYPOINT: Skipping start of '$watchDirectoryJobName'. To enable you should mount a directory into 'C:\deploy'.")
}

# Print ready message
Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: ready!"

& "C:\\service\\$($env:WORKER_EXECUTABLE_NAME_ENV)"
Write-Host ("$(Get-Date -Format $timeFormat): Development ENTRYPOINT: Skipping start of '$watchDirectoryJobName'. To enable you should mount a directory into '$watchFolder'.")
Write-Host "$(Get-Date -Format $timeFormat): Development ENTRYPOINT: ready!"
& "$executable"
}
63 changes: 58 additions & 5 deletions image/src/scripts/Watch-Directory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Default directories to skip during sync. Default is "obj", "Properties", "node_modules".
.PARAMETER ExcludeDirectories
Additional directories to skip during sync.
.PARAMETER Executable
The executable to start and restart after files are synced
.EXAMPLE
PS C:\> .\Watch-Directory.ps1 -Path 'C:\source' -Destination 'C:\destination' -ExcludeFiles @("web.config")
.INPUTS
Expand Down Expand Up @@ -52,7 +54,10 @@ param(
[array]$DefaultExcludedDirectories = @("obj", "Properties", "node_modules"),

[Parameter(Mandatory = $false)]
[array]$ExcludeDirectories = @()
[array]$ExcludeDirectories = @(),

[Parameter(Mandatory = $false)]
[string]$Executable = ""
)

# Setup
Expand Down Expand Up @@ -119,6 +124,14 @@ Register-ObjectEvent $watcher Deleted -SourceIdentifier "FileDeleted" -MessageDa
}
} | Out-Null

function StartExecutable()
{
Write-Information "$(Get-Date -Format $timeFormat): Starting executable '$Executable'."
$process = (Start-Process $Executable -PassThru)
Write-Information "$(Get-Date -Format $timeFormat): Succesfully started process with Id $($process.Id)"
return $process
}

function Sync
{
param(
Expand All @@ -129,11 +142,18 @@ function Sync
[Parameter(Mandatory = $false)]
$ExcludeFiles,
[Parameter(Mandatory = $false)]
$ExcludeDirectories
$ExcludeDirectories,
[Parameter(Mandatory = $false)]
$ListOnly
)

$command = @("robocopy", "`"$Path`"", "`"$Destination`"", "/E", "/XX", "/MT:1", "/NJH", "/NJS", "/FP", "/NDL", "/NP", "/NS", "/R:5", "/W:1")

if($ListOnly)
{
$command += "/L "
}

if ($ExcludeDirectories.Count -gt 0)
{
$command += "/XD "
Expand Down Expand Up @@ -164,27 +184,60 @@ function Sync
$line = $_.Trim().Replace("`r`n", "").Replace("`t", " ")
$dirty = ![string]::IsNullOrEmpty($line)

if ($dirty)
if ($dirty -and $ListOnly -eq $false)
{
Write-Information "$(Get-Date -Format $timeFormat): $line"
}
}

if ($dirty)
if ($dirty -and $ListOnly -eq $false)
{
Write-Information "$(Get-Date -Format $timeFormat): Done syncing..."
}

return $dirty
}

try
{
$process = $null
if($Executable)
{
$process = StartExecutable
}

Write-Information "$(Get-Date -Format $timeFormat): Watching '$Path' for changes, will copy to '$Destination'..."

# Main loop
$timer = [System.Diagnostics.Stopwatch]::StartNew()
while ($Timeout -eq 0 -or $timer.ElapsedMilliseconds -lt $Timeout)
{
Sync -Path $Path -Destination $Destination -ExcludeFiles $fileRules -ExcludeDirectories $directoryRules
$filesChanged = Sync -Path $Path -Destination $Destination -ExcludeFiles $fileRules -ExcludeDirectories $directoryRules -ListOnly $true

if($filesChanged)
{
Write-Information "$(Get-Date -Format $timeFormat): File changes have been detected"

if($process)
{
$process.Refresh()
if($process.HasExited -eq $false)
{
Write-Information "$(Get-Date -Format $timeFormat): Waiting for process $($process.Id) to exit."
Stop-Process $process
$process.WaitForExit();
}
}

Write-Information "$(Get-Date -Format $timeFormat): Going to sync file changes"
$filesChanged = Sync -Path $Path -Destination $Destination -ExcludeFiles $fileRules -ExcludeDirectories $directoryRules -ListOnly $false

if($process)
{
Write-Information "$(Get-Date -Format $timeFormat): File changes have been synced, restarting executable."
$process = StartExecutable
}
}

Start-Sleep -Milliseconds $Sleep
}
Expand Down
2 changes: 1 addition & 1 deletion image/test/scripts/Watch-Directory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Describe 'Watch-Directory.ps1' {
$dst = New-Item -Path (Join-Path $TestDrive (Get-Random)) -ItemType 'Directory'

$job = Start-Job -ScriptBlock { Start-Sleep -Milliseconds 500; New-Item -Path "$($args[0])\file.txt" } -ArgumentList $src
& $script -Path $src -Destination $dst -Timeout 1000 -Sleep 100
& $script -Path $src -Destination $dst -Timeout 2000 -Sleep 100
$job | Wait-Job | Remove-Job

"$($dst)\file.txt" | Should -Exist
Expand Down