openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Invoke-CodeGen.ps1
345lines · modecode
| 1 | [CmdletBinding(DefaultParameterSetName = 'Default')] |
| 2 | param( |
| 3 | [Parameter(Mandatory = $true, ParameterSetName = 'GitHub')] |
| 4 | [string]$GitHubOwner, |
| 5 | |
| 6 | [Parameter(Mandatory = $true, ParameterSetName = 'GitHub')] |
| 7 | [string]$GitHubRepository, |
| 8 | |
| 9 | [Parameter(Mandatory = $false, ParameterSetName = 'GitHub')] |
| 10 | [string]$CommitHash = "706a4fb91ea18b441723f8a57a7e2c67e3061bb6", |
| 11 | |
| 12 | [Parameter(Mandatory = $false, ParameterSetName = 'GitHub')] |
| 13 | [string]$GitHubToken, |
| 14 | |
| 15 | [Parameter(Mandatory = $true, ParameterSetName = 'Local')] |
| 16 | [string]$LocalRepositoryPath, |
| 17 | |
| 18 | [Parameter(Mandatory = $false)] |
| 19 | [switch]$Force, |
| 20 | |
| 21 | [Parameter(Mandatory = $false)] |
| 22 | [switch]$Clean, |
| 23 | |
| 24 | [Parameter(Mandatory = $false)] |
| 25 | [switch]$SkipExperimentalValidation |
| 26 | ) |
| 27 | |
| 28 | function Invoke-ScriptWithLogging { |
| 29 | [CmdletBinding()] |
| 30 | param( |
| 31 | [Parameter(Mandatory = $true)] |
| 32 | [scriptblock]$Script |
| 33 | ) |
| 34 | |
| 35 | $scriptString = $Script | Out-String |
| 36 | Write-Host "--------------------------------------------------------------------------------`n> $scriptString" |
| 37 | & $Script |
| 38 | $exitCode = $LASTEXITCODE |
| 39 | Write-Host "" |
| 40 | |
| 41 | if ($exitCode -ne 0) { |
| 42 | throw "Command failed with exit code $exitCode" |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | $script:startTime = Get-Date |
| 48 | $scriptName = $MyInvocation.MyCommand.Name |
| 49 | |
| 50 | function Write-ElapsedTime { |
| 51 | [CmdletBinding()] |
| 52 | param( |
| 53 | [Parameter(Mandatory = $true)] |
| 54 | [string]$Message |
| 55 | ) |
| 56 | |
| 57 | $elapsedTime = [math]::Round(((Get-Date) - $script:startTime).TotalSeconds, 1) |
| 58 | Write-Host "[${scriptName}] ${Message}. Total elapsed time: $elapsedTime seconds." |
| 59 | Write-Host "" |
| 60 | } |
| 61 | |
| 62 | function Get-GitHubApiHeaders { |
| 63 | param( |
| 64 | [Parameter(Mandatory = $false)] |
| 65 | [string]$GitHubToken |
| 66 | ) |
| 67 | |
| 68 | $headers = @{ |
| 69 | 'Accept' = 'application/vnd.github+json' |
| 70 | 'X-GitHub-Api-Version' = '2022-11-28' |
| 71 | } |
| 72 | |
| 73 | if ($GitHubToken) { |
| 74 | $headers.Authorization = "Bearer $GitHubToken" |
| 75 | } |
| 76 | |
| 77 | return $headers |
| 78 | } |
| 79 | |
| 80 | function Get-TempDownloadPath { |
| 81 | $tempPath = [System.IO.Path]::GetTempPath() |
| 82 | return Join-Path $tempPath ([System.IO.Path]::GetRandomFileName()) |
| 83 | } |
| 84 | |
| 85 | function Test-GitHubRepoExists { |
| 86 | param( |
| 87 | [Parameter(Mandatory = $true)] |
| 88 | [string]$GitHubOwner, |
| 89 | |
| 90 | [Parameter(Mandatory = $true)] |
| 91 | [string]$GitHubRepository, |
| 92 | |
| 93 | [Parameter(Mandatory = $false)] |
| 94 | [string]$GitHubToken |
| 95 | ) |
| 96 | |
| 97 | $apiUrl = "https://api.github.com/repos/$GitHubOwner/$GitHubRepository" |
| 98 | try { |
| 99 | $headers = Get-GitHubApiHeaders -GitHubToken $GitHubToken |
| 100 | Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers -ErrorAction Stop |
| 101 | return $true |
| 102 | } |
| 103 | catch { |
| 104 | Write-Warning "Repository check failed: $_" |
| 105 | return $false |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | function Get-GitHubRepoContent { |
| 110 | [CmdletBinding()] |
| 111 | param( |
| 112 | [Parameter(Mandatory = $true)] |
| 113 | [string]$GitHubOwner, |
| 114 | |
| 115 | [Parameter(Mandatory = $true)] |
| 116 | [string]$GitHubRepository, |
| 117 | |
| 118 | [Parameter(Mandatory = $true)] |
| 119 | [string]$CommitHash, |
| 120 | |
| 121 | [Parameter(Mandatory = $false)] |
| 122 | [string]$GitHubToken, |
| 123 | |
| 124 | [Parameter(Mandatory = $true)] |
| 125 | [string]$SubdirectoryPath, |
| 126 | |
| 127 | [Parameter(Mandatory = $true)] |
| 128 | [string]$Destination |
| 129 | ) |
| 130 | |
| 131 | # Validate if the repository exists |
| 132 | if (-not (Test-GitHubRepoExists -GitHubOwner $GitHubOwner -GitHubRepository $GitHubRepository -GitHubToken $GitHubToken)) { |
| 133 | Write-Error "Repository '$GitHubOwner/$GitHubRepository' does not exist or is not accessible." |
| 134 | return $false |
| 135 | } |
| 136 | |
| 137 | # Create temporary directory for download |
| 138 | $downloadPath = Get-TempDownloadPath |
| 139 | New-Item -ItemType Directory -Path $downloadPath -Force | Out-Null |
| 140 | |
| 141 | try { |
| 142 | # Construct the download URL using GitHub API endpoint |
| 143 | $archiveUrl = "https://api.github.com/repos/$GitHubOwner/$GitHubRepository/zipball/$CommitHash" |
| 144 | $zipPath = Join-Path $downloadPath "repo.zip" |
| 145 | |
| 146 | # Get GitHub API headers with optional token |
| 147 | $headers = Get-GitHubApiHeaders -GitHubToken $GitHubToken |
| 148 | |
| 149 | # Download the repository |
| 150 | Write-Host "Downloading repository archive from $GitHubOwner/$GitHubRepository @ $CommitHash..." |
| 151 | Write-Host "" |
| 152 | Invoke-WebRequest -Uri $archiveUrl -OutFile $zipPath -Headers $headers |
| 153 | |
| 154 | # Extract the contents |
| 155 | Write-Host "Extracting repository contents..." |
| 156 | Write-Host "" |
| 157 | Expand-Archive -Path $zipPath -DestinationPath $downloadPath |
| 158 | |
| 159 | # Get the extracted folder name (it will be repository-commithash) |
| 160 | $extractedFolder = Get-ChildItem -Path $downloadPath -Directory | Select-Object -First 1 |
| 161 | |
| 162 | # Create the destination directory if it doesn't exist |
| 163 | New-Item -ItemType Directory -Path $Destination -Force | Out-Null |
| 164 | |
| 165 | $sourcePath = Join-Path $extractedFolder.FullName $SubdirectoryPath |
| 166 | if (-not (Test-Path $sourcePath)) { |
| 167 | Write-Error "Specified path '$SubdirectoryPath' does not exist in the repository." |
| 168 | return $false |
| 169 | } |
| 170 | |
| 171 | # Copy the contents directly to destination, preserving only the internal structure |
| 172 | if (Test-Path $sourcePath -PathType Container) { |
| 173 | Copy-Item -Path "$sourcePath\*" -Destination $Destination -Recurse -Force |
| 174 | } |
| 175 | else { |
| 176 | Copy-Item -Path $sourcePath -Destination $Destination -Force |
| 177 | } |
| 178 | |
| 179 | Write-Host "Downloaded repository contents to: $Destination" |
| 180 | Write-Host "" |
| 181 | return $true |
| 182 | } |
| 183 | catch { |
| 184 | Write-Error "An error occurred: $_" |
| 185 | return $false |
| 186 | } |
| 187 | finally { |
| 188 | # Cleanup temporary files |
| 189 | if (Test-Path $downloadPath) { |
| 190 | Remove-Item -Path $downloadPath -Recurse -Force |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function Get-LocalRepoContent { |
| 196 | [CmdletBinding()] |
| 197 | param( |
| 198 | [Parameter(Mandatory = $true)] |
| 199 | [string]$LocalRepositoryPath, |
| 200 | |
| 201 | [Parameter(Mandatory = $true)] |
| 202 | [string]$SubdirectoryPath, |
| 203 | |
| 204 | [Parameter(Mandatory = $true)] |
| 205 | [string]$Destination |
| 206 | ) |
| 207 | |
| 208 | try { |
| 209 | $sourcePath = Join-Path $LocalRepositoryPath $SubdirectoryPath |
| 210 | |
| 211 | if (-not (Test-Path $sourcePath)) { |
| 212 | Write-Error "Specified path '$SubdirectoryPath' does not exist in the local repository at: $LocalRepositoryPath" |
| 213 | return $false |
| 214 | } |
| 215 | |
| 216 | # Create the destination directory if it doesn't exist |
| 217 | New-Item -ItemType Directory -Path $Destination -Force | Out-Null |
| 218 | |
| 219 | # Copy the contents directly to destination, preserving only the internal structure |
| 220 | if (Test-Path $sourcePath -PathType Container) { |
| 221 | Copy-Item -Path "$sourcePath\*" -Destination $Destination -Recurse -Force |
| 222 | } |
| 223 | else { |
| 224 | Copy-Item -Path $sourcePath -Destination $Destination -Force |
| 225 | } |
| 226 | |
| 227 | Write-Host "Copied repository contents to: $Destination" |
| 228 | Write-Host "" |
| 229 | return $true |
| 230 | } |
| 231 | catch { |
| 232 | Write-Error "An error occurred: $_" |
| 233 | return $false |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $repoRootPath = Join-Path $PSScriptRoot .. -Resolve |
| 238 | $specificationFolderPath = Join-Path $repoRootPath "specification" |
| 239 | $baseSpecificationFolderPath = Join-Path $repoRootPath "specification\base" |
| 240 | $codegenFolderPath = Join-Path $repoRootPath "codegen" |
| 241 | |
| 242 | $scriptStartTime = Get-Date |
| 243 | |
| 244 | if ($PSCmdlet.ParameterSetName -eq 'Default') { |
| 245 | if (-not (Test-Path $baseSpecificationFolderPath)) { |
| 246 | Write-Error "Base specification path does not exist: $baseSpecificationFolderPath" |
| 247 | throw "Default specification path not found" |
| 248 | } |
| 249 | |
| 250 | Write-Host "Using existing base specification at: $baseSpecificationFolderPath" |
| 251 | Write-Host "" |
| 252 | } |
| 253 | else { |
| 254 | $shouldDownload = $true |
| 255 | |
| 256 | if (Test-Path $baseSpecificationFolderPath) { |
| 257 | Write-Host "Base specification already exists at: $baseSpecificationFolderPath" |
| 258 | Write-Host "" |
| 259 | |
| 260 | if ($Force) { |
| 261 | Write-Host "Overwriting existing base specification..." |
| 262 | Write-Host "" |
| 263 | Remove-Item -Path $baseSpecificationFolderPath -Recurse -Force |
| 264 | } |
| 265 | else { |
| 266 | $shouldDownload = $false |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if ($shouldDownload) { |
| 271 | Write-Host "Retrieving base specification..." |
| 272 | Write-Host "" |
| 273 | |
| 274 | if ($PSCmdlet.ParameterSetName -eq 'GitHub') { |
| 275 | $success = Get-GitHubRepoContent -GitHubOwner $GitHubOwner ` |
| 276 | -GitHubRepository $GitHubRepository ` |
| 277 | -CommitHash $CommitHash ` |
| 278 | -GitHubToken $GitHubToken ` |
| 279 | -SubdirectoryPath "openai-in-typespec" ` |
| 280 | -Destination $baseSpecificationFolderPath |
| 281 | } |
| 282 | elseif ($PSCmdlet.ParameterSetName -eq 'Local') { |
| 283 | $success = Get-LocalRepoContent -LocalRepositoryPath $LocalRepositoryPath ` |
| 284 | -SubdirectoryPath "openai-in-typespec" ` |
| 285 | -Destination $baseSpecificationFolderPath |
| 286 | } |
| 287 | |
| 288 | if (-not $success) { |
| 289 | Write-Error "Failed to get repository contents." |
| 290 | throw "Repository content retrieval failed." |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | Write-ElapsedTime "Spec retrieved" |
| 296 | |
| 297 | Push-Location $repoRootPath |
| 298 | |
| 299 | try { |
| 300 | Invoke-ScriptWithLogging { npm ci } |
| 301 | if ($LASTEXITCODE -ne 0) { |
| 302 | exit $LASTEXITCODE |
| 303 | } |
| 304 | |
| 305 | Write-ElapsedTime "npm ci complete" |
| 306 | |
| 307 | if ($Clean) { |
| 308 | Invoke-ScriptWithLogging { npm run clean -w $codegenFolderPath } |
| 309 | if ($LASTEXITCODE -ne 0) { |
| 310 | exit $LASTEXITCODE |
| 311 | } |
| 312 | Write-ElapsedTime "npm run clean complete" |
| 313 | } |
| 314 | |
| 315 | Invoke-ScriptWithLogging { npm run build -w $codegenFolderPath } |
| 316 | if ($LASTEXITCODE -ne 0) { |
| 317 | exit $LASTEXITCODE |
| 318 | } |
| 319 | Write-ElapsedTime "npm run build complete" |
| 320 | |
| 321 | Set-Location $specificationFolderPath |
| 322 | Invoke-ScriptWithLogging { npx tsp compile . --stats --trace @typespec/http-client-csharp } |
| 323 | |
| 324 | Write-ElapsedTime "tsp compile complete" |
| 325 | |
| 326 | # Validate that all public APIs in stable classes have correct [Experimental] decoration. |
| 327 | # This catches custom code that bypasses the code generator's ExperimentalAttributeVisitor. |
| 328 | if ($SkipExperimentalValidation) { |
| 329 | Write-Host "Skipping experimental attribute validation (-SkipExperimentalValidation)." -ForegroundColor Yellow |
| 330 | } |
| 331 | else { |
| 332 | $validationScript = Join-Path $repoRootPath "scripts" "Test-ExperimentalAttributes.ps1" |
| 333 | Write-Host "Running experimental attribute validation..." -ForegroundColor Cyan |
| 334 | Write-Host "" |
| 335 | & pwsh -NoProfile -File $validationScript |
| 336 | if ($LASTEXITCODE -ne 0) { |
| 337 | throw "Experimental attribute validation failed with exit code $LASTEXITCODE" |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | finally { |
| 342 | Pop-Location |
| 343 | } |
| 344 | |
| 345 | Write-ElapsedTime "Script completed" |