-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathT4Helper.ttinclude
More file actions
101 lines (89 loc) · 3.58 KB
/
T4Helper.ttinclude
File metadata and controls
101 lines (89 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly Name="EnvDTE" #>
<#@ Import Namespace="EnvDTE" #>
<#@ Import Namespace="System.IO" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="EnvDTE" #>
<#+
//Generating Seperate Files
public void ProcessContent(string outputFileName, string content)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
string outputDirectoryPath = Path.GetDirectoryName(outputFilePath);
if(!Directory.Exists(outputDirectoryPath))
{
Directory.CreateDirectory(outputDirectoryPath);
}
File.WriteAllText(outputFilePath, content);
IServiceProvider hostServiceProvider = (IServiceProvider)Host;
EnvDTE.DTE dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
ProjectItem containingProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
containingProjectItem.ProjectItems.AddFromFile(outputFilePath);
}
public void CreateFile(string fileName)
{
ProcessContent(fileName, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
//Get Projects
public Project GetActiveProject(DTE dte)
{
Project activeProject = null;
Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
{
activeProject = activeSolutionProjects.GetValue(0) as Project;
}
return activeProject;
}
//
public List<CodeClass> FindClasses(Project project, string ns, string className)
{
List<CodeClass> result = new List<CodeClass>();
FindClasses(project.CodeModel.CodeElements, className, ns, result, false);
return result;
}
private void FindClasses(CodeElements elements, string className, string searchNamespace, List<CodeClass> result, bool isNamespaceOk)
{
if (elements == null) return;
foreach (CodeElement element in elements)
{
if (element is CodeNamespace)
{
CodeNamespace ns = element as CodeNamespace;
if (ns != null)
{
if (ns.FullName == searchNamespace)
FindClasses(ns.Members, className, searchNamespace, result, true);
else
FindClasses(ns.Members, className, searchNamespace, result, false);
}
}
else if (element is CodeClass && isNamespaceOk)
{
CodeClass c = element as CodeClass;
if (c != null)
{
if (c.FullName.Contains(className))
result.Add(c);
FindClasses(c.Members, className, searchNamespace, result, true);
}
}
}
}
//Naming
public string GetProperClassName(string className)
{
string returnString = className.ToLower();
returnString = returnString.Replace("_", " ");
TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
returnString = ti.ToTitleCase(returnString);
returnString = returnString.Replace(" ", "");
return returnString;
}
private static string CharToUpper(string input, int position)
{
return input.First().ToString().ToUpper() + input.Substring(position+1);
}
#>