5

会社の MVC4 テンプレート用の nuget パッケージを作成しています。Global.asax.cs次の 2 行を追加するために を変更する必要があるという問題に遭遇しました。

using System.Web.Optimization;

上部の名前空間の前と

BundleConfig.RegisterBundles(BundleTable.Bundles);

Application_Start()メソッドの最後に

Global.asax.cs.ppその中にwithを作成しようとしました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) .pp で aGlobal.asaxを変更することができない場合、この nuget がプロジェクトに追加されたときにこれを自動的に実行するために作成する必要のある powershell スクリプトは何ですか? また、何か特別なことをする必要がありますか?それを実行するには(ドキュメントを読むだけでよいようInstall.ps1ですtools\)?

4

4 に答える 4

7

誰かがそれを必要とする場合の答えは次のとおりです。これはフォルダーInstall.ps1内にあります。Tools

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 は、global.asax を変更せずにアプリケーションにスタートアップ コードとシャットダウン コードを追加するために使用できる NuGet パッケージです。

バンドルの登録が最後に行われるように、おそらく PostApplicationStartMethod 属性を使用することをお勧めします。

于 2012-11-06T10:54:17.823 に答える
0

誰かがこのスレッドを見た場合に備えて、2013 年 4 月にリリースされたバージョン 2.5 の時点で、Nuget がコンテンツ ファイルを上書きすることを指摘したいと思います。GUI を使用している場合、インストール プロセスは問題を検出し、ファイルを上書きするかどうかを確認します。新しいオプション -FileConflictAction を使用すると、デフォルト値を設定できます。

リリース ノートを参照してください: http://docs.nuget.org/docs/release-notes/nuget-2.5

于 2014-06-24T14:32:01.327 に答える
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 に答える