Code Cleanup for environment data gathering in build.psm1 (#11572)

This commit is contained in:
xtqqczze 2020-01-15 19:03:48 +00:00 committed by Travis Plunk
parent 3d35a51cbc
commit ba9a7b1948

View file

@ -99,21 +99,19 @@ function Get-PSCommitId
function Get-EnvironmentInformation
{
$environment = @{}
$environment = @{'IsWindows' = [System.Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT}
# PowerShell will likely not be built on pre-1709 nanoserver
if ($PSVersionTable.ContainsKey("PSEdition") -and "Core" -eq $PSVersionTable.PSEdition) {
$environment += @{'IsCoreCLR' = $true}
$environment += @{'IsLinux' = $IsLinux}
$environment += @{'IsMacOS' = $IsMacOS}
$environment += @{'IsWindows' = $IsWindows}
if ('System.Management.Automation.Platform' -as [type]) {
$environment += @{'IsCoreCLR' = [System.Management.Automation.Platform]::IsCoreCLR}
$environment += @{'IsLinux' = [System.Management.Automation.Platform]::IsLinux}
$environment += @{'IsMacOS' = [System.Management.Automation.Platform]::IsMacOS}
} else {
$environment += @{'IsCoreCLR' = $false}
$environment += @{'IsLinux' = $false}
$environment += @{'IsMacOS' = $false}
$environment += @{'IsWindows' = $true}
}
if ($Environment.IsWindows)
if ($environment.IsWindows)
{
$environment += @{'IsAdmin' = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)}
$environment += @{'nugetPackagesRoot' = "${env:USERPROFILE}\.nuget\packages"}
@ -123,7 +121,7 @@ function Get-EnvironmentInformation
$environment += @{'nugetPackagesRoot' = "${env:HOME}/.nuget/packages"}
}
if ($Environment.IsMacOS) {
if ($environment.IsMacOS) {
$environment += @{'UsingHomebrew' = [bool](Get-Command brew -ErrorAction ignore)}
$environment += @{'UsingMacports' = [bool](Get-Command port -ErrorAction ignore)}
@ -132,7 +130,7 @@ function Get-EnvironmentInformation
}
}
if ($Environment.IsLinux) {
if ($environment.IsLinux) {
$LinuxInfo = Get-Content /etc/os-release -Raw | ConvertFrom-StringData
$lsb_release = Get-Command lsb_release -Type Application -ErrorAction Ignore | Select-Object -First 1
if ($lsb_release) {
@ -144,23 +142,23 @@ function Get-EnvironmentInformation
$environment += @{'LinuxInfo' = $LinuxInfo}
$environment += @{'IsDebian' = $LinuxInfo.ID -match 'debian' -or $LinuxInfo.ID -match 'kali'}
$environment += @{'IsDebian9' = $Environment.IsDebian -and $LinuxInfo.VERSION_ID -match '9'}
$environment += @{'IsDebian10' = $Environment.IsDebian -and $LinuxInfo.VERSION_ID -match '10'}
$environment += @{'IsDebian11' = $Environment.IsDebian -and $LinuxInfo.PRETTY_NAME -match 'bullseye'}
$environment += @{'IsDebian9' = $environment.IsDebian -and $LinuxInfo.VERSION_ID -match '9'}
$environment += @{'IsDebian10' = $environment.IsDebian -and $LinuxInfo.VERSION_ID -match '10'}
$environment += @{'IsDebian11' = $environment.IsDebian -and $LinuxInfo.PRETTY_NAME -match 'bullseye'}
$environment += @{'IsUbuntu' = $LinuxInfo.ID -match 'ubuntu' -or $LinuxID -match 'Ubuntu'}
$environment += @{'IsUbuntu16' = $Environment.IsUbuntu -and $LinuxInfo.VERSION_ID -match '16.04'}
$environment += @{'IsUbuntu18' = $Environment.IsUbuntu -and $LinuxInfo.VERSION_ID -match '18.04'}
$environment += @{'IsUbuntu16' = $environment.IsUbuntu -and $LinuxInfo.VERSION_ID -match '16.04'}
$environment += @{'IsUbuntu18' = $environment.IsUbuntu -and $LinuxInfo.VERSION_ID -match '18.04'}
$environment += @{'IsCentOS' = $LinuxInfo.ID -match 'centos' -and $LinuxInfo.VERSION_ID -match '7'}
$environment += @{'IsFedora' = $LinuxInfo.ID -match 'fedora' -and $LinuxInfo.VERSION_ID -ge 24}
$environment += @{'IsOpenSUSE' = $LinuxInfo.ID -match 'opensuse'}
$environment += @{'IsSLES' = $LinuxInfo.ID -match 'sles'}
$environment += @{'IsRedHat' = $LinuxInfo.ID -match 'rhel'}
$environment += @{'IsRedHat7' = $Environment.IsRedHat -and $LinuxInfo.VERSION_ID -match '7' }
$environment += @{'IsOpenSUSE13' = $Environmenst.IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '13'}
$environment += @{'IsOpenSUSE42.1' = $Environment.IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '42.1'}
$environment += @{'IsDebianFamily' = $Environment.IsDebian -or $Environment.IsUbuntu}
$environment += @{'IsRedHatFamily' = $Environment.IsCentOS -or $Environment.IsFedora -or $Environment.IsRedHat}
$environment += @{'IsSUSEFamily' = $Environment.IsSLES -or $Environment.IsOpenSUSE}
$environment += @{'IsRedHat7' = $environment.IsRedHat -and $LinuxInfo.VERSION_ID -match '7' }
$environment += @{'IsOpenSUSE13' = $environment.IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '13'}
$environment += @{'IsOpenSUSE42.1' = $environment.IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '42.1'}
$environment += @{'IsDebianFamily' = $environment.IsDebian -or $environment.IsUbuntu}
$environment += @{'IsRedHatFamily' = $environment.IsCentOS -or $environment.IsFedora -or $environment.IsRedHat}
$environment += @{'IsSUSEFamily' = $environment.IsSLES -or $environment.IsOpenSUSE}
$environment += @{'IsAlpine' = $LinuxInfo.ID -match 'alpine'}
# Workaround for temporary LD_LIBRARY_PATH hack for Fedora 24
@ -184,7 +182,7 @@ function Get-EnvironmentInformation
return [PSCustomObject] $environment
}
$Environment = Get-EnvironmentInformation
$environment = Get-EnvironmentInformation
# Autoload (in current session) temporary modules used in our tests
$TestModulePath = Join-Path $PSScriptRoot "test/tools/Modules"
@ -294,11 +292,11 @@ function Start-PSBuild {
$PSModuleRestore = $true
}
if ($Runtime -eq "linux-arm" -and -not $Environment.IsUbuntu) {
if ($Runtime -eq "linux-arm" -and -not $environment.IsUbuntu) {
throw "Cross compiling for linux-arm is only supported on Ubuntu environment"
}
if ("win-arm","win-arm64" -contains $Runtime -and -not $Environment.IsWindows) {
if ("win-arm","win-arm64" -contains $Runtime -and -not $environment.IsWindows) {
throw "Cross compiling for win-arm or win-arm64 is only supported on Windows environment"
}
function Stop-DevPowerShell {
@ -376,7 +374,7 @@ Fix steps:
$Arguments += "--output", (Split-Path $Options.Output)
}
if ($Options.Runtime -like 'win*' -or ($Options.Runtime -like 'fxdependent*' -and $Environment.IsWindows)) {
if ($Options.Runtime -like 'win*' -or ($Options.Runtime -like 'fxdependent*' -and $environment.IsWindows)) {
$Arguments += "/property:IsWindows=true"
}
else {
@ -501,18 +499,18 @@ Fix steps:
$psVersion = git --git-dir="$PSScriptRoot/.git" describe
}
if ($Environment.IsRedHatFamily -or $Environment.IsDebian) {
if ($environment.IsRedHatFamily -or $environment.IsDebian) {
# Symbolic links added here do NOT affect packaging as we do not build on Debian.
# add two symbolic links to system shared libraries that libmi.so is dependent on to handle
# platform specific changes. This is the only set of platforms needed for this currently
# as Ubuntu has these specific library files in the platform and macOS builds for itself
# against the correct versions.
if ($Environment.IsDebian10 -or $Environment.IsDebian11){
if ($environment.IsDebian10 -or $environment.IsDebian11){
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.1"
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1"
}
elseif ($Environment.IsDebian9){
elseif ($environment.IsDebian9){
# NOTE: Debian 8 doesn't need these symlinks
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.0.2"
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2"
@ -538,7 +536,7 @@ Fix steps:
# publish powershell.config.json
$config = @{}
if ($Environment.IsWindows) {
if ($environment.IsWindows) {
$config = @{ "Microsoft.PowerShell:ExecutionPolicy" = "RemoteSigned" }
}
@ -743,9 +741,9 @@ function New-PSOptions {
Write-Verbose "Using framework '$Framework'"
if (-not $Runtime) {
if ($Environment.IsLinux) {
if ($environment.IsLinux) {
$Runtime = "linux-x64"
} elseif ($Environment.IsMacOS) {
} elseif ($environment.IsMacOS) {
$Runtime = "osx-x64"
} else {
$RID = dotnet --info | ForEach-Object {
@ -767,7 +765,7 @@ function New-PSOptions {
}
}
$PowerShellDir = if ($Runtime -like 'win*' -or ($Runtime -like 'fxdependent*' -and $Environment.IsWindows)) {
$PowerShellDir = if ($Runtime -like 'win*' -or ($Runtime -like 'fxdependent*' -and $environment.IsWindows)) {
"powershell-win-core"
} else {
"powershell-unix"
@ -783,9 +781,9 @@ function New-PSOptions {
$Executable = if ($Runtime -like 'fxdependent*') {
"pwsh.dll"
} elseif ($Environment.IsLinux -or $Environment.IsMacOS) {
} elseif ($environment.IsLinux -or $environment.IsMacOS) {
"pwsh"
} elseif ($Environment.IsWindows) {
} elseif ($environment.IsWindows) {
"pwsh.exe"
}
@ -1038,12 +1036,12 @@ function Start-PSPester {
# we need to do few checks and if user didn't provide $ExcludeTag explicitly, we should alternate the default
if ($Unelevate)
{
if (-not $Environment.IsWindows)
if (-not $environment.IsWindows)
{
throw '-Unelevate is currently not supported on non-Windows platforms'
}
if (-not $Environment.IsAdmin)
if (-not $environment.IsAdmin)
{
throw '-Unelevate cannot be applied because the current user is not Administrator'
}
@ -1053,21 +1051,21 @@ function Start-PSPester {
$ExcludeTag += 'RequireAdminOnWindows'
}
}
elseif ($Environment.IsWindows -and (-not $Environment.IsAdmin))
elseif ($environment.IsWindows -and (-not $environment.IsAdmin))
{
if (-not $PSBoundParameters.ContainsKey('ExcludeTag'))
{
$ExcludeTag += 'RequireAdminOnWindows'
}
}
elseif (-not $Environment.IsWindows -and (-not $Sudo.IsPresent))
elseif (-not $environment.IsWindows -and (-not $Sudo.IsPresent))
{
if (-not $PSBoundParameters.ContainsKey('ExcludeTag'))
{
$ExcludeTag += 'RequireSudoOnUnix'
}
}
elseif (-not $Environment.IsWindows -and $Sudo.IsPresent)
elseif (-not $environment.IsWindows -and $Sudo.IsPresent)
{
if (-not $PSBoundParameters.ContainsKey('Tag'))
{
@ -1081,7 +1079,7 @@ function Start-PSPester {
$publishArgs = @{ }
# if we are building for Alpine, we must include the runtime as linux-x64
# will not build runnable test tools
if ( $Environment.IsAlpine ) {
if ( $environment.IsAlpine ) {
$publishArgs['runtime'] = 'alpine-x64'
}
Publish-PSTestTools @publishArgs | ForEach-Object {Write-Host $_}
@ -1101,7 +1099,7 @@ function Start-PSPester {
$command += '$env:PSModulePath = '+"'$newPathFragment'" + '+$env:PSModulePath;'
# Windows needs the execution policy adjusted
if ($Environment.IsWindows) {
if ($environment.IsWindows) {
$command += "Set-ExecutionPolicy -Scope Process Unrestricted; "
}
@ -1197,7 +1195,7 @@ function Start-PSPester {
## Create the config.json file to enable the given experimental feature.
## On Windows, we need to have 'RemoteSigned' declared for ExecutionPolicy because the ExecutionPolicy is 'Restricted' by default.
## On Unix, ExecutionPolicy is not supported, so we don't need to declare it.
if ($Environment.IsWindows) {
if ($environment.IsWindows) {
$content = @"
{
"Microsoft.PowerShell:ExecutionPolicy":"RemoteSigned",
@ -1375,7 +1373,7 @@ function script:Start-UnelevatedProcess
[string]$process,
[string[]]$arguments
)
if (-not $Environment.IsWindows)
if (-not $environment.IsWindows)
{
throw "Start-UnelevatedProcess is currently not supported on non-Windows platforms"
}
@ -1552,9 +1550,9 @@ function Start-PSxUnit {
# Path manipulation to obtain test project output directory
if(-not $Environment.IsWindows)
if(-not $environment.IsWindows)
{
if($Environment.IsMacOS)
if($environment.IsMacOS)
{
$nativeLib = "$Content/libpsl-native.dylib"
}
@ -1613,17 +1611,19 @@ function Install-Dotnet {
$uninstallObtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
# Install for Linux and OS X
if ($Environment.IsLinux -or $Environment.IsMacOS) {
if ($environment.IsLinux -or $environment.IsMacOS) {
$curl = Get-Command -Name curl -CommandType Application -TotalCount 1 -ErrorAction Stop
# Uninstall all previous dotnet packages
$uninstallScript = if ($Environment.IsUbuntu) {
$uninstallScript = if ($environment.IsUbuntu) {
"dotnet-uninstall-debian-packages.sh"
} elseif ($Environment.IsMacOS) {
} elseif ($environment.IsMacOS) {
"dotnet-uninstall-pkgs.sh"
}
if ($uninstallScript) {
Start-NativeExecution {
curl -sO $uninstallObtainUrl/uninstall/$uninstallScript
& $curl -sO $uninstallObtainUrl/uninstall/$uninstallScript
Invoke-Expression "$sudo bash ./$uninstallScript"
}
} else {
@ -1633,15 +1633,15 @@ function Install-Dotnet {
# Install new dotnet 1.1.0 preview packages
$installScript = "dotnet-install.sh"
Start-NativeExecution {
curl -sO $installObtainUrl/$installScript
& $curl -sO $installObtainUrl/$installScript
bash ./$installScript -c $Channel -v $Version
}
} elseif ($Environment.IsWindows) {
} elseif ($environment.IsWindows) {
Remove-Item -ErrorAction SilentlyContinue -Recurse -Force ~\AppData\Local\Microsoft\dotnet
$installScript = "dotnet-install.ps1"
Invoke-WebRequest -Uri $installObtainUrl/$installScript -OutFile $installScript
if (-not $Environment.IsCoreCLR) {
if (-not $environment.IsCoreCLR) {
& ./$installScript -Channel $Channel -Version $Version
} else {
# dotnet-install.ps1 uses APIs that are not supported in .NET Core, so we run it with Windows PowerShell
@ -1653,9 +1653,9 @@ function Install-Dotnet {
}
function Get-RedHatPackageManager {
if ($Environment.IsCentOS) {
if ($environment.IsCentOS) {
"yum install -y -q"
} elseif ($Environment.IsFedora) {
} elseif ($environment.IsFedora) {
"dnf install -y -q"
} else {
throw "Error determining package manager for this distribution."
@ -1682,19 +1682,19 @@ function Start-PSBootstrap {
Push-Location $PSScriptRoot/tools
try {
if ($Environment.IsLinux -or $Environment.IsMacOS) {
if ($environment.IsLinux -or $environment.IsMacOS) {
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
if ($BuildLinuxArm -and -not $Environment.IsUbuntu) {
if ($BuildLinuxArm -and -not $environment.IsUbuntu) {
Write-Error "Cross compiling for linux-arm is only supported on Ubuntu environment"
return
}
# Install ours and .NET's dependencies
$Deps = @()
if ($Environment.IsUbuntu) {
if ($environment.IsUbuntu) {
# Build tools
$Deps += "curl", "g++", "cmake", "make"
@ -1704,8 +1704,8 @@ function Start-PSBootstrap {
# .NET Core required runtime libraries
$Deps += "libunwind8"
if ($Environment.IsUbuntu16) { $Deps += "libicu55" }
elseif ($Environment.IsUbuntu18) { $Deps += "libicu60"}
if ($environment.IsUbuntu16) { $Deps += "libicu55" }
elseif ($environment.IsUbuntu18) { $Deps += "libicu60"}
# Packaging tools
if ($Package) { $Deps += "ruby-dev", "groff", "libffi-dev" }
@ -1724,7 +1724,7 @@ function Start-PSBootstrap {
# change the apt frontend back to the original
$env:DEBIAN_FRONTEND=$originalDebianFrontEnd
}
} elseif ($Environment.IsRedHatFamily) {
} elseif ($environment.IsRedHatFamily) {
# Build tools
$Deps += "which", "curl", "gcc-c++", "cmake", "make"
@ -1748,7 +1748,7 @@ function Start-PSBootstrap {
Start-NativeExecution {
Invoke-Expression "$baseCommand $Deps"
}
} elseif ($Environment.IsSUSEFamily) {
} elseif ($environment.IsSUSEFamily) {
# Build tools
$Deps += "gcc", "cmake", "make"
@ -1768,10 +1768,10 @@ function Start-PSBootstrap {
Start-NativeExecution {
Invoke-Expression "$baseCommand $Deps"
}
} elseif ($Environment.IsMacOS) {
if ($Environment.UsingHomebrew) {
} elseif ($environment.IsMacOS) {
if ($environment.UsingHomebrew) {
$PackageManager = "brew"
} elseif ($Environment.UsingMacports) {
} elseif ($environment.UsingMacports) {
$PackageManager = "$sudo port"
}
@ -1784,7 +1784,7 @@ function Start-PSBootstrap {
# Install dependencies
# ignore exitcode, because they may be already installed
Start-NativeExecution ([ScriptBlock]::Create("$PackageManager install $Deps")) -IgnoreExitcode
} elseif ($Environment.IsAlpine) {
} elseif ($environment.IsAlpine) {
$Deps += 'libunwind', 'libcurl', 'bash', 'cmake', 'clang', 'build-base', 'git', 'curl'
Start-NativeExecution {
@ -1798,7 +1798,7 @@ function Start-PSBootstrap {
# We cannot guess if the user wants to run gem install as root on linux and windows,
# but macOs usually requires sudo
$gemsudo = ''
if($Environment.IsMacOS -or $env:TF_BUILD) {
if($environment.IsMacOS -or $env:TF_BUILD) {
$gemsudo = $sudo
}
Start-NativeExecution ([ScriptBlock]::Create("$gemsudo gem install fpm -v 1.11.0 --no-document"))
@ -1823,7 +1823,7 @@ function Start-PSBootstrap {
if($Force.IsPresent) {
Write-Log "Installing dotnet due to -Force."
}
elseif(!$dotNetExistis) {
elseif(!$dotNetExists) {
Write-Log "dotnet not present. Installing dotnet."
}
else {
@ -1838,7 +1838,7 @@ function Start-PSBootstrap {
}
# Install Windows dependencies if `-Package` or `-BuildWindowsNative` is specified
if ($Environment.IsWindows) {
if ($environment.IsWindows) {
## The VSCode build task requires 'pwsh.exe' to be found in Path
if (-not (Get-Command -Name pwsh.exe -CommandType Application -ErrorAction Ignore))
{
@ -1872,7 +1872,7 @@ function Start-DevPowerShell {
$BinDir = Split-Path (New-PSOptions -Configuration $Configuration).Output
}
if ((-not $NoNewWindow) -and ($Environment.IsCoreCLR)) {
if ((-not $NoNewWindow) -and ($environment.IsCoreCLR)) {
Write-Warning "Start-DevPowerShell -NoNewWindow is currently implied in PowerShellCore edition https://github.com/PowerShell/PowerShell/issues/1543"
$NoNewWindow = $true
}
@ -1989,7 +1989,7 @@ function Start-ResGen
function Find-Dotnet() {
$originalPath = $env:PATH
$dotnetPath = if ($Environment.IsWindows) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" }
$dotnetPath = if ($environment.IsWindows) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" }
# If there dotnet is already in the PATH, check to see if that version of dotnet can find the required SDK
# This is "typically" the globally installed dotnet
@ -2208,10 +2208,10 @@ function Start-CrossGen {
}
# Get the path to crossgen
$crossGenExe = if ($Environment.IsWindows) { "crossgen.exe" } else { "crossgen" }
$crossGenExe = if ($environment.IsWindows) { "crossgen.exe" } else { "crossgen" }
# The crossgen tool is only published for these particular runtimes
$crossGenRuntime = if ($Environment.IsWindows) {
$crossGenRuntime = if ($environment.IsWindows) {
if ($Runtime -match "-x86") {
"win-x86"
} elseif ($Runtime -match "-x64") {
@ -2249,11 +2249,11 @@ function Start-CrossGen {
# clrjit.dll on Windows or libclrjit.so/dylib on Linux/OS X
$crossGenRequiredAssemblies = @("mscorlib.dll", "System.Private.CoreLib.dll")
$crossGenRequiredAssemblies += if ($Environment.IsWindows) {
$crossGenRequiredAssemblies += if ($environment.IsWindows) {
"clrjit.dll"
} elseif ($Environment.IsLinux) {
} elseif ($environment.IsLinux) {
"libclrjit.so"
} elseif ($Environment.IsMacOS) {
} elseif ($environment.IsMacOS) {
"libclrjit.dylib"
}
@ -2303,7 +2303,7 @@ function Start-CrossGen {
)
# Add Windows specific libraries
if ($Environment.IsWindows) {
if ($environment.IsWindows) {
$psCoreAssemblyList += @(
"Microsoft.PowerShell.CoreCLR.Eventing.dll",
"Microsoft.WSMan.Management.dll",
@ -3101,12 +3101,12 @@ function New-TestPackage
$rootFolder = $env:AGENT_WORKFOLDER
}
Write-Verbose -Verbose "RootFolder: $rootFolder"
Write-Verbose -Message "RootFolder: $rootFolder" -Verbose
$packageRoot = Get-UniquePackageFolderName -Root $rootFolder
$null = New-Item -ItemType Directory -Path $packageRoot -Force
$packagePath = Join-Path $Destination "TestPackage.zip"
Write-Verbose -Verbose "PackagePath: $packagePath"
Write-Verbose -Message "PackagePath: $packagePath" -Verbose
# Build test tools so they are placed in appropriate folders under 'test' then copy to package root.
$null = Publish-PSTestTools -runtime $Runtime