tmds / Tmds.DBus

D-Bus for .NET
MIT License
278 stars 52 forks source link

Provide a Source Generator backend in addition to the S.R.E backend. #79

Closed jkoritzinsky closed 1 year ago

jkoritzinsky commented 4 years ago

Currently, using Tmds.DBus uses reflection exclusively to dynamically emit code to connect to D-Bus. With the new Roslyn source generators feature, it should be possible to provide a no-runtime-emit path via generated source. This would enable reliable usage with the IL Linker to help enable smaller program sizes with reliable functionality.

tmds commented 4 years ago

@jkoritzinsky I agree, this would be really useful. I won't get to it on short term though.

tmds commented 2 years ago

I've created https://github.com/tmds/Tmds.DBus/issues/119 to expose a D-Bus protocol API. A source generator would target that API.

tmds commented 2 years ago

A source generator would target that API.

The tool can now generate code that targets the Protocol API.

It can only generate code for proxies (that is: to use an object from a service), not for adaptors (that is: to provide an object).

If you want to try it (and maybe give feedback) you can grab packages of this NuGet feed:

<add key="tmds" value="https://www.myget.org/F/tmds/api/v3/index.json" />
tmds commented 2 years ago

@jkoritzinsky any interest in being an early adopter of the Protocol library?

jkoritzinsky commented 2 years ago

I don't have time at the moment, but I'll pass this along to the other contributors to the project that prompted me to open this issue.

affederaffe commented 1 year ago

I created a source generator some time ago that uses the new Protocol library, see https://github.com/affederaffe/Tmds.DBus.SourceGenerator. There's also a pending PR to replace the S.R.E backend usage in Avalonia, though there are still some blockers:

  1. There's a bug in the Protocol library which would affect Avalonia, though there's already a PR to fix this
  2. There's still no NuGet package, so a submodule is currently used, which is suboptimal
  3. The source generator itself isn't on NuGet, though I'd like to have 1. and 2. fixed before publishing.

I'd love to see those PRs merged within Avalonia 11, are there any plans on releasing Tmds.DBus.Protocol on NuGet?

tmds commented 1 year ago

I like that you built a source-generator for the protocol library.

Are you aware the Tmds.DBus.Tool can generate proxies for the protocol library? Have you tried using it? I haven't found much time to work on this in the past year.

There's a bug in the Protocol library which would affect Avalonia, though there's already https://github.com/tmds/Tmds.DBus/pull/179

I'll look at the PR.

are there any plans on releasing Tmds.DBus.Protocol on NuGet?

And I'll make a release of the library in its current state by early next week.

tmds commented 1 year ago

Are you aware the Tmds.DBus.Tool can generate proxies for the protocol library?

What it can't do is: generate code for you to implement objects to be made available through D-Bus. Does your source-generator support that?

affederaffe commented 1 year ago

The source generator has some advantages over the Tmds.DBus.Tool generation:

  1. It doesn't require you to add auto-generated code to your repository, see https://github.com/AvaloniaUI/Avalonia/pull/9810#issuecomment-1367968429
  2. It generates needed methods for writing generic types, so there's no reflection at all (Tmds.DBus.Protocol uses MakeGenericType for example)
  3. It also deals with variants

Yes, it also generates abstract handler classes for the user to implement.

Thanks, I appreciate your effort!

tmds commented 1 year ago

It doesn't require you to add auto-generated code to your repository, see https://github.com/AvaloniaUI/Avalonia/pull/9810#issuecomment-1367968429

I don't have issues adding generated code to a project. I opted for that because I assumed a user might want to tweak the code, but probably they don't.

It generates needed methods for writing generic types, so there's no reflection at all (Tmds.DBus.Protocol uses MakeGenericType for example)

I'm curious how your library handles a type like: (int, (string, string))[]. I will look at some generated code.

It also deals with variants

Tmds.DBus.Protocol supports variants as objects (for reading and writing). But it does use MakeGenericType to make that work. Again, I'll take a look how this works.

The code generated by Tmds.DBus.Tool, even though using MakeGenericType, is AOT compatible.

Yes, it also generates abstract handler classes for the user to implement.

Nice!

If it's ok with you, I'll put a reference to your generator on the README.

affederaffe commented 1 year ago

Sure, that'd be great!

tmds commented 1 year ago

@affederaffe I'll be making a release to nuget.org today.

If you want to verify it will work for you, you can try with the latest CI build: Tmds.DBus.Protocol.0.11.1-160-9b0363f81af98d82a459973c41a8a5a7295bea57.nupkg from https://www.myget.org/F/tmds/api/v2/package.

tmds commented 1 year ago

The protocol library is now on nuget.org, and can be targeted using the source generator implemented by @affederaffe.