angularsen / UnitsNet

Makes life working with units of measurement just a little bit better.
https://www.nuget.org/packages/UnitsNet/
MIT No Attribution
2.19k stars 380 forks source link

Look into alternatives to code generators #522

Closed angularsen closed 5 years ago

angularsen commented 5 years ago

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:

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:

Cons:

ptr1120 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

Related to https://github.com/angularsen/UnitsNet/issues/410

angularsen commented 5 years ago

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 commented 5 years ago

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.