I spent a lot of time on planes and in airports the last few weeks so I put together a present.
After posting about CodeConverter.NET and the View as PowerShell Chrome Extension, I had some feedback that it would be great if the code for this was open-source. After some clean up, I’ve published the conversion code to the CodeConverter.NET SDK GitHub Repository.
The way code converter works is by converting ASTs from specific languages to a common AST. From there, we can converter the AST to any language that supports writing out that AST. The implementation is very basic and doesn’t support all the goodies of the Roslyn AST. CodeConverter.NET has always been about getting close enough to a code conversion to save enough typing where it’s useful. Achieving a 100% successful conversion is very hard to do and the extra effort doesn’t seem worth it to me.
Many language features like Lambdas, Delegates and even simple ones like array initializers aren’t implemented. This is very much a work in progress. If there is something you’d like, create a new issue on GitHub. Extending the AST is easy to do by example. From there we need to update the different languages to support parsing and writing that AST element.
Do not expect the AST to be stable! It can change drastically for the time being.
C# to PowerShell was the first conversion direction that I implemented. I used Roslyn to parse the C# and then write out PowerShell. It was very coupled to the Roslyn API; hence the common AST. The SyntaxTreeVisitor class takes a string and converts it to the common AST. C# uses Roslyn to do this and PowerShell uses the Parser and associated PowerShell AST to do this. The CodeWriter class takes a common AST and writes it to a string. It is accomplished by visiting the AST and building sections of the code based on the type of Node visited.
C# to PowerShell Support
Just as with the previous version of the code converter, the C# to PowerShell conversion is functional. It passes about 95% of the tests implemented by the previous version so it still has a bit of work. It also implements some additional formatting logic to make the output a bit more readable. I’ve beefed up the testing framework a bit to be easier to work with so it should be more straight forward to cover the code.
What’s not included?
CodeConverter.NET Web UI
There isn’t much too it and I didn’t see the value of publishing it yet. Maybe in the future.
View as PowerShell Chrome Extension
I’ll make this open source eventually. I would like to update it to support more than just C# to PowerShell before doing so.
I’d like to get PowerShell to C# conversions working since it’s partially there already. From there, I think I will look at either VB.NET or F# conversions. What’s nice is that once the C# and PowerShell pieces are implemented, they should be able to accept a common AST from any language supporting it so we will be able to convert from F# to C# and PowerShell once it’s all built.
I need to update the CodeConverter.NET page to use the new version of the CodeConverter SDK. After I fix up a few more things, I’ll make this happen.
All the issues are available on GitHub.
I chose LGPL for the license. This should allow anyone, company or otherwise, to use this library without disclosing all their source code if it’s used in a binary format. Any derivations of the work would require disclosing that source. I hope this ensures that any changes to this library show up in the repository. From experience, I’m sure people will find issue with this so please start a discussion on GitHub.