Nuget PowerShell script to modify Global.asax.cs

I am creating a nuget package for my company MVC2 template. I ran into a problem when I need to modify Global.asax.cs to add these two lines:

 using System.Web.Optimization; 

above, in front of the namespace and

 BundleConfig.RegisterBundles(BundleTable.Bundles); 

at the end of the Application_Start() method

I tried creating Global.asax.cs.pp with namespace $rootnamespace$ inside it, but this does not work. Nuget won't seem to overwrite existing files?

My last option, as I see it, is to write a powershell script ( Install.ps1 ?) For this. Here is 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 times, 1) Am I doing something wrong in my .nuspec? and 2) if modifying Global.asax with .pp is not an option, then what is a powershell script that I will need to write so that this run automatically when this nuget is added to the project, and I need to do something special for it launch (reading documents seems to be just placing Install.ps1 in tools\ )?

+4
source share
4 answers

Here's the answer in case someone needs it, it will be 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) } 
+5
source

I will review WebActivator before going down the path 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 to your application without having to change global.asax.

You probably want to use the PostApplicationStartMethod attribute so that your package registration is completed at the end.

+5
source

If you want to โ€œreplaceโ€ some text in some file, you can do it as follows:

 $file = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" } if($file) { $file.Open() $file.Document.Activate() $file.Document.Selection.StartOfDocument() $file.Document.ReplaceText("TextToFind`n", "TextToReplace") } 

Note the `n , which runs for \ n or type (CR).

If you need the quote character " , it can also be escaped `"

0
source

If anyone sees this stream, I want to indicate that from version 2.5 released in April 2013, Nuget will overwrite content files. If you use the GUI, the installation process will detect a problem and ask if you want to overwrite the file. The new option -FileConflictAction allows you to set the default value.

See release notes: http://docs.nuget.org/docs/release-notes/nuget-2.5

0
source

Source: https://habr.com/ru/post/1444231/


All Articles