novotnyllc / MSBuildSdkExtras

Extra properties for MSBuild SDK projects
MIT License
348 stars 42 forks source link

Do I still need MSBuild.Sdk.Extras with .net6? #274

Closed datoml closed 3 years ago

datoml commented 3 years ago

Hello, I hope it is ok to ask in the issue section but I wonder If I still need MSBuild.Sdk.Extras with .net6 coming out pretty soon.

Based on the new MAUI examples I tried to achieve the same with pure SDK style project. So what I did was creating a sample project and added the following lines to it.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net6.0-windows;net6.0-ios;net6.0-android;net6.0-maccatalyst</TargetFrameworks>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <Compile Remove="Platforms\**\*.*" />
        <None Include="Platforms\**\*.*" />
    </ItemGroup>

    <ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
        <PackageReference Include="System.Management" Version="5.0.0" />
        <PackageReference Include="System.IO.Ports" Version="6.0.0-preview.2.21154.6" />
        <Compile Include="Platforms\Windows\**\*.cs" />
    </ItemGroup>

    <ItemGroup Condition="$(TargetFramework.Contains('-android'))">
        <Compile Include="Platforms\Android\**\*.cs" />
    </ItemGroup>

</Project>

This works exactly like I hoped but when I reference the project to a console application for instance I am not able to call the methods. Is there something crucial missing here or is it still not supported? Thanks!

clairernovotny commented 3 years ago

What is the TFM of your console application?

datoml commented 3 years ago

The TFM is set to net6.0-windows. I made a Multi-Target library and referenced it to a net6.0 library that is also referenced in my console application. So I think the issue was on my side that I used it wrong. I had to reference the Multi-Target library in my net6.0-windows app and pass it with an interface to the .net6-0.

This is the correct and only way because the net6-0 library doesn't know the OS. Is this assumption correct?

datoml commented 3 years ago

It was 100% on my side. I mixed up some things in my head. 😅

If someone comes across the same "problem", this is the solution to it.

To give you a little bit of context. The idea is to create a multi-target library that is able to work with the .net6.0 TFM. What I do here is to separate android, linux and windows. Android is easy because there is a TFM for it. For linux and windows I use a separate configuration. Default will be windows.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net6.0;net6.0-android</TargetFrameworks>
    <LangVersion>Preview</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="Platforms\**\*.*" />
    <None Include="Platforms\**\*.*" />
  </ItemGroup>

  <ItemGroup Condition="$(TargetFramework.Contains('-android'))">
    <Compile Include="Platforms\Android\**\*.cs" />
  </ItemGroup>

  <ItemGroup Condition="!$(TargetFramework.Contains('-android')) and ($(Configuration) == 'Debug' or $(Configuration) == 'Release')">
    <Compile Include="Platforms\Windows\**\*.cs" />
  </ItemGroup>

  <ItemGroup Condition="!$(TargetFramework.Contains('-android')) and $(Configuration.Contains('-Linux'))">
    <Compile Include="Platforms\Linux\**\*.cs" />
  </ItemGroup>

</Project>