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...")
        Remove-Item (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -Force -Recurse -ErrorAction Ignore
        Remove-Item (Join-Path $StartPath "\$SelectedSolution\WebResources\typings\XRM") -Force -Recurse -ErrorAction Ignore

        New-Item -ItemType Directory -Path (Join-Path $StartPath "\$SelectedSolution\Entities\Context") -ErrorAction Ignore
        New-Item -ItemType Directory -Path (Join-Path $StartPath "\$SelectedSolution\WebResources\typings\XRM") -ErrorAction Ignore

        Install-XrmDefinitelyTyped
        Install-XrmContext

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

        Get-DataverseLogin -requiresManagementApi $false

        $CurrentLocation = Get-Location

        $exclude = @('*.ps1', '*.config')
        if (Test-Path (Join-Path $StartPath "\$SelectedSolution\XrmContext")) {
            Set-Location -Path (Join-Path $StartPath "\$SelectedSolution\XrmContext")
            Copy-Item -Path $env:APPDATA\Microsoft.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"
    
            }

        }

        if (Test-Path (Join-Path $StartPath "\$SelectedSolution\XrmDefinitelyTyped")) {
            Set-Location -Path (Join-Path $StartPath "\$SelectedSolution\XrmDefinitelyTyped")
            Copy-Item -Path $env:APPDATA\Microsoft.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"
            }
        }
        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')
 
        $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)
        }

        #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"))
    
    }

    catch {
        Write-Host $_
        pause
    }

}