AL/Add-TestAppsToAppJson.ps1

<#
 .Synopsis
  Add Test apps to app.json
 .Description
  Test apps defined in the settings.json are added to the app.json for the test app
 .Parameter SourcePath
  Path to the project including settings.json and app.json
 .Example
  Add-TestAppsToAppJson -SourcePath 'C:\'
#>

function Add-TestAppsToAppJson {
    Param(
        #Path to determine the environment or app .json file location
        [Parameter(Mandatory = $false)]
        [String]
        $SourcePath = (Get-Location)
    )

    #Get test apps property from settings.json
    $TempTestappsJsonContent = Get-EnvironmentKeyValue -SourcePath $SourcePath -KeyName 'testapps'
    $TestappsVersion = Get-AppKeyValue -SourcePath $SourcePath -KeyName "application"

    # add standard test apps to the list above
    $TestappsJsonContent = Add-StandardTestApp -TestApps $TempTestappsJsonContent -Version $TestappsVersion

    if ($TestappsJsonContent.Length -gt 0) {
        #app has no current dependencies, add the test dependencies directly
        $AppJsonContent = Get-Content (Join-Path $SourcePath 'app.json') -Raw
        $AppJson = ConvertFrom-Json $AppJsonContent
        $Dependencies = Get-AppKeyValue -SourcePath $SourcePath -KeyName 'dependencies'
        if ($Dependencies -eq '') {
            $AppJson | Add-Member -Name 'dependencies' -value $TestappsJsonContent -MemberType NoteProperty
        }
        else {
            #Check the test app dependency isnt already in the app.json
            $AppJson.PSObject.Properties.Remove('dependencies')

            if ($null -eq $Dependencies ) {
                $AppJson | Add-Member -Name 'dependencies' -value $TestappsJsonContent -MemberType NoteProperty
            }
            else {
                $compiler = Get-AppKeyValue -SourcePath $TestPath -KeyName 'runtime'
                [System.Array]$Dependencies = $Dependencies
                foreach ($TestDependency in $TestappsJsonContent) {
                    $SkipDependency = $false
                    foreach ($Dependency in $Dependencies) {
                        if (!$SkipDependency) {
                            if ($compiler -ge '4.3') {
                                if ($Dependency.id -eq $TestDependency.appId) {
                                    $SkipDependency = $true
                                }
                            }
                            else {
                                if ($Dependency.appId -eq $TestDependency.appId) {
                                    $SkipDependency = $true
                                }
                            }
                        }
                    }
                    if (!$SkipDependency) {
                        $Dependencies += $TestDependency
                    }
                }
                $AppJson | Add-Member -Name 'dependencies' -value $Dependencies -MemberType NoteProperty
            }
        }
        Set-Content -Path (Join-Path $SourcePath 'app.json') -Value (ConvertTo-Json $AppJson)
    }
}