Scripts/Get-EarlyTypes.ps1

function Get-EarlyTypes {    
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )

    try {
        Write-Host "Running for path : " $StartPath
        Get-ConfigJSON($StartPath)
    
        Write-Host("Cleaning up Context Files...")

        $message = "Establishing connection to $global:devops_ServerUrl"
        Write-Host $message

        Get-DataverseLogin -requiresManagementApi $false

        $CurrentLocation = Get-Location

        $exclude = @('*.ps1', '*.config')

        $processXrmContext = $false
        $processXrmDefinitelyTyped = $false
        $processPACModelBuilder = $false

        if (Test-Path (Join-Path $StartPath "\$SelectedSolution\builderSettings.json")) {
            $processPACModelBuilder = $true
            Write-PPDOMessage "builderSettings.json found ... Using PAC Model Builder to Generate Early Bound Types" -Type command -RunLocally $true
            if ($global:devops_DataverseCredType -eq "servicePrincipal") {
                & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth delete --name ppdo
                # Added escaped quotes required to get secrets working that started with - (https://github.com/dylanhaskins/Capgemini.PowerPlatform.DevOps/issues/150)
                & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth create --applicationId $global:devops_ClientID --clientSecret `"$global:clientSecret`" --environment $global:devops_ServerUrl  --tenant $global:devops_TenantID  --name ppdo
                #--name $global:devops_SolutionName
            }
            if ($global:devops_DataverseCredType -eq "user") {
                & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth delete --name ppdo
                & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth create --username $global:devops_DataverseEmail --password $global:Password --environment $global:devops_ServerUrl --name ppdo
                #--name $global:devops_SolutionName
            }
            Remove-Item (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -Force -Recurse -ErrorAction Ignore
            Remove-Item (Join-Path $StartPath "\$SelectedSolution\Models") -Force -Recurse -ErrorAction Ignore

            try {
                Set-Location -Path (Join-Path $StartPath "\$SelectedSolution")
                $pacCLI = "$env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe"
                [Array]$PACarguments = "modelbuilder", "build", "--outdirectory", "Models", "--settingsTemplateFile", "builderSettings.json", "--environment", $global:devops_ServerUrl
                & $pacCLI $PACarguments 3>&1 | Tee-Object -Variable pacOutput

                                    
                if ($pacOutput -match "Error:") { Throw $pacOutput -match "Error:" }
                if ($pacOutput -match "FAILURE:") { Throw $pacOutput -match "FAILURE:" }
            }
            catch {
                Write-Host $pacOutput
                pause
            }
        }

        if ((Test-Path (Join-Path $StartPath "\$SelectedSolution\XrmContext")) -And !$processPACModelBuilder) {
            
            $processXrmContext = $true
            
            Remove-Item (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -Force -Recurse -ErrorAction Ignore
            New-Item -ItemType Directory -Path (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -ErrorAction Ignore        
    
            Install-XrmContext

            Set-Location -Path (Join-Path $StartPath "\$SelectedSolution\XrmContext")
            Copy-Item -Path $env:APPDATA\Capgemini.PowerPlatform.DevOps\XrmContext\*.* -Destination . -Exclude $exclude -Force -ErrorAction SilentlyContinue
            if ($global:devops_ClientId) {
                . .\XrmContext.exe /url:$global:devops_ServerUrl/XRMServices/2011/Organization.svc /useconfig /out:"../Entities/Context" /method:ClientSecret /mfaAppId:$global:devops_ClientId /mfaClientSecret:$global:clientSecret
            }
            else {
                if (!$Credentials) {
                    $message = "Getting Credentials for $global:devops_ServerUrl"
                    Write-Host $message
      
                    $Credentials = Get-Credential -Message "Credentials : $global:devops_SolutionName @ $global:devops_ServerUrl"
                }
                if (!$UserName) {
                    $UserName = $Credentials.GetNetworkCredential().UserName
                    $Password = $Credentials.GetNetworkCredential().Password
                } 
                . .\XrmContext.exe /url:$global:devops_ServerUrl/XRMServices/2011/Organization.svc /useconfig /out:"../Entities/Context" /username:$username /password:$password /method:OAuth /mfaAppId:"51f81489-12ee-4a9e-aaae-a2591f45987d" /mfaReturnUrl:"app://58145B91-0C36-4500-8554-080854F2AC97"
    
            }

        }
        else {
            Write-Host "No Folder Found at: $(Join-Path $StartPath "\$SelectedSolution\XrmContext"), Skipping Entity Type/XrmContext Generation"
        }

        if (Test-Path (Join-Path $StartPath "\$SelectedSolution\XrmDefinitelyTyped")) {

            $processXrmDefinitelyTyped = $true
            
            Remove-Item (Join-Path $StartPath "\$SelectedSolution\WebResources\typings\XRM") -Force -Recurse -ErrorAction Ignore
            New-Item -ItemType Directory -Path (Join-Path $StartPath "\$SelectedSolution\WebResources\typings\XRM") -ErrorAction Ignore

            Install-XrmDefinitelyTyped            

            Set-Location -Path (Join-Path $StartPath "\$SelectedSolution\XrmDefinitelyTyped")
            Copy-Item -Path $env:APPDATA\Capgemini.PowerPlatform.DevOps\XrmDefinitelyTyped\*.* -Destination . -Exclude $exclude -Force -ErrorAction SilentlyContinue
            if ($global:devops_ClientId) {
                . .\XrmDefinitelyTyped.exe /url:$global:devops_ServerUrl/XRMServices/2011/Organization.svc /useconfig /out:"../Webresources/typings/XRM" /jsLib:"../Webresources/src/library" /method:ClientSecret /mfaAppId:$global:devops_ClientId /mfaClientSecret:$global:clientSecret
            }
            else {
                . .\XrmDefinitelyTyped.exe /url:$global:devops_ServerUrl/XRMServices/2011/Organization.svc /useconfig /out:"../Webresources/typings/XRM" /jsLib:"../Webresources/src/library" /username:$username /password:$password /method:OAuth /mfaAppId:"51f81489-12ee-4a9e-aaae-a2591f45987d" /mfaReturnUrl:"app://58145B91-0C36-4500-8554-080854F2AC97"
            }
        }
        else {
            Write-Host "No Folder Found at: $(Join-Path $StartPath "\$SelectedSolution\XrmDefinitelyTyped"), Skipping TypeScript Generation"
        }

        if (!$processXrmContext -And !$processXrmDefinitelyTyped -And !$processPACModelBuilder) {
            Write-Host "Nothing To Process..."
            pause
            return
        }

        Set-Location -Path $CurrentLocation

        ##Add Files to Project
        [xml]$xdoc = (Get-Content (Join-Path $StartPath "\$SelectedSolution\$SelectedSolution.csproj"))
 
 
        [System.Xml.XmlNamespaceManager] $nsmgr = $xdoc.NameTable
        $nsmgr.AddNamespace('a', 'http://schemas.microsoft.com/developer/msbuild/2003')
 

        #Entities/ModelBuilder
        if ($processPACModelBuilder) {

            $nodes = $xdoc.SelectNodes("//a:Compile[contains(@Include,'Models')]", $nsmgr)
                
            for ($i = 0; $i -le ($nodes.Count - 1); $i++) {
                $nodes[$i].ParentNode.RemoveChild($nodes[$i])
            }
         
            $newnodes = $xdoc.SelectNodes("//a:Compile", $nsmgr)
            $addNode = $newnodes[0].Clone()
         
                
            Get-ChildItem (Join-Path $StartPath "\$SelectedSolution\Models") -Name -Recurse -File | ForEach-Object {
                $newnodes = $xdoc.SelectNodes("//a:Compile", $nsmgr)
                $addNode = $newnodes[0].Clone()
                $addNode.Include = "Models\$_"; $newnodes[0].ParentNode.AppendChild($addNode)
            }
        }

        #Entities/CRMContext
        if ($processXrmContext) {

            $nodes = $xdoc.SelectNodes("//a:Compile[contains(@Include,'Entities\Context')]", $nsmgr)
            
            for ($i = 0; $i -le ($nodes.Count - 1); $i++) {
                $nodes[$i].ParentNode.RemoveChild($nodes[$i])
            }
     
            $newnodes = $xdoc.SelectNodes("//a:Compile", $nsmgr)
            $addNode = $newnodes[0].Clone()
     
            
            Get-ChildItem (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -Name | ForEach-Object {
                $newnodes = $xdoc.SelectNodes("//a:Compile", $nsmgr)
                $addNode = $newnodes[0].Clone()
                $addNode.Include = "Entities\Context\$_"; $newnodes[0].ParentNode.AppendChild($addNode)
            }
        }

        if ($processXrmDefinitelyTyped) {

            #TypeScript Nodes
            $nodes = $xdoc.SelectNodes("//a:TypeScriptCompile[contains(@Include,'WebResources\typings')]", $nsmgr)
            for ($i = 0; $i -le ($nodes.Count - 1); $i++) {
                $nodes[$i].ParentNode.RemoveChild($nodes[$i])
            }
    
            $newnodes = $xdoc.SelectNodes("//a:TypeScriptCompile", $nsmgr)
            $addNode = $newnodes[0].Clone()
    
            Get-ChildItem (Join-Path $StartPath "\$SelectedSolution\WebResources\typings") -Name -Recurse -File | ForEach-Object {
                $newnodes = $xdoc.SelectNodes("//a:TypeScriptCompile", $nsmgr)
                $addNode = $newnodes[0].Clone()
                $addNode.Include = "WebResources\typings\$_"; $newnodes[0].ParentNode.AppendChild($addNode)
            }
        }
    
        $xdoc.Save((Join-Path $StartPath "\$SelectedSolution\$SelectedSolution.csproj"))
        pause
    }

    catch {
        Write-Host $_
        pause
    }

}