Nuget PowerShell脚本修改Global.asax.cs

Ser*_*gan 5 asp.net-mvc powershell nuget

我正在为我公司的MVC4模板构建一个nuget包.我遇到了一个问题,我需要Global.asax.cs修改它来添加这两行:

using System.Web.Optimization;
Run Code Online (Sandbox Code Playgroud)

在命名空间和顶部之前的顶部

BundleConfig.RegisterBundles(BundleTable.Bundles);
Run Code Online (Sandbox Code Playgroud)

Application_Start()方法的最后

我试图创建一个Global.asax.cs.ppnamespace $rootnamespace$它里面,但似乎并没有工作.看来Nuget不会覆盖现有文件?

我认为,我的最后一个选择是编写一个PowerShell脚本(Install.ps1?)来执行此操作.这是我的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>
Run Code Online (Sandbox Code Playgroud)

我的问题是2折,1)我在.nuspec做错了吗?2)如果Global.asax使用.pp 修改a 不是一个选项,那么当将这个nuget添加到项目中时,我需要编写什么powershell脚本才能自动运行该脚本,我是否需要做任何特殊的事情它运行(阅读文档好像只是将Install.ps1tools\会做)?

Ser*_*gan 7

这是答案,如果有人需要它,这在你Install.ps1Tools文件夹里面:

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)
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*ard 5

在编写PowerShell脚本以更新现有源代码之前,先看看WebActivator.WebActivator是一个NuGet包,可用于将启动和关闭代码添加到应用程序中,而无需修改global.asax.

您可能希望使用PostApplicationStartMethod属性,以便最后完成捆绑注册.