daveaglick / Scripty

Tools to let you use Roslyn-powered C# scripts for code generation
MIT License
621 stars 69 forks source link

Mix "raw" output and generated code #35

Closed fleed closed 8 years ago

fleed commented 8 years ago

Is it possible to mix "raw" output and generated parts? I think it's a little bit painful to write an entire c# file using statements.. I'd prefer to mix a basic part written as "plain text" with parts generated. I'm referring to something like Razor, but also T4 and any other kind of template engine (like handlebars for js) already supports it.

daveaglick commented 8 years ago

Sorry for the delayed response - I was on vacation and lost track of this issue. The closest you can probably get to using Scripty as a templating engine is to call one from inside your script.

For example, let's say you wanted to use Liquid as the templating syntax for subbing out your generated source files. You could tell Scripty to use the DotLiquid library from NuGet and write your stub source files as separate text files alongside your script. Then within your Scripty script you could use the System.IO classes to read those stub files and pass their content to the DotLiquid rendering engine along with whatever model classes are appropriate for your templates. The result would be a string which you could then pass to the Scripty Output object. In fact, this exact scenario is why the Scripty output classes derive from TextWriter - it gives you the flexibility to pass those output classes to any other library that knows how to write to a TextWriter (such as templating engines).

Does that make sense?

daveaglick commented 8 years ago

I'm going to go ahead and close this - hopefully I've answered your question. Just let me know if you want to discuss further and I'll reopen the issue.

fleed commented 8 years ago

Sorry for the very late answer, but I've also been on vacation.

The solution you proposed would solve the problem, but not as first class citizen. From a code generation tool, I would expect the possibility to write a single file with mixed content and dynamic statements, as I can do with T4 or in a Razor file.

It's not a MUST HAVE because I could achieve the same result with your solution, but still it would need separate file and some additional work to get it done. And this work should be copied over all usages.

There are probably other improvements with higher priority, but I hope you will consider this suggested feature again in the future.

daveaglick commented 8 years ago

I agree it would be nice to have support for a templating language in addition to the pure-code approach we have today.

Razor is a bit problematic for this given how closely it's tied to XML/HTML. Specifically, it uses angle-based tags to help distinguish between template and code contexts. The @ syntax is a good one though, and perhaps it would be possible to disable the tag-based portion of the Razor parser.

In any case, I'll certainly keep this in mind once the more urgent issues get addressed.