我正在解析我们从 Visual Studio 项目文件中获取的一些项目,并且需要在其中一些文件上运行命令行实用程序。我需要使用的参数基于辅助文件的集合,这些辅助文件通常存储为分号分隔的列表,存储为项目文件中的项目元数据:
<ItemGroup>
  <Content Include="js\main.js">
    <Concatenate>True</Concatenate>
    <MoreFiles>path\to\file-1.js;path\to\file-2.js;path\to-another\file-3.js;path\to-yet-another\file-4.js</MoreFiles>
  </Content>
  <Content Include="js\another.js">
    <Concatenate>True</Concatenate>
    <MoreFiles>path\to\file-5.js;path\to\file-6.js;path\to\file-7.js;path\to-another\file-8.js</MoreFiles>
  </Content>
</ItemGroup>
我目前正在使用JSFiles我从 构建的属性 检索这些@(Content),因此:
<ItemGroup>
  <JSFiles Include="@(Content)" KeepMetadata="Concatenate;MoreFiles" Condition="'%(Content.Extension)' = '.js' AND '%(Content.Concatenate)' == 'true'" />
</ItemGroup>
然后我使用一个次要目标,它@(JSFiles)用作它的输入:
<Target Name="ConcatenateJS" Inputs="@(JSFiles)" Outputs="%(JSFiles.Identity).concatenatetemp">
  <Message Importance="high" Text="  %(JSFiles.Identity):" />
  <PropertyGroup>
    <MoreFiles>%(JSFiles.MoreFiles)</MoreFiles>
  </PropertyGroup>
  <ItemGroup>
    <MoreFilesArray Include="$(MoreFiles.Split(';'))" />
  </ItemGroup>
  <Message Importance="high" Text="    MoreFiles: %(MoreFilesArray.Identity)" />
</Target>
到目前为止,一切都很好。至此,我可以使用一个<Message />任务来输出Split操作的内容,这给了我我所期望的:
ConcatenateJS:
  js\main.js:
    MoreFiles: path\to\file-1.js
    MoreFiles: path\to\file-2.js
    MoreFiles: path\to-another\file-3.js
    MoreFiles: path\to-yet-another\file-4.js
ConcatenateJS:
  js\another.js:
    MoreFiles: path\to\file-5.js
    MoreFiles: path\to\file-6.js
    MoreFiles: path\to\file-7.js
    MoreFiles: path\to-another\file-8.js
但是,为了正确地将这些文件传递给命令行实用程序,它们需要是完整路径,因此我需要在所有项目前面$(MoreFiles)加上$(MSBuildProjectDirectory).
我尝试过使用批处理操作(使用$(MSBuildProjectDirectory)\%(MoreFilesArray.Identity)甚至$([System.IO.Path]::Combine($(MSBuildProjectDirectory), %(MoreFilesArray.Identity))无济于事),并且我尝试过<CreateItem>使用该AdditionalMetadata属性,但这对我来说似乎不太好用(尽管我不确定我是否正在使用它正确)。
我怎样才能做到这一点,以便我的构建过程的输出如下所示:
ConcatenateJS:
  js\main.js:
    MoreFiles: C:\full\path\to\file-1.js
    MoreFiles: C:\full\path\to\file-2.js
    MoreFiles: C:\full\path\to-another\file-3.js
    MoreFiles: C:\full\path\to-yet-another\file-4.js
ConcatenateJS:
  js\another.js:
    MoreFiles: C:\full\path\to\file-5.js
    MoreFiles: C:\full\path\to\file-6.js
    MoreFiles: C:\full\path\to\file-7.js
    MoreFiles: C:\full\path\to-another\file-8.js
谢谢!