Fix up init.cmd to set env vars from .ps1 child process

This commit is contained in:
Andrew Arnott 2020-08-19 12:13:49 -06:00
Родитель d5f3e774f5
Коммит 9469bdf8c4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A9B9910CDCCDA441
4 изменённых файлов: 65 добавлений и 28 удалений

Просмотреть файл

@ -4,8 +4,13 @@
Azure Pipeline and CMD environments are considered.
.PARAMETER Variables
A hashtable of variables to be set.
.PARAMETER PrependPath
A set of paths to prepend to the PATH environment variable.
.OUTPUTS
A boolean indicating whether the environment variables can be expected to propagate to the caller's environment.
.DESCRIPTION
The CmdEnvScriptPath environment variable may be optionally set to a path to a cmd shell script to be created (or appended to if it already exists) that will set the environment variables in cmd.exe that are set within the PowerShell environment.
This is used by init.cmd in order to reapply any new environment variables to the parent cmd.exe process that were set in the powershell child process.
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
@ -18,7 +23,7 @@ if ($Variables.Count -eq 0) {
return $true
}
$cmdInstructions = !$env:TF_BUILD -and !$env:GITHUB_ACTIONS -and $env:PS1UnderCmd -eq '1'
$cmdInstructions = !$env:TF_BUILD -and !$env:GITHUB_ACTIONS -and !$env:CmdEnvScriptPath -and ($env:PS1UnderCmd -eq '1')
if ($cmdInstructions) {
Write-Warning "Environment variables have been set that will be lost because you're running under cmd.exe"
Write-Host "Environment variables that must be set manually:" -ForegroundColor Blue
@ -38,6 +43,7 @@ if ($env:GITHUB_ACTIONS) {
Write-Host "GitHub Actions detected. Logging commands will be used to propagate environment variables and prepend path."
}
$CmdEnvScript = ''
$Variables.GetEnumerator() |% {
Set-Item -Path env:$($_.Key) -Value $_.Value
@ -52,6 +58,8 @@ $Variables.GetEnumerator() |% {
if ($cmdInstructions) {
Write-Host "SET $($_.Key)=$($_.Value)"
}
$CmdEnvScript += "SET $($_.Key)=$($_.Value)`r`n"
}
$pathDelimiter = ';'
@ -73,7 +81,17 @@ if ($PrependPath) {
if ($env:GITHUB_ACTIONS) {
Write-Host "::add-path::$_"
}
$CmdEnvScript += "SET PATH=$_$pathDelimiter%PATH%"
}
}
if ($env:CmdEnvScriptPath) {
if (Test-Path $env:CmdEnvScriptPath) {
$CmdEnvScript = (Get-Content -Path $env:CmdEnvScriptPath) + $CmdEnvScript
}
Set-Content -Path $env:CmdEnvScriptPath -Value $CmdEnvScript
}
return !$cmdInstructions

Просмотреть файл

@ -1,4 +1,20 @@
@echo off
SETLOCAL
set PS1UnderCmd=1
:: Get the datetime in a format that can go in a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%
set CmdEnvScriptPath=%temp%\envvarscript_%_my_datetime%.cmd
powershell.exe -NoProfile -NoLogo -ExecutionPolicy bypass -Command "try { & '%~dpn0.ps1' %*; exit $LASTEXITCODE } catch { write-host $_; exit 1 }"
:: Set environment variables in the parent cmd.exe process.
IF EXIST "%CmdEnvScriptPath%" (
ENDLOCAL
CALL "%CmdEnvScriptPath%"
DEL "%CmdEnvScriptPath%"
)

Просмотреть файл

@ -2,28 +2,31 @@
<#
.SYNOPSIS
Installs dependencies required to build and test the projects in this repository.
Installs dependencies required to build and test the projects in this repository.
.DESCRIPTION
This MAY not require elevation, as the SDK and runtimes are installed to a per-user location,
unless the `-InstallLocality` switch is specified directing to a per-repo or per-machine location.
See detailed help on that switch for more information.
This MAY not require elevation, as the SDK and runtimes are installed to a per-user location,
unless the `-InstallLocality` switch is specified directing to a per-repo or per-machine location.
See detailed help on that switch for more information.
The CmdEnvScriptPath environment variable may be optionally set to a path to a cmd shell script to be created (or appended to if it already exists) that will set the environment variables in cmd.exe that are set within the PowerShell environment.
This is used by init.cmd in order to reapply any new environment variables to the parent cmd.exe process that were set in the powershell child process.
.PARAMETER InstallLocality
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
.PARAMETER NoPrerequisites
Skips the installation of prerequisite software (e.g. SDKs, tools).
Skips the installation of prerequisite software (e.g. SDKs, tools).
.PARAMETER UpgradePrerequisites
Takes time to install prerequisites even if they are already present in case they need to be upgraded.
No effect if -NoPrerequisites is specified.
Takes time to install prerequisites even if they are already present in case they need to be upgraded.
No effect if -NoPrerequisites is specified.
.PARAMETER NoRestore
Skips the package restore step.
Skips the package restore step.
.PARAMETER AccessToken
An optional access token for authenticating to Azure Artifacts authenticated feeds.
An optional access token for authenticating to Azure Artifacts authenticated feeds.
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param (

Просмотреть файл

@ -2,19 +2,19 @@
<#
.SYNOPSIS
Installs the .NET SDK specified in the global.json file at the root of this repository,
along with supporting .NET Core runtimes used for testing.
Installs the .NET SDK specified in the global.json file at the root of this repository,
along with supporting .NET Core runtimes used for testing.
.DESCRIPTION
This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location,
unless `-InstallLocality machine` is specified.
This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location,
unless `-InstallLocality machine` is specified.
.PARAMETER InstallLocality
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
Param (