2017-06-16 136 views
1

我试图设置一个MSBuild目标来运行npm install作为构建过程的一部分。MSBuild查找工具路径

<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在几个位置搜索以找到该工具的任何建议?

回答

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

你能解释一下(也许增加一些意见),这段代码是如何实现你所说的? –

+0

@PaulTurner - 完成。 :) – weir

+0

反序排序评估是我失踪的把戏;好东西。 –