Git 如何获取从";返回的msbuild路径;nuget.exe还原";

Git 如何获取从";返回的msbuild路径;nuget.exe还原";,git,msbuild,nuget,githooks,Git,Msbuild,Nuget,Githooks,我不熟悉使用git钩子,现在我正在尝试为我的解决方案中的项目恢复nuget包,然后构建解决方案。 我所做的是创建一个git钩子“post merge”,并添加了以下代码: #!/bin/sh RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' projectRoot="$(git rev-parse --show-toplevel)" solutionFile="solution.sln" msBuild="C:\Program Files (x

我不熟悉使用git钩子,现在我正在尝试为我的解决方案中的项目恢复nuget包,然后构建解决方案。 我所做的是创建一个git钩子“post merge”,并添加了以下代码:

#!/bin/sh
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

projectRoot="$(git rev-parse --show-toplevel)"
solutionFile="solution.sln"

msBuild="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"

echo -e "\n${RED}Restoring nuget packages for ${solutionFile}...${NC}"
./nuget.exe restore $projectRoot/development/$solutionFile
echo -e "${GREEN}Done restoring nuget packages for ${solutionFile}.${NC}\n"

echo -e "\n${RED}Building ${solutionFile}...${NC}"
"$msBuild"  $projectRoot/development/$solutionFile
echo -e "${GREEN}Done building ${solutionFile}.${NC}\n"

exit 1
我添加了exit 1,这样我就可以在不实际拉动项目的情况下测试我的命令

现在,在解决方案路径上运行nuget.exe后,它将返回msbuild自动检测路径,我想使用此路径查找msbuild.exe,以便根据解决方案使用的版本在该解决方案上运行msbuild

我该怎么做? 如果有任何方法可以改进或改进此代码,请让我知道


谢谢大家!

您可以不捕获和解析NuGet的输出

从wiki页面:

安装Visual Studio 2017 Update 2或更新版本后,您可以在%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe中找到vswhere,或者确保它在您的回购中始终可用,请参阅以获取使用NuGet的选项

powershell示例,如果要继续使用Bash,您需要适应Bash

$path = vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
if ($path) {
  $path = join-path $path 'MSBuild\15.0\Bin\MSBuild.exe'
  if (test-path $path) {
    & $path $args
  }
}
编辑:如果已安装.NET Core SDK,则可以使用
dotnet msbuild
运行msbuild,因为安装.NET Core SDK会将
dotnet.exe
放在路径上。使用Visual Studio MSBuild和.NET核心MSBuild之间唯一的真正区别在于扩展路径不同。但是,如果生成不依赖于MSBuild扩展,则使用
dotnet MSBuild
可以工作,而无需首先找到可执行文件的路径