简体   繁体   English

如何获取从“ nuget.exe restore”返回的msbuild路径

[英]How to get the msbuild path returned from “nuget.exe restore”

I'm new to using git hooks and now I'm trying to restore nuget packages for the projects in my solution and after that build the solution. 我是使用git hook的新手,现在我正在尝试为解决方案中的项目还原nuget程序包,然后再构建解决方案。 What I did is create a git hook 'post-merge' and added the following code: 我所做的是创建一个git hook'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

I added exit 1 so that I can test my commands without actually pulling the project. 我添加了出口1,这样我就可以测试我的命令而无需实际拉项目。

Now after I run nuget.exe on the solution path, it returns a msbuild auto-detection path, I want to use this path to locate msbuild.exe so I can run msbuild on the solution based on the version it's using. 现在,在解决方案路径上运行nuget.exe之后,它会返回一个msbuild自动检测路径,我想使用此路径来定位msbuild.exe,以便我可以根据其使用的版本在解决方案上运行msbuild。

How do I do this? 我该怎么做呢? And if there's any way to improve or make this code better please let me know! 如果有任何方法可以改善或改进此代码,请告诉我!

Thank you! 谢谢!

Rather than capturing and parsing NuGet's output, you can use VSWhere to find MSBuild . 您可以使用VSWhere查找MSBuild ,而不是捕获和解析NuGet的输出。

From the wiki page: 从Wiki页面:

With Visual Studio 2017 Update 2 or newer installed, you can find vswhere at %ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe, or to make sure it's always available in your repo see Installing for an option using NuGet. 安装Visual Studio 2017 Update 2或更高版本后,您可以在%ProgramFiles(x86)%\\ Microsoft Visual Studio \\ Installer \\ vswhere.exe中找到vswhere,或者要确保它在您的存储库中始终可用,请参阅使用NuGet 安装选项。

powershell example that you'll need to adapt to Bash if you want to keep using bash 如果要继续使用bash,需要适应bash的powershell示例

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

EDIT: If you have the .NET Core SDK installed, you can just use dotnet msbuild to run msbuild, since installing the .NET Core SDK puts dotnet.exe on the path. 编辑:如果您安装了.NET Core SDK,则可以仅使用dotnet msbuild来运行msbuild,因为安装.NET Core SDK会将dotnet.exe放在路径上。 The only real difference between using a Visual Studio MSBuild and the .NET Core MSBuild is that the extensions path will be different. 使用Visual Studio MSBuild和.NET Core MSBuild之间的唯一真正区别是扩展路径将有所不同。 But if your build doesn't depend on an MSBuild extension, then using dotnet msbuild works without having to find the path to an executable first. 但是,如果您的构建不依赖于MSBuild扩展,则使用dotnet msbuild工作,而不dotnet msbuild查找可执行文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM