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)?
If you want to "replace" some text in some file you can do like this:
Note the
`n
which is escaping for \n or enter (CR).If you need quote character
"
it can be escaped as`"
as wellIn case anyone sees this thread, I want to point out that as of version 2.5 released April, 2013 Nuget will overwrite content files. If using the GUI, the install process will detect the issue and prompt whether or not to overwrite the file. A new option -FileConflictAction allows the setting of a default value.
See the release notes: http://docs.nuget.org/docs/release-notes/nuget-2.5
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.
Here's the answer in case someone needs it, this goes in your
Install.ps1
inside theTools
folder: