Scripts/Scripting.ps1


function CmxGetPendingChanges()
{
    $result = GetPendingChanges

    $exitCode = $result.ExitCode

    if ($exitCode -ne 0)
    {
        Write-Output "ExitCode = $exitCode"
        Write-Output "$($result.StandardError)"
        Write-Output "$($result.StandardOutput)"
    }
    if ($result.ExitedInTime -eq $false)
    {
        Write-Output "The process didn't exit it time!"
    }

    [array]$changeList = ConvertPendingChangesOutputToChangeList($result.StandardOutput)
    Write-Output ""
    Write-Output "ChangeCount: $($changeList.Count)"
    if ($changeList.Count -gt 0)
    {
        $changeList | Format-Table LocalPath, ChangeType
    }
}

function CmxGetBuild
{
    <#
    .SYNOPSIS
    Downloads the latest succeeded build of the given build definition into the given target folder.
     
    .PARAMETER BuildDefinition
    The build definition from which to get the build.
    If this argument is missing, then the default value "TPE.Step7_Safety_T.Rolling" is used.
     
    .PARAMETER TargetFolder
    The target folder to which the build is copied.
    If this argument is missing, the default value "$LocalWorkspace\Binaries\Release\x64_download"
    is used.
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(HelpMessage = "The help on the build def")]
        [string]$BuildDefinition,
        [string]$TargetFolder
    )

    [PsCustomObject]$result = @{
        Succeeded = $false
        Duration = $null
    }

    if([string]::IsNullOrEmpty($BuildDefinition))
    {
        $BuildDefinition = "TPE.Step7_Safety_T.Rolling"
        Write-Verbose "Use default build definition"
    }

    if([string]::IsNullOrEmpty($TargetFolder))
    {
        $TargetFolder = "$LocalWorkspace\Binaries\Release\x64_download"
        Write-Verbose "Use default target folder"
    }

    Write-Verbose "BuildDefinition: $BuildDefinition"
    Write-Verbose "TargetFolder: $TargetFolder"
    
    if (ExistsPath $TargetFolder)
    {
        Write-Verbose "The download can not start as the target folder already exists."
        $result.Succeeded = $false
        return $result
    }

    Write-Verbose "Try to get the build folder by build definition."
    $sourceFolder = CmxGetDropFolderByBuildDefinition -BuildDefinition $BuildDefinition
    Write-Verbose "SourceFolder: $sourceFolder"
    if (!(ExistsPath $sourceFolder))
    {
        Write-Verbose "Try to get the build folder by searching the drop folders."
        $sourceFolder = GetLatestSucceededBuild
        if (!(ExistsPath $sourceFolder))
        {
            Write-Verbose "The download can not start as the source folder does not exist."
            $result.Succeeded = $false
            return $result
        }
    }

    Write-Verbose "Download has started."
    $downloadResult = CmxDownloadBuild -BuildFolder "$sourceFolder" -CopyTarget "$TargetFolder"
    Write-Verbose "Download has finished."

    $result.Succeeded = $downloadResult.Succeeded
    $result.Duration = $downloadResult.TimeSpan
    return $result
}

function CmxGetLatest 
{
    [CmdletBinding()]
    param()

    Write-Output "This script gets the latest changes from the TFS server."
    $workspace = $LocalWorkspace
    $changeset = "T" # T = Gets the latest changeset
    Write-Output "Changeset: $changeset"
    Write-Output "Workspace: $workspace"

    Write-Output "Run preview . . . "
    $result = PreviewGetLatestByVersion -RootDir $workspace -ItemVersion $changeset
    Write-Output "Run preview has finished"

    if ($result.ExitCode -ne 0)
    {
        $previewResultString = "The preview detected issues, so the real command is not executed. "
        $previewResultString += "Please fix these issues first, then try again."
        Write-Output "PreviewResultString: $previewResultString"

        Write-Output ""
        Write-Output "ExitCode: $($result.ExitCode)"
        Write-Output ""
        Write-Output "Issues: "
        Write-Output "$($result.StandardError)"
    }
    else
    {
        $lineCount = GetLineCount($result.StandardOutput)
        if ($lineCount -le 1)
        {
            Write-Output "Your workspace is already up-to-date."
        }
        else
        {
            Write-Output "Run command . . . "
            $realResult = GetLatestByVersion -RootDir $workspace -ItemVersion $changeset
            Write-Output "Run command has finished"
            if ($realResult.ExitCode -ne 0)
            {
                $realResultString = "The command did not succeed. Please see the issues."
                Write-Output "RealResultString: $realResultString"

                Write-Output ""
                Write-Output "ExitCode: $($realResult.ExitCode)"
                Write-Output ""
                Write-Output "Issues: "
                Write-Output "$($realResult.StandardError)"
            }
            else
            {
                $realResultString = "The command succeeded."
                Write-Output "RealResultString: $realResultString"          
            } 
        }
    }
}