Modifing code files in install.ps1

Oct 27, 2010 at 4:12 PM

As part of an install of a package I need to change the base type of the HttpApplication located in the global.asax.cs file.

I was able to navigate to the element using the following powershell code.

$proj = get-project

$proj.ProjectItems.Item("global.asax").ProjectItems.Item("global.asax.cs").FileCodeModel.CodeElements.Children.Item(1).Bases.FullName

My question is. What is the best way to change the base type. David mentioned that the CodeElements is read only.?

So I want to change the following line from

    public class MvcApplication : HttpApplication

To

    public class MvcApplication : Dell.UIFramework.Web.UIFrameworkHttpApplication

 

 

Thoughts on the best approach?

Oct 27, 2010 at 10:56 PM

ok. I did find out that you can modify the runtime properties.  Here is my final code, that I am using for an internal company framework.

$proj = get-project
$mvcapp = $proj.ProjectItems.Item("global.asax").ProjectItems.Item("global.asax.cs").FileCodeModel.CodeElements | where-object {$_.Kind -eq 5}
$base = $mvcapp.Children.Item(1).Bases.Item(1)
$mvcapp.Children.Item(1).RemoveBase($base)
$mvcapp.Children.Item(1).AddBase("Dell.UIFramework.Web.UIFrameworkHttpApplication",-1)
$startup = $mvcapp.Children.Item(1).Members | Where-object {$_.Name -eq 'Application_Start'}
$startup.Name = "UIFramework_Application_Start"
$startup.OverrideKind = [EnvDTE80.vsCMOverrideKind]::vsCMOverrideKindOverride

Let me know if you find a cleaner way to do this.  I am thinking I can roll some of this into solution factory.

 

Oct 27, 2010 at 11:35 PM

Yes, you can absolutely modify things (and T4MVC does it, e.g. to change methods to virtual).  What I meant is that the object model stops at the declaration level.  You could not write code that drills into the statement level to a method.  e.g. if you wanted to inject something is App_Start in global.asax, I'm not sure that you could.

The code looks a bit cleaner in C#, but generally what you're doing is probably fine.  e.g. from T4MVC:

        foreach (var type in projectItem.FileCodeModel.CodeElements.OfType<CodeClass2>()) { ... }

Oct 28, 2010 at 3:41 AM

Yeah.. I guess discussion list are a better medium than say twitter to express ideas.