Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update all package references
<Project>
<Target Name="_CollectPackagesToUpdate" Returns="@(_PackageToUpdate)">
<ItemGroup>
<_PackageToUpdate Include="@(PackageReference)" Condition="'%(PackageReference.IsImplicitlyDefined)' != 'true' and '%(PackageReference.DoNotUpdate)' != 'true'"
TargetFramework="$(TargetFramework)" />
</ItemGroup>
</Target>
<Target Name="_UpdatePackagesSingleTfm" DependsOnTargets="_CollectPackagesToUpdate" Condition="'$(TargetFramework)' != '' and '$(TargetFrameworks)' == ''">
<Exec Command="dotnet remove &quot;$(MSBuildProjectFile)&quot; package %(_PackageToUpdate.Identity) &amp;&amp; dotnet add &quot;$(MSBuildProjectFile)&quot; package %(_PackageToUpdate.Identity)" />
</Target>
<Target Name="_UpdatePackagesMultiTargeting" DependsOnTargets="_CollectPackagesToUpdate" Condition="'$(TargetFramework)' == '' and '$(TargetFrameworks)' != ''">
<ItemGroup>
<_OuterPackages Include="@(_PackageToUpdate)" />
<_TargetFrameworksForUpdate Include="$(TargetFrameworks)" />
</ItemGroup>
<MSBuild Projects="$(MSBuildProjectFile)" Targets="_CollectPackagesToUpdate" Properties="TargetFramework=%(_TargetFrameworksForUpdate.Identity)">
<Output TaskParameter="TargetOutputs" ItemName="_InnerPackages" />
</MSBuild>
<ItemGroup>
<_InnerPackages Remove="@(_OuterPackages)" />
</ItemGroup>
<RemoveDuplicates
Inputs="@(_InnerPackages);@(_OuterPackages)">
<Output
TaskParameter="Filtered"
ItemName="_AllPackages"/>
</RemoveDuplicates>
<Exec Command="dotnet remove &quot;$(MSBuildProjectFile)&quot; package %(_AllPackages.Identity)" />
<Exec Command="dotnet add &quot;$(MSBuildProjectFile)&quot; package %(_OuterPackages.Identity)" Condition="'%(_OuterPackages.Identity)' != ''" />
<Exec Command="dotnet add &quot;$(MSBuildProjectFile)&quot; package %(_InnerPackages.Identity) -f %(_InnerPackages.TargetFramework)"
Condition="'%(_InnerPackages.Identity)' != ''" />
</Target>
<Target Name="UpdatePackages" DependsOnTargets="_UpdatePackagesSingleTfm;_UpdatePackagesMultiTargeting" />
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment