h.s.
h.s.

Reputation: 243

msbuild: How to extract items without metadata?

From a msbuild "Item", I want to to extract all those members that have no metadata (please forgive me if I am using wrong terminology).

This is my background: I am preparing files for creation of an installation package. The files that should go into the package are defined in an "Item" like this:

<ItemGroup>
  <Files Include="readme.txt" />
  <Files Include="myfile.txt">
    <Rename>help.txt</Rename>
  </Files>
  <Files Include="changes.log" />
  <!-- more files -->
</ItemGroup>

While readme.txt should be included as-is, myfile.txt should be renamed into help.txt because someone decided that myfile.txt is a poor name. I created a sequence (inside a Target) that renames files that have the "Rename" metadata.

<ItemGroup>
  <FilesToRename Include="@(ChangeLogs->HasMetadata('Rename'))"/>
</ItemGroup>
<Move SourceFiles="@(FilesToRename->'$(WorkDir)\source\%(Identity)')" DestinationFiles="@(FilesToRename->'$(WorkDir)\source\%(Rename)')" />

For copying those files into the installation package, I need an item that consists of the "Rename" names for renamed files, and original names for the others; in this example, "readme.txt;help.txt;changes.log" (in any order). My first attempt was to use "FilesToRename" from above, and add the "inverted" expression like this:

<FilesToCopy Include="@(FilesToRename->'%(Rename)')" />
<FilesToCopy Include="@(Files->Not HasMetadata('Rename'))" />

but that is not expanded. Neither could I make a "Condition" work, like

<FilesToCopy Include="@(Files)" Condition="Not @(Files->HasMetadata('Rename'))/>

which only results in a syntax error "An unexpected token was found".

Any ideas would be appreciated.

Thanks Hans

Upvotes: 1

Views: 282

Answers (1)

h.s.
h.s.

Reputation: 243

I could have thought of this solution before...

<FilesToCopy Include="@(FilesToRename->'%(Rename)')" />
<FilesToCopy Include="@Files" Exclude="@(Files->HasMetadata('Rename'))" />

So simple...

Upvotes: 2

Related Questions