Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget PowerShell script to modify Global.asax.cs

I'm building a nuget package for my company MVC4 template. I have run into an issue where I need the Global.asax.cs to be modified to add these two lines:

using System.Web.Optimization;

at the top, before the namespace and

BundleConfig.RegisterBundles(BundleTable.Bundles);

at the end of the Application_Start() method

I tried creating a Global.asax.cs.pp with namespace $rootnamespace$ inside it, but that doesn't seem to work. It seems Nuget wont overwrite existing files?

My last option, as I see it, is to write a powershell script (Install.ps1?) to do this. Here's my template.nuspec

<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <id>Template.MVC4</id>
    <version>1.5</version>    
    <title>MVC4 Template</title>
    <description>Installs and configures files for MVC 4 application template.</description>
    <authors>Me</authors>
    <language>en-US</language>
    <dependencies>
        <dependency id="Microsoft.AspNet.Web.Optimization" />
    </dependencies>
    <iconUrl>http://www.someurl.com/Logo.jpg</iconUrl>
  </metadata>
  <files>   
    <file src="Template\Helpers\*" target="content\Helpers\" />
    <file src="Template\Content\*.css" target="content\Content\" />
    <file src="Template\Content\animGif\*" target="content\Content\animGif\" />
    <file src="Template\Content\custom-theme\*" target="content\Content\custom-theme\" />
    <file src="Template\Content\templateImages\*" target="content\Content\templateImages\" />
    <file src="Template\Scripts\*" target="content\Scripts\" />
    <file src="Template\Views\Shared\*" target="content\Views\Shared\" />
    <file src="Template\Views\Home\*" target="content\Views\Home\" />
    <file src="Template\Views\_ViewStart.cshtml" target="content\Views\" />
    <file src="NuGetPackage\App_Start\*" target="content\App_Start\" />
    <file src="NuGetPackage\Controllers\*" target="content\Controllers\" />
    <file src="NuGetPackage\Helpers\*" target="content\Helpers\" />
    <file src="NuGetPackage\Models\*" target="content\Models\" />
    <file src="NuGetPackage\Views\*" target="content\Views\" />
    <file src="NuGetPackage\*" target="content\" />
  </files>
</package>

My question is 2 fold, 1) am I doing something wrong in my .nuspec? and 2) if modifying a Global.asax with .pp isn't an option, then what is the powershell script that I'd need to write to have this run automatically when this nuget is added to a project, and do I need to do anything special for it to run (reading the docs seems like just placing Install.ps1 in tools\ will do)?

like image 919
Serj Sagan Avatar asked Nov 05 '12 23:11

Serj Sagan


2 Answers

Here's the answer in case someone needs it, this goes in your Install.ps1 inside the Tools folder:

param($installPath, $toolsPath, $package, $project)

# Read the transformed text from the custom template included in the package
$customGlobalAsax = $project.ProjectItems | where { $_.Name -eq "Global.asax.cs.custom" }
$customGlobalAsax.Open()
$customGlobalAsax.Document.Activate()
$customGlobalAsax.Document.Selection.SelectAll(); 
$replacementGlobalAsax = $customGlobalAsax.Document.Selection.Text;
$customGlobalAsax.Delete()

# Replace the contents of Global.asax.cs
$globalAsax = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" }
if($globalAsax) {
    $globalAsax.Open()
    $globalAsax.Document.Activate()
    $globalAsax.Document.Selection.SelectAll()
    $globalAsax.Document.Selection.Insert($replacementGlobalAsax)
    $globalAsax.Document.Selection.StartOfDocument()
    $globalAsax.Document.Close(0)
}
like image 196
Serj Sagan Avatar answered Oct 03 '22 13:10

Serj Sagan


I would take a look at WebActivator before going down the route of writing a PowerShell script to update existing source code. WebActivator is a NuGet package that you can use to add startup and shutdown code into an application without having to modify global.asax.

You will probably want to use the PostApplicationStartMethod attribute so your bundle registration is done at the end.

like image 24
Matt Ward Avatar answered Oct 03 '22 11:10

Matt Ward