读取文本文件并拆分MSBuild中的每一行 [英] Read text file and split every line in MSBuild

查看:86
本文介绍了读取文本文件并拆分MSBuild中的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MSBuild中遇到以下问题. 我有一个文本文件(buildsolutions1.txt),其中包含列表(逐行),其中包含我需要构建的所有解决方案以及以逗号分隔的相关开发人员电子邮件:

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

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)

我的想法是创建一个项目组Lines,其中每个项目都是该文件中的一行,它具有2个元数据值: 解决方案–该行的第一部分,直到逗号为止 电子邮件-从逗号到行尾的行第二部分

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

因此,我创建了一个Property Grroup和一个目标ReadSolutions,如下所示.

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

我逐行读取了文件,但我不知道如何为每个订单项设置元数据:
FirstPartOfTheCurrentLine(%(LinesFromFile.Identity))
SecondPartOfTheCurrentLine(%(LinesFromFile.Identity))

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))

此语法不起作用:
%(LinesFromFile.Identity.Split(',')[0])
%(LinesFromFile.Identity.Split(',')[1])

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

这是代码:

    <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>

推荐答案

以下是我正在使用的数据:

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.

这是修改后的代码:

<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.

以下是输出:

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

这篇关于读取文本文件并拆分MSBuild中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