我维护了一个非常简单的 PowerShell 脚本 ( gist ) 来使用正确配置的 Visual Studio 环境变量运行 CI/CLI 构建。它使用vswhere.exe
和VsDevCmd.bat
。作为 Azure DevOps 管道的一部分,它在构建代理上运行良好。INCLUDE
特别是对于 C++ 项目,LIB
正确设置是至关重要的,这就是VsDevCmd.bat
它的作用。
MSBuild
例如,为当前文件夹中的解决方案运行构建:
powershell -f _invoke-build.ps1 -buildCommand "msbuild ."
将 VS 的INCLUDE
环境变量复制到剪贴板(为了好玩):
powershell -f _invoke-build.ps1 -buildCommand "set INCLUDE | clip"
当前版本的脚本:
<#
.Synopsis
Find Visual Studio and run its VsDevCmd.bat, then run a build command.
.Example
powershell -f _invoke-build.ps1 -buildCommand "msbuild ."
.Link
https://gist.github.com/noseratio/843bb4d9c410c42081fac9d8b7a33b5e
#>
#Requires -Version 5.0
# by @noseratio
param([Parameter(Mandatory = $true)] [string] $buildCommand)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
echo "Building via '$buildCommand' ..."
$vs_not_found = "Visual Studio hasn't been detected!"
$vswhere = "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe"
if (!(Test-Path $vswhere)) { throw $vs_not_found }
$vs_ide_path = $(& $vswhere -format value -property productPath)
if (!(Test-Path $vs_ide_path)) { throw $vs_not_found }
$vs_ide_folder = "$(Split-Path $vs_ide_path)"
$vs_dev_cmd = "$vs_ide_folder/../Tools/VsDevCmd.bat"
$invoke = "call ""$vs_dev_cmd"" && cd ""$pwd"" && $buildCommand"
# we need to run via CMD for proper propagation of Visual Studio environment vars
& $env:comspec /s /c ""$invoke""
if (!$?) { throw "Failed with error code: $LastExitCode" }