Skip to content

Expand Item Metadata in Item Functions #6196

Open
@Rabadash8820

Description

@Rabadash8820

Based on my testing, it is possible to use MSBuild properties in Item Functions, as in this example project:

<SomeItem Include="path/to/file.VERSION.ext" />
<SomeItemWithProp Include="@(SomeItem->Replace('VERSION', $(Version)))" />

However, it is not possible to use item metadata in Item Functions, as in this example project:

<SomeItem Include="path/to/file.VERSION.ext" Version="1.2.3" />
<SomeItemWithMetadata Include="@(SomeItem->Replace('VERSION', %(Version)))" />
<SomeItemWithMetadata Include="@(SomeItem->Replace('VERSION', Metadata('Version')))" />

The SomeItemWithMetadata items will be path/to/file.%(VERSION).ext and path/to/file.Metadata('Version').ext, resp.

I think this feature would be generally useful, but let me briefly explain my use case: I am writing managed plugins for the Unity game engine. The C# projects for these plugins need to reference Unity assemblies stored in "packages", which Unity stores in a folder called PackageCache. There, every package is in a folder called com.unity.packageName@<version> where <version> is a SemVer string. I want to define MSBuild properties for some of the common assembly/package paths, for reuse between C# projects. So, ideally, I would like my projects to look as follows (with Project tags ommitted...):

<!-- Shared .props file, imported at top of .csproj files -->
<PropertyGroup>
    <UnityPackageCachePath>./relative/path/to/Unity/project/Library/PackageCache</UnityPackageCachePath>
    <CommonUnityAssemblyAPath>com.unity.packageA@VERSION/sufolders/SomeAssemblyA.dll</CommonUnityAssemblyAPath>
    <CommonUnityAssemblyBPath>com.unity.packageB@VERSION/sufolders/SomeAssemblyB.dll</CommonUnityAssemblyBPath>
</PropertyGroup>

<!-- Example .csproj file -->
<ItemGroup>
    <UnityReference Include="$(CommonUnityAssemblyAPath)" Version="1.2.3" />
    <UnityReference Include="$(CommonUnityAssemblyBPath)" Version="4.5.6" />
</ItemGroup>

<!-- Shared .targets file, imported at bottom of .csproj files -->
<ItemGroup>
    <Reference Include="@(UnityReference->Replace('VERSION', '%(Version)'))" Private="false" />
</ItemGroup>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: LanguageIssues impacting the MSBuild programming language.needs-designRequires discussion with the dev team before attempting a fix.triaged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions