Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to copy metadata attributes from PackageReference to corresponding ReferencePath? #5829

Closed
Denis535 opened this issue Oct 22, 2020 · 2 comments
Labels
needs-triage Have yet to determine what bucket this goes in.

Comments

@Denis535
Copy link

Denis535 commented Oct 22, 2020

There are PackageReference items with some metadata attributes. And there are corresponding ReferencePath items.
I need to move those metadata attributes from source PackageReference items to corresponding ReferencePath items.

MSBuild do it for ProjectReference but doesn't for PackageReference.
I'm not sure is it bug or by design. So, I have described it here https://github.com/dotnet/msbuild/issues/5809#issue-724350443

@Denis535 Denis535 added the needs-triage Have yet to determine what bucket this goes in. label Oct 22, 2020
@Denis535
Copy link
Author

Denis535 commented Oct 22, 2020

I did it in the following way:

<Target Name="CopyMetadataFromPackageReferenceToReferencePath" Outputs="%(PackageReference.Identity)">
    <PropertyGroup>
        <Local_MyMetadata>%(PackageReference.MyMetadata)</Local_MyMetadata>
        <Local_NugetPackageId>%(PackageReference.Identity)</Local_NugetPackageId>
    </PropertyGroup>
    <ItemGroup Condition=" $(Local_MyMetadata) != '' ">
        <ReferencePath 
            MyMetadata="$(Local_MyMetadata)" 
            Condition=" '%(ReferencePath.NugetPackageId)' == '$(Local_NugetPackageId)' " />
    </ItemGroup>
</Target>

It works. But it only worked when I've moved/allocated some values into properties. I wonder why?
Also I'm wondering if there is a better way to do this? Now If I will have many metadata then I will be forced to write ItemGroup for each metadata. So, is it possible to do it better?

@Denis535
Copy link
Author

Denis535 commented Nov 9, 2020

I think the better way is to make new reference items.

// get references with MyMetadata == true
<Target Name="MakeReferences_1" Returns="@(MyReferences )">
    <ItemGroup>
        <MyReferences Include="@(ReferencePath -> WithMetadataValue( 'MyMetadata', 'true' ))" />
    </ItemGroup>
</Target>

// get references which are pointing on PackageReference with MyMetadata == true
<Target Name="MakeReferences_2" Outputs="%(PackageReference.Identity)" Returns="@(MyReferences )">
    <PropertyGroup>
        <Local_MyMetadata>%(PackageReference.MyMetadata)</Local_MyMetadata>
        <Local_NuGetPackageId>%(PackageReference.Identity)</Local_NuGetPackageId>
    </PropertyGroup>
    <ItemGroup Condition="$(Local_MyMetadata) == 'true'">
        <MyReferences Include="@(ReferencePath -> WithMetadataValue( 'NuGetPackageId', $(Local_NuGetPackageId) ))" />
    </ItemGroup>
</Target>

Related comment #5847 (comment)

@Denis535 Denis535 changed the title How to copy metadata attributes from PackageReference to ReferencePath? How to copy metadata attributes from PackageReference to corresponding ReferencePath? Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Have yet to determine what bucket this goes in.
Projects
None yet
Development

No branches or pull requests

1 participant