【问题标题】:MSBuild finding a tool pathMSBuild 查找工具路径
【发布时间】:2017-06-17 06:20:33
【问题描述】:

我正在尝试设置一个 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下创建。

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

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

【问题讨论】:

    标签: msbuild


    【解决方案1】:

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

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

    【讨论】:

    • 你能解释一下(也许添加一些cmets),这段代码是如何实现你所说的?
    • @PaulTurner - 完成。 :)
    • 逆序求值是我缺少的技巧;很棒的东西。
    • 实现这一目标的非常优雅的解决方案!
    • @EmielKoning - 谢谢! (如果觉得有用,可以点赞。)
    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多