2012-11-05 104 views
4

我正在为我公司的MVC4模板构建nuget包。我遇到了我需要的Global.asax.cs进行修改,以添加这两条线的一个问题:Nuget PowerShell脚本修改Global.asax.cs

using System.Web.Optimization; 

顶部,命名空间之前和

BundleConfig.RegisterBundles(BundleTable.Bundles); 

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> 

我的问题是2倍,1)我做错了什么,我.nuspec? 2)如果使用.pp修改Global.asax不是一个选项,那么当我将这个nuget添加到项目中时,我需要写的powershell脚本是否会自动运行,并且是否需要执行任何操作特别为它运行(阅读文档似乎只需将Install.ps1tools\会做)?

回答

5

这里的,以防有人回答需要它,这正好在你的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) 
} 
+1

我们如何将文本附加到文档的末尾 – ppoliani

5

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

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

+0

我宁愿避免额外的依赖... –

+0

那么你的替代方案,因为你怀疑,写一个PowerShell脚本install.ps1。此脚本需要通过传递给脚本的$ dte或$ project变量来访问Visual Studio对象模型,以找到正确的类方法并向其中插入代码。与使用WebActivator相比,PowerShell脚本路由是一个合理的工作量。 –

+0

另一件需要注意的事情 - .NET 4.0之前的框架版本不支持WebActivator。 – NightOwl888

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正在转义\ n或输入(CR)。

如果需要引号字符"可以转义为`"以及

0

万一有人看到这个线程,我想指出的是,由于2.5版发布四月,2013的NuGet将覆盖内容的文件。如果使用GUI,安装过程将检测到问题并提示是否覆盖该文件。新选项-FileConflictAction允许设置默认值。

参阅发行说明: http://docs.nuget.org/docs/release-notes/nuget-2.5