读取文本文件并在 MSBuild 中分割每一行

2023-12-10

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

常见\Common.sln,am@email,com
ExcGw/ExcDataService.sln,p[电子邮件受保护];am@email,com;[电子邮件受保护];[电子邮件受保护];[电子邮件受保护]
MessB/MessB/Message.sln,am@email,com 风险S/风险S2.sln,[电子邮件受保护];[电子邮件受保护];[电子邮件受保护];[电子邮件受保护];[电子邮件受保护]

我需要逐行读取此文件,编译每个解决方案,如果失败 – 向相关开发人员发送电子邮件

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

因此,我创建了一个属性组和一个目标解决方案,如下所示。

我逐行读取文件,但我不知道如何设置每个行项目的元数据:
当前行的第一部分(%(LinesFromFile.Identity))
SecondPartOfTheCurrentLine(%(LinesFromFile.Identity))

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

也许有人知道如何正确设置元数据,或者可能有另一种方法来完成此任务。 谢谢

这是代码:

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

这是我正在使用的数据:

Common\Common.sln,am@email,com 
ExcGw/ExcDataService.sln,[email protected];am@email,com;[email protected];[email protected];[email protected]
MessB/MessB/Message.sln,am@email,com 
RiskS/RiskS2.sln,j[email protected];[email protected];[email protected];[email protected];[email protected]

稍微修改您的示例以包含第四个解决方案的换行符。

这是修改后的代码:

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

我们将值复制到属性,以便我们可以使用属性函数来拆分值并获取我们需要的部分。

这是输出:

Solutions to Emails-> Common\Common.sln -> am@email
Solutions to Emails-> ExcGw/ExcDataService.sln -> [email protected];am@email
Solutions to Emails-> MessB/MessB/Message.sln -> am@email
Solutions to Emails-> RiskS/RiskS2.sln -> j[email protected];[email protected];[email protected];[email protected];[email protected]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

读取文本文件并在 MSBuild 中分割每一行 的相关文章

随机推荐