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