通过任务选择在Target中生成的ItemGroup文件的位置

Ale*_*cke 7 msbuild f# msbuild-task

我有以下设置(为了简洁,删除了无趣的XML):

MyProject.fsproj

<Project ...>
  <Import Project="MyTask.props" />
  ...
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

MyTask.props

<Project ...>
  <UsingTask XXX.UpdateAssemblyInfo />
  <Target Name="UpdateAssemblyInfo"
          BeforeTargets="CoreCompile">
    <UpdateAssemblyInfo ...>
      <Output
        TaskParameter="AssemblyInfoTempFilePath"
        PropertyName="AssemblyInfoTempFilePath" />
    </UpdateAssemblyInfo>

    <ItemGroup>
      <Compile Include="$(AssemblyInfoTempFilePath)" />
    </ItemGroup>
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

问题在于MyTask.props添加的ItemGroup是最后添加的,尽管它是在项目的最开始时导入的.我假设这是因为ItemGroup实际上并未导入 - 它是在运行任务时添加的.

这在F#中不是一件好事,因为文件顺序很重要 - 包括构建列表末尾的文件意味着构建EXE是不可能的(例如,入口点必须在最后一个文件中).

因此我的问题是 - 有没有办法让我输出一个ItemGroup作为Target的一部分,并将生成的ItemGroup作为第一个?

Rol*_*olo 3

有点晚了,但这可能会对将来的人有所帮助,我没有在这个示例上使用 import 标签,但它会以相同的方式工作,重要的部分是“UpdateAssemblyInfo”目标,主要思想是清除和使用适当的排序顺序重新生成 Compile ItemGroup。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>

  <Target Name="Build" DependsOnTargets="UpdateAssemblyInfo">

  </Target>

  <Target Name="UpdateAssemblyInfo">
    <!-- Generate your property -->
    <PropertyGroup>
      <AssemblyInfoTempFilePath>ABC.xyz</AssemblyInfoTempFilePath>
    </PropertyGroup>

    <!-- Copy current Compile ItemGroup to TempCompile -->
    <ItemGroup>
      <TempCompile Include="@(Compile)"></TempCompile>
    </ItemGroup>

    <!-- Clear the Compile ItemGroup-->
    <ItemGroup>
      <Compile Remove="@(Compile)"/>
    </ItemGroup>

    <!-- Create the new Compile ItemGroup using the required order -->    
    <ItemGroup>
      <Compile Include="$(AssemblyInfoTempFilePath)"/>
      <Compile Include="@(TempCompile)"/>
    </ItemGroup>

    <!-- Display the Compile ItemGroup ordered -->
    <Message Text="Compile %(Compile.Identity)"/>
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)