Skip to content

Commit

Permalink
Fixed Invoke-Exe
Browse files Browse the repository at this point in the history
  • Loading branch information
Haplois committed Jan 13, 2022
1 parent 48b4d2a commit 8683f41
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 43 deletions.
65 changes: 32 additions & 33 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ if([string]::IsNullOrWhiteSpace($Version))
# Build configuration
#
Write-Verbose "Setup build configuration."
$TPB_Solution = "TestPlatform.sln"
$TPB_TestAssets = Join-Path $env:TP_ROOT_DIR "test\TestAssets\"
$TPB_Solution = Join-Path $env:TP_ROOT_DIR "TestPlatform.sln"
$TPB_TestAssets_Solution = Join-Path $TPB_TestAssets "TestAssets.sln"
$TPB_TestAssets_CILAssets = Join-Path $TPB_TestAssets "CILProject\CILProject.proj"
$TPB_TargetFramework45 = "net45"
Expand Down Expand Up @@ -126,12 +126,11 @@ function Invoke-Build
$dotnetExe = Get-DotNetPath

Write-Log ".. .. Build: Source: $TPB_Solution"
Invoke-Exe $dotnetExe "build $TPB_Solution --configuration $TPB_Configuration -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:TestPlatform.binlog"
Write-Host -ForegroundColor Yellow $LASTEXITCODE
Invoke-Exe $dotnetExe -Arguments "build $TPB_Solution --configuration $TPB_Configuration -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:TestPlatform.binlog"
Write-Log ".. .. Build: Complete."

Write-Log ".. .. Build: Source: $TPB_TestAssets_CILAssets"
Invoke-Exe $dotnetExe "build $TPB_TestAssets_CILAssets --configuration $TPB_Configuration -v:minimal -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:""$($env:TP_ROOT_DIR)\CILAssets.binlog"""
Invoke-Exe $dotnetExe -Arguments "build $TPB_TestAssets_CILAssets --configuration $TPB_Configuration -v:minimal -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:""$($env:TP_ROOT_DIR)\CILAssets.binlog"""
Write-Log ".. .. Build: Complete."
Write-Log "Invoke-Build: Complete. {$(Get-ElapsedTime($timer))}"
}
Expand All @@ -145,11 +144,15 @@ function Invoke-TestAssetsBuild
$nugetConfig = Join-Path $TPB_TestAssets "NuGet.config"

