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