openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Export-Api.ps1
186lines · modecode
| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Generates the public API surface for the OpenAI .NET library using GenAPI. |
| 4 | |
| 5 | .DESCRIPTION |
| 6 | This script invokes the MSBuild GenerateApi target to produce C# source files |
| 7 | representing the public API contract of the OpenAI library. The output files |
| 8 | are placed in the 'api' folder at the repository root. |
| 9 | |
| 10 | .EXAMPLE |
| 11 | .\Export-Api.ps1 |
| 12 | Generates API for all target frameworks defined in |
| 13 | ClientTargetFrameworks (Directory.Build.props) using the Release configuration. |
| 14 | |
| 15 | .NOTES |
| 16 | Outputs are written to api/OpenAI.<TargetFramework>.cs |
| 17 | #> |
| 18 | |
| 19 | [CmdletBinding()] |
| 20 | param( |
| 21 | ) |
| 22 | |
| 23 | $ErrorActionPreference = "Stop" |
| 24 | |
| 25 | $configuration = "Release" |
| 26 | |
| 27 | # Resolve paths |
| 28 | $repoRootPath = Join-Path $PSScriptRoot ".." -Resolve |
| 29 | $projectPath = Join-Path $repoRootPath "src" "OpenAI.csproj" |
| 30 | $outputDirectory = Join-Path $repoRootPath "api" |
| 31 | |
| 32 | # Get ClientTargetFrameworks from Directory.Build.props |
| 33 | $propsPath = Join-Path $repoRootPath "Directory.Build.props" |
| 34 | $clientTargetFrameworks = "" |
| 35 | if (Test-Path $propsPath) { |
| 36 | $propsContent = Get-Content $propsPath -Raw |
| 37 | if ($propsContent -match '<ClientTargetFrameworks>([^<]+)</ClientTargetFrameworks>') { |
| 38 | $clientTargetFrameworks = $Matches[1] |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (-not $clientTargetFrameworks) { |
| 43 | Write-Error "Could not find ClientTargetFrameworks in Directory.Build.props" |
| 44 | exit 1 |
| 45 | } |
| 46 | |
| 47 | Write-Host "" |
| 48 | Write-Host "Target Frameworks: $clientTargetFrameworks" -ForegroundColor Green |
| 49 | Write-Host "Configuration: $configuration" |
| 50 | Write-Host "" |
| 51 | |
| 52 | # Ensure output directory exists and is clean (only remove generated .cs files, not other files like README.md) |
| 53 | if (Test-Path $outputDirectory) { |
| 54 | Write-Host "Cleaning existing output directory..." -ForegroundColor Cyan |
| 55 | try { |
| 56 | Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" -Force | Remove-Item -Force |
| 57 | } |
| 58 | catch { |
| 59 | Write-Warning "Failed to clean some items in output directory: $_" |
| 60 | } |
| 61 | } else { |
| 62 | New-Item -ItemType Directory -Path $outputDirectory -Force | Out-Null |
| 63 | Write-Host "Created output directory: $outputDirectory" |
| 64 | } |
| 65 | |
| 66 | # Build the dotnet command arguments |
| 67 | $buildArgs = @( |
| 68 | "build" |
| 69 | $projectPath |
| 70 | "-t:ExportApi" |
| 71 | "-c:$configuration" |
| 72 | "-p:ExportingApi=true" |
| 73 | "-m" |
| 74 | ) |
| 75 | |
| 76 | Write-Host "Output Directory: $outputDirectory" |
| 77 | Write-Host "" |
| 78 | Write-Host "Running GenAPI for all target frameworks..." -ForegroundColor Cyan |
| 79 | Write-Host "" |
| 80 | |
| 81 | # Run a single build command - the MSBuild target handles all frameworks |
| 82 | & dotnet @buildArgs |
| 83 | if ($LASTEXITCODE -ne 0) { |
| 84 | Write-Error "GenAPI failed with exit code $LASTEXITCODE" |
| 85 | exit $LASTEXITCODE |
| 86 | } |
| 87 | |
| 88 | Write-Host "" |
| 89 | Write-Host "Cleaning up generated files..." -ForegroundColor Cyan |
| 90 | |
| 91 | # Clean up each generated file |
| 92 | Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object { |
| 93 | Write-Host " Cleaning $($_.Name)..." |
| 94 | |
| 95 | $content = Get-Content $_.FullName -Raw |
| 96 | |
| 97 | # Replace the generic auto-generated header with one that explicitly names this script. |
| 98 | $customHeader = @" |
| 99 | //------------------------------------------------------------------------------ |
| 100 | // <auto-generated> |
| 101 | // This file is auto-generated by scripts/Export-Api.ps1 and must not be |
| 102 | // modified manually. To update it, make the necessary source-code changes |
| 103 | // and re-run scripts/Export-Api.ps1 to regenerate it. |
| 104 | // </auto-generated> |
| 105 | //------------------------------------------------------------------------------ |
| 106 | "@ |
| 107 | $content = $content -creplace '(?s)^//[-]+.*?//[-]+\r?\n', ($customHeader + "`n") |
| 108 | |
| 109 | # Normalize line breaks and whitespace. |
| 110 | $content = $content -creplace '\r?\n\r?\n', "`n" |
| 111 | $content = $content -creplace '\r?\n *{', " {" |
| 112 | |
| 113 | # Remove fully-qualified namespace prefixes. |
| 114 | @( |
| 115 | "Diagnostics\.CodeAnalysis\.", |
| 116 | "System\.ComponentModel\.", |
| 117 | "System\.ClientModel\.Primitives\.", |
| 118 | "System\.ClientModel\.", |
| 119 | "System\.Collections\.Generic\.", |
| 120 | "System\.Collections\.", |
| 121 | "System\.Threading\.Tasks\.", |
| 122 | "System\.Threading\.", |
| 123 | "System\.Text\.Json\.", |
| 124 | "System\.Text\.", |
| 125 | "System\.IO\.", |
| 126 | "System\." # System must be last to avoid partial matches |
| 127 | ) | ForEach-Object { $content = $content -creplace $_, "" } |
| 128 | |
| 129 | # Remove OpenAI sub-namespace prefixes. |
| 130 | @( |
| 131 | "Assistants", |
| 132 | "Audio", |
| 133 | "Batch", |
| 134 | "Chat", |
| 135 | "Common", |
| 136 | "Containers", |
| 137 | "Conversations", |
| 138 | "Embeddings", |
| 139 | "Evals", |
| 140 | "Files", |
| 141 | "FineTuning", |
| 142 | "Graders", |
| 143 | "Images", |
| 144 | "Models", |
| 145 | "Moderations", |
| 146 | "Realtime", |
| 147 | "Responses", |
| 148 | "VectorStores", |
| 149 | "Videos" |
| 150 | ) | ForEach-Object { $content = $content -creplace "$_\.", "" } |
| 151 | |
| 152 | # Remove non-public APIs. |
| 153 | $content = $content -creplace " * internal.*`n", "" |
| 154 | $content = $content -creplace ".*private.*dummy.*`n", "" |
| 155 | |
| 156 | # Remove Diagnostics.DebuggerStepThrough attribute. |
| 157 | $content = $content -creplace ".*Diagnostics.DebuggerStepThrough.*\n", "" |
| 158 | |
| 159 | # Remove ModelReaderWriterBuildable attributes. |
| 160 | $content = $content -creplace '\[ModelReaderWriterBuildable\(typeof\([^\)]+\)\)\]\s*', '' |
| 161 | |
| 162 | # Remove IJsonModel/IPersistableModel interface method entries. |
| 163 | $content = $content -creplace " .*(IJsonModel|IPersistableModel).*`n", "" |
| 164 | |
| 165 | # Remove JsonModelCreateCore, JsonModelWriteCore, PersistableModelCreateCore, PersistableModelWriteCore methods, |
| 166 | # including any attribute lines (e.g. [Experimental(...)]) that immediately precede them. |
| 167 | $content = $content -creplace "(?: \[[^\n]+\]\r?\n)* protected (?:virtual|override) [^\n]*(JsonModelCreateCore|JsonModelWriteCore|PersistableModelCreateCore|PersistableModelWriteCore)[^\n]*\r?\n", "" |
| 168 | |
| 169 | # Other cosmetic simplifications. |
| 170 | $content = $content -creplace "partial class", "class" |
| 171 | $content = $content -creplace " { throw null; }", ";" |
| 172 | $content = $content -creplace " { }", ";" |
| 173 | |
| 174 | Set-Content -Path $_.FullName -Value $content -NoNewline |
| 175 | } |
| 176 | |
| 177 | Write-Host "" |
| 178 | Write-Host "API generation completed successfully." -ForegroundColor Green |
| 179 | Write-Host "" |
| 180 | |
| 181 | # List generated files |
| 182 | Write-Host "Generated files:" -ForegroundColor Cyan |
| 183 | Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object { |
| 184 | Write-Host " - $($_.Name)" |
| 185 | } |
| 186 | Write-Host "" |
| 187 | |