Write-Log ".. .. Build: Source: $TPB_TestAssets_Solution"
Write-Log ".. .. Build: Source: $TPB_TestAssets_Solution -- add NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe "sources add -Name ""locally-built-testplatform-packages"" -Source $env:TP_TESTARTIFACTS\packages\ -ConfigFile ""$nugetConfig"""
Invoke-Exe $dotnetExe "build $TPB_TestAssets_Solution --configuration $TPB_Configuration -v:minimal -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:""$($env:TP_ROOT_DIR)\TestAssets.binlog"""
Write-Log ".. .. Build: Source: $TPB_TestAssets_Solution -- remove NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe "sources remove -Name ""locally-built-testplatform-packages"" -ConfigFile ""$nugetConfig"""
try {
Write-Log ".. .. Build: Source: $TPB_TestAssets_Solution -- add NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe -Arguments "sources add -Name ""locally-built-testplatform-packages"" -Source $env:TP_TESTARTIFACTS\packages\ -ConfigFile ""$nugetConfig"""
Invoke-Exe $dotnetExe -Arguments "build $TPB_TestAssets_Solution --configuration $TPB_Configuration -v:minimal -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:""$($env:TP_ROOT_DIR)\TestAssets.binlog"""
}
finally {
Write-Log ".. .. Build: Source: $TPB_TestAssets_Solution -- remove NuGet source"
Invoke-Exe -IgnoreExitCode 1 $nugetExe -Arguments "sources remove -Name ""locally-built-testplatform-packages"" -ConfigFile ""$nugetConfig"""
}
Write-Log ".. .. Build: Complete."
Write-Log "Invoke-TestAssetsBuild: Complete. {$(Get-ElapsedTime($timer))}"
}
Expand Down Expand Up @@ -187,7 +190,6 @@ function Publish-Package
{
$timer = Start-Timer
Write-Log "Publish-Package: Started."
$dotnetExe = Get-DotNetPath
$fullCLRPackage451Dir = Get-FullCLRPackageDirectory
$fullCLRPackage45Dir = Get-FullCLRPackageDirectory45
$uap100PackageDir = $(Join-Path $env:TP_OUT_DIR "$TPB_Configuration\$TPB_TargetFrameworkUap100");
Expand Down Expand Up @@ -567,9 +569,8 @@ function Publish-Package

function Publish-Tests
{
if($TPB_PublishTests)
if($TPB_PublishTests)
{
$dotnetExe = Get-DotNetPath
Write-Log "Publish-Tests: Started."

# Adding only Perf project for now
Expand All @@ -595,12 +596,14 @@ function Publish-Tests

function Publish-PackageInternal($packagename, $framework, $output)
{
Invoke-Exe $dotnetExe "publish $packagename --configuration $TPB_Configuration --framework $framework --output $output -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild"
$dotnetExe = Get-DotNetPath
Invoke-Exe $dotnetExe -Arguments "publish $packagename --configuration $TPB_Configuration --framework $framework --output $output -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild"
}

function Publish-PackageWithRuntimeInternal($packagename, $framework, $runtime, $selfcontained, $output)
{
Invoke-Exe $dotnetExe "publish $packagename --configuration $TPB_Configuration --framework $framework --runtime $runtime --self-contained $selfcontained --output $output -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild"
$dotnetExe = Get-DotNetPath
Invoke-Exe $dotnetExe -Arguments "publish $packagename --configuration $TPB_Configuration --framework $framework --runtime $runtime --self-contained $selfcontained --output $output -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild"
}

function Copy-Loc-Files($sourceDir, $destinationDir, $dllName)
Expand Down Expand Up @@ -650,8 +653,8 @@ function Create-VsixPackage
if($TPB_LocalizedBuild) {
Copy-Loc-Files $traceDataCollectorPackageDirectory $extensionsPackageDir "Microsoft.VisualStudio.TraceDataCollector.resources.dll"
}
# Copy Microsoft.VisualStudio.Core to Extensions

# Copy Microsoft.VisualStudio.Core to Extensions
$codeCoverageCorePackagesDir = Join-Path $env:TP_PACKAGES_DIR "microsoft.visualstudio.coverage.core\$codeCoverageExternalsVersion\lib\$TPB_TargetFrameworkNS20"
Copy-Item $codeCoverageCorePackagesDir\Microsoft.VisualStudio.Coverage.Core.dll $extensionsPackageDir -Force

Expand Down Expand Up @@ -731,7 +734,7 @@ function Create-VsixPackage
Update-VsixVersion $vsixProjectDir

# Build vsix project to get TestPlatform.vsix
Invoke-Exe $msbuildPath """$vsixProjectDir\TestPlatform.csproj"" -p:Configuration=$Configuration"
Invoke-Exe $msbuildPath -Arguments """$vsixProjectDir\TestPlatform.csproj"" -p:Configuration=$Configuration"
}
else
{
Expand Down Expand Up @@ -831,7 +834,7 @@ function Create-NugetPackages
$uap10Nuget = $testhostUapPackageDir
}

Invoke-Exe $nugetExe "pack $stagingDir\$file -OutputDirectory $packageOutputDir -Version $TPB_Version -Properties Version=$TPB_Version``;JsonNetVersion=$JsonNetVersion``;Runtime=$TPB_TargetRuntime``;NetCoreTargetFramework=$TPB_TargetFrameworkCore20``;FakesPackageDir=$FakesPackageDir``;NetStandard10Framework=$TPB_TargetFrameworkNS10``;NetStandard13Framework=$TPB_TargetFrameworkNS13``;NetStandard20Framework=$TPB_TargetFrameworkNS20``;Uap10Framework=$uap10Nuget``;BranchName=$TPB_BRANCH``;CommitId=$TPB_COMMIT $additionalArgs"
Invoke-Exe $nugetExe -Arguments "pack $stagingDir\$file -OutputDirectory $packageOutputDir -Version $TPB_Version -Properties Version=$TPB_Version;JsonNetVersion=$JsonNetVersion;Runtime=$TPB_TargetRuntime;NetCoreTargetFramework=$TPB_TargetFrameworkCore20;FakesPackageDir=$FakesPackageDir;NetStandard10Framework=$TPB_TargetFrameworkNS10;NetStandard13Framework=$TPB_TargetFrameworkNS13;NetStandard20Framework=$TPB_TargetFrameworkNS20;Uap10Framework=$uap10Nuget;BranchName=$TPB_BRANCH;CommitId=$TPB_COMMIT $additionalArgs"
}

# Verifies that expected number of files gets shipped in nuget packages.
Expand Down Expand Up @@ -910,7 +913,7 @@ function Update-LocalizedResources
}

