icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
793 stars 207 forks source link

Convert ASP.NET (+core) cshtml/vbhtml files #172

Open GrahamTheCoder opened 5 years ago

GrahamTheCoder commented 5 years ago

Related info for aspx is probably relevant here: https://github.com/icsharpcode/CodeConverter/issues/175

May be necessary to use things like

Out of scope: ASP Net Classic will not be supported

Background

For each file that currently has an open editor, we receive a whole project object (same file path as the one containing the vbhtml) with a bit of boilerplate and then a projection of each code block in the vbhtml

e.g. About.vbhtml

@Code
    ViewData("Title") = "About" & CStr(1)
End Code

<main aria-labelledby="title">
    <h2 id="title">@ViewData("Title").</h2><h3 id="title">@ViewData("Title2").</h3>
    <h3>@ViewData("Message")</h3>

    <p>Use this area to provide additional information.</p>
</main>

About.vbhtml.__projection.g.vb:


#ExternalSource("C:\Users\gph77\source\repos\CodeConverterTests\ExampleVbHtml\Views\Home\About.vbhtml",1)

    Me.ViewData("Title") = "About" & CStr(1)

#End ExternalSource

#ExternalSource("C:\Users\gph77\source\repos\CodeConverterTests\ExampleVbHtml\Views\Home\About.vbhtml",2)
              Global.ASP._Page_Views_Home_About_vbhtml.__o = Me.ViewData("Title")

#End ExternalSource

#ExternalSource("C:\Users\gph77\source\repos\CodeConverterTests\ExampleVbHtml\Views\Home\About.vbhtml",3)
                                                     Global.ASP._Page_Views_Home_About_vbhtml.__o = Me.ViewData("Title2")

#End ExternalSource

#ExternalSource("C:\Users\gph77\source\repos\CodeConverterTests\ExampleVbHtml\Views\Home\About.vbhtml",4)
   Global.ASP._Page_Views_Home_About_vbhtml.__o = Me.ViewData("Message")

So we need:

Using internals

Given a bunch of internal interfaces, this may be a good time to try out the new way of accessing such things (with appropriate guards of course): https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute?view=net-8.0#examples

Side note

There's a snippet here which tells us how we should be getting the right one of these projects normally (currently it just grabs the first!) https://github.com/dotnet/roslyn/blob/e102ec27b9a89b4ee462f93bcd3295a66c439329/src/VisualStudio/Core/Def/Implementation/HierarchyItemToProjectIdMap.cs#L76-L87

GrahamTheCoder commented 5 years ago

A similar solution could probably also cover https://github.com/icsharpcode/CodeConverter/issues/219

camainc commented 2 weeks ago

Has there been movement on this? We have a huge web application with hundreds of vbhtml files and could use a helping hand.

GrahamTheCoder commented 2 weeks ago

Just to check I'm looking at the right thing, am I right in thinking it's .net framework 4.8 that's the latest version that supports vbhtml? Is that what you're using?

I've started adding some background information to the issue description. The good news is that it's possible to get at and convert the code itself, the less good news is that it needs a fair amount of surrounding code to be useful on the scale. If you, or anyone else is interested in doing some digging in the roslyn repo for the relevant parts I've mentioned that would be a huge help.

GrahamTheCoder commented 2 weeks ago

I've got a spike that gets me access to what's needed I think, but it's reflection heavy: https://github.com/icsharpcode/CodeConverter/pull/1110