Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Anda dapat menulis host Anda sendiri yang memproses templat teks. Host kustom dasar ditunjukkan dalam Panduan: Membuat Host Templat Teks Kustom. Anda dapat memperluas host tersebut untuk menambahkan fungsi seperti menghasilkan beberapa file output.
Dalam panduan ini, Anda memperluas host kustom Anda sehingga mendukung templat teks yang memanggil prosesor direktif. Saat Anda menentukan bahasa khusus domain, ia menghasilkan prosesor direktif untuk model domain. Prosesor direktif memudahkan pengguna untuk menulis templat yang mengakses model, mengurangi kebutuhan untuk menulis assembly dan mengimpor direktif dalam templat.
Catatan
Panduan ini dibangun di Panduan : Membuat Host Templat Teks Kustom. Lakukan penelusuran itu terlebih dahulu.
Panduan ini mencakup tugas-tugas berikut:
Menggunakan Alat Bahasa Khusus Domain untuk menghasilkan prosesor direktif yang didasarkan pada model domain.
Koneksi host templat teks kustom ke prosesor direktif yang dihasilkan.
Menguji host kustom dengan prosesor direktif yang dihasilkan.
Prasyarat
Untuk mendefinisikan bahasa khusus domain, Anda harus menginstal komponen berikut:
| Komponen | Tautan |
|---|---|
| Visual Studio | http://go.microsoft.com/fwlink/?LinkId=185579 |
| SDK Visual Studio | http://go.microsoft.com/fwlink/?LinkId=185580 |
| Visual Studio Visualisasi dan Pemodelan SDK |
Catatan
Komponen Transformasi Template Teks secara otomatis diinstal sebagai bagian dari beban kerja pengembangan ekstensi Visual Studio. Anda juga dapat menginstalnya dari tab Komponen individual Alat Penginstal Visual Studio, di bawah kategori SDK, pustaka, dan kerangka kerja. Instal komponen SDK Pemodelan dari tab Komponen individual.
Selain itu, Anda harus membuat transformasi templat teks kustom di Panduan: Membuat Host Templat Teks Kustom.
Menggunakan Alat Bahasa Khusus Domain untuk Menghasilkan Prosesor Direktif
Dalam panduan ini, Anda menggunakan Wizard Desainer Bahasa Khusus Domain untuk membuat bahasa khusus domain untuk solusi DSLMinimalTest.
Buat solusi bahasa khusus domain yang memiliki karakteristik berikut:
Nama: DSLMinimalTest
Templat solusi: Bahasa Minimal
Ekstensi file: min
Nama perusahaan: Fabrikam
Untuk informasi selengkapnya tentang membuat solusi bahasa khusus domain, lihat Petunjuk: Membuat Solusi Bahasa Khusus Domain.
Pada menu Build, klik Solusi Build.
Penting
Langkah ini menghasilkan prosesor direktif dan menambahkan kunci untuk itu di registri.
Pada menu Debug, klik Mulai Penelusuran Kesalahan.
Instans kedua Visual Studio terbuka.
Dalam build eksperimental, di Penjelajah Solusi, klik dua kali file sample.min.
File terbuka di perancang. Perhatikan bahwa model memiliki dua elemen, ExampleElement1 dan ExampleElement2, dan tautan di antaranya.
Tutup instans kedua Visual Studio.
Simpan solusi, lalu tutup Perancang Bahasa Khusus Domain.
Koneksi Host Templat Teks Kustom ke Prosesor Direktif
Setelah Anda membuat prosesor direktif, Anda menyambungkan prosesor direktif dan host templat teks kustom yang Anda buat di Panduan: Membuat Host Templat Teks Kustom.
Buka solusi CustomHost.
Pada menu Proyek, klik Tambahkan Referensi.
Kotak dialog Tambahkan Referensi terbuka dengan tab .NET ditampilkan.
Tambahkan referensi berikut:
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
Di bagian atas Program.cs atau Module1.vb, tambahkan baris kode berikut:
Temukan kode untuk properti
StandardAssemblyReferences, dan ganti dengan kode berikut:Catatan
Dalam langkah ini, Anda menambahkan referensi ke rakitan yang diperlukan oleh prosesor direktif yang dihasilkan yang akan didukung host Anda.
//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 }; } }Temukan kode untuk fungsi
ResolveDirectiveProcessor, dan ganti dengan kode berikut:Penting
Kode ini berisi referensi yang dikodekan secara permanen ke nama prosesor direktif yang dihasilkan yang ingin Anda sambungkan. Anda dapat dengan mudah membuat ini lebih umum, dalam hal ini mencari semua prosesor direktif yang tercantum dalam registri dan mencoba menemukan kecocokan. Dalam hal ini, host akan bekerja dengan prosesor direktif yang dihasilkan.
//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"); }Pada menu Berkas, klik Simpan Semua.
Pada menu Build, klik Solusi Build.
Menguji Host Kustom dengan Prosesor Direktif
Untuk menguji host templat teks kustom, pertama-tama Anda harus menulis templat teks yang memanggil prosesor direktif yang dihasilkan. Kemudian Anda menjalankan host kustom, meneruskan nama templat teks, dan memverifikasi bahwa arahan diproses dengan benar.
Membuat templat teks untuk menguji host kustom
Buat file teks, dan beri
TestTemplateWithDP.ttnama. Anda dapat menggunakan editor teks apa pun, seperti Notepad, untuk membuat file.Tambahkan yang berikut ini ke file teks:
Catatan
Bahasa pemrograman templat teks tidak perlu cocok dengan host kustom.
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(""); } #>Dalam kode, ganti <JALUR> ANDA dengan jalur file Sample.min dari bahasa khusus desain yang Anda buat di prosedur pertama.
Simpan dan tutup file.
Menguji host kustom
Buka jendela Prompt Perintah.
Ketik jalur file yang dapat dieksekusi untuk host kustom, tetapi jangan tekan ENTER.
Misalnya, ketik:
<YOUR PATH>CustomHost\bin\Debug\CustomHost.exeCatatan
Alih-alih mengetik alamat, Anda dapat menelusuri ke file CustomHost.exe di Windows Explorer, lalu menyeret file ke jendela Prompt Perintah.
Ketik spasi.
Ketik jalur file templat teks, lalu tekan ENTER.
Misalnya, ketik:
<YOUR PATH>TestTemplateWithDP.txtCatatan
Alih-alih mengetik alamat, Anda dapat menelusuri ke file TestTemplateWithDP.txt di Windows Explorer, lalu menyeret file ke jendela Prompt Perintah.
Aplikasi host kustom berjalan dan memulai proses transformasi templat teks.
Di Windows Explorer, telusuri ke folder yang berisi file TestTemplateWithDP.txt.
Folder juga berisi file TestTemplateWithDP1.txt.
Buka file ini untuk melihat hasil transformasi templat teks.
Hasil output teks yang dihasilkan muncul dan akan terlihat seperti ini:
Text Template Host Test Box: ExampleElement1 Linked to: ExampleElement2 Box: ExampleElement2 Linked from: ExampleElement1