$localizationProject = Join-Path $env:TP_PACKAGE_PROJ_DIR "Localize\Localize.proj"
Invoke-Exe $dotnetExe "msbuild $localizationProject -m -nologo -v:minimal -t:Localize -p:LocalizeResources=true -nodeReuse:False"
Invoke-Exe $dotnetExe -Arguments "msbuild $localizationProject -m -nologo -v:minimal -t:Localize -p:LocalizeResources=true -nodeReuse:False"
Write-Log ".. Update-LocalizedResources: Complete. {$(Get-ElapsedTime($timer))}"
}

Expand Down Expand Up @@ -981,13 +984,13 @@ function Locate-VsInstallPath
Write-Verbose "VSInstallation requirements : $requiredPackageIds"

Try
{
{
if ($TPB_CIBuild) {
$vsInstallPath = Invoke-Exe $vswhere "-version ""(15.0"" -products * -requires $requiredPackageIds -property installationPath"
$vsInstallPath = Invoke-Exe $vswhere -CaptureOutput -Arguments "-version (15.0 -products * -requires $requiredPackageIds -property installationPath"
}
else {
# Allow using pre release versions of VS for dev builds
$vsInstallPath = Invoke-Exe $vswhere "-version ""(15.0"" -prerelease -products * -requires $requiredPackageIds -property installationPath"
$vsInstallPath = Invoke-Exe $vswhere -CaptureOutput -Arguments "-version (15.0 -prerelease -products * -requires $requiredPackageIds -property installationPath"
}
}
Catch [System.Management.Automation.MethodInvocationException]
Expand All @@ -1010,8 +1013,6 @@ function Locate-VsInstallPath
function Update-VsixVersion($vsixProjectDir)
{
Write-Log "Update-VsixVersion: Started."

$packageDir = Get-FullCLRPackageDirectory
$vsixVersion = $Version

# Build number comes in the form 20170111-01(yyyymmdd-buildNoOfThatDay)
Expand All @@ -1021,7 +1022,7 @@ function Update-VsixVersion($vsixProjectDir)
$vsixVersion = "$vsixVersion.$($vsixVersionSuffix[0])$($vsixVersionSuffix[1])"
}

$manifestContentWithVersion = Get-Content "$vsixProjectDir\source.extension.vsixmanifest" -raw | % {$_.ToString().Replace("`$version`$", "$vsixVersion") }
$manifestContentWithVersion = Get-Content "$vsixProjectDir\source.extension.vsixmanifest" -raw | ForEach-Object {$_.ToString().Replace("`$version`$", "$vsixVersion") }
Set-Content -path "$vsixProjectDir\source.extension.vsixmanifest" -value $manifestContentWithVersion

Write-Log "Update-VsixVersion: Completed."
Expand All @@ -1035,7 +1036,7 @@ function Generate-Manifest ($PackageFolder)
$generateManifestPath = Join-Path $env:TP_ROOT_DIR "scripts\build\GenerateManifest.proj"
$msbuildPath = Locate-MSBuildPath

Invoke-Exe $msbuildPath "$generateManifestPath /t:PublishToBuildAssetRegistry /p:PackagesToPublishPattern=$PackageFolder\*.nupkg /p:BUILD_BUILDNUMBER=$BuildNumber /p:PackagesPath=""$PackageFolder"" /p:Configuration=$TPB_Configuration /bl:""$env:TP_OUT_DIR\log\$Configuration\manifest-generation-$packagesFolderName.binlog"""
Invoke-Exe $msbuildPath -Arguments "$generateManifestPath /t:PublishToBuildAssetRegistry /p:PackagesToPublishPattern=$PackageFolder\*.nupkg /p:BUILD_BUILDNUMBER=$BuildNumber /p:PackagesPath=""$PackageFolder"" /p:Configuration=$TPB_Configuration /bl:""$env:TP_OUT_DIR\log\$Configuration\manifest-generation-$packagesFolderName.binlog"""

Write-Log "Generate-Manifest ($packagesFolderName): Completed."
}
Expand Down Expand Up @@ -1063,17 +1064,17 @@ function Build-SpecificProjects
}
}

