RicoSuter / DNT

DNT (DotNetTools): Command line tools to manage .NET projects and solutions.
MIT License
421 stars 64 forks source link

Keep ItemGroup Condition when switching projects <-> packages #127

Open Leon99 opened 1 year ago

Leon99 commented 1 year ago

Currently, switching between projects and packages may cause unexpected changes due to moving the PackageReference/ProjectReference to an ItemGroup without a Condition.

A typical approach when multitargeting is to have something like this:

...
  <ItemGroup Condition="'$(TargetFramework.StartsWith(`net4`))'">
    <PackageReference Include="NetFrameworkOnlyPackage" Version="*" />
...

Using dnt to switch that package will result in referencing it unconditionally.

Is there a way to keep the replaced reference with the same ItemGroup on switching?

ssteiner commented 1 year ago

I think the MSBuild APis don't allow that. I'm looking at the source for another matter, but the API used AddItem)))) doesn't seem to allow selecting the ItemGroup, let alone creating an ItemGroup with conditions. But I've just started playign around with it. If you care to have a look yourself, look at SwitchProjectsToPackagesCommand and SwitchPackagesToProjectsCommand - the code is reasonably easy to understand.