用于修改 Global.asax.cs 的 Nuget PowerShell 脚本 [英] Nuget PowerShell script to modify Global.asax.cs

查看:11
本文介绍了用于修改 Global.asax.cs 的 Nuget PowerShell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a nuget package for my company MVC4 template. I have run into an issue where I need the Global.asax.cs to be modified to add these two lines:

using System.Web.Optimization;

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

at the top, before the namespace and

BundleConfig.RegisterBundles(BundleTable.Bundles);

Application_Start() 方法的末尾

我尝试创建一个 Global.asax.cs.pp,其中包含 namespace $rootnamespace$,但这似乎不起作用.似乎 Nuget 不会覆盖现有文件?

I tried creating a Global.asax.cs.pp with namespace $rootnamespace$ inside it, but that doesn't seem to work. It seems Nuget wont overwrite existing files?

在我看来,我的最后一个选择是编写一个 powershell 脚本 (Install.ps1?) 来执行此操作.这是我的 template.nuspec

My last option, as I see it, is to write a powershell script (Install.ps1?) to do this. Here's 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="TemplateHelpers*" target="contentHelpers" />
    <file src="TemplateContent*.css" target="contentContent" />
    <file src="TemplateContentanimGif*" target="contentContentanimGif" />
    <file src="TemplateContentcustom-theme*" target="contentContentcustom-theme" />
    <file src="TemplateContent	emplateImages*" target="contentContent	emplateImages" />
    <file src="TemplateScripts*" target="contentScripts" />
    <file src="TemplateViewsShared*" target="contentViewsShared" />
    <file src="TemplateViewsHome*" target="contentViewsHome" />
    <file src="TemplateViews\_ViewStart.cshtml" target="contentViews" />
    <file src="NuGetPackageApp_Start*" target="contentApp_Start" />
    <file src="NuGetPackageControllers*" target="contentControllers" />
    <file src="NuGetPackageHelpers*" target="contentHelpers" />
    <file src="NuGetPackageModels*" target="contentModels" />
    <file src="NuGetPackageViews*" target="contentViews" />
    <file src="NuGetPackage*" target="content" />
  </files>
</package>

我的问题是 2 倍,1) 我在 .nuspec 中做错了吗?和 2)如果用 .pp 修改 Global.asax 不是一个选项,那么当将此 nuget 添加到项目,我是否需要为它运行做任何特别的事情(阅读文档似乎只是将 Install.ps1 放在 tools 中就可以了)?

My question is 2 fold, 1) am I doing something wrong in my .nuspec? and 2) if modifying a Global.asax with .pp isn't an option, then what is the powershell script that I'd need to write to have this run automatically when this nuget is added to a project, and do I need to do anything special for it to run (reading the docs seems like just placing Install.ps1 in tools will do)?

推荐答案

如果有人需要,这里有答案,这在 Tools 中的 Install.ps1 中文件夹:

Here's the answer in case someone needs it, this goes 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)
}

这篇关于用于修改 Global.asax.cs 的 Nuget PowerShell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