115 lines
3.6 KiB
PowerShell
115 lines
3.6 KiB
PowerShell
|
[CmdletBinding(SupportsShouldProcess)]param(
|
||
|
[ArgumentCompleter({ param (
|
||
|
$commandName,
|
||
|
$parameterName,
|
||
|
$wordToComplete,
|
||
|
$commandAst,
|
||
|
$fakeBoundParameters
|
||
|
)
|
||
|
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
|
||
|
})]
|
||
|
[string[]]$ProjectPath=@($PWD),
|
||
|
[switch]$Recurse,
|
||
|
[int]$Depth=1,
|
||
|
[switch]$Force,
|
||
|
[switch]$OneLine,
|
||
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true, mandatory)]
|
||
|
[array]$CliParams
|
||
|
)
|
||
|
|
||
|
$script:JobQueue = @()
|
||
|
$script:CleanupJobQueue = @()
|
||
|
function CompleteJobs{
|
||
|
if( -not $JobQueue ) { return }
|
||
|
$local:currentJobQueue = $JobQueue
|
||
|
$JobQueue = @()
|
||
|
$CleanupJobQueue += $currentJobQueue
|
||
|
|
||
|
Wait-Job $currentJobQueue | ForEach-Object {
|
||
|
$_ | Receive-Job | ForEach-Object {
|
||
|
Write-Host "---: $($_.Path) | $($_.Params)" -ForegroundColor Cyan
|
||
|
$_.Results
|
||
|
}
|
||
|
}
|
||
|
Write-Host '--- Background jobs completed' -ForegroundColor Green
|
||
|
}
|
||
|
|
||
|
if( $CliParams[0] -is [string] ) {
|
||
|
$CliParams = @($CliParams,@($null))
|
||
|
}
|
||
|
|
||
|
$local:dcPathList = $ProjectPath | ForEach-Object {
|
||
|
if( (Test-Path (Join-Path $_ 'docker-compose.yml')) -and -not $Recurse ) {
|
||
|
Resolve-Path $_
|
||
|
} elseif( $Recurse ) {
|
||
|
$local:gciParams = [ordered]@{
|
||
|
Recurse = $true
|
||
|
}
|
||
|
if( $Depth ) {
|
||
|
$gciParams.Depth = $Depth
|
||
|
}
|
||
|
Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams |
|
||
|
Select-Object -ExpandProperty Directory |
|
||
|
Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) }
|
||
|
|
||
|
# _DIRS_=$( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -exec test -f {}/.ask.all \; -print | sort )
|
||
|
# _DIRS_="$_DIRS_ $( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -not -exec test -f {}/.ask.all \; -not -exec test -f {}/.noauto.all \; -print | sort )"
|
||
|
}
|
||
|
}
|
||
|
if( -not $dcPathList ) { return }
|
||
|
Write-Verbose "$dcPathList"
|
||
|
|
||
|
foreach( $local:p in $CliParams ) {
|
||
|
if( -not $p ) { continue }
|
||
|
|
||
|
$local:AsJob = $false;
|
||
|
$local:Wait = $false;
|
||
|
|
||
|
if( $p -isnot [String] ) {
|
||
|
$local:skipFirst = $true
|
||
|
switch( $p[0] ) {
|
||
|
'&' { $AsJob = $true }
|
||
|
'!' { $Wait = $true }
|
||
|
default { $skipFirst = $false }
|
||
|
}
|
||
|
if( $skipFirst ) { $p = $p | Select-Object -Skip 1 }
|
||
|
}
|
||
|
|
||
|
$dcParams = $p | ForEach-Object {
|
||
|
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
|
||
|
else { "'$($_.Replace( "'", "''" ))'" }
|
||
|
}
|
||
|
|
||
|
$dcPathList | ForEach-Object {
|
||
|
$local:dcPath = Join-Path $_ 'docker-compose.yml'
|
||
|
if( -not (Test-Path $dcPath) ) { if( $Recurse ) { return } else { throw "ERROR: dcPath ($dcPath) does not exists" } }
|
||
|
|
||
|
Write-Verbose "& docker-compose --file $dcPath $dcParams"
|
||
|
if( $Wait ) {
|
||
|
CompleteJobs
|
||
|
}
|
||
|
|
||
|
if( -not $dcParams ) { continue }
|
||
|
|
||
|
if( $AsJob ) {
|
||
|
$JobQueue += Start-Job -ArgumentList @($dcPath,$dcParams) -ScriptBlock {
|
||
|
param($dcPath,$dcParams)
|
||
|
[PSCustomObject] $([ordered]@{
|
||
|
Path = $dcPath
|
||
|
Params = $dcParams
|
||
|
Results = $(& docker-compose --file $dcPath $dcParams)
|
||
|
})
|
||
|
}
|
||
|
} else {
|
||
|
& docker-compose --file $dcPath $dcParams
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
CompleteJobs
|
||
|
if( $CleanupJobQueue ) {
|
||
|
$CleanupJobQueue | Remove-Job
|
||
|
}
|