if( $ProjectsToBuild -eq $null){
if( $null -eq $ProjectsToBuild){
Write-Error "No csproj name match for given pattern: $ProjectNamePatterns"
}

# Build Projects.
foreach($ProjectToBuild in $ProjectsToBuild) {
Write-Log "Building Project $ProjectToBuild"
# Restore and Build
$output = Invoke-Exe $dotnetPath "restore $ProjectToBuild"
$output = Invoke-Exe $dotnetPath -Arguments "restore $ProjectToBuild"
PrintAndExit-OnError $output
$output = Invoke-Exe $dotnetPath "build $ProjectToBuild"
$output = Invoke-Exe $dotnetPath -Arguments "build $ProjectToBuild"
PrintAndExit-OnError $output

if (-Not ($ProjectToBuild.FullName -contains "$($env:TP_ROOT_DIR)$([IO.Path]::DirectorySeparatorChar)src")) {
Expand All @@ -1090,7 +1091,7 @@ function Build-SpecificProjects
Write-Log "Copying artifacts from $fromDir to $toDir"
Get-ChildItem $fromDir | ForEach-Object {
if(-not ($_.PSIsContainer)) {
copy $_.FullName $toDir
Copy-Item $_.FullName $toDir
}
}
}
Expand Down Expand Up @@ -1142,7 +1143,6 @@ if ($Force -or $Steps -contains "Publish" -or $Steps -contains "Manifest") {
{
Generate-Manifest -PackageFolder $TPB_SourceBuildPackageOutDir
}

Copy-PackageIntoStaticDirectory
}

Expand All @@ -1151,12 +1151,11 @@ if ($Force -or $Steps -contains "PrepareAcceptanceTests") {
Invoke-TestAssetsBuild
Publish-Tests
}


if ($Script:ScriptFailed) {
Write-Log "Build failed. {$(Get-ElapsedTime($timer))}" -Level "Error"
Exit 1
} else {
Write-Log "Build succeeded. {$(Get-ElapsedTime($timer))}"
Exit 0
}
}
153 changes: 143 additions & 10 deletions scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ function Install-DotNetCli
Get-ChildItem "Env:\dotnet_*"

"`n`n---- x64 dotnet"
Invoke-Exe "$env:DOTNET_ROOT\dotnet.exe" "--info"
Invoke-Exe "$env:DOTNET_ROOT\dotnet.exe" -Arguments "--info"

"`n`n---- x86 dotnet"
# avoid erroring out because we don't have the sdk for x86 that global.json requires
try {
Invoke-Exe "${env:DOTNET_ROOT(x86)}\dotnet.exe" "--info" 2> $null
(Invoke-Exe "${env:DOTNET_ROOT(x86)}\dotnet.exe" -Arguments "--info") 2> $null
} catch {}
Write-Log "Install-DotNetCli: Complete. {$(Get-ElapsedTime($timer))}"
}
Expand All @@ -161,9 +161,7 @@ function Restore-Package
$timer = Start-Timer
Write-Log "Restore-Package: Start restoring packages to $env:TP_PACKAGES_DIR."
$dotnetExe = Get-DotNetPath

Write-Log ".. .. Restore-Package: Source: $env:TP_ROOT_DIR\src\package\external\external.csproj"
Invoke-Exe $dotnetExe "restore $env:TP_ROOT_DIR\src\package\external\external.csproj --packages $env:TP_PACKAGES_DIR -v:minimal -warnaserror -p:Version=$TPB_Version -bl:""$env:TP_OUT_DIR\log\$Configuration\external.binlog"""
Invoke-Exe $dotnetExe -Arguments "restore $env:TP_ROOT_DIR\src\package\external\external.csproj --packages $env:TP_PACKAGES_DIR -v:minimal -warnaserror -p:Version=$TPB_Version -bl:""$env:TP_OUT_DIR\log\$Configuration\external.binlog"""
Write-Log ".. .. Restore-Package: Complete."
Write-Log "Restore-Package: Complete. {$(Get-ElapsedTime($timer))}"
}
Expand Down Expand Up @@ -221,12 +219,147 @@ function Invoke-Exe {
[Parameter(Mandatory)]
[string] $Command,
[string] $Arguments,
[int[]] $IgnoreExitCode
[int[]] $IgnoreExitCode,
[switch] $CaptureOutput
)
Write-Verbose "Invoking: $Command $Arguments"
([ScriptBlock]::Create("$Command $Arguments")).Invoke()

if ($IgnoreExitCode -notcontains $LASTEXITCODE) {
Write-Verbose "Invoking: > $Command $Arguments"
Write-Verbose " > Ignored exit-codes: 0, $($IgnoreExitCode -join ', ')"

$workingDirectory = [System.IO.Path]::GetDirectoryName($Command)
$process = Start-InlineProcess -Path $Command -Arguments $Arguments -WorkingDirectory $workingDirectory -SuppressOutput:$CaptureOutput.IsPresent
$exitCode = $process.ExitCode

Write-Verbose "Done. Exit code: $exitCode"

if ($exitCode -ne 0 -and ($IgnoreExitCode -notcontains $exitCode)) {
if($CaptureOutput)
{
$process.StdErr
}
Set-ScriptFailedOnError -Command $Command -Arguments $Arguments
}

if($CaptureOutput)
{
$process.StdOut
}
}

Add-Type -TypeDefinition @'
using System;
using System.Text;
using System.Collections.Generic;
public class ProcessOutputter {
private readonly ConsoleColor _color;
private readonly List<string> _output;
private int nullCount = 0;
public ProcessOutputter (ConsoleColor color, bool suppressOutput = false) {
_color = color;
_output = new List<string>();
OutputHandler = (s, e) => {
AppendLine(e.Data);
if (!suppressOutput) {
var fg = Console.ForegroundColor;
try
{
Console.ForegroundColor = _color;
Console.WriteLine(e.Data);
}
finally
{
Console.ForegroundColor = fg;
}
}
};
}
public ProcessOutputter () {
_output = new List<string>();
OutputHandler = (s, e) => AppendLine(e.Data);
}
public System.Diagnostics.DataReceivedEventHandler OutputHandler { get; private set; }
public IEnumerable<string> Output { get { return _output; } }
private void AppendLine(string line) {
if (string.IsNullOrEmpty(line)) {
nullCount++;
return;
}
while (nullCount > 0) {
--nullCount;
_output.Add(string.Empty);
}
_output.Add(line);
}
}
'@

function Start-InlineProcess {
param (
[string]
$Path,

[string]
$WorkingDirectory,

[string]
$Arguments,

[switch]
$Elevate,

[switch]
$SuppressOutput
)

$processInfo = [System.Diagnostics.ProcessStartInfo]::new()
$processInfo.FileName = $Path
$processInfo.Arguments = $Arguments
$processInfo.WorkingDirectory = $WorkingDirectory

$processInfo.RedirectStandardError = $true
$processInfo.RedirectStandardOutput = $true
$processInfo.UseShellExecute = $false

if ($Elevate) {
$processInfo.Verb = "runas"
}

$outputHandler = [ProcessOutputter]::new("White", $SuppressOutput.IsPresent)
$errorHandler = [ProcessOutputter]::new("Red", $SuppressOutput.IsPresent)
$outputDataReceived = $outputHandler.OutputHandler
$errorDataReceivedEvent = $errorHandler.OutputHandler

$process = New-Object System.Diagnostics.Process
$process.EnableRaisingEvents = $true
$process.add_OutputDataReceived($outputDataReceived)
$process.add_ErrorDataReceived($errorDataReceivedEvent)

try {
$process.StartInfo = $processInfo
if (-not $process.Start()) {
return $null
}
$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
$process.WaitForExit()

return @{
ExitCode = $process.ExitCode
StdOut = $outputHandler.Output
StdErr = $errorHandler.Output
}
}
finally {
$process.remove_OutputDataReceived($outputDataReceived)
$process.remove_ErrorDataReceived($errorDataReceived)
$process.Dispose()
}
}

0 comments on commit 8683f41

Please sign in to comment.