C
C#5mo ago
Lyno

source generator

Is it possible to output a .json file from a source generator or does it only work with c# files?
10 Replies
Lyno
Lyno5mo ago
private void Generate(SourceProductionContext context, (Compilation compilation, ImmutableArray<(string, string)> files) value)
{
var content = "{}";

context.AddSource("test.json", content);
}
private void Generate(SourceProductionContext context, (Compilation compilation, ImmutableArray<(string, string)> files) value)
{
var content = "{}";

context.AddSource("test.json", content);
}
this is what i have currently and it generates a "test.json.cs" file instead
Lyno
Lyno5mo ago
ontop of getting this error
No description
Angius
Angius5mo ago
It might be possible to output it as a resource or a const string Source generators, IIRC, hook directly into the compiler and emit the files there They don't work like T4 templates
Lyno
Lyno5mo ago
i see.. how would i output as a resource?
Angius
Angius5mo ago
Huh, now that I'm looking at it, I'm not sure it's possible after all... Maybe peeps in #roslyn would know more Only thing I could find is this GH issue
jcotton42
jcotton425mo ago
atm SGs can only produce source I asked a similar question recently https://discord.com/channels/143867839282020352/598678594750775301/1195136293181591602
MODiX
MODiX5mo ago
jcotton42
random question, what approach would you take to generate something during your build that is an SG isn't appropriate for? separate console project that you msbuild Exec (how would ensure the project gets built first?) MSBuild Task? (those looked awkward to have in the same project tho)
Quoted by
<@446416451230760990> from #roslyn (click here)
React with ❌ to remove this embed.
Angius
Angius5mo ago
FWIW I generate a manifest of JS files for use with a webworker in one of my projects, and I do it with... just a normal method that uses Directory and File classes to do that lmao Then I just call await JsManifest.Generate() on startup and call it a day
333fred
333fred5mo ago
Is it possible to output a .json file from a source generator or does it only work with c# files?
Only C# files
mindhardt
mindhardt5mo ago
C# file with a raw string literal json seems like a workaround