2014-02-05 41 views
0

我解析了许多我们从Visual Studio项目文件中获得的项目,并且需要在这些文件的许多文件上运行命令行实用程序。我需要使用的参数是基于辅助文件的集合,通常存储为分号分隔的列表,项目文件中存储为项元数据:如何在我通过分割字符串创建的M​​SBuild ItemGroup中添加项目?

<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 

谢谢!

回答

1

MsBuild项目有一个名为'FullPath'的well-known metadata,它将显示项目的完整路径。

<Message Importance="high" Text=" MoreFiles: %(MoreFilesArray.FullPath)" /> 
+0

辉煌 - 我曾经想过,因为它使用的是一个字符串数组,从字符串本身分离出来,所以这不起作用,甚至没有尝试过。谢谢。 – abitgone