Utility to migrate your cshtml views to Precompiled views

I always felt that any Sitecore patching can be generalized in the form of utilities so that each developer doesn't write his own patch. Maybe a bit too ambitious but still I think this is achievable! So, this is my first go at one such functionality to convert an application with cshtml files to precompiled views. This utility can convert a sln with multiple csproj files each having many cshtml rendering / views to precompiled views!

This utility expects the following args:

RazorGeneratorUtility.exe 7 <sln path> <grand parent path>

In my case, the above looked like this:

RazorGeneratorUtility.exe  7 C:\Projects\xyz\xyz-main-site-9.2\xyz.MainSite\xyz.Mainsite.sln C:\Projects\xyz\

Here is the functionality covered by this utility:

1. UpdateCshtmlFileswithBuildActionNone: Updates Build Action to None for all cshtml files in the sln

 2. AddRazorBuildReferenceinPackageConfig: Adds reference to RazorGenerator.MsBuild in all the packages configs within projects having cshtml across the sln 

3.   AddRazorBuildRef: Add reference to RazorGenerator.MsBuild package in the projects with cshtml

4. The utility then finds the <sites> config patch location and creates a RazorGen.Custom.config in the same location:



5. Within this config file, the utility finds and adds all the assemblies having cshtml files:



6. Then sets the Mvc.UsePhysicalViewsIfNewer to true in the generated RazorGen.Custom.config patch:


Note that the RazorGen.Custom.Config patch file must be added manually to the sln since that's a piece missing currently although the file gets created in the file system. Will update the github code once I add that piece.

Try it if you are on a github repo so that you can easily undo in case of any issues! Any feedback is welcome.

Comments