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

Update CodeGen pacakge to use v0.7 of CodeGeneration.Roslyn #754

Closed
wants to merge 12 commits into from

Conversation

garyng
Copy link

@garyng garyng commented Apr 6, 2020

  • move attributes out into separate project (as per readme)
  • move generators out into separate project (now the CodeGenerationAttribute uses string instead of with typeof)
  • add samples - a cli using the codegen, and a netstandard library using the codegen (made possible by making changes to the reference, as per readme)
  • fix TestBed sample
  • test all generators (only tested with Union and Record)
  • create metapackage, seems like CodeGeneration.Roslyn.PluginMetapackage.Sdk is added but not pushed to nuget yet
  • update wiki on codegen

Related to AArnott/CodeGeneration.Roslyn#191, AArnott/CodeGeneration.Roslyn#193

I'm not sure whether separating the project into a metapackage, generators, and attributes is necessary though. After some experimentation, the following usage does not work:
1. a library that only references the Attributes package
2. a console app that reference the library, the Generators and Attributes, and CodeGeneration.Roslyn.Tool.

The codegen will not run on the library unless it also references the Generators and CodeGeneration.Roslyn.Tool.
see #754 (comment)

@amis92
Copy link

amis92 commented Apr 6, 2020

After some experimentation, the following usage does not work

How does it "not work"? I want to help.

@garyng
Copy link
Author

garyng commented Apr 6, 2020

After some experimentation, the following usage does not work

How does it "not work"? I want to help.

Uhmm, let me elaborate:

Lets say there is Attributes and a Generators package; where Attributes define all the attributes that is used for annotating classes/structs/etc.; and the Generators contains all the codegen code

As a consumer,

  1. I create a new netstandard library, Models, only uses the attributes defined in the Attributes to annotate my classes/etc.
  2. I then create a netcoreapp, App -- this reference the Attributes, Generators, CodeGeneration.Roslyn.Tool, and the netstandard library Models.

I first though that compiling App would be sufficient for the tool to discover and run codegen on all the annotated classes from Models; but it turns out it doesn't -- the Models library needs to reference Generators and CodeGeneration.Roslyn.Tool as well.

Adding those references (especially Generators) requires a lot of manual work, so I was hoping:

  1. the metapackage would solve this problem, so the Models library can reference the metapackage and everything works
  2. or the tool will automatically discover all stuff annotated with Attributes and run codegen for them accordingly

@amis92
Copy link

amis92 commented Apr 7, 2020

Ok, so:

  1. codegen is only working on source files in the current project (so no generation for Models in App), as you've found out - it's very much by design
  2. Metapackage solves this problem: you only add single reference to it in the consuming project (Models); and everything should work perfectly.

Note that there's a major difference between using codegen P2P (ProjectReferefence) and as NuGets (PackageReference).

Everything should be documented in CodeGeneration.Roslyn's Readme. :)

@garyng
Copy link
Author

garyng commented Apr 7, 2020

As a library consumer, option 2 is the way to go right? The library author create a metapackage, the users just use the metapackage?

Note that there's a major difference between using codegen P2P (ProjectReferefence) and as NuGets (PackageReference).

What you meant is directly referencing the Generators project (ie. they are in the same solution) is different from consuming it as a nuget package (eg. the metapackage), right?

@garyng
Copy link
Author

garyng commented Apr 8, 2020

As stated in the readme (and in AArnott/CodeGeneration.Roslyn#191 (comment)), the metapackage is only applicable for nuget consumer. When the generator lives in the same solution as the consumer, some modifications need to be done manually to the ProjectReference (see https://github.com/AArnott/CodeGeneration.Roslyn#customize-generator-reference).

A local nuget source can be used for testing the nuget package locally though.


Lots of thanks to @amis92 for helping me!

@amis92
Copy link

amis92 commented Apr 8, 2020

Also a general note, it seems csproj files are using tab indent.

@garyng
Copy link
Author

garyng commented Apr 8, 2020

Also a general note, it seems csproj files are using tab indent.

Yeah, my visual studio is configured to use tabs... I will replace them with spaces for consistency.

@amis92
Copy link

amis92 commented Apr 9, 2020

After this is done, can you please post an issue back in CG.R to add this in the Migration wiki as a helpful example? :)

@garyng
Copy link
Author

garyng commented Apr 9, 2020

After this is done, can you please post an issue back in CG.R to add this in the Migration wiki as a helpful example? :)

Like this? AArnott/CodeGeneration.Roslyn#223

@amis92
Copy link

amis92 commented Apr 18, 2020

@louthy If there's anything I can help with to push it, please don't hesitate to ask :) I'm eagerly awaiting wider adoption ;)

@garyng
Copy link
Author

garyng commented Apr 18, 2020

@amis92 Seems like @louthy will be busy for these few weeks because of work, #755

@ffgiraldez
Copy link

Hi, please,I need this, I don't know why but I have both 3.1 and 2.1 runtime and sdk but vs for mac does not recognize me codegen

@louthy
Copy link
Owner

louthy commented Jun 16, 2020

@garyng I've made some pretty large changes since you started this. I'm ready to start looking into it, but I wonder if you'd mind closing this PR down and re-doing it with the latest main branch? I realise it's a bit of work, but it'll be the quickest way of getting this into the project.

Thanks!

@garyng
Copy link
Author

garyng commented Jun 16, 2020

@louthy Sure! I'll look into it probably on the weekends.
Seems like Microsoft also announced Source Generators, which might possibly replace CodeGeneration.Roslyn (AArnott/CodeGeneration.Roslyn#229)

@louthy
Copy link
Owner

louthy commented Dec 4, 2020

@garyng I'm going to close this down, because the next step probably will be with Source Generators. Thanks for the effort though, sorry it didn't work out.

@louthy louthy closed this Dec 4, 2020
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

4 participants