我解析了許多我們從Visual Studio項目文件中獲得的項目,並且需要在這些文件的許多文件上運行命令行實用程序。我需要使用的參數是基於輔助文件的集合,通常存儲爲分號分隔的列表,項目文件中存儲爲項元數據:如何在我通過分割字符串創建的MSBuild 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
謝謝!
輝煌 - 我曾經想過,因爲它使用的是一個字符串數組,從字符串本身分離出來,所以這不起作用,甚至沒有嘗試過。謝謝。 – abitgone