dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

Source Generators: Allow embedding of files other than source #49935

Open chsienki opened 3 years ago

chsienki commented 3 years ago

We've seen from several customers the want to embed other files into the output assembly, particularly EmbeddedResources (see https://github.com/dotnet/roslyn/discussions/49923)

We should explore an API that allows generators to add 'other' inputs to the compilation other than just source. This could be scoped explicitly to EmbeddedResources, or we might want to allow e.g. AdditionalFiles too.

Expanding it out to more types might have implications for analyzers, as they would be able to see them as well, but given that analyzers today see the generated source this seems desirable (although we might need an API that returns the provenance of the thing, such as Generated or Not).

lsoft commented 3 years ago

Could I ask to add a possibility to include a non-compilable additional files? I need to send some metadata from SG to my VSIX (and update custom codelenses with that metadata), so I need to access that metadata (for example XML file) in Compilation, but I don't need to have that XML in the binaries (so, it is not a resource, not an embedded resource, but something like BuildAction=None in Property window).

ChrML commented 2 years ago

This would be great. Source generators should be able to add other types of files to the output. One example would be Swagger that could generate the OpenAPI file at compile time and add the resulting .json to the compile output. Unless there is another way that could be done?

elv1s42 commented 2 years ago

Would be very useful! Another example is to use source generators for generation of some partial views like .cshtml or .razor files.

khaled-saleh-sf commented 2 years ago

Any updates on this?

zlepper commented 2 years ago

Concrete use-case from my company: We have a shared library that defines a set of formats that we can generate based on other files (think images from videos and such). Based on those format classes we generate a whole API and database definition (with ef core). Right now we do this by using the metadata loader, and then inspecting the whole assembly, however this is both slow and means we need 2 code paths in our generator, one for dealing with assemblies and one for dealing with compilation metadata (for example for when the source generator is invoked from an IDE). Instead we would like to generate json file (or equivalent) that contains metadata about the formats, that we can then use to generate the full controllers/database. We can technically get away with putting in an assembly level annotation, but just having an embedded resource we could read would probably be much easier and much clearer.

rick-palmsens commented 1 year ago

Our use case: We have a single C# backend, and build multiple applications on top of that using different frameworks. Most of these applications will use a lot of the same colors and styling. We would like to create a single source of truth and generate resources/themes/styles for each platform from that.

AaronSymons commented 1 year ago

I think this is a great idea!

Generating C# source code is fantastic, but being able to generate other file types, such as .cshtml, would increase the value of the generators even more.

I understand that .cshtml files are not considered source code, so perhaps an IContentGenerator could be the way to go for that. Further, there could be an IResourceGenerator to handle creating other types of files and resources (I'm starting to think that different generator interfaces could be concerned with different resource build actions). This way, separation of concern is preserved, and the feature availability of code generators in general is expanded and enriched.

As well as that, we could perhaps give flags to context.AddSource() in order to describe what kind of resource or file we're generating, and therefore describing how to handle the generation with regards to project inclusion and build action, for example.

Personally, if I can use generators to create .cshtml and .xaml files, I will be hitting a new and higher level of productivity!

Thanks, and much respect to the dev team (who I imagine are already considering such ideas)! 😄

P.S. As a fellow developer, I understand that requests and ideas are often far beyond the current concerns and/or reach of development goals. I'm just here as a voice in favour of supporting generating different file types because it's incredibly useful and... it's awesome!

mgaffigan commented 7 months ago

I'd love to see this available from source generators. Two use cases come to mind:

  1. Generate bindings to .Net types from callers in other languages (C# types -> C headers, idl, tlb, and reg files for COM)
  2. Generate ADMX files from C# models

Both of these are possible through secondary build actions which analyze the produced dll, but source generators are much easier.

cdhanna commented 3 weeks ago

this feature would help me as well. I'm using source generators to generate a virtual lookup table, but I would also love to generate some static resource file about that metadata that downstream consumers could read without having to load up the .dll file itself to get the coded lookup table.