Testing/Install-BuildHelper.ps1

<#
.synopsis
    Installs the NAV-X Build Helper into a container
.description
    The NAV-X Build Helper is used to generate test suites. It will be replaced with new functionality. Do not use in new development
.parameter ContainerName
    Container to be used. Can be provided in the settings.json
.parameter Reinstall
    Add this switch to reinstall the app when already existing
.parameter BuildHelperVersion
    Version of NAV/BC to use. You can use "Detect" to let the system determine the version
.example
    Install-BuildHelper -ContainerName test -Reinstall
#>

function Install-BuildHelper {
    param (
        # Container name to install the Build Helper into
        [Parameter(Mandatory = $false)]
        [string]
        $ContainerName = (Get-ContainerFromLaunchJson),
        # Whether to force a reinstall of the app
        [Parameter(Mandatory = $false)]
        [switch]
        $Reinstall,
        # Version of the build helper app to install, use BC15 if not specified
        [Parameter(Mandatory = $false)]
        [string]
        [ValidateSet('Detect', 'NAV2018', 'BC13', 'BC14', 'BC15', 'BC16')]
        $BuildHelperVersion = 'Detect'
    )

    if ($BuildHelperVersion -eq 'Detect') {
        [Version]$PlatformVersion = [Version]::Parse((Get-AppKeyValue -SourcePath (Get-Location) -KeyName 'platform'))
        switch ($PlatformVersion.Major) {
            11 { $BuildHelperVersion = 'NAV2018' }
            13 { $BuildHelperVersion = 'BC13' }
            14 { $BuildHelperVersion = 'BC14' }
            15 { $BuildHelperVersion = 'BC15' }
            16 { $BuildHelperVersion = 'BC16' }
        }
    }

    $Install = $false

    if ($Reinstall.IsPresent) {
        $Install = $true
    }
    else {
        #test for the existence of the build helper service
        try {
            switch ($BuildHelperVersion) {
                'NAV2018' { $Uri = 'http://{0}:7047/NAV/WS/Codeunit/NavxAutomatedTestManagement' -f (Get-NavContainerIpAddress $ContainerName) }
                'BC13' { $Uri = 'http://{0}:7047/NAV/WS/Codeunit/NavxAutomatedTestManagement' -f (Get-NavContainerIpAddress $ContainerName) }
                'BC14' { $Uri = 'http://{0}:7047/NAV/WS/Codeunit/NavxAutomatedTestManagement' -f (Get-NavContainerIpAddress $ContainerName) }
                'BC15' { $Uri = 'http://{0}:7047/BC/WS/Codeunit/NavxAutomatedTestManagement' -f (Get-NavContainerIpAddress $ContainerName) }
                'BC16' { $Uri = 'http://{0}:7047/BC/WS/Codeunit/NavxAutomatedTestManagement' -f (Get-NavContainerIpAddress $ContainerName) }
            }
            Invoke-WebRequest $Uri -Credential (Get-CredentialFromEnvironmentJson)
        }
        catch {
            $Install = $true
        }
    }

    if ($Install) {
        $DestinationFile = Join-Path (New-TempDirectory) 'NavBuildHelper.app'
        switch ($BuildHelperVersion) {
            'NAV2018' { $SourceURL = 'https://navxdev.blob.core.windows.net/files/NAV-X_NAV-X%20Build%20Helper_1.0.0.0_NAV2018.app' }
            'BC13' { $SourceURL = 'https://navxdev.blob.core.windows.net/files/NAV-X_NAV-X%20Build%20Helper_1.0.0.0_BC13.app' }
            'BC14' { $SourceURL = 'https://navxdev.blob.core.windows.net/files/NAV-X_NAV-X%20Build%20Helper_1.0.0.0_BC14.app' }
            'BC15' { $SourceURL = 'https://navxdev.blob.core.windows.net/files/NAV-X_NAV-X%20Build%20Helper_1.0.0.0.app' }
            'BC16' { $SourceURL = 'https://navxdev.blob.core.windows.net/files/NAV-X_NAV-X%20Build%20Helper_1.0.0.0.app' }
        }

        Download-File -sourceUrl $SourceURL -destinationFile $DestinationFile
        Publish-NavContainerApp -containerName $ContainerName -appFile $DestinationFile -install -sync
    }
}

Export-ModuleMember -Function Install-BuildHelper