src/init.ps1


function initCommand() {
    $parentStack = (Get-PSCallStack)[1]

    if (!$global:commandStarted) {
        # top level started Command
        $global:commandStarted = $parentStack.Command
        moveLastCommandEntity 
        addLastParamsFromStack $parentStack
    }
}

function finalizeCommand() {
    $parentStack = (Get-PSCallStack)[1]

    if ($global:commandStarted -eq $parentStack.Command) {
        $global:commandStarted = $false
    }
    if($result -match "^Error:*"){
        Write-host "$result" -ForegroundColor Red -NoNewline
        ([ref]$result).value = ''
    }
}


function addGetPropertyIfExists($name, $value) {
    if ($value) {
        if ($GetProperty) {
            ([ref]$GetProperty).value += '&'
        }
        ([ref]$GetProperty).value += "$name=$value"
    }
} 

function moveLastCommandEntity (){
    $commandName = ($parentStack.InvocationInfo.InvocationName)
    if($commandName -match "^xms*"){
        if (test-path alias:$commandName) {
            $commandName = $commandName -replace 'xms',''
            # split by uppercase letters
            $first, $second, $rest = ($commandName -csplit "([A-Z][a-z]+)" | where { $_ })

            if ($xmsEntitiesList -contains "$first"){
                $entity = "$first"
            }
            if ($xmsEntitiesList -contains "$first$second") {
                $entity = "$first$second"
            }
            if ($xmsEntitiesList -contains $entity){
                # move current entity to the first position
                [void]($global:xmsEntitiesList.Remove($entity))
                [void]($global:xmsEntitiesList.Insert(0, $entity))
            }
        }
    }
}