openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Invoke-CodeGen.ps1
325lines · modeblame
9320b2aaShivangiReja1 years ago | 1 | [CmdletBinding(DefaultParameterSetName = 'Default')] |
dff9bb58Jose Arriaga Maldonado1 years ago | 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')] | |
3ab4a3c4Jose Arriaga Maldonado1 years ago | 10 | [string]$CommitHash = "706a4fb91ea18b441723f8a57a7e2c67e3061bb6", |
dff9bb58Jose Arriaga Maldonado1 years ago | 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)] | |
84fb83b2Jorge Rangel10 months ago | 19 | [switch]$Force, |
| 20 | | |
| 21 | [Parameter(Mandatory = $false)] | |
| 22 | [switch]$Clean | |
dff9bb58Jose Arriaga Maldonado1 years ago | 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 | |
76356c85JoshLove-msft9 months ago | 35 | $exitCode = $LASTEXITCODE |
dff9bb58Jose Arriaga Maldonado1 years ago | 36 | Write-Host "" |
76356c85JoshLove-msft9 months ago | 37 | |
| 38 | if ($exitCode -ne 0) { | |
| 39 | throw "Command failed with exit code $exitCode" | |
| 40 | } | |
dff9bb58Jose Arriaga Maldonado1 years ago | 41 | } |
| 42 | | |
7af490a0JoshLove-msft11 months ago | 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 | | |
dff9bb58Jose Arriaga Maldonado1 years ago | 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 | | |
9320b2aaShivangiReja1 years ago | 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" | |
dff9bb58Jose Arriaga Maldonado1 years ago | 248 | Write-Host "" |
9320b2aaShivangiReja1 years ago | 249 | } |
| 250 | else { | |
| 251 | $shouldDownload = $true | |
dff9bb58Jose Arriaga Maldonado1 years ago | 252 | |
9320b2aaShivangiReja1 years ago | 253 | if (Test-Path $baseSpecificationFolderPath) { |
| 254 | Write-Host "Base specification already exists at: $baseSpecificationFolderPath" | |
dff9bb58Jose Arriaga Maldonado1 years ago | 255 | Write-Host "" |
9320b2aaShivangiReja1 years ago | 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 | } | |
dff9bb58Jose Arriaga Maldonado1 years ago | 265 | } |
| 266 | | |
9320b2aaShivangiReja1 years ago | 267 | if ($shouldDownload) { |
| 268 | Write-Host "Retrieving base specification..." | |
| 269 | Write-Host "" | |
dff9bb58Jose Arriaga Maldonado1 years ago | 270 | |
9320b2aaShivangiReja1 years ago | 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 | } | |
dff9bb58Jose Arriaga Maldonado1 years ago | 284 | |
9320b2aaShivangiReja1 years ago | 285 | if (-not $success) { |
| 286 | Write-Error "Failed to get repository contents." | |
| 287 | throw "Repository content retrieval failed." | |
| 288 | } | |
dff9bb58Jose Arriaga Maldonado1 years ago | 289 | } |
| 290 | } | |
| 291 | | |
7af490a0JoshLove-msft11 months ago | 292 | Write-ElapsedTime "Spec retrieved" |
| 293 | | |
dff9bb58Jose Arriaga Maldonado1 years ago | 294 | Push-Location $repoRootPath |
| 295 | | |
| 296 | try { | |
| 297 | Invoke-ScriptWithLogging { npm ci } | |
994e843fJoshLove-msft11 months ago | 298 | if ($LASTEXITCODE -ne 0) { |
| 299 | exit $LASTEXITCODE | |
| 300 | } | |
| 301 | | |
7af490a0JoshLove-msft11 months ago | 302 | Write-ElapsedTime "npm ci complete" |
| 303 | | |
84fb83b2Jorge Rangel10 months ago | 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 | | |
dff9bb58Jose Arriaga Maldonado1 years ago | 312 | Invoke-ScriptWithLogging { npm run build -w $codegenFolderPath } |
994e843fJoshLove-msft11 months ago | 313 | if ($LASTEXITCODE -ne 0) { |
| 314 | exit $LASTEXITCODE | |
| 315 | } | |
7af490a0JoshLove-msft11 months ago | 316 | Write-ElapsedTime "npm run build complete" |
dff9bb58Jose Arriaga Maldonado1 years ago | 317 | |
| 318 | Set-Location $specificationFolderPath | |
936e6984JoshLove-msft11 months ago | 319 | Invoke-ScriptWithLogging { npx tsp compile . --stats --trace @typespec/http-client-csharp } |
dff9bb58Jose Arriaga Maldonado1 years ago | 320 | } |
| 321 | finally { | |
| 322 | Pop-Location | |
| 323 | } | |
| 324 | | |
7af490a0JoshLove-msft11 months ago | 325 | Write-ElapsedTime "Script completed" |