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

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

问题描述

我要建一个的NuGet包我公司MVC4模板。我遇到了,我需要一个问题的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 命名空间$ 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="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是不是一种选择,那么什么是我需要编写PowerShell脚本来自动当有此运行这是的NuGet添加到一个项目,我需要做什么特别的它运行(阅读文档好像只是把 Install.ps1 工具\\ 会做)?

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

推荐答案

下面是万一答案有人需要它,这正好在你的 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)
}

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

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