dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.66k stars 1.06k forks source link

dotnet tools targeting Windows APIs #12055

Open wli3 opened 4 years ago

wli3 commented 4 years ago

Due to this issue, WPF and Winforms net5.0 dotnet tools are not available

What happens if a dotent tools targeting net5.0-win and want to call Windows specific API? Say access Bluetooth to help debugging a device.

Currently the target framework selection is choosing the most compatible TFM according to the TFM SDK is built on. In this case, SDK will be built on net5.0, and it won't choose net5.0-win.

This need external dependency. We need runtime team to provide us a mechanism to tell if 5.0 windows runtime is install on the machine, which we've been asked a long time.

wli3 commented 4 years ago

cc @KathleenDollard

wli3 commented 4 years ago

at the same time I don't think we have net5.0-linux and net5.0-mac.

wli3 commented 4 years ago

my opinion is this is a low priority. I don't think there are many tools want to access platform specific API. We could ship without this support and see the reaction.

marcpopMSFT commented 3 years ago

https://github.com/dotnet/runtime/issues/46128

tthiery commented 3 years ago

It would be lovely if this makes it into the .NET 6 release ;). As in the other issue described, I have exactly the bluetooth case described in the original description.

guillaume86 commented 2 years ago

I would also use this, my use case is showing Toast notifications if the platform allows it.

JanKotschenreuther commented 2 years ago

We also got use cases.

For example we got cli tools which got large lists of options as input. Because of the size of these commands, we keep those in scripts. We actually build UI tools to help managing those scripts, keeping track of and changing all options and their states.

lindexi commented 2 years ago

How can I ignore the NETSDK1146 error?

lindexi commented 2 years ago

How can I ignore the NETSDK1146 error?

One of the hack code (Beaking in .NET 8):

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

Update in https://github.com/dotnet/sdk/issues/12055#issuecomment-2126355866

LazaroOnline commented 1 year ago
    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

Pardon my ignorance, but where should I put that code? I tried in csproj like this:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    ....
  </Target>
</Project>

but didn't work. It throws an error "MSB4232 Items that are outside Target elements must have one of the following operations: Include, Update, or Remove."

So I suppose I need to create some other file related to msbuild, but I'm not familiar with that process. Please, can someone explain how to add that Target hack code?

KalleOlaviNiemitalo commented 1 year ago

@LazaroOnline did you place the Target element within an ItemGroup element. I expect that would cause the MSB4232 error. If you did, move it out. If you didn't, please show in more detail what you did.

LazaroOnline commented 1 year ago

@KalleOlaviNiemitalo yes, I also tried placing it into an ItemGroup and had the same error message. Here is a more detailed example of the csproj file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <PackAsTool>True</PackAsTool>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
  </ItemGroup>

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
      <PropertyGroup>
        <TargetPlatformIdentifier></TargetPlatformIdentifier>
      </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
      <PropertyGroup>
        <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
      </PropertyGroup>
    </Target>

</Project>

Error: Items that are outside Target elements must have one of the following operations: Include, Update, or Remove.

KalleOlaviNiemitalo commented 1 year ago

The Target elements should not be inside the ItemGroup element.

LazaroOnline commented 1 year ago

@KalleOlaviNiemitalo True, I got it to work without the ItemGroup, however VisualStudio seems to have some stability problems handling these manual changes, it intermittently complains about the csproj. I will edit my example above to leave a working example so it is less confusing for others.

Anyway, even if I could pack it as a nuget dotnet-tool, and publish it to nuget.org, it fails when the user tries to install, since in my case I only have 1 target net7.0-windows which is invalid.

Anduin2017 commented 3 months ago

How can I ignore the NETSDK1146 error?

One of the hack code:

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

At @lindexi I tired adding this hack.

However when I was installing it, I got an error:

$ dotnet tool install MyApp--global
Tool 'xxxxxxxxxxxxxxxx.xxxxx' failed to update due to the following:
The settings file in the tool's NuGet package is invalid: Settings file 'DotnetToolSettings.xml' was not found in the package.
Tool 'xxxxxxxxxxxxxxxx.xxxxx' failed to install. Contact the tool author for assistance.

I tried to download the nupkg and unzip it locally, but found DotnetToolSettings.xml locally.

image

lindexi commented 3 months ago

@Anduin2017 Update the hack csproj code for .NET 8.0 or greater

  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    <PropertyGroup>
      <TargetPlatformIdentifier></TargetPlatformIdentifier>
      <TargetPlatformMoniker></TargetPlatformMoniker>
    </PropertyGroup>
  </Target>

  <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation" BeforeTargets="PackTool">
    <PropertyGroup>
      <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
    </PropertyGroup>
  </Target>

All the csproj code:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <PackAsTool>true</PackAsTool>
    <ToolCommandName>lindexi</ToolCommandName>
  </PropertyGroup>

  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    <PropertyGroup>
      <TargetPlatformIdentifier></TargetPlatformIdentifier>
      <TargetPlatformMoniker></TargetPlatformMoniker>
    </PropertyGroup>
  </Target>

  <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation" BeforeTargets="PackTool">
    <PropertyGroup>
      <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
    </PropertyGroup>
  </Target>

</Project>

And you can find all my code in https://github.com/lindexi/lindexi_gd/blob/fdd3959ce65cf2ebeead9706c04901caafb1f89c/WinemwhajallawLigawakuja/WinemwhajallawLigawakuja.csproj

Anduin2017 commented 3 months ago

Thank you @lindexi ! I tried your hack an it works! Now I can distribute my WPF app via dotnet tool!