Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Component source generator project #323

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Dreamescaper
Copy link
Contributor

@Dreamescaper Dreamescaper commented Feb 13, 2021

Contributes to #309 (2-4).

This PR has 4 commits, I suggests reviewing them one-by-one, ignoring whitespace changes.

  1. Use Roslyn instead of reflection for Component Generator
    This commit updates ComponentWrapperGenerator project to use Roslyn instead of reflection. In order to be able to use Roslyn, I create Roslyn Workspace in Program.cs.
    I've tried to preserve the output exactly same as before, and to make as few changes as possible to ComponentWrapperGenerator.cs to make review easier.

  2. Add Component source generator project
    In this commit I moved most of the generator classes to separate project Microsoft.MobileBlazorBindings.ComponentGenerator. I've added SourceGenerator class, which implements ISourceGenerator, and invokes ComponentWrapperGenerator for each required type.
    ComponentWrapperGenerator class is updated to return (string HintName, string Source) for generated files instead of writing them to disk.
    Source generator retrieves settings (namespace, types to generate) from 'component-generator.json' file in the project. Adding nuget dependencies to analyzers/source-generators is a bit troublesome, so I decided to parse json file using couple of regexes (at least for now, when json file is quite simple).
    Updated ComponentWrapperGenerator project to retain same functionality as before by adding reference to ComponentGenerator project and adding writing files to disk there.

  3. Update generator to better support third party components
    Minor updates to ComponentWrapperGenerator class to better support third party components. Add missing usings, add alias for element's namespace. If generator does not know how to handle property type, and it is not ignored, then it fallbacks to AttributeHelper.ObjectToDelegate / AttributeHelper.DelegateToObject.

  4. Add generator to Weather sample
    Add project Microsoft.MobileBlazorBindings.PancakeView.SourceGenerator, and use generator there to create component wrapper for PancakeView.
    Note that it is possible to use generator directly in Razor project, but it requires using .NET5 SDK, while MBB requires 3.1 (in global.json).

Naming is a bit confusing now - we have Source Generator project Microsoft.MobileBlazorBindings.ComponentGenerator (which should be published to nuget), and console project ComponentWrapperGenerator (for internal usage, at least for now).

Feel free to ask whatever questions you have!

Visual Studio screenshot

image

Base automatically changed from master to main March 16, 2021 18:13
@Dreamescaper Dreamescaper marked this pull request as draft June 7, 2022 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant