openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/Invoke-CodeGen.ps1

325lines · modeblame

9320b2aaShivangiReja1 years ago1[CmdletBinding(DefaultParameterSetName = 'Default')]
dff9bb58Jose Arriaga Maldonado1 years ago2param(
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 ago10[string]$CommitHash = "706a4fb91ea18b441723f8a57a7e2c67e3061bb6",
dff9bb58Jose Arriaga Maldonado1 years ago11
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 ago19[switch]$Force,
20
21[Parameter(Mandatory = $false)]
22[switch]$Clean
dff9bb58Jose Arriaga Maldonado1 years ago23)
24
25function Invoke-ScriptWithLogging {
26[CmdletBinding()]
27param(
28[Parameter(Mandatory = $true)]
29[scriptblock]$Script
30)
31
32$scriptString = $Script | Out-String
33Write-Host "--------------------------------------------------------------------------------`n> $scriptString"
34& $Script
76356c85JoshLove-msft9 months ago35$exitCode = $LASTEXITCODE
dff9bb58Jose Arriaga Maldonado1 years ago36Write-Host ""
76356c85JoshLove-msft9 months ago37
38if ($exitCode -ne 0) {
39throw "Command failed with exit code $exitCode"
40}
dff9bb58Jose Arriaga Maldonado1 years ago41}
42
7af490a0JoshLove-msft11 months ago43
44$script:startTime = Get-Date
45$scriptName = $MyInvocation.MyCommand.Name
46
47function Write-ElapsedTime {
48[CmdletBinding()]
49param(
50[Parameter(Mandatory = $true)]
51[string]$Message
52)
53
54$elapsedTime = [math]::Round(((Get-Date) - $script:startTime).TotalSeconds, 1)
55Write-Host "[${scriptName}] ${Message}. Total elapsed time: $elapsedTime seconds."
56Write-Host ""
57}
58
dff9bb58Jose Arriaga Maldonado1 years ago59function Get-GitHubApiHeaders {
60param(
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
70if ($GitHubToken) {
71$headers.Authorization = "Bearer $GitHubToken"
72}
73
74return $headers
75}
76
77function Get-TempDownloadPath {
78$tempPath = [System.IO.Path]::GetTempPath()
79return Join-Path $tempPath ([System.IO.Path]::GetRandomFileName())
80}
81
82function Test-GitHubRepoExists {
83param(
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"
95try {
96$headers = Get-GitHubApiHeaders -GitHubToken $GitHubToken
97Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers -ErrorAction Stop
98return $true
99}
100catch {
101Write-Warning "Repository check failed: $_"
102return $false
103}
104}
105
106function Get-GitHubRepoContent {
107[CmdletBinding()]
108param(
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
129if (-not (Test-GitHubRepoExists -GitHubOwner $GitHubOwner -GitHubRepository $GitHubRepository -GitHubToken $GitHubToken)) {
130Write-Error "Repository '$GitHubOwner/$GitHubRepository' does not exist or is not accessible."
131return $false
132}
133
134# Create temporary directory for download
135$downloadPath = Get-TempDownloadPath
136New-Item -ItemType Directory -Path $downloadPath -Force | Out-Null
137
138try {
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
147Write-Host "Downloading repository archive from $GitHubOwner/$GitHubRepository @ $CommitHash..."
148Write-Host ""
149Invoke-WebRequest -Uri $archiveUrl -OutFile $zipPath -Headers $headers
150
151# Extract the contents
152Write-Host "Extracting repository contents..."
153Write-Host ""
154Expand-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
160New-Item -ItemType Directory -Path $Destination -Force | Out-Null
161
162$sourcePath = Join-Path $extractedFolder.FullName $SubdirectoryPath
163if (-not (Test-Path $sourcePath)) {
164Write-Error "Specified path '$SubdirectoryPath' does not exist in the repository."
165return $false
166}
167
168# Copy the contents directly to destination, preserving only the internal structure
169if (Test-Path $sourcePath -PathType Container) {
170Copy-Item -Path "$sourcePath\*" -Destination $Destination -Recurse -Force
171}
172else {
173Copy-Item -Path $sourcePath -Destination $Destination -Force
174}
175
176Write-Host "Downloaded repository contents to: $Destination"
177Write-Host ""
178return $true
179}
180catch {
181Write-Error "An error occurred: $_"
182return $false
183}
184finally {
185# Cleanup temporary files
186if (Test-Path $downloadPath) {
187Remove-Item -Path $downloadPath -Recurse -Force
188}
189}
190}
191
192function Get-LocalRepoContent {
193[CmdletBinding()]
194param(
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
205try {
206$sourcePath = Join-Path $LocalRepositoryPath $SubdirectoryPath
207
208if (-not (Test-Path $sourcePath)) {
209Write-Error "Specified path '$SubdirectoryPath' does not exist in the local repository at: $LocalRepositoryPath"
210return $false
211}
212
213# Create the destination directory if it doesn't exist
214New-Item -ItemType Directory -Path $Destination -Force | Out-Null
215
216# Copy the contents directly to destination, preserving only the internal structure
217if (Test-Path $sourcePath -PathType Container) {
218Copy-Item -Path "$sourcePath\*" -Destination $Destination -Recurse -Force
219}
220else {
221Copy-Item -Path $sourcePath -Destination $Destination -Force
222}
223
224Write-Host "Copied repository contents to: $Destination"
225Write-Host ""
226return $true
227}
228catch {
229Write-Error "An error occurred: $_"
230return $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 ago241if ($PSCmdlet.ParameterSetName -eq 'Default') {
242if (-not (Test-Path $baseSpecificationFolderPath)) {
243Write-Error "Base specification path does not exist: $baseSpecificationFolderPath"
244throw "Default specification path not found"
245}
246
247Write-Host "Using existing base specification at: $baseSpecificationFolderPath"
dff9bb58Jose Arriaga Maldonado1 years ago248Write-Host ""
9320b2aaShivangiReja1 years ago249}
250else {
251$shouldDownload = $true
dff9bb58Jose Arriaga Maldonado1 years ago252
9320b2aaShivangiReja1 years ago253if (Test-Path $baseSpecificationFolderPath) {
254Write-Host "Base specification already exists at: $baseSpecificationFolderPath"
dff9bb58Jose Arriaga Maldonado1 years ago255Write-Host ""
9320b2aaShivangiReja1 years ago256
257if ($Force) {
258Write-Host "Overwriting existing base specification..."
259Write-Host ""
260Remove-Item -Path $baseSpecificationFolderPath -Recurse -Force
261}
262else {
263$shouldDownload = $false
264}
dff9bb58Jose Arriaga Maldonado1 years ago265}
266
9320b2aaShivangiReja1 years ago267if ($shouldDownload) {
268Write-Host "Retrieving base specification..."
269Write-Host ""
dff9bb58Jose Arriaga Maldonado1 years ago270
9320b2aaShivangiReja1 years ago271if ($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}
279elseif ($PSCmdlet.ParameterSetName -eq 'Local') {
280$success = Get-LocalRepoContent -LocalRepositoryPath $LocalRepositoryPath `
281-SubdirectoryPath "openai-in-typespec" `
282-Destination $baseSpecificationFolderPath
283}
dff9bb58Jose Arriaga Maldonado1 years ago284
9320b2aaShivangiReja1 years ago285if (-not $success) {
286Write-Error "Failed to get repository contents."
287throw "Repository content retrieval failed."
288}
dff9bb58Jose Arriaga Maldonado1 years ago289}
290}
291
7af490a0JoshLove-msft11 months ago292Write-ElapsedTime "Spec retrieved"
293
dff9bb58Jose Arriaga Maldonado1 years ago294Push-Location $repoRootPath
295
296try {
297Invoke-ScriptWithLogging { npm ci }
994e843fJoshLove-msft11 months ago298if ($LASTEXITCODE -ne 0) {
299exit $LASTEXITCODE
300}
301
7af490a0JoshLove-msft11 months ago302Write-ElapsedTime "npm ci complete"
303
84fb83b2Jorge Rangel10 months ago304if ($Clean) {
305Invoke-ScriptWithLogging { npm run clean -w $codegenFolderPath }
306if ($LASTEXITCODE -ne 0) {
307exit $LASTEXITCODE
308}
309Write-ElapsedTime "npm run clean complete"
310}
311
dff9bb58Jose Arriaga Maldonado1 years ago312Invoke-ScriptWithLogging { npm run build -w $codegenFolderPath }
994e843fJoshLove-msft11 months ago313if ($LASTEXITCODE -ne 0) {
314exit $LASTEXITCODE
315}
7af490a0JoshLove-msft11 months ago316Write-ElapsedTime "npm run build complete"
dff9bb58Jose Arriaga Maldonado1 years ago317
318Set-Location $specificationFolderPath
936e6984JoshLove-msft11 months ago319Invoke-ScriptWithLogging { npx tsp compile . --stats --trace @typespec/http-client-csharp }
dff9bb58Jose Arriaga Maldonado1 years ago320}
321finally {
322Pop-Location
323}
324
7af490a0JoshLove-msft11 months ago325Write-ElapsedTime "Script completed"