T4 loading files to the solution

Source: Internet
Author: User

Use EnvDTE add/remove multiple files to ProjectbyAdmin | décembre Un Commentaire

Project Source

Source:envdtemanipulate
Licence:mit

EnvDTE is a assembly-wrapped COM library containing the objects and members for Visual Studio core automation.

It ' s widely used in Visual Studio Plugins to provide ability manipulate Visual studio functionalities.

By using the EnvDTE inside texttemplate, we can create a easy solution for massive file creation.

Usage

First you need include EnvDTE to Texttemplate

<#@ Assembly Name= "EnvDTE" #><#@ import namespace= "EnvDTE" #>

Also, need to set the hostspecific to «true».

<#@ template debug= "false" hostspecific= "true" language= "C #" #>

This would allow your to use itexttemplateenginehost instance (Host), which allows your to access current Texttemplat e instance.

Then using following code to obtain an instance of EnvDTE.DTE

var host = this. Host; Itexttemplateenginehostvar serviceprovider = (IServiceProvider) host; Convert Host to Iserviceprovidervar DTE = (envdte.dte) serviceprovider.getservice (typeof (EnvDTE.DTE)); Using IServiceProvider to get instance for EnvDTE
Get Current Solution
<summary>///Get Current Solution instance///Http://msdn.microsoft.com/en-us/library/envdte.solution_ Members (v=vs.90). Aspx///</summary>envdte.solution getcurrentsolution (DTE DTE) {return DTE. Solution;}
Get project item of the text template file
<summary>///Get Project Item of this text template file///http://msdn.microsoft.com/en-us/library/ Envdte.projectitem_members (v=vs.90). Aspx///</summary>envdte.projectitem Getprojectitemofthistexttemplate ( DTE DTE) {return DTE. Solution.findprojectitem (this. Host.templatefile);}
Get Current Project
EnvDTE.Project Getcurrentproject (DTE DTE) {return getprojectitemofthistexttemplate (DTE). ContainingProject;}
ADD subitem under Project text template
void Addsubitem (DTE DTE, string path) {var item = getprojectitemofthistexttemplate (DTE);//item. Collection//http://msdn.microsoft.com/fr-fr/library/envdte.projectitems_members (v=vs.90). aspx//Using Add from Template to add Sub item into Text Templateitem.ProjectItems.AddFromTemplate (this. Host.templatefile, path);}
Clear all subitems under the current text template file
void Cleartexttemplategenerateditems (DTE DTE, BOOL Deletediskfile = False) {var item = getprojectitemofthistexttemplate ( DTE);//item. Collection only provides insertion Functions.foreach (ProjectItem subitem in item. ProjectItems) {file.delete (subitem.filenames[1]); Subitem.delete ();}}
Example

Create class file and SQL file from XML file:Models.xml

<?xml version= "1.0" encoding= "Utf-8"? ><models><model name= "MyUser" ><property name= "FirstName" Type= "string" ></property><property name= "LastName" type= "string" ></Property></Model> <model name= "MyGroup" ><property name= "name" type= "string" ></property></model></models >

Create models for this XML structure:

Class Model {public string name {get; private set;} Public Ireadonlylist Properties {get; private set;} Public Model (XmlNode node) {if (node. attributes["name"]!=null) name = node. attributes["Name"]. Value;var properties = new List (); foreach (XmlNode subnode in node. SelectNodes ("property")) {properties. ADD (new Property (subnode));} This.properties = properties;}} Class Property{public string name {get; private set;} public string type {get; private set;} Public Property (XmlNode node) {if (node. attributes["name"]!=null) name = node. attributes["Name"]. Value;if (node. attributes["type"]!=null) type = node. attributes["type"]. Value;} public string Getsqltype () {if (type== "string") return "nvarchar"; return "ERROR:" +type+ "";}}

Using XmlDocument class load XML file:

XmlDocument doc = new XmlDocument ();d OC. Load (this. Host.resolvepath ("Models.xml")); This.cleartexttemplategenerateditems (DTE, TRUE); List models = new list (); foreach (XmlNode node in Doc. SelectNodes ("/models/model")) Models. ADD (New Model (node));

Then generate class file from Models

<summary>///Generate CSharp class files///</summary>void Generateclassfile (DTE DTE, IEnumerable models {foreach (Model m in models) {#>public class <#= m.name #> {<#+ foreach (var p in m.properties) {  #>publ IC <#= p.type #> <#= p.name #> {get; set;} <#+} #>}<#+var FilePath = Host.resolvepath ("") + "\ \" + M.name + ". cs"; Attention, file path must use "\" not "/" using (StreamWriter w = file.createtext (FilePath)) {//write The code generated T o Filew. WriteLine (this. Generationenvironment.tostring ());//empty the code generatedthis. Generationenvironment.remove (0, this. Generationenvironment.length);} ADD Item to Solutionthis.addprojectitem (Dte,filepath);}}

and generate SQL files from Models

<summary>///Generate SQL files///</summary>void generatesqlfile (DTE DTE, IEnumerable models) {foreach ( Model m in models) {#>create TABLE <#= m.name #> (<#+ foreach (var p in m.properties) {  #><#= p.name # > <#= p.getsqltype () #><#+} #>) <#+var FilePath = Host.resolvepath ("") + "\ \" + M.name + ". sql"; Attention, file path must use "\" not "/" using (StreamWriter w = file.createtext (FilePath)) {//write The code generated T o Filew. WriteLine (this. Generationenvironment.tostring ());//empty the code generatedthis. Generationenvironment.remove (0, this. Generationenvironment.length);} ADD Item to Solutionthis.addprojectitem (Dte,filepath);}}


Transferred from: http://shengjieinsight.com/blog/2013/12/use-envdte-addremove-multiple-files-to-project/

T4 loading files to the solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.