diff --git a/packages/http-client-csharp/eng/scripts/CadlRanch-Helper.psm1 b/packages/http-client-csharp/eng/scripts/CadlRanch-Helper.psm1 new file mode 100644 index 0000000000..d10541e085 --- /dev/null +++ b/packages/http-client-csharp/eng/scripts/CadlRanch-Helper.psm1 @@ -0,0 +1,45 @@ +$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..') + +function IsGenerated { + param ( + [string]$dir + ) + + if (-not ($dir.EndsWith("Generated"))) { + return $false + } + + $csFiles = Get-ChildItem -Path $dir -Filter *.cs -File + return $csFiles.Count -gt 0 +} + +function Capitalize-FirstLetter { + param ( + [string]$inputString + ) + + if ([string]::IsNullOrEmpty($inputString)) { + return $inputString + } + + $firstChar = $inputString[0].ToString().ToUpper() + $restOfString = $inputString.Substring(1) + + return $firstChar + $restOfString +} + +function Get-Namespace { + param ( + [string]$dir + ) + + $words = $dir.Split('-') + $namespace = "" + foreach ($word in $words) { + $namespace += Capitalize-FirstLetter $word + } + return $namespace +} + +Export-ModuleMember -Function "IsGenerated" +Export-ModuleMember -Function "Get-Namespace" diff --git a/packages/http-client-csharp/eng/scripts/Generate.ps1 b/packages/http-client-csharp/eng/scripts/Generate.ps1 index e3d80e92d8..f296b689c1 100644 --- a/packages/http-client-csharp/eng/scripts/Generate.ps1 +++ b/packages/http-client-csharp/eng/scripts/Generate.ps1 @@ -1,4 +1,5 @@ #Requires -Version 7.0 +param($filter) Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force; @@ -7,24 +8,26 @@ $solutionDir = Join-Path $packageRoot 'generator' Refresh-Build -Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan -$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local' +if ($null -eq $filter -or $filter -eq "Unbranded-TypeSpec") { + Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan + $testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local' -$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec" + $unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec" -Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject) + Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject) -# exit if the generation failed -if ($LASTEXITCODE -ne 0) { - exit $LASTEXITCODE -} + # exit if the generation failed + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } -Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan -Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj" + Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan + Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj" -# exit if the generation failed -if ($LASTEXITCODE -ne 0) { - exit $LASTEXITCODE + # exit if the generation failed + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } } $specsDirectory = "$packageRoot/node_modules/@azure-tools/cadl-ranch-specs" @@ -100,6 +103,10 @@ foreach ($directory in $directories) { $subPath = $directory.FullName.Substring($specsDirectory.Length + 1) $folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar) + if (-not (Compare-Paths $subPath $filter)) { + continue + } + if ($folders.Contains("azure")) { continue } @@ -135,45 +142,48 @@ foreach ($directory in $directories) { # TODO need to build but depends on https://github.com/Azure/autorest.csharp/issues/4463 } -Write-Host "Writing new launch settings" -ForegroundColor Cyan -$mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe" -$sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe" -$unbrandedSpec = "TestProjects/Local/Unbranded-TypeSpec" - -$launchSettings = @{} -$launchSettings.Add("profiles", @{}) -$launchSettings["profiles"].Add("Unbranded-TypeSpec", @{}) -$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p ClientModelPlugin") -$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandName", "Executable") -$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("executablePath", $mgcExe) -$launchSettings["profiles"].Add("Debug-Plugin-Test-TypeSpec", @{}) -$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p SampleCodeModelPlugin") -$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandName", "Executable") -$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("executablePath", $sampleExe) - -foreach ($kvp in $cadlRanchLaunchProjects.GetEnumerator()) { - $launchSettings["profiles"].Add($kvp.Key, @{}) - $launchSettings["profiles"][$kvp.Key].Add("commandLineArgs", "`$(SolutionDir)/$($kvp.Value) -p StubLibraryPlugin") - $launchSettings["profiles"][$kvp.Key].Add("commandName", "Executable") - $launchSettings["profiles"][$kvp.Key].Add("executablePath", $mgcExe) -} +# only write new launch settings if no filter was passed in +if ($null -eq $filter) { + Write-Host "Writing new launch settings" -ForegroundColor Cyan + $mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe" + $sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe" + $unbrandedSpec = "TestProjects/Local/Unbranded-TypeSpec" + + $launchSettings = @{} + $launchSettings.Add("profiles", @{}) + $launchSettings["profiles"].Add("Unbranded-TypeSpec", @{}) + $launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p ClientModelPlugin") + $launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandName", "Executable") + $launchSettings["profiles"]["Unbranded-TypeSpec"].Add("executablePath", $mgcExe) + $launchSettings["profiles"].Add("Debug-Plugin-Test-TypeSpec", @{}) + $launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p SampleCodeModelPlugin") + $launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandName", "Executable") + $launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("executablePath", $sampleExe) + + foreach ($kvp in $cadlRanchLaunchProjects.GetEnumerator()) { + $launchSettings["profiles"].Add($kvp.Key, @{}) + $launchSettings["profiles"][$kvp.Key].Add("commandLineArgs", "`$(SolutionDir)/$($kvp.Value) -p StubLibraryPlugin") + $launchSettings["profiles"][$kvp.Key].Add("commandName", "Executable") + $launchSettings["profiles"][$kvp.Key].Add("executablePath", $mgcExe) + } + + $sortedLaunchSettings = @{} + $sortedLaunchSettings.Add("profiles", [ordered]@{}) + $launchSettings["profiles"].Keys | Sort-Object | ForEach-Object { + $profileKey = $_ + $originalProfile = $launchSettings["profiles"][$profileKey] -$sortedLaunchSettings = @{} -$sortedLaunchSettings.Add("profiles", [ordered]@{}) -$launchSettings["profiles"].Keys | Sort-Object | ForEach-Object { - $profileKey = $_ - $originalProfile = $launchSettings["profiles"][$profileKey] - - # Sort the keys inside each profile - # This is needed due to non deterministic ordering of json elements in powershell - $sortedProfile = [ordered]@{} - $originalProfile.GetEnumerator() | Sort-Object Key | ForEach-Object { - $sortedProfile[$_.Key] = $_.Value + # Sort the keys inside each profile + # This is needed due to non deterministic ordering of json elements in powershell + $sortedProfile = [ordered]@{} + $originalProfile.GetEnumerator() | Sort-Object Key | ForEach-Object { + $sortedProfile[$_.Key] = $_.Value + } + + $sortedLaunchSettings["profiles"][$profileKey] = $sortedProfile } - $sortedLaunchSettings["profiles"][$profileKey] = $sortedProfile + # Write the launch settings to the launchSettings.json file + $launchSettingsPath = Join-Path $solutionDir "Microsoft.Generator.CSharp" "src" "Properties" "launchSettings.json" + $sortedLaunchSettings | ConvertTo-Json | Set-Content $launchSettingsPath } - -# Write the launch settings to the launchSettings.json file -$launchSettingsPath = Join-Path $solutionDir "Microsoft.Generator.CSharp" "src" "Properties" "launchSettings.json" -$sortedLaunchSettings | ConvertTo-Json | Set-Content $launchSettingsPath diff --git a/packages/http-client-csharp/eng/scripts/Generation.psm1 b/packages/http-client-csharp/eng/scripts/Generation.psm1 index 885023e115..5fdb064e3d 100644 --- a/packages/http-client-csharp/eng/scripts/Generation.psm1 +++ b/packages/http-client-csharp/eng/scripts/Generation.psm1 @@ -54,6 +54,21 @@ function Refresh-Build { } } +function Compare-Paths { + param ( + [string]$path1, + [string]$path2 + ) + + # Normalize the directory separators + $normalizedPath1 = $path1 -replace '/', '\' + $normalizedPath2 = $path2 -replace '/', '\' + + # Compare the normalized paths + return $normalizedPath1.Contains($normalizedPath2) +} + Export-ModuleMember -Function "Invoke" Export-ModuleMember -Function "Get-TspCommand" Export-ModuleMember -Function "Refresh-Build" +Export-ModuleMember -Function "Compare-Paths" diff --git a/packages/http-client-csharp/eng/scripts/Get-CadlRanch-Coverage.ps1 b/packages/http-client-csharp/eng/scripts/Get-CadlRanch-Coverage.ps1 new file mode 100644 index 0000000000..0793f64225 --- /dev/null +++ b/packages/http-client-csharp/eng/scripts/Get-CadlRanch-Coverage.ps1 @@ -0,0 +1,76 @@ +# cspell:ignore cadlranch + +#Requires -Version 7.0 + +Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force; +Import-Module "$PSScriptRoot\CadlRanch-Helper.psm1" -DisableNameChecking -Force; + +$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..') + +Refresh-Build + +$specsDirectory = Join-Path $packageRoot 'node_modules' '@azure-tools' 'cadl-ranch-specs' +$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch' +$directories = Get-ChildItem -Path "$cadlRanchRoot" -Directory -Recurse +$cadlRanchCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch.Tests' 'TestProjects.CadlRanch.Tests.csproj' +$runSettings = Join-Path $packageRoot 'eng' 'test-configurations' 'cadlranch.runsettings' + +$coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage' + +if (-not (Test-Path $coverageDir)) { + New-Item -ItemType Directory -Path $coverageDir | Out-Null +} + +# generate all +foreach ($directory in $directories) { + if (-not (IsGenerated $directory.FullName)) { + continue + } + + $outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1) + $subPath = $outputDir.Substring($cadlRanchRoot.Length + 1) + + Write-Host "Regenerating $subPath" -ForegroundColor Cyan + + $specFile = Join-Path $specsDirectory $subPath "main.tsp" + + $command = Get-TspCommand $specFile $outputDir + Invoke $command + # exit if the generation failed + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } +} + +# test all +Write-Host "Testing $subPath" -ForegroundColor Cyan +$command = "dotnet test $cadlRanchCsproj --settings $runSettings" +Invoke $command +# exit if the testing failed +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +# restore all +foreach ($directory in $directories) { + if (-not (IsGenerated $directory.FullName)) { + continue + } + + $outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1) + $subPath = $outputDir.Substring($cadlRanchRoot.Length + 1) + + Write-Host "Restoring $subPath" -ForegroundColor Cyan + $command = "git clean -xfd $outputDir" + Invoke $command + # exit if the restore failed + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } + $command = "git restore $outputDir" + Invoke $command + # exit if the restore failed + if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE + } +} diff --git a/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 b/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 index f33ce504d5..6c6db03fbd 100644 --- a/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 +++ b/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 @@ -1,8 +1,10 @@ # cspell:ignore cadlranch #Requires -Version 7.0 +param($filter) Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force; +Import-Module "$PSScriptRoot\CadlRanch-Helper.psm1" -DisableNameChecking -Force; $packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..') @@ -20,47 +22,6 @@ if (-not (Test-Path $coverageDir)) { New-Item -ItemType Directory -Path $coverageDir | Out-Null } -function IsGenerated { - param ( - [string]$dir - ) - - if (-not ($dir.EndsWith("Generated"))) { - return $false - } - - $csFiles = Get-ChildItem -Path $directory -Filter *.cs -File - return $csFiles.Count -gt 0 -} - -function Capitalize-FirstLetter { - param ( - [string]$inputString - ) - - if ([string]::IsNullOrEmpty($inputString)) { - return $inputString - } - - $firstChar = $inputString[0].ToString().ToUpper() - $restOfString = $inputString.Substring(1) - - return $firstChar + $restOfString -} - -function Get-Namespace { - param ( - [string]$dir - ) - - $words = $dir.Split('-') - $namespace = "" - foreach ($word in $words) { - $namespace += Capitalize-FirstLetter $word - } - return $namespace -} - foreach ($directory in $directories) { if (-not (IsGenerated $directory.FullName)) { continue @@ -70,6 +31,10 @@ foreach ($directory in $directories) { $subPath = $outputDir.Substring($cadlRanchRoot.Length + 1) $folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar) + if (-not (Compare-Paths $subPath $filter)) { + continue + } + $testFilter = "TestProjects.CadlRanch.Tests" foreach ($folder in $folders) { $testFilter += ".$(Get-Namespace $folder)" @@ -89,7 +54,7 @@ foreach ($directory in $directories) { Write-Host "Testing $subPath" -ForegroundColor Cyan $command = "dotnet test $cadlRanchCsproj --filter `"FullyQualifiedName~$testFilter`" --settings $runSettings" Invoke $command - # exit if the generation failed + # exit if the testing failed if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } @@ -97,13 +62,13 @@ foreach ($directory in $directories) { Write-Host "Restoring $subPath" -ForegroundColor Cyan $command = "git clean -xfd $outputDir" Invoke $command - # exit if the generation failed + # exit if the restore failed if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } $command = "git restore $outputDir" Invoke $command - # exit if the generation failed + # exit if the restore failed if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } diff --git a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 index 79a014c8b2..26edc0caab 100644 --- a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 +++ b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 @@ -24,7 +24,7 @@ try { # test the generator Invoke-LoggedCommand "dotnet test ./generator" -GroupOutput - Invoke-LoggedCommand "./eng/scripts/Test-CadlRanch.ps1" -GroupOutput + Invoke-LoggedCommand "./eng/scripts/Get-CadlRanch-Coverage.ps1" -GroupOutput } finally { Pop-Location