MSBuild找到一个工具路径

时间:2017-06-16 16:15:26

标签: msbuild

我正在尝试设置一个MSBuild目标,以便在构建过程中运行npm install

<Target Name="EnsureNpmBuildImports" BeforeTargets="PrepareForBuild">
   <PropertyGroup>
       <NpmToolExe Condition="$(NpmToolExe) == '')">npm</NpmToolExe>
   </PropertyGroup>

   <Exec Command="$(NpmToolExe) install" />
</Target>

如果用户自己安装了Node.js,我想使用该版本。假设它已将其位置安装到Windows上的%PATH%环境变量中,则上述目标将起作用。

我遇到问题的部分是尝试使用与Visual Studio捆绑在一起的npm工具的回退(对于我的团队中没有进行JS开发但仍然将项目作为他们的一部分的人解)。此版本可以在$(VsInstallDir)Web/External下创建。

虽然我可以构建ItemGroup文件的npm.cmd个可能位置,但我不知道如何将其作为有序列表并使用存在的第一个版本。

有关如何让MSBuild搜索几个位置以查找工具的任何建议?

1 个答案:

答案 0 :(得分:2)

这是我创建的目标,用于查找不同的可执行文件;它应该很容易适应您的要求。

  <Target Name="FindBestSqlServerToolsDir">

    <!-- This target populates the property SqlServerToolsDir, which should be used when executing SQLCMD.EXE and BCP.EXE. -->

    <ItemGroup>
      <Temp_SqlServerVersions Include="130" />
      <Temp_SqlServerVersions Include="120" />
      <Temp_SqlServerVersions Include="110" />
      <Temp_SqlServerVersions Include="100" />

      <!-- Create an item for each possible path, ordered from most-preferred to least. -->
      <Temp_SqlServerToolsDirs Include="C:\Program Files\Microsoft SQL Server\%(Temp_SqlServerVersions.Identity)\Tools\Binn\" />
    </ItemGroup>

    <Message Text="About to check the following directories in the order listed for the files BCP.EXE and SQLCMD.EXE. The first one where both are found will be used as the value for $ (SqlServerToolsDir)." />
    <Message Text=" - %(Temp_SqlServerToolsDirs.Identity)" />

     <!-- Create a copy of the list with its order reversed. -->
    <ItemGroup>
      <Temp_SqlServerToolsDirs_Reversed Include="@(Temp_SqlServerToolsDirs->Reverse())" />
    </ItemGroup>

    <PropertyGroup>
      <!-- Test all paths, from the least-preferred to the most. Whenever a path passes -->
      <!-- the condition, set/overwrite the value of this property. The final value -->
      <!-- of this property will thus be the most-preferred path that passes the condition. -->
      <SqlServerToolsDir
        Condition="Exists('%(Temp_SqlServerToolsDirs_Reversed.Identity)BCP.EXE')
              And Exists('%(Temp_SqlServerToolsDirs_Reversed.Identity)SQLCMD.EXE')">%(Temp_SqlServerToolsDirs_Reversed.Identity)</SqlServerToolsDir>
    </PropertyGroup>

    <Error Condition=" '$(SqlServerToolsDir)' == '' " Text="None of the following directories contained both BCP.EXE and SQLCMD.EXE: @(Temp_SqlServerToolsDirs)" />

    <Message Text="$ (SqlServerToolsDir): $(SqlServerToolsDir)" />
  </Target>