Read text file and split every line in MSBuild

2019-07-20 16:18发布

问题:

I am stuck at the following issue I have in MSBuild. I have a text file (buildsolutions1.txt) containing the list (line by line) with all the solutions I need to build and the related developers emails separated by comma :

Common\Common.sln,am@email,com
ExcGw/ExcDataService.sln,pm@email.com;am@email,com;jk@email.com;mk@email.com;Ppp@email.com
MessB/MessB/Message.sln,am@email,com RiskS/RiskS2.sln,jp@email.com;mz@email.com;mk@email.com;jk@email.com;ps@email.com

I need to read this file line by line,compile each solution and in case it fails –send email to related developer(s)

My idea is to create an item group Lines where every item is a line from this file and it has 2 metadata values: Solution –first part of the line until comma Emails –second part of the line from comma to the end of line

So I created a Property Grroup and a target ReadSolutions like below.

I read the file line by line but I do not know how to set the metadata for each line item:
FirstPartOfTheCurrentLine(%(LinesFromFile.Identity))
SecondPartOfTheCurrentLine(%(LinesFromFile.Identity))

This syntax does not work:
%(LinesFromFile.Identity.Split(',')[0])
%(LinesFromFile.Identity.Split(',')[1])

Maybe someone would know how to set the metadata correctly or maybe has another approach to this task. Thanks

Here is the code:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         ToolsVersion="4.0"  DefaultTargets="CoreBuild">

  <PropertyGroup>
    <TPath>$(MSBuildProjectDirectory)\..\tools\MSBuild Extension Pack Binaries\MSBuild.ExtensionPack.tasks</TPath>
  </PropertyGroup>
  <Import Project="$(TPath)"/>

<PropertyGroup>
   <!-- Default working folder -->
  <RootFolder Condition=" '$(RootFolder)' == '' " >c:\ff\</RootFolder>
  <BuildSolutionsFile >buildsolutions1.txt</BuildSolutionsFile>
   <BuildSolutionsFileFullPath >$(RootFolder)$(BuildSolutionsFile)</BuildSolutionsFileFullPath>
 </PropertyGroup>


<Target Name="ReadSolutions">
  <Message Text=" Build solutions text file is : $(BuildSolutionsFileFullPath)" />

    <!—Read the file and store each line as an item into  LinesFromFile  item group-->
  <ReadLinesFromFile
    File="$(BuildSolutionsFileFullPath)" >
    <Output
        TaskParameter="Lines"
        ItemName="LinesFromFile"/>
  </ReadLinesFromFile>

   <Message Text="Current line : %(LinesFromFile.Identity)" />
  <Message Text="===================================" />

    <!—Create  the other item group where each item is a line and has the metadata Solution and Emails -->

  <ItemGroup>
    <Lines Include="%(LinesFromFile.Identity)" >
      <Solution>FirstPartOfTheCurrentLine(%(LinesFromFile.Identity))</Solution>
      <Emails>SecondPartOfTheCurrentLine(%(LinesFromFile.Identity)) </Emails>

    </Lines>

  </ItemGroup>


  <Message Text="All the Lines :%0A@(Lines,'%0A')" />



</Target>

回答1:

Here is the data I was working with:

Common\Common.sln,am@email,com 
ExcGw/ExcDataService.sln,pm@email.com;am@email,com;jk@email.com;mk@email.com;Ppp@email.com
MessB/MessB/Message.sln,am@email,com 
RiskS/RiskS2.sln,jp@email.com;mz@email.com;mk@email.com;jk@email.com;ps@email.com

Slightly modified your sample to include a line break for the fourth solution.

Here is the modified code:

<ItemGroup>
    <Lines Include="@(LinesFromFile)" >
        <Solution>$([System.String]::Copy('%(LinesFromFile.Identity)').Split(',')[0])</Solution>
        <Emails>$([System.String]::Copy('%(LinesFromFile.Identity)').Split(',')[1])</Emails>
    </Lines>
</ItemGroup>
<Message Text="Solutions to Emails-> %(Lines.Solution) -> %(Lines.Emails)" />

We're copying the value to a property so we can use a property function to split the value and get the part we need.

Here is the output:

Solutions to Emails-> Common\Common.sln -> am@email
Solutions to Emails-> ExcGw/ExcDataService.sln -> pm@email.com;am@email
Solutions to Emails-> MessB/MessB/Message.sln -> am@email
Solutions to Emails-> RiskS/RiskS2.sln -> jp@email.com;mz@email.com;mk@email.com;jk@email.com;ps@email.com