Skip to content

Look into alternatives to code generators #522

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

Closed
angularsen opened this issue Oct 15, 2018 · 7 comments · Fixed by #656
Closed

Look into alternatives to code generators #522

angularsen opened this issue Oct 15, 2018 · 7 comments · Fixed by #656

Comments

@angularsen
Copy link
Owner

angularsen commented Oct 15, 2018

PowerShell works, but it's slow and the type system is very limited and can be challenging to work with compared to C#. For instance, parsing JSON into types for intellisense and sharing types between script files does not work very well in Visual Studio, VS Code or Rider.

This issue is just to have a place to dump ideas for improving the code generator, we will only make the jump if it makes considerable improvements over PowerShell.

Geco

Simple code generator based on a console project, running on .Net core and using C# interpolated strings.
https://github.com/iQuarc/Geco

Pros:

  • It's C# - familiar and works with normal tooling
  • Specifically made with code generation in mind
  • Similar to T4 templates but supports debugging

ScriptCS (out of favor due to poor tooling support)

Write C# apps with a text editor, nuget and the power of Roslyn!
https://github.com/scriptcs/scriptcs

Pros:

  • It's C# - familiar and works with normal tooling
  • Run C# .csx files without any projects
  • Entire C# and .NET at your disposal

Cons:

  • Poor tooling support, intellisense and refactoring does not work well or at all
  • Requires installing ScriptCS on developer PCs
  • Not built-in way to emit generated text/code, but this could be as simple as StringBuilder and File.WriteAllText
@ptr1120
Copy link

ptr1120 commented Nov 17, 2018

Hello,

Another option could be usind CodeDom (https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-create-a-class-using-codedom) to generate the classes. The code for class generation could be packed in a dotnet core tools package and the be executed from commandline.

@angularsen
Copy link
Owner Author

Thanks @ptr1120, I forgot about that approach.

Looking through the samples though, I think maybe it adds a lot of generator complexity and is perhaps better suited for more dynamic code generation. Our needs are quite simple, almost static with just some parameters, and for that purpose I think I would prefer PowerShell or some other string/template-based generators.

@ptr1120
Copy link

ptr1120 commented Nov 18, 2018

@angularsen, you are right I know CodeDom is very complex. A template-based approach would be also better for supporting other languages like java, or javascript. In the future, it would be nice to have a complete data/model-driven approach where a command-line tool (npm/nuget) allows the generation of the complete code (preferably also the CustomCode) from the UnitDefinitions files.

@angularsen
Copy link
Owner Author

Yes exactly, I've been toying with the idea of generating for TypeScript/JavaScript and other languages but I simply haven't had the time and need for it to purse the idea further - but it's completely possible, and as you say this would be simpler to reuse with a template-based generator.

@angularsen
Copy link
Owner Author

Related to #410

@angularsen
Copy link
Owner Author

angularsen commented Mar 12, 2019

I've tried ScriptCS lately in a different project. Although I like the promise of C# instead of PowerShell and built-in support for nugets with no project files required to run - it fails on a simple but essential thing; there is very poor tooling support as of today.

VS Code and JetBrains Rider fail to recognize this variant of C# syntax, so all intellisense and refactoring tools are out the window.

In that project, I wound up creating a simple .NET Core console app instead and using dotnet run --project ./scripts/my-handy-tool-directory/.

With nugets like https://github.com/commandlineparser/commandline, you can get very nice help screens and argument parsers with verbs/commands support. You don't get the nice auto-complete feature PowerShell has for script arguments, and you need a separate folder per "script" since there is a project file and typically some other files too beyond the Program.cs file. It's still pretty lightweight, runs much faster since it is compiled and you get full C# tooling support.

Update 2019-04-17:
Microsoft is rolling out a new, to-be defacto standard for CLI parsing. For .NET Core you can also use its "Dragonfruit" nuget, which is a brand new .NET Core app model where you can in fact get auto-completion on parameters similar to PowerShell. For example type -fil and it will autocomplete -filename for you if that is a parameter. Exciting stuff!

https://github.com/dotnet/command-line-api/wiki

@angularsen
Copy link
Owner Author

angularsen commented Apr 17, 2019

I took a closer look at Geco, and although I think it has some nice helper code it seems very oriented towards Entity Framework so I'm thinking it might be enough to copy some of the helper code and write our own CLI using Microsoft's new Dragonfruit CLI parser linked above and doing plain old string outputs to files. It should be pretty straight forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants