Nuget PowerShell脚本修改Global.asax.cs

5

我正在为我们公司的MVC4模板构建一个NuGet包。我遇到了一个问题,我需要修改Global.asax.cs文件以添加以下两行代码:

using System.Web.Optimization;

在命名空间之前的顶部
BundleConfig.RegisterBundles(BundleTable.Bundles);

Application_Start() 方法的末尾
我尝试创建一个名为 Global.asax.cs.pp 的文件,并在其中添加 namespace $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>

我的问题有两个方面,第一个是我的.nuspec文件做错了吗?第二个是如果不能使用.pp修改Global.asax,那么我需要编写什么PowerShell脚本才能在将此NuGet添加到项目时自动运行它,并且我需要做些特殊的事情使其运行吗?(根据文档,似乎只需将Install.ps1放置在tools\中即可)。
4个回答

7

如果有人需要,这是答案,将其放入Tools文件夹内的Install.ps1中:

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)
}

1
我们如何将文本附加到文档的末尾? - ppoliani

5

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

您可能需要使用PostApplicationStartMethod属性,以便在最后完成捆绑注册。


那么你的另一种选择,正如你所怀疑的那样,就是编写一个PowerShell install.ps1脚本。该脚本需要通过传递给脚本的$dte或$project变量访问Visual Studio对象模型,以找到正确的类方法并将代码插入其中。与使用WebActivator相比,PowerShell脚本路线需要合理的工作量。 - Matt Ward
还有一件需要注意的事情 - WebActivator 不支持 .NET 4.0 之前的框架版本。 - NightOwl888

0
如果有人看到这个帖子,我想指出从2013年4月发布的2.5版本开始,Nuget将覆盖内容文件。如果使用GUI,安装过程将检测到此问题并提示是否覆盖文件。新选项-FileConflictAction允许设置默认值。
请参阅发行说明: http://docs.nuget.org/docs/release-notes/nuget-2.5

0
如果你想在某个文件中“替换”一些文本,你可以像这样操作:
$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")
}

请注意`n是用于转义换行符(CR)的。
如果您需要引号字符",也可以使用`"进行转义。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接