From c73761db96d9ac786cb55f1db70e829cfec851e5 Mon Sep 17 00:00:00 2001 From: nicole mazzuca Date: Fri, 7 Jun 2019 13:20:26 -0700 Subject: [PATCH] Fix OpenConsole.psm1 to use vswhere (#1113) * Fix OpenConsole.psm1 to use vswhere I'm not sure this is good, since I don't write a lot of powershell, and I don't know the project very well, but hopefully it's good! * Do as @DHowett-MSFT says and use VSSetup whee! * try to do what @heaths is recommending * fix `Import-LocalModule` * fix openconsole.psm1 for hopefully the last time --- .gitignore | 3 ++ tools/OpenConsole.psm1 | 78 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 2dafa9162..2fcb1c1d9 100644 --- a/.gitignore +++ b/.gitignore @@ -263,6 +263,9 @@ build*.metadata # .razzlerc.cmd file - used by dev environment tools/.razzlerc.* +# .PowershellModules - if one needs a powershell module dependency, one +# can save it here. used by tools/OpenConsole.psm1 +.PowershellModules # message compiler output MSG*.bin /*.exe diff --git a/tools/OpenConsole.psm1 b/tools/OpenConsole.psm1 index b9b28afe6..8c7918302 100644 --- a/tools/OpenConsole.psm1 +++ b/tools/OpenConsole.psm1 @@ -2,21 +2,87 @@ # The project's root directory. Set-Item -force -path "env:OpenConsoleRoot" -value "$PSScriptRoot\.." +#.SYNOPSIS +# Finds and imports a module that should be local to the project +#.PARAMETER ModuleName +# The name of the module to import +function Import-LocalModule +{ + [CmdletBinding()] + param( + [parameter(Mandatory=$true, Position=0)] + [string]$Name + ) + + $ErrorActionPreference = 'Stop' + + $modules_root = "$env:OpenConsoleRoot\.PowershellModules" + + $local = $null -eq (Get-Module -Name $Name) + + if (-not $local) + { + return + } + + if (-not (Test-Path $modules_root)) { + New-Item $modules_root -ItemType 'directory' | Out-Null + } + + if (-not (Test-Path "$modules_root\$Name")) { + Write-Verbose "$Name not downloaded -- downloading now" + $module = Find-Module "$Name" + $version = $module.Version + + Write-Verbose "Saving $Name to $modules_root" + Save-Module -InputObject $module -Path $modules_root + Import-Module "$modules_root\$Name\$version\$Name.psd1" + } else { + Write-Verbose "$Name already downloaded" + $versions = Get-ChildItem "$modules_root\$Name" | Sort-Object + + Get-ChildItem -Path $versions[0] "$Name.psd1" | Import-Module + } +} + #.SYNOPSIS # Grabs all environment variable set after vcvarsall.bat is called and pulls # them into the Powershell environment. -function Set-MsbuildDevEnvironment() +function Set-MsbuildDevEnvironment { - $path = "$env:VS140COMNTOOLS\..\.." - pushd $path - cmd /c "vcvarsall.bat&set" | foreach { - if ($_ -match "=") + [CmdletBinding()] + param() + + $ErrorActionPreference = 'Stop' + + Import-LocalModule -Name 'VSSetup' + + Write-Verbose 'Searching for VC++ instances' + $vsinfo = ` + Get-VSSetupInstance -All ` + | Select-VSSetupInstance ` + -Latest -Product * ` + -Require 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' + + $vspath = $vsinfo.InstallationPath + + switch ($env:PROCESSOR_ARCHITECTURE) { + "amd64" { $arch = "x64" } + "x86" { $arch = "x86" } + default { throw "Unknown architecture: $switch" } + } + + $vcvarsall = "$vspath\VC\Auxiliary\Build\vcvarsall.bat" + + Write-Verbose 'Setting up environment variables' + cmd /c ("`"$vcvarsall`" $arch & set") | ForEach-Object { + if ($_ -match '=') { $s = $_.Split("="); Set-Item -force -path "env:\$($s[0])" -value "$($s[1])" } } - popd + Write-Host "Dev environment variables set" -ForegroundColor Green }