MSBuild: Stars and Strange Actions ItemGroup Exclude

I have a script that tries to build ItemGroupfrom all files in a specific directory, excluding files with specific names (regardless of extension).

The list of files that will be excluded initially contains file extensions, and I use Community Tasks RegexReplaceto replace the extensions with an asterisk. Then I use this list in the attribute Exclude. For some reason, files are not properly excluded, although the list seems to be correct.

To try to find the reason, I created a test script (below) that has two tasks: first it initializes two properties with a list of file templates in two different ways . The second task prints both the properties and the files resulting from using both of these properties in the attribute Exclude.

Property values ​​look the same, but the resulting groups are different. How is this possible?

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         DefaultTargets="Init;Test" ToolsVersion="3.5">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

  <Target Name="Init">
    <ItemGroup>
      <OriginalFilenames Include="TestDir\SampleProj.exe"/>
      <OriginalFilenames Include="TestDir\SampleLib1.dll"/>
    </ItemGroup>
    <RegexReplace Input="@(OriginalFilenames)" Expression="\.\w+$" Replacement=".*">
      <Output TaskParameter="Output" ItemName="PatternedFilenames"/>
    </RegexReplace>
    <PropertyGroup>
      <ExcludeFilesA>TestDir\SampleProj.*;TestDir\SampleLib1.*</ExcludeFilesA>
      <ExcludeFilesB>@(PatternedFilenames)</ExcludeFilesB>
    </PropertyGroup>
  </Target>

  <Target Name="Test">
    <Message Text='ExcludeFilesA: $(ExcludeFilesA)' />
    <Message Text='ExcludeFilesB: $(ExcludeFilesB)' />
    <ItemGroup>
      <AllFiles Include="TestDir\**"/>
      <RemainingFilesA Include="TestDir\**" Exclude="$(ExcludeFilesA)"/>
      <RemainingFilesB Include="TestDir\**" Exclude="$(ExcludeFilesB)"/>
    </ItemGroup>
    <Message Text="&#xA;**AllFiles**&#xA;@(AllFiles, '&#xA;')" />
    <Message Text="&#xA;**PatternedFilenames**&#xA;@(PatternedFilenames, '&#xA;')" />
    <Message Text="&#xA;**RemainingFilesA**&#xA;@(RemainingFilesA, '&#xA;')" />
    <Message Text="&#xA;**RemainingFilesB**&#xA;@(RemainingFilesB, '&#xA;')" />
  </Target>

</Project>

Output (reformatted for clarity):

ExcludeFilesA: TestDir\SampleProj.*;TestDir\SampleLib1.*
ExcludeFilesB: TestDir\SampleProj.*;TestDir\SampleLib1.*

AllFiles:
  TestDir\SampleLib1.dll
  TestDir\SampleLib1.pdb
  TestDir\SampleLib2.dll
  TestDir\SampleLib2.pdb
  TestDir\SampleProj.exe
  TestDir\SampleProj.pdb

PatternedFilenames:
  TestDir\SampleProj.*
  TestDir\SampleLib1.*

RemainingFilesA:
  TestDir\SampleLib2.dll
  TestDir\SampleLib2.pdb

RemainingFilesB:
  TestDir\SampleLib1.dll
  TestDir\SampleLib1.pdb
  TestDir\SampleLib2.dll
  TestDir\SampleLib2.pdb
  TestDir\SampleProj.exe
  TestDir\SampleProj.pdb

Note that both ExcludeFilesAand ExcludeFilesBlook the same, but the resulting group RemainingFilesAand RemainingFilesBdifferent.

Ultimately, I want to get a list RemainingFilesAusing a template generated in the same way ExcludeFilesB. Can you suggest a way, or do I need to completely rethink my approach?

+3
2

, .

ExcludeFilesA TestDir\SampleProj.*;TestDir\SampleLib1.*, . ExcludeFilesB TestDir\SampleProj.%2a;TestDir\SampleLib1.%2a.

Message , Include Exclude . , , -.

, , , , ( ), , script.

+2

, PatternedFilenames ItemGroup " " . CreateItem , PatternedFilenames :

<RegexReplace Input="@(OriginalFilenames)" Expression="\.\w+$" Replacement=".*">
  <Output TaskParameter="Output" ItemName="PatternedFilenames_tmp"/>
</RegexReplace>
<CreateItem Include="@(PatternedFilenames_tmp)">
  <Output TaskParameter="Include" ItemName="PatternedFilenames"/>
</CreateItem>
0

Source: https://habr.com/ru/post/1726106/


All Articles