From 1ba9491a45ea7c32a035bdb3be00b84e32aee949 Mon Sep 17 00:00:00 2001 From: Flaviu Tritean Date: Thu, 5 Dec 2024 10:50:17 +0200 Subject: [PATCH] minor updates to scripts --- build/PowerShellScripts/BuildSlnForCTE.ps1 | 97 ++++++++++++++++--- .../GenerateReportForCompileTimeError.ps1 | 51 ++++++++-- build/PowerShellScripts/MergeReports.ps1 | 24 +++++ 3 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 build/PowerShellScripts/MergeReports.ps1 diff --git a/build/PowerShellScripts/BuildSlnForCTE.ps1 b/build/PowerShellScripts/BuildSlnForCTE.ps1 index f66da965d..d2de0ae7a 100644 --- a/build/PowerShellScripts/BuildSlnForCTE.ps1 +++ b/build/PowerShellScripts/BuildSlnForCTE.ps1 @@ -2,17 +2,77 @@ [string]$defaultWorkingDirectory = "" ) + if (-not $defaultWorkingDirectory) { $defaultWorkingDirectory = $env:SYSTEM_DEFAULTWORKINGDIRECTORY } +if (-not $poolname) { + $poolname = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + $folderName = "NuGet" $appDataPath = [System.Environment]::GetFolderPath('ApplicationData') $folderPath = Join-Path -Path $appDataPath -ChildPath $folderName + # Get all .csproj files in the repository $csprojFiles = Get-ChildItem -Path $defaultWorkingDirectory -Recurse -Filter *.sln | Select-Object -ExpandProperty FullName + +function UnitTestsExists { + param ( + [string]$solutionPath + ) + + $testAssemblie = Get-ChildItem -Path $solutionPath -Recurse -Include "*UnitTests.dll" | Where-Object { $_.FullName -notlike "*TestAdapter.dll" -and $_.FullName -notlike "*\obj\*" } + + if ($testAssemblie.Count -ne 0) { + return $true + } + + return $false +} + +function BuildUnitTests { + param ( + [string]$solutionPath + ) + + $testAssemblie = Get-ChildItem -Path $solutionPath -Recurse -Include "*UnitTests.dll" | Where-Object { $_.FullName -notlike "*TestAdapter.dll" -and $_.FullName -notlike "*\obj\*" } + + if ($testAssemblie.Count -ne 0) { + + & "$vsTestPath" "$testAssemblie" /Logger:"Console;Verbosity=normal" ` + | Tee-Object -FilePath "$defaultWorkingDirectory\Logs\UnitTestsLog.txt" -Append + + } + +} + +function Get-VSTestConsolePath { + $possibleLocations = @( + "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe", + "C:\Program Files\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" + ) + + foreach ($location in $possibleLocations) { + if (Test-Path $location) { + return $location + } + } + + Write-Host "VSTest.Console.exe not found in the standard locations." -ForegroundColor Red + return $null +} + +$vsTestPath = Get-VSTestConsolePath + # Function to find MSBuild location by checking common paths function Get-MSBuildLocation { # Array of possible paths where MSBuild might be located @@ -48,22 +108,35 @@ Set-Alias MSBuild -Value $msbuildLocation; $feedName = 'SDLNuget' $nugetRestoreArguments = "/p:RestoreSources=https://pkgs.dev.azure.com/sdl/_packaging/$feedName/nuget/v3/index.json" -$msbuildArguments = "/flp:logfile=$defaultWorkingDirectory/GitHubLogs/MyLog.log;append=true" +$msbuildArguments = "/flp:logfile=$defaultWorkingDirectory/Logs/MyLog.log;append=true" + +Write-Host ">>>>>>> : $poolName, $index" foreach ($project in $csprojFiles) { if (Test-Path -Path $folderPath) { Remove-Item -Path $folderPath -Recurse -Force } - # MSBuild "/bl" -m "$project" "/t:Restore" /p:nugetInteractive=true $nugetRestoreArguments - MSBuild "/bl" -m "$project" "/t:Restore" $nugetRestoreArguments -p:RestorePackagesConfig=true - MSBuild -m "$project" "/t:Rebuild" $msbuildArguments - - if (! $?) { write-Host "msbuild failed" -ForegroundColor Red ; } - - $itemFolder = $project -split '\\' - $joinedString = ($itemFolder[0..(4)] -join '\') - if (Test-Path -Path $joinedString) { - Write-Host "~~~ Following will be deleted: $joinedString" - Remove-Item -Path $joinedString -Recurse -Force + + if (Test-Path -Path $project) { + + MSBuild "/bl" -m "$project" "/t:Restore" $nugetRestoreArguments -p:RestorePackagesConfig=true + MSBuild -m "$project" "/t:Rebuild" $msbuildArguments + + if (! $?) { write-Host "msbuild failed" -ForegroundColor Red ; } + + $itemFolder = $project -split '\\' + $joinedString = ($itemFolder[0..($itemFolder.Count - 2)] -join '\') + + $unitTestExists = UnitTestsExists -solutionPath $joinedString + + if ($unitTestExists){ + BuildUnitTests -solutionPath $joinedString + } + + if (Test-Path -Path $joinedString) { + Write-Host "~~~ Following will be deleted: $joinedString" + Remove-Item -Path $joinedString -Recurse -Force + } } + } \ No newline at end of file diff --git a/build/PowerShellScripts/GenerateReportForCompileTimeError.ps1 b/build/PowerShellScripts/GenerateReportForCompileTimeError.ps1 index 86681c6ce..662b6ddf5 100644 --- a/build/PowerShellScripts/GenerateReportForCompileTimeError.ps1 +++ b/build/PowerShellScripts/GenerateReportForCompileTimeError.ps1 @@ -1,9 +1,12 @@ param( [string]$directoryPath = "", [string]$resultPath = "", -[string]$reportName = "" +[string]$reportName = "", +[string]$poolName = "" ) +$agentDirectory + if (-not $directoryPath) { $directoryPath = $env:SYSTEM_DEFAULTWORKINGDIRECTORY } @@ -12,6 +15,10 @@ if (-not $resultPath) { $resultPath = $env:SYSTEM_DEFAULTWORKINGDIRECTORY } +if (-not $poolName) { + $poolName = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + if (-not $reportName) { $reportName = "Report" } @@ -31,7 +38,7 @@ $reportFile = "$directoryPath\DemoReport.txt" # Path to your output report file # Initialize flags and data $inBuildFailedSection = $false -$buildFailedFound = $false +$inTestFailedSection = $false # Clear the report file if it already exists Clear-Content -Path $reportFile -ErrorAction Ignore @@ -43,7 +50,6 @@ Get-Content $logFile | ForEach-Object { # Check if the line contains "Build FAILED." if ($line -match "Build FAILED.") { $inBuildFailedSection = $true - $buildFailedFound = $true } # If we're in the failed build section, append the line to the report file @@ -55,6 +61,19 @@ Get-Content $logFile | ForEach-Object { if ($line -match "Time Elapsed") { $inBuildFailedSection = $false } + + if ($line -match "Starting test execution, please wait...") { + $inTestFailedSection = $true + } + + if ($inTestFailedSection) { + Add-Content -Path $reportFile -Value $line + } + + if ($line -match "Total time:") { + $inTestFailedSection = $false + } + } # Define file paths @@ -68,7 +87,6 @@ $processedLine = $false # Clear the output file if it already exists Clear-Content -Path $outputFile -ErrorAction Ignore - if (Test-Path $inputFile) { # Read the input file line by line Get-Content $inputFile | ForEach-Object { @@ -77,8 +95,12 @@ Get-Content $inputFile | ForEach-Object { # Check if we are processing a section if ($processing) { if (-not $processedLine -and $line -ne "") { - # Process the first non-empty line - $line = $line -replace "D:\\a\\1\\s\\", "~" + + if ($poolName -eq "FlaviusPool") { + $line = $line -replace "C:\\agent\\_work\\11\\s\\", "~" + } elseif ($poolName -eq "Azure Pipelines") { + $line = $line -replace "D:\\a\\1\\s\\", "~" + } # Truncate at the first backslash if ($line -match "\\") { @@ -130,10 +152,24 @@ Get-Content $input1 | ForEach-Object { $line = $_ # Check if the line does NOT contain the word 'warning' - if ($line -notmatch "warning") { + if ($line -notmatch "warning" -and $inTestFailedSection -eq $false) { # Write the line to the output file Add-Content -Path $output1 -Value $line } + + if ($line -match "Failed ") { + Add-Content -Path $output1 -Value $line + $inTestFailedSection = $true + } + + if ($inTestFailedSection) { + Add-Content -Path $output1 -Value $line + } + + if ($line -match "Stack Trace:") { + $inTestFailedSection = $false + } + } # Define file paths @@ -199,7 +235,6 @@ $output5 = "$directoryPath\ReplaceString.txt" # Path to the output file # Clear the output file if it already exists Clear-Content -Path $output5 -ErrorAction Ignore -# Read the input file line by line Get-Content $input5 | ForEach-Object { $line = $_ diff --git a/build/PowerShellScripts/MergeReports.ps1 b/build/PowerShellScripts/MergeReports.ps1 new file mode 100644 index 000000000..c54713505 --- /dev/null +++ b/build/PowerShellScripts/MergeReports.ps1 @@ -0,0 +1,24 @@ +param( +[string]$directoryPath = "", +[string]$outputFile = "", +[string]$file1 = "", +[string]$file2 = "" +) + +if (-not $directoryPath) { + $directoryPath = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + +if (-not $outputFile) { + $outputFile = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + +if (-not $file1) { + $file1 = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + +if (-not $file2) { + $file2 = $env:SYSTEM_DEFAULTWORKINGDIRECTORY +} + +Get-Content -Path $file1, ($directoryPath + $file2) | Set-Content -Path $outputFile \ No newline at end of file