5

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

using System.Web.Optimization;

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

BundleConfig.RegisterBundles(BundleTable.Bundles);

Application_Start()方法结束时

我尝试在其中创建一个Global.asax.cs.ppwith 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>

我的问题是 2 倍,1)我在 .nuspec 中做错了吗?和2)如果Global.asax用.pp修改a不是一个选项,那么当这个nuget被添加到项目时,我需要编写什么powershell脚本才能自动运行,我需要做任何特别的事情吗让它运行(阅读文档似乎只是放入Install.ps1tools\可以了)?

4

4 回答 4

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)
}
于 2012-11-06T16:32:17.647 回答
5

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

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

于 2012-11-06T10:54:17.823 回答
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)。

如果您需要引号字符,它也"可以被转义`"

于 2013-03-26T12:49:46.320 回答
0

如果有人看到这个帖子,我想指出,从 2013 年 4 月发布的 2.5 版开始,Nuget 将覆盖内容文件。如果使用 GUI,安装过程将检测问题并提示是否覆盖文件。新选项 -FileConflictAction 允许设置默认值。

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

于 2014-06-24T14:32:01.327 回答