Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Mar 6, 2024
1 parent b9f9ba2 commit c031ab1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
19 changes: 15 additions & 4 deletions docs/contributing/Powershell Guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,35 @@ script

### Coding Guidelines

Use `Exec-*` functions to execute scripts, programs or `dotnet` commands. This adds automatic
Use `Exec-*` functions to execute programs or `dotnet` commands. This adds automatic
error detection on invocation failure, incorrect parameters, etc ...

``` powershell
```powershell
# DO NOT
& msbuild /v:m /m Roslyn.sln
& dotnet build Roslyn.sln
.\eng\make-bootstrap.ps1
# DO
Exec-Command "msbuild" "/v:m /m Roslyn.sln"
Exec-DotNet "build Roslyn.sln"
Exec-Script "eng\make-bootstrap.ps1"
```

Scripts that have many executions of `dotnet` commands can store the `dotnet` command in a variable
and use `Exec-Command` instead.

Call `Test-LastExitCode` after invoking a powershell script to make sure failure is not ignored.

```powershell
# DO NOT
& eng/make-bootstrap.ps1
Write-Host "Done with Bootstrap"
# DO
& eng/make-bootstrap.ps1
Test-LastExitCode
Write-Host "Done with Bootstrap"
```

Whenever comparing with `$null` always make sure to put `$null` on the left hand side of the
operator. For non-collection types this doesn't really affect behavior. For collection types though
having a collection on the left hand side changes the meaning of `-ne` and `-eq`. Instead of checking for `$null` it will instead compare collection contents.
Expand Down
19 changes: 6 additions & 13 deletions eng/build-utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ function Exec-Command([string]$command, [string]$commandArgs, [switch]$useConsol
Exec-CommandCore -command $command -commandArgs $commandArgs -useConsole:$useConsole -echoCommand:$echoCommand
}

# Handy function for executing a powershell script in a clean environment with
# arguments. Prefer this over & sourcing a script as it will both use a clean
# environment and do proper error checking
#
# The -useConsole argument controls if the process should re-use the current
# console for output or return output as a string
function Exec-Script([string]$script, [string]$scriptArgs = "", [switch]$useConsole = $true, [switch]$echoCommand = $true) {
if ($args -ne "") {
throw "Extra arguments passed to Exec-Script: $args"
}
Exec-CommandCore -command "powershell" -commandArgs "-noprofile -executionPolicy RemoteSigned -file `"$script`" $scriptArgs" -useConsole:$useConsole -echoCommand:$echoCommand
}

# Handy function for executing a dotnet command without having to track down the
# proper dotnet executable or ensure it's on the path.
function Exec-DotNet([string]$commandArgs = "", [switch]$useConsole = $true, [switch]$echoCommand = $true) {
Expand Down Expand Up @@ -182,6 +169,12 @@ function Ensure-DotnetSdk() {
throw "Could not find dotnet executable in $dotnetInstallDir"
}

function Test-LastExitCode() {
if ($LASTEXITCODE -ne 0) {
throw "Last command failed with exit code $LASTEXITCODE"
}
}

# Walks up the source tree, starting at the given file's directory, and returns a FileInfo object for the first .csproj file it finds, if any.
function Get-ProjectFile([object]$fileInfo) {
Push-Location
Expand Down
3 changes: 2 additions & 1 deletion eng/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,8 @@ try {
if ($bootstrap -and $bootstrapDir -eq "") {
Write-Host "Building bootstrap Compiler"
$bootstrapDir = Join-Path (Join-Path $ArtifactsDir "bootstrap") "build"
Exec-Script (Join-Path $PSScriptRoot "make-bootstrap.ps1") "-output $bootstrapDir -force -ci:$ci"
& eng/make-bootstrap.ps1 -output $bootstrapDir -force -ci:$ci
Test-LastExitCode
}

if ($restore -or $build -or $rebuild -or $pack -or $sign -or $publish) {
Expand Down
10 changes: 6 additions & 4 deletions eng/test-build-correctness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ try {

if ($bootstrapDir -eq "") {
Write-Host "Building bootstrap compiler"
$bootstrapDir = Join-Path $ArtifactsDir "bootstrap" "correctness"
Exec-Script (Join-Path $PSScriptRoot "make-bootstrap.ps1") "-output $bootstrapDir -ci:$ci"
$bootstrapDir = Join-Path $ArtifactsDir (Join-Path "bootstrap" "correctness")
& eng/make-bootstrap.ps1 -output $bootstrapDir -ci:$ci
}

Write-Host "Building Roslyn"
Exec-Script (Join-Path $PSScriptRoot "build.ps1") "-restore -build -bootstrapDir:$bootstrapDir -ci:$true -prepareMachine:$true -runAnalyzers:$true -configuration:$configuration -pack -binaryLog -useGlobalNuGetCache:$false -warnAsError:$true -properties `"/p:RoslynEnforceCodeStyle=true`""
& eng/build.ps1 -restore -build -bootstrapDir:$bootstrapDir -prepareMachine:true -runAnalyzers:$true -configuration:$configuration -pack -binaryLog -useGlobalNuGetCache:$false -warnAsError:$true -properties `"/p:RoslynEnforceCodeStyle=true`
Test-LastExitCode

Subst-TempDir

Expand All @@ -62,7 +63,8 @@ try {

# Verify the state of our generated syntax files
Write-Host "Checking generated compiler files"
Exec-Script (Join-Path $PSScriptRoot "generate-compiler-code.ps1") "-test -configuration:$configuration"
& eng/generate-compiler-code.ps1 -test -configuration:$configuration
Test-LastExitCode
Exec-DotNet "tool run dotnet-format whitespace . --folder --include-generated --include src/Compilers/CSharp/Portable/Generated/ src/Compilers/VisualBasic/Portable/Generated/ src/ExpressionEvaluator/VisualBasic/Source/ResultProvider/Generated/ --verify-no-changes"
Write-Host ""

Expand Down
14 changes: 7 additions & 7 deletions eng/test-determinism.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ function Run-Test() {

try {
. (Join-Path $PSScriptRoot "build-utils.ps1")
Push-Location $RepoRoot
$prepareMachine = $ci

# Create all of the logging directories
$errorDir = Join-Path $LogDir "DeterminismFailures"
Expand All @@ -276,7 +278,6 @@ try {
Create-Directory $errorDirLeft
Create-Directory $errorDirRight

$ci = $true
$runAnalyzers = $false
$binaryLog = $true
$officialBuildId = ""
Expand All @@ -286,21 +287,20 @@ try {
if ($bootstrapDir -eq "") {
Write-Host "Building bootstrap compiler"
$bootstrapDir = Join-Path $ArtifactsDir "bootstrap" "determinism"
Exec-Script (Join-Path $PSScriptRoot "make-bootstrap.ps1") "-output $bootstrapDir -ci:$ci"
& eng/make-bootstrap.ps1 -output $bootstrapDir -ci:$ci
Test-LastExitCode
}

Run-Test
exit 0
ExitWithExitCode 0
}
catch {
Write-Host $_
Write-Host $_.Exception
Write-Host $_.ScriptStackTrace
exit 1
ExitWithExitCode 1
}
finally {
Write-Host "Stopping VBCSCompiler"
Get-Process VBCSCompiler -ErrorAction SilentlyContinue | Stop-Process
Write-Host "Stopped VBCSCompiler"
Pop-Location
}

3 changes: 2 additions & 1 deletion eng/test-rebuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ try {

if ($bootstrap) {
Write-Host "Building Roslyn"
Exec-Script (Join-Path $PSScriptRoot "build.ps1") "-restore -build -bootstrap -prepareMachine:$prepareMachine -ci:$ci -useGlobalNuGetCache:$useGlobalNuGetCache -configuration:$configuration -pack -binaryLog"
& eng/build.ps1 -restore -build -bootstrap -prepareMachine:$prepareMachine -ci:$ci -useGlobalNuGetCache:$useGlobalNuGetCache -configuration:$configuration -pack -binaryLog
Test-LastExitCode
}

Subst-TempDir
Expand Down

0 comments on commit c031ab1

Please sign in to comment.