|
| 1 | +#Requires -Version 7.0 |
| 2 | +param( |
| 3 | + $filter, |
| 4 | + [bool]$Stubbed = $true, |
| 5 | + [bool]$LaunchOnly = $false |
| 6 | +) |
| 7 | + |
| 8 | +Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force; |
| 9 | + |
| 10 | +$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..') |
| 11 | +$solutionDir = Join-Path $packageRoot 'generator' |
| 12 | + |
| 13 | +if (-not $LaunchOnly) { |
| 14 | + Refresh-Build |
| 15 | + |
| 16 | + if ($null -eq $filter -or $filter -eq "Basic-TypeSpec") { |
| 17 | + Write-Host "Generating BasicTypeSpec" -ForegroundColor Cyan |
| 18 | + $testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local' |
| 19 | + |
| 20 | + $unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Basic-TypeSpec" |
| 21 | + $unbrandedTypespecTestProject = $unbrandedTypespecTestProject |
| 22 | + |
| 23 | + Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Basic-TypeSpec.tsp" $unbrandedTypespecTestProject) |
| 24 | + |
| 25 | + # exit if the generation failed |
| 26 | + if ($LASTEXITCODE -ne 0) { |
| 27 | + exit $LASTEXITCODE |
| 28 | + } |
| 29 | + |
| 30 | + Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan |
| 31 | + Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Basic-TypeSpec/src/BasicTypeSpec.csproj" |
| 32 | + |
| 33 | + # exit if the generation failed |
| 34 | + if ($LASTEXITCODE -ne 0) { |
| 35 | + exit $LASTEXITCODE |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +$specsDirectory = "$packageRoot/node_modules/@typespec/http-specs" |
| 41 | +$azureSpecsDirectory = "$packageRoot/node_modules/@azure-tools/azure-http-specs" |
| 42 | +$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch' |
| 43 | + |
| 44 | +function IsSpecDir { |
| 45 | + param ( |
| 46 | + [string]$dir |
| 47 | + ) |
| 48 | + $subdirs = Get-ChildItem -Path $dir -Directory |
| 49 | + return -not ($subdirs) -and (Test-Path "$dir/main.tsp") |
| 50 | +} |
| 51 | + |
| 52 | +$failingSpecs = @( |
| 53 | + Join-Path 'http' 'payload' 'pageable' |
| 54 | + Join-Path 'http' 'payload' 'xml' |
| 55 | + Join-Path 'http' 'type' 'model' 'flatten' |
| 56 | + Join-Path 'http' 'type' 'model' 'templated' |
| 57 | + Join-Path 'http' 'payload' 'multipart' |
| 58 | + Join-Path 'http' 'server' 'path' 'multiple' |
| 59 | + Join-Path 'http' 'server' 'versions' 'versioned' |
| 60 | + Join-Path 'http' 'type' 'union' |
| 61 | + Join-Path 'http' 'type' 'enum' 'extensible' |
| 62 | + Join-Path 'http' 'type' 'model' 'inheritance' 'enum-discriminator' |
| 63 | + Join-Path 'http' 'type' 'property' 'additional-properties' |
| 64 | + Join-Path 'http' 'type' 'property' 'optionality' |
| 65 | + Join-Path 'http' 'type' 'property' 'value-types' |
| 66 | + Join-Path 'http' 'versioning' 'added' |
| 67 | + Join-Path 'http' 'versioning' 'madeOptional' |
| 68 | + Join-Path 'http' 'versioning' 'removed' |
| 69 | + Join-Path 'http' 'versioning' 'renamedFrom' |
| 70 | + Join-Path 'http' 'versioning' 'returnTypeChangedFrom' |
| 71 | + Join-Path 'http' 'versioning' 'typeChangedFrom' |
| 72 | + Join-Path 'http' 'client' 'naming' |
| 73 | + Join-Path 'http' 'resiliency' 'srv-driven' |
| 74 | + Join-Path 'http' 'client' 'structure' 'client-operation-group' |
| 75 | + Join-Path 'http' 'client' 'structure' 'renamed-operation' |
| 76 | + Join-Path 'http' 'client' 'structure' 'multi-client' |
| 77 | + Join-Path 'http' 'client' 'structure' 'two-operation-group' |
| 78 | + Join-Path 'http' 'encode' 'bytes' |
| 79 | + Join-Path 'http' 'encode' 'datetime' |
| 80 | + Join-Path 'http' 'encode' 'duration' |
| 81 | + Join-Path 'http' 'parameters' 'collection-format' |
| 82 | + Join-Path 'http' 'routes' |
| 83 | + Join-Path 'http' 'type' 'array' |
| 84 | + Join-Path 'http' 'type' 'dictionary' |
| 85 | + Join-Path 'http' 'type' 'scalar' |
| 86 | +) |
| 87 | + |
| 88 | +$azureAllowSpecs = @( |
| 89 | + Join-Path 'http' 'client' 'naming' |
| 90 | + Join-Path 'http' 'client' 'structure' 'client-operation-group' |
| 91 | + Join-Path 'http' 'client' 'structure' 'default' |
| 92 | + Join-Path 'http' 'client' 'structure' 'multi-client' |
| 93 | + Join-Path 'http' 'client' 'structure' 'renamed-operation' |
| 94 | + Join-Path 'http' 'client' 'structure' 'two-operation-group' |
| 95 | + Join-Path 'http' 'resiliency' 'srv-driven' |
| 96 | +) |
| 97 | + |
| 98 | +$cadlRanchLaunchProjects = @{} |
| 99 | + |
| 100 | +# Loop through all directories and subdirectories of the cadl ranch specs |
| 101 | +$directories = @(Get-ChildItem -Path "$specsDirectory/specs" -Directory -Recurse) |
| 102 | +$directories += @(Get-ChildItem -Path "$azureSpecsDirectory/specs" -Directory -Recurse) |
| 103 | +foreach ($directory in $directories) { |
| 104 | + if (-not (IsSpecDir $directory.FullName)) { |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + $fromAzure = $directory.FullName.Contains("azure-http-specs") |
| 109 | + |
| 110 | + $specFile = Join-Path $directory.FullName "client.tsp" |
| 111 | + if (-not (Test-Path $specFile)) { |
| 112 | + $specFile = Join-Path $directory.FullName "main.tsp" |
| 113 | + } |
| 114 | + $subPath = if ($fromAzure) {$directory.FullName.Substring($azureSpecsDirectory.Length + 1)} else {$directory.FullName.Substring($specsDirectory.Length + 1)} |
| 115 | + $subPath = $subPath -replace '^specs', 'http' # Keep consistent with the previous folder name because 'http' makes more sense then current 'specs' |
| 116 | + $folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar) |
| 117 | + |
| 118 | + if (-not (Compare-Paths $subPath $filter)) { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if ($fromAzure -eq $true -and !$azureAllowSpecs.Contains($subPath)) { |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + if ($failingSpecs.Contains($subPath)) { |
| 127 | + Write-Host "Skipping $subPath" -ForegroundColor Yellow |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + $generationDir = $cadlRanchRoot |
| 132 | + foreach ($folder in $folders) { |
| 133 | + $generationDir = Join-Path $generationDir $folder |
| 134 | + } |
| 135 | + |
| 136 | + # create the directory if it doesn't exist |
| 137 | + if (-not (Test-Path $generationDir)) { |
| 138 | + New-Item -ItemType Directory -Path $generationDir | Out-Null |
| 139 | + } |
| 140 | + |
| 141 | + if ($folders.Contains("versioning")) { |
| 142 | + Generate-Versioning $directory.FullName $generationDir -generateStub $stubbed |
| 143 | + $cadlRanchLaunchProjects.Add($($folders -join "-") + "-v1", $("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v1") |
| 144 | + $cadlRanchLaunchProjects.Add($($folders -join "-") + "-v2", $("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v2") |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + # srv-driven contains two separate specs, for two separate clients. We need to generate both. |
| 149 | + if ($folders.Contains("srv-driven")) { |
| 150 | + Generate-Srv-Driven $directory.FullName $generationDir -generateStub $stubbed |
| 151 | + $cadlRanchLaunchProjects.Add($($folders -join "-") + "-v1", $("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v1") |
| 152 | + $cadlRanchLaunchProjects.Add($($folders -join "-") + "-v2", $("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v2") |
| 153 | + continue |
| 154 | + } |
| 155 | + |
| 156 | + $cadlRanchLaunchProjects.Add(($folders -join "-"), ("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))")) |
| 157 | + if ($LaunchOnly) { |
| 158 | + continue |
| 159 | + } |
| 160 | + |
| 161 | + Write-Host "Generating $subPath" -ForegroundColor Cyan |
| 162 | + Invoke (Get-TspCommand $specFile $generationDir $stubbed) |
| 163 | + |
| 164 | + # exit if the generation failed |
| 165 | + if ($LASTEXITCODE -ne 0) { |
| 166 | + exit $LASTEXITCODE |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +# only write new launch settings if no filter was passed in |
| 171 | +if ($null -eq $filter) { |
| 172 | + Write-Host "Writing new launch settings" -ForegroundColor Cyan |
| 173 | + $mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe" |
| 174 | + $sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe" |
| 175 | + $unbrandedSpec = "TestProjects/Local/Basic-TypeSpec" |
| 176 | + |
| 177 | + $launchSettings = @{} |
| 178 | + $launchSettings.Add("profiles", @{}) |
| 179 | + $launchSettings["profiles"].Add("Basic-TypeSpec", @{}) |
| 180 | + $launchSettings["profiles"]["Basic-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.dll `$(SolutionDir)/$unbrandedSpec -p AzureClientPlugin") |
| 181 | + $launchSettings["profiles"]["Basic-TypeSpec"].Add("commandName", "Executable") |
| 182 | + $launchSettings["profiles"]["Basic-TypeSpec"].Add("executablePath", "dotnet") |
| 183 | + |
| 184 | + foreach ($kvp in $cadlRanchLaunchProjects.GetEnumerator()) { |
| 185 | + $launchSettings["profiles"].Add($kvp.Key, @{}) |
| 186 | + $launchSettings["profiles"][$kvp.Key].Add("commandLineArgs", "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.dll `$(SolutionDir)/$($kvp.Value) -p AzureStubPlugin") |
| 187 | + $launchSettings["profiles"][$kvp.Key].Add("commandName", "Executable") |
| 188 | + $launchSettings["profiles"][$kvp.Key].Add("executablePath", "dotnet") |
| 189 | + } |
| 190 | + |
| 191 | + $sortedLaunchSettings = @{} |
| 192 | + $sortedLaunchSettings.Add("profiles", [ordered]@{}) |
| 193 | + $launchSettings["profiles"].Keys | Sort-Object | ForEach-Object { |
| 194 | + $profileKey = $_ |
| 195 | + $originalProfile = $launchSettings["profiles"][$profileKey] |
| 196 | + |
| 197 | + # Sort the keys inside each profile |
| 198 | + # This is needed due to non deterministic ordering of json elements in powershell |
| 199 | + $sortedProfile = [ordered]@{} |
| 200 | + $originalProfile.GetEnumerator() | Sort-Object Key | ForEach-Object { |
| 201 | + $sortedProfile[$_.Key] = $_.Value |
| 202 | + } |
| 203 | + |
| 204 | + $sortedLaunchSettings["profiles"][$profileKey] = $sortedProfile |
| 205 | + } |
| 206 | + |
| 207 | + # Write the launch settings to the launchSettings.json file |
| 208 | + $launchSettingsPath = Join-Path $solutionDir "Azure.Generator" "src" "Properties" "launchSettings.json" |
| 209 | + # Write the settings to JSON and normalize line endings to Unix style (LF) |
| 210 | + $sortedLaunchSettings | ConvertTo-Json | ForEach-Object { $_ -replace "`r`n", "`n" } | Set-Content $launchSettingsPath |
| 211 | +} |
0 commit comments