This project is read-only.

Redo-RazorGenerator generates for all views?

Dec 13, 2012 at 2:33 PM

Is there anyway to make Redo-RazorGenerator only apply to certain views? Or, more specifically, to exclude certain views from being generated?

I have a bunch of common views which are referenced from a common project. When I run Redo-RazorGenerator on the outermost web project, I don't want it to generate the code for those imported/copied views. (The common views are copied to the local project in the after-build target.) I'd like to mark these views as "do not generate." The reason is because the code is already generated for these views in the common project. If I generate the code again in the referencing project then the view can become out of sync with the generated code since RazorGenerator/VS does not regenerate the generated views if the common views change.

 

Thanks for this great tool!

Coordinator
Dec 13, 2012 at 5:36 PM

Short answer is that there is no way to do this today. But note that normally you shouldn't have to use the redo command very frequently, since views are regenerated as soon as you make a change to the file and safe. redo is mostly for when you update RazorGenerator to a version that generates things differently and you want it to redo it again on everything.

Dec 13, 2012 at 5:46 PM

The views aren't regenerated when the file is overwritten it seems, only when saved in VS. Can you confirm that this is the expected behavior? We also have some generated views (*.cshtml) that we then want RazorGenerator to ... generate ... so that is two levels of generation. When we re-generate the cshtmls, RazorGenerator doesn't pick up on this and doesn't regenerate the *.cs for those views.

Coordinator
Dec 13, 2012 at 5:49 PM

Right, it only happens when you save in VS, since it works as a VS single file generator. There is no one to do generation when you overwrite the files (VS may not even be running at all).

As for your original question, I guess we'd need to come up with some kind of exclusion marker, either at the folder or file level.

Dec 14, 2012 at 3:37 AM

It could be something as simple as a setting a flag which will cause Enable-RazorGenerator and Redo-RazorGenerator to not touch files which are marked Readonly...

Coordinator
Dec 14, 2012 at 6:11 PM
Edited Dec 14, 2012 at 6:13 PM

You could add this script (https://gist.github.com/4287384) to your powershell profile and it should get you the behavior you're looking for. (Haven't tested it completely, but it does look workable).

In case you weren't sure how to set up your PS profile - http://docs.nuget.org/docs/start-here/using-the-package-manager-console#Setting_up_a_NuGet_Powershell_Profile 

Dec 14, 2012 at 6:16 PM

Thank you, I'll take a look.

Coordinator
Dec 15, 2012 at 11:52 PM

So by default we do the generation even when the file are read-only? Are we explicitly removing the read-only flag, or does it somehow just happen?

Coordinator
Dec 16, 2012 at 5:03 AM

All we do is find all *.cshtml files under the project and invoke the custom tool action on it via DTE. We aren't doing anything to mangle the read-only flag - if that's what is happening, then it is VS.

Dec 17, 2012 at 7:10 PM

In my case, the common views that are copied in are only copied with the *.cshtml - the generated *.cs is not copied. So my assumption is that RazorGenerator does what pranavkm said - just looks for all *.cshtml and generates the *.cs for all of them.

Just to be clear, in the after-build target I copy the common views to the main project and then mark the *.cshtml as read-only. My suggestion was for RazorGenerator to ignore *.cshtml files that are read-only. But I do this just using a different script as pranavkm suggested will work for me. 

Coordinator
Dec 17, 2012 at 7:19 PM

Glad that helped. If you are up for it, feel free to contribute the script back with a -IgnoreReadOnly switch added to it. Ideally it would accept a -Files list that you could have pre-filtered in whatever manner you prefer, but that's a bit much to do.