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
This commit is contained in:
nicole mazzuca 2019-06-07 13:20:26 -07:00 committed by msftbot[bot]
parent b9d8bf55c4
commit c73761db96
2 changed files with 75 additions and 6 deletions

3
.gitignore vendored
View file

@ -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

View file

@ -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
}