テキスト テンプレートを処理する独自のホストを作成できます。基本的なカスタム ホストを チュートリアル: カスタム テキスト テンプレート ホストの作成 に表示されます。複数の出力ファイルの生成などの関数を追加するためにホストを拡張できます。
このチュートリアルではディレクティブ プロセッサを呼び出すテキスト テンプレートをサポートするカスタム ホストを展開します。ドメイン固有言語を定義する場合はドメイン モデルの ディレクティブ プロセッサが 生成されます。ディレクティブ プロセッサはユーザーがテンプレートのアセンブリとインポート ディレクティブを記述する必要性が低くモデルにアクセスするテンプレートを作成できるようにが簡単になります。
注意 |
|---|
このチュートリアルでは チュートリアル: カスタム テキスト テンプレート ホストの作成 に基づいています。このチュートリアル 1 番目のを実行します。 |
このチュートリアルでは次のタスクが含まれています :
ドメイン モデルに基づくディレクティブ プロセッサを生成する ドメイン固有言語ツール を使用します。
生成されたディレクティブ プロセッサに対するカスタム テキスト テンプレート ホストの接続。
生成されたディレクティブ プロセッサを搭載したカスタム ホストのテスト。
必須コンポーネント
DSL を定義するには次のコンポーネントがインストールされている必要があります :
Visual Studio |
|
Visual Studio SDK |
|
Visual Studio の Visualization and Modeling SDK |
またチュートリアル: カスタム テキスト テンプレート ホストの作成 で作成したカスタム テキスト テンプレート変換が必要です。
ディレクティブ プロセッサを生成するドメイン固有言語ツールを使用します。
このチュートリアルではソリューション DSLMinimalTest のドメイン固有言語 (DSL) を作成するためにドメイン固有言語デザイナー ウィザードを使用します。
ドメイン固有言語ツールはドメイン モデルに基づくディレクティブ プロセッサを生成するには
次の特性を持つドメイン固有言語のソリューションを作成します :
名前 : DSLMinimalTest
ソリューション テンプレート : 最小限の言語
ファイル拡張子 : 最小値
会社名 : Fabrikam
ドメイン固有言語のソリューションを作成する方法の詳細については方法: ドメイン固有言語ソリューションを作成する を参照してください。
[ビルド] メニューの [ソリューションのビルド] をクリックします。
重要この手順ではディレクティブ プロセッサを生成しそのレジストリのキーを追加します。
[デバッグ] メニューの [デバッグ開始] をクリックします。
Visual Studio が表示されます。2 番目のインスタンス。
の実験用ビルドでは ソリューション エクスプローラー で各ファイル *** sample.min *** クリックします。
デザイナーでファイルが開きます。モデルを 2 要素および ExampleElement1 ExampleElement2 を持つと間のリンクすることに注意してください。
Visual Studio の 2 番目のインスタンスを閉じます。
ソリューションを保存しドメイン固有言語デザイナーを閉じます。
ディレクティブ プロセッサに対するカスタム テキスト テンプレート ホストの接続
ディレクティブ プロセッサを生成した後チュートリアル: カスタム テキスト テンプレート ホストの作成 で作成したカスタム テキスト テンプレート ホストとディレクティブ プロセッサに接続します。
カスタム テキスト テンプレート ホストの生成されたディレクティブ プロセッサに接続するには
CustomHost ソリューションを開きます。
[プロジェクト] メニューの [参照の追加] をクリックします。
ENT3ENT [表示] タブの [ENT2ENT] ダイアログ ボックスが表示されます。
以下の参照を追加します :
Microsoft.VisualStudio.Modeling.Sdk.11.0
Microsoft.VisualStudio.Modeling.Sdk.Diagrams.11.0
Microsoft.VisualStudio.TextTemplating.11.0
Microsoft.VisualStudio.TextTemplating.Interfaces.11.0
Microsoft.VisualStudio.TextTemplating.Modeling.11.0
Microsoft.VisualStudio.TextTemplating.VSHost.11.0
Program.cs ファイルまたは Module1.vb の先頭に次のコード行を追加します :
using Microsoft.Win32;Imports Microsoft.Win32プロパティ StandardAssemblyReferences のコードを探し次のコードに置き換えます。:
[!メモ]
この手順ではホストはサポートされます。生成されたディレクティブ プロセッサに必要となるアセンブリへの参照を追加します。
//the host can provide standard assembly references //the engine will use these references when compiling and //executing the generated transformation class //-------------------------------------------------------------- public IList<string> StandardAssemblyReferences { get { return new string[] { //if this host searches standard paths and the GAC //we can specify the assembly name like this: //"System" //since this host only resolves assemblies from the //fully qualified path and name of the assembly //this is a quick way to get the code to give us the //fully qualified path and name of the System assembly //--------------------------------------------------------- typeof(System.Uri).Assembly.Location, typeof(System.Uri).Assembly.Location, typeof(Microsoft.VisualStudio.Modeling.ModelElement).Assembly.Location, typeof(Microsoft.VisualStudio.Modeling.Diagrams.BinaryLinkShape).Assembly.Location, typeof(Microsoft.VisualStudio.TextTemplating.VSHost.ITextTemplating).Assembly.Location, typeof(Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation).Assembly.Location }; } }関数 ResolveDirectiveProcessor のコードを探し次のコードに置き換えます。:
重要このコードは接続先の生成されたディレクティブ プロセッサの名前にハード コーディングされた参照が含まれます。レジストリに表示されるすべてのディレクティブ プロセッサを検索する場合は一致を検索するよりも一般にすることができます。この場合ホストは生成されたディレクティブ プロセッサを使用します。
//the engine calls this method based on the directives the user has //specified it in the text template //this method can be called 0, 1, or more times //--------------------------------------------------------------------- public Type ResolveDirectiveProcessor(string processorName) { //check the processor name, and if it is the name of the processor the //host wants to support, return the type of the processor //--------------------------------------------------------------------- if (string.Compare(processorName, "DSLMinimalTestDirectiveProcessor", StringComparison.InvariantCultureIgnoreCase) == 0) { try { string keyName = @"Software\Microsoft\VisualStudio\10.0Exp_Config\TextTemplating\DirectiveProcessors\DSLMinimalTestDirectiveProcessor"; using (RegistryKey specificKey = Registry.CurrentUser.OpenSubKey(keyName)) { if (specificKey != null) { List<string> names = new List<String>(specificKey.GetValueNames()); string classValue = specificKey.GetValue("Class") as string; if (!string.IsNullOrEmpty(classValue)) { string loadValue = string.Empty; System.Reflection.Assembly processorAssembly = null; if (names.Contains("Assembly")) { loadValue = specificKey.GetValue("Assembly") as string; if (!string.IsNullOrEmpty(loadValue)) { //the assembly must be installed in the GAC processorAssembly = System.Reflection.Assembly.Load(loadValue); } } else if (names.Contains("CodeBase")) { loadValue = specificKey.GetValue("CodeBase") as string; if (!string.IsNullOrEmpty(loadValue)) { //loading local assembly processorAssembly = System.Reflection.Assembly.LoadFrom(loadValue); } } if (processorAssembly == null) { throw new Exception("Directive Processor not found"); } Type processorType = processorAssembly.GetType(classValue); if (processorType == null) { throw new Exception("Directive Processor not found"); } return processorType; } } } } catch (Exception e) { //if the directive processor can not be found, throw an error throw new Exception("Directive Processor not found"); } } //if the directive processor is not one this host wants to support throw new Exception("Directive Processor not supported"); }[ファイル] メニューの [すべてを保存] をクリックします。
[ビルド] メニューの [ソリューションのビルド] をクリックします。
ディレクティブ プロセッサが搭載されたカスタム ホストのテスト
カスタム テキスト テンプレート ホストをテストするには最初に生成されたディレクティブ プロセッサを呼び出すテキスト テンプレートを記述する必要があります。次にカスタム ホストを実行しテキスト テンプレートの名前を渡しディレクティブが正しく処理されるようにします。
テキスト テンプレートを作成してカスタム ホストをテストするには
テキスト ファイルを作成しTestTemplateWithDP.tt という名前を付けます。ファイルを作成するときにテキスト エディター (メモ帳など) を使用できます。
次の内容をテキスト ファイルに追加します。
[!メモ]
テキスト テンプレートのプログラミング言語はカスタム ホストのプログラミング言語と同じでなくてもかまいません。
Text Template Host Test <#@ template debug="true" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #> <# //this is the call to the examplemodel directive in the generated directive processor #> <#@ DSLMinimalTest processor="DSLMinimalTestDirectiveProcessor" requires="fileName='<Your Path>\Sample.min'" provides="ExampleModel=ExampleModel" #> <# //uncomment this line to test that the host allows the engine to set the extension #> <# //@ output extension=".htm" #> <# //uncomment this line if you want to see the generated transformation class #> <# //System.Diagnostics.Debugger.Break(); #> <# //this code uses the results of the examplemodel directive #> <# foreach ( ExampleElement box in this.ExampleModel.Elements ) { WriteLine("Box: {0}", box.Name); foreach (ExampleElement linkedTo in box.Targets) { WriteLine("Linked to: {0}", linkedTo.Name); } foreach (ExampleElement linkedFrom in box.Sources) { WriteLine("Linked from: {0}", linkedFrom.Name); } WriteLine(""); } #>Text Template Host Test <#@ template debug="true" language="VB" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #> <# 'this is the call to the examplemodel directive in the generated directive processor #> <#@ DSLMinimalTest processor="DSLMinimalTestDirectiveProcessor" requires="fileName='<Your Path>\Sample.min'" provides="ExampleModel=ExampleModel" #> <# 'Uncomment this line to test that the host allows the engine to set the extension. #> <# '@ output extension=".htm" #> <# 'Uncomment this line if you want to see the generated transformation class. #> <# 'System.Diagnostics.Debugger.Break() #> <# 'this code uses the results of the examplemodel directive #> <# For Each box as ExampleElement In Me.ExampleModel.Elements WriteLine("Box: {0}", box.Name) For Each LinkedTo as ExampleElement In box.Targets WriteLine("Linked to: {0}", LinkedTo.Name) Next For Each LinkedFrom as ExampleElement In box.Sources WriteLine("Linked from: {0}", LinkedFrom.Name) Next WriteLine("") Next #>コードでは<YOUR PATH> 最初の手順で作成したデザイン固有の言語から Sample.min ファイルのパスに置き換えます。
ファイルを保存して閉じます。
カスタム ホストをテストするには
コマンド プロンプト ウィンドウを開きます。
カスタム ホストの実行可能ファイルのパスを入力します。ただし、Enter キーはまだ押さないでください。
たとえば、次のように入力します。
<YOUR PATH>CustomHost\bin\Debug\CustomHost.exe
[!メモ]
アドレスを入力する代わりにWindows エクスプローラー (X)CustomHost.exe ファイルを参照しコマンド プロンプト ウィンドウにファイルをドラッグします。
空白を入力します。
テキスト テンプレート ファイルのパスを入力し、Enter キーを押します。
たとえば、次のように入力します。
<YOUR PATH>TestTemplateWithDP.txt
[!メモ]
アドレスを入力する代わりにWindows エクスプローラー (X) の TestTemplateWithDP.txt ファイルを参照しコマンド プロンプト ウィンドウにファイルをドラッグします。
カスタム ホスト アプリケーションはテキスト テンプレート変換プロセスを実行して開始します。
Windows エクスプローラー (X) ではTestTemplateWithDP.txt ファイルを含むフォルダーに移動します。
フォルダーはファイル TestTemplateWithDP1.txt が含まれています。
このファイルを開き、テキスト テンプレート変換の結果を確認します。
生成されたテキスト出力結果が表示され次のようになります。:
Text Template Host Test Box: ExampleElement1 Linked to: ExampleElement2 Box: ExampleElement2 Linked from: ExampleElement1