Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 140 additions & 44 deletions packages/http-client-csharp/eng/scripts/Add-Debug-Profile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@

.DESCRIPTION
This script:
1. Installs tsp-client if not already installed
2. Runs tsp-client sync in the target SDK directory
3. Runs tsp-client generate --save-inputs to create tspCodeModel.json
1. Installs tsp-client if not already installed (for Azure SDK scenarios)
2. Runs tsp-client sync in the target SDK directory (for Azure SDK scenarios)
3. Runs tsp-client generate --save-inputs to create tspCodeModel.json (for Azure SDK scenarios)
4. Reads the emitter configuration from tsp-location.yaml to determine the generator
5. Adds a new debug profile to launchSettings.json that targets the DLL

The script automatically detects which emitter/generator to use by:
- First checking tsp-location.yaml for the configured emitter package
- Falling back to auto-detection based on SDK path if tsp-location.yaml is not found
(e.g., paths containing "ResourceManager" use ManagementClientGenerator)
- Detecting OpenAI plugin by checking if the path contains "openai-dotnet"

.PARAMETER SdkDirectory
Path to the target SDK service directory
Path to the target SDK service directory (for Azure SDK) or OpenAI repository root (for OpenAI plugin)

.EXAMPLE
.\Add-Debug-Profile.ps1 -SdkDirectory "C:\path\to\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs"

.EXAMPLE
.\Add-Debug-Profile.ps1 -SdkDirectory ".\local-sdk-dir"

.EXAMPLE
.\Add-Debug-Profile.ps1 -SdkDirectory "C:\path\to\openai-dotnet"
#>

param(
Expand Down Expand Up @@ -76,6 +80,18 @@ function Test-CommandExists {
}
}

# Check if the SDK directory is an OpenAI plugin repository
function Test-IsOpenAIPlugin {
param([string]$SdkPath)

# Check if the path contains "openai-dotnet"
if ($SdkPath -match 'openai-dotnet') {
return $true
}

return $false
}

# Check if tsp-client is installed
function Test-TspClientInstalled {
try {
Expand All @@ -94,7 +110,7 @@ function Install-TspClient {
Write-Host "tsp-client installed successfully." -ForegroundColor Green
}

# Run tsp-client commands in the target directory
# Run tsp-client commands in the target directory (for Azure SDK scenarios)
function Invoke-TspClientCommands {
param([string]$SdkPath)

Expand Down Expand Up @@ -127,6 +143,39 @@ function Invoke-TspClientCommands {
}
}

# Build OpenAI plugin codegen package
function Build-OpenAICodegen {
param([string]$OpenAIRepoPath)

$codegenPath = Join-Path $OpenAIRepoPath "codegen"

if (-not (Test-Path $codegenPath)) {
throw "Codegen directory not found at: $codegenPath. Please ensure you're pointing to the OpenAI repository root."
}

Write-Host "Building OpenAI codegen package..." -ForegroundColor Cyan

try {
# Install dependencies in the repo root
Write-Host "Installing dependencies in OpenAI repo root..." -ForegroundColor Yellow
Invoke-Command-Safe "npm ci" -WorkingDirectory $OpenAIRepoPath

# Clean the cache before building
Write-Host "Cleaning codegen cache..." -ForegroundColor Yellow
Invoke-Command-Safe "npm run clean" -WorkingDirectory $codegenPath

# Build the codegen package
Write-Host "Building codegen package..." -ForegroundColor Yellow
Invoke-Command-Safe "npm run build" -WorkingDirectory $codegenPath

Write-Host "OpenAI codegen build completed." -ForegroundColor Green
}
catch {
Write-Error "Failed to build OpenAI codegen: $($_.Exception.Message)"
throw
}
}

# Get the path to launchSettings.json
function Get-LaunchSettingsPath {
$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')
Expand Down Expand Up @@ -252,19 +301,27 @@ function Build-LocalGeneratorSolution {
}
}

# Copy local generator DLLs to the SDK's node_modules location
# Copy local generator DLLs to the SDK's node_modules location or OpenAI codegen dist
function Copy-LocalGeneratorDlls {
param(
[string]$SdkPath,
[string]$PackageName,
[string]$ScopeName
[string]$ScopeName,
[bool]$IsOpenAI = $false
)

$scriptDir = Split-Path $MyInvocation.PSCommandPath -Parent
$packageRoot = Split-Path (Split-Path $scriptDir -Parent) -Parent
$sourceDir = Join-Path $packageRoot "dist/generator"

$targetDir = Join-Path $SdkPath "TempTypeSpecFiles/node_modules/$ScopeName/$PackageName/dist/generator"
if ($IsOpenAI) {
# For OpenAI, copy to codegen/dist/generator
$targetDir = Join-Path $SdkPath "codegen/dist/generator"
}
else {
# For Azure SDK, copy to node_modules
$targetDir = Join-Path $SdkPath "TempTypeSpecFiles/node_modules/$ScopeName/$PackageName/dist/generator"
}

# Rebuild the solution first to ensure fresh DLLs
$buildSuccess = Build-LocalGeneratorSolution $packageRoot
Expand All @@ -284,7 +341,7 @@ function Copy-LocalGeneratorDlls {
"Microsoft.TypeSpec.Generator.Input.dll"
)

Write-Host "Copying local generator DLLs to node_modules location..." -ForegroundColor Yellow
Write-Host "Copying local generator DLLs to $targetDir..." -ForegroundColor Yellow

foreach ($dll in $dllsToCopy) {
$sourcePath = Join-Path $sourceDir $dll
Expand All @@ -304,27 +361,43 @@ function Copy-LocalGeneratorDlls {
# Add or update a debug profile in launchSettings.json
function Add-DebugProfile {
param(
[string]$SdkPath
[string]$SdkPath,
[bool]$IsOpenAI = $false
)

$launchSettings = Get-LaunchSettings
$profileName = Get-ProfileName $SdkPath
$resolvedSdkPath = Resolve-Path $SdkPath

# Try to read emitter configuration from tsp-location.yaml
$emitterPackage = Get-EmitterFromTspLocation $SdkPath

# Get generator configuration based on emitter package
$generatorConfig = Get-GeneratorConfig $emitterPackage
$packageName = $generatorConfig.PackageName
$generatorName = $generatorConfig.GeneratorName
$scopeName = $generatorConfig.ScopeName

# Copy local DLLs to the node_modules location
Copy-LocalGeneratorDlls $resolvedSdkPath $packageName $scopeName

# Use the node_modules DLL path
$dllPath = "`"$resolvedSdkPath/TempTypeSpecFiles/node_modules/$scopeName/$packageName/dist/generator/Microsoft.TypeSpec.Generator.dll`""
if ($IsOpenAI) {
# For OpenAI plugin
$generatorName = "OpenAILibraryGenerator"
$packageName = "openai-codegen"
$scopeName = $null

# Copy local DLLs to the OpenAI codegen dist directory
Copy-LocalGeneratorDlls $resolvedSdkPath $packageName $scopeName -IsOpenAI $true

# Use the OpenAI codegen DLL path
$dllPath = "`"$resolvedSdkPath/codegen/dist/generator/Microsoft.TypeSpec.Generator.dll`""
}
else {
# For Azure SDK
# Try to read emitter configuration from tsp-location.yaml
$emitterPackage = Get-EmitterFromTspLocation $SdkPath

# Get generator configuration based on emitter package
$generatorConfig = Get-GeneratorConfig $emitterPackage
$packageName = $generatorConfig.PackageName
$generatorName = $generatorConfig.GeneratorName
$scopeName = $generatorConfig.ScopeName

# Copy local DLLs to the node_modules location
Copy-LocalGeneratorDlls $resolvedSdkPath $packageName $scopeName -IsOpenAI $false

# Use the node_modules DLL path
$dllPath = "`"$resolvedSdkPath/TempTypeSpecFiles/node_modules/$scopeName/$packageName/dist/generator/Microsoft.TypeSpec.Generator.dll`""
}

# Create the new profile
$newProfile = @{
Expand All @@ -343,8 +416,13 @@ function Add-DebugProfile {
Write-Host " - Executable: dotnet" -ForegroundColor White
Write-Host " - Arguments: $dllPath `"$resolvedSdkPath`" -g $generatorName" -ForegroundColor White
Write-Host " - Generator: $generatorName" -ForegroundColor White
Write-Host " - Package: $packageName" -ForegroundColor White
Write-Host " - Emitter: $emitterPackage (from tsp-location.yaml)" -ForegroundColor White
if (-not $IsOpenAI) {
Write-Host " - Package: $packageName" -ForegroundColor White
Write-Host " - Emitter: $emitterPackage (from tsp-location.yaml)" -ForegroundColor White
}
else {
Write-Host " - Mode: OpenAI Plugin" -ForegroundColor White
}

return $profileName
}
Expand All @@ -359,28 +437,46 @@ try {
throw "npm is not installed or not in PATH"
}

# Install tsp-client if not installed
if (-not (Test-TspClientInstalled)) {
Write-Host "tsp-client is not installed. Installing now..." -ForegroundColor Yellow
try {
Install-TspClient
}
catch {
Write-Warning "Failed to install tsp-client. You may need to install it manually with:"
Write-Warning "npm install -g @azure-tools/typespec-client-generator-cli"
Write-Warning "Error: $($_.Exception.Message)"
}
# Auto-detect if this is an OpenAI plugin repository
$isOpenAI = Test-IsOpenAIPlugin $sdkPath

if ($isOpenAI) {
# OpenAI plugin workflow
Write-Host "Detected OpenAI plugin repository. Setting up debug profile..." -ForegroundColor Cyan

# Build OpenAI codegen
Build-OpenAICodegen $sdkPath

# Add debug profile
$profileName = Add-DebugProfile $sdkPath -IsOpenAI $true
}
else {
Write-Host "tsp-client is already installed." -ForegroundColor Green
# Azure SDK workflow
Write-Host "Setting up debug profile for Azure SDK..." -ForegroundColor Cyan

# Install tsp-client if not installed
if (-not (Test-TspClientInstalled)) {
Write-Host "tsp-client is not installed. Installing now..." -ForegroundColor Yellow
try {
Install-TspClient
}
catch {
Write-Warning "Failed to install tsp-client. You may need to install it manually with:"
Write-Warning "npm install -g @azure-tools/typespec-client-generator-cli"
Write-Warning "Error: $($_.Exception.Message)"
}
}
else {
Write-Host "tsp-client is already installed." -ForegroundColor Green
}

# Run tsp-client commands
Invoke-TspClientCommands $sdkPath

# Add debug profile
$profileName = Add-DebugProfile $sdkPath -IsOpenAI $false
}

# Run tsp-client commands
Invoke-TspClientCommands $sdkPath

# Add debug profile
$profileName = Add-DebugProfile $sdkPath

Write-Host "`nSetup completed successfully!" -ForegroundColor Green
Write-Host "You can now debug the '$profileName' profile in Visual Studio or VS Code." -ForegroundColor Cyan
}
Expand Down
Loading