openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1026-again

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/Export-Api.ps1

170lines · 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()]
20param(
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 = ""
35if (Test-Path $propsPath) {
36 $propsContent = Get-Content $propsPath -Raw
37 if ($propsContent -match '<ClientTargetFrameworks>([^<]+)</ClientTargetFrameworks>') {
38 $clientTargetFrameworks = $Matches[1]
39 }
40}
41
42if (-not $clientTargetFrameworks) {
43 Write-Error "Could not find ClientTargetFrameworks in Directory.Build.props"
44 exit 1
45}
46
47Write-Host ""
48Write-Host "Target Frameworks: $clientTargetFrameworks" -ForegroundColor Green
49Write-Host "Configuration: $configuration"
50Write-Host ""
51
52# Ensure output directory exists and is clean
53if (Test-Path $outputDirectory) {
54 Write-Host "Cleaning existing output directory..." -ForegroundColor Cyan
55 try {
56 Get-ChildItem -Path $outputDirectory -Force | Remove-Item -Recurse -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
76Write-Host "Output Directory: $outputDirectory"
77Write-Host ""
78Write-Host "Running GenAPI for all target frameworks..." -ForegroundColor Cyan
79Write-Host ""
80
81# Run a single build command - the MSBuild target handles all frameworks
82& dotnet @buildArgs
83if ($LASTEXITCODE -ne 0) {
84 Write-Error "GenAPI failed with exit code $LASTEXITCODE"
85 exit $LASTEXITCODE
86}
87
88Write-Host ""
89Write-Host "Cleaning up generated files..." -ForegroundColor Cyan
90
91# Clean up each generated file
92Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object {
93 Write-Host " Cleaning $($_.Name)..."
94
95 $content = Get-Content $_.FullName -Raw
96
97 # Normalize line breaks and whitespace.
98 $content = $content -creplace '\r?\n\r?\n', "`n"
99 $content = $content -creplace '\r?\n *{', " {"
100
101 # Remove fully-qualified namespace prefixes.
102 @(
103 "Diagnostics\.CodeAnalysis\.",
104 "System\.ComponentModel\.",
105 "System\.ClientModel\.Primitives\.",
106 "System\.ClientModel\.",
107 "System\.Collections\.Generic\.",
108 "System\.Collections\.",
109 "System\.Threading\.Tasks\.",
110 "System\.Threading\.",
111 "System\.Text\.Json\.",
112 "System\.Text\.",
113 "System\.IO\.",
114 "System\." # System must be last to avoid partial matches
115 ) | ForEach-Object { $content = $content -creplace $_, "" }
116
117 # Remove OpenAI sub-namespace prefixes.
118 @(
119 "Assistants",
120 "Audio",
121 "Batch",
122 "Chat",
123 "Common",
124 "Containers",
125 "Conversations",
126 "Embeddings",
127 "Evals",
128 "Files",
129 "FineTuning",
130 "Graders",
131 "Images",
132 "Models",
133 "Moderations",
134 "Realtime",
135 "Responses",
136 "VectorStores",
137 "Videos"
138 ) | ForEach-Object { $content = $content -creplace "$_\.", "" }
139
140 # Remove non-public APIs.
141 $content = $content -creplace " * internal.*`n", ""
142 $content = $content -creplace ".*private.*dummy.*`n", ""
143
144 # Remove Diagnostics.DebuggerStepThrough attribute.
145 $content = $content -creplace ".*Diagnostics.DebuggerStepThrough.*\n", ""
146
147 # Remove ModelReaderWriterBuildable attributes.
148 $content = $content -creplace '\[ModelReaderWriterBuildable\(typeof\([^\)]+\)\)\]\s*', ''
149
150 # Remove IJsonModel/IPersistableModel interface method entries.
151 $content = $content -creplace " .*(IJsonModel|IPersistableModel).*`n", ""
152
153 # Other cosmetic simplifications.
154 $content = $content -creplace "partial class", "class"
155 $content = $content -creplace " { throw null; }", ";"
156 $content = $content -creplace " { }", ";"
157
158 Set-Content -Path $_.FullName -Value $content -NoNewline
159}
160
161Write-Host ""
162Write-Host "API generation completed successfully." -ForegroundColor Green
163Write-Host ""
164
165# List generated files
166Write-Host "Generated files:" -ForegroundColor Cyan
167Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object {
168 Write-Host " - $($_.Name)"
169}
170Write-Host ""
171