AL/Remove-ALTestDependencyList.ps1

<#
 .Synopsis
  removes test dependencies from app.json
 .Description
  removes all test dependencies from app.json
 .Parameter SourcePath
  Path to the current project
 .Example
  Remove-ALTestDependencyList
#>

function Remove-ALTestDependencyList {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact="low")]
    Param(
        [Parameter(Mandatory = $false)]
        [string] $sourcePath = (Get-Location)
    )

    $TempTestApps = Get-EnvironmentKeyValue -SourcePath $sourcePath -KeyName "testapps"
    $TestAppsVersion = Get-AppKeyValue -SourcePath $sourcePath -KeyName "application"
    $dependencies = Get-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies"

    $testapps = Add-StandardTestApp -TestApps $TempTestApps -Version $TestAppsVersion

    [Version]$PlatformVersion = [Version]::Parse((Get-AppKeyValue -SourcePath $sourcePath -KeyName 'platform'))
    $compiler = Get-AppKeyValue -SourcePath $TestPath -KeyName 'runtime'
    if ($PSCmdlet.ShouldProcess("app.json", "Removes test apps from app.json")) {
        if ($compiler -ge '4.3') {
            try {
                $dependencies = [System.Array]($dependencies | Where-Object { $testapps.appId -notcontains $_.Id })
                if ($null -eq $dependencies) {
                    Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue @()
                }
                else {
                    Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue $dependencies #(ConvertTo-Json $dependencies -Compress)
                }
            }
            catch {
                throw "Could not remove test dependencies"
            }
        }
        else {
            if ($PlatformVersion.Major -ge 15) {
                try {
                    $dependencies = [System.Array]($dependencies | Where-Object { $testapps.appId -notcontains $_.appId })
                    if ($null -eq $dependencies) {
                        Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue @()
                    }
                    else {
                        Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue $dependencies
                    }
                }
                catch {
                    throw "Could not remove test dependencies"
                }
            }
            else {
                Set-AppKeyValue -SourcePath $sourcePath -KeyName test -KeyValue ''
            }
        }
    }
}