-
Notifications
You must be signed in to change notification settings - Fork 81
/
EntityContext.tt
143 lines (120 loc) · 4.76 KB
/
EntityContext.tt
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ CleanupBehavior processor="T4VSHost" CleanupAfterProcessingtemplate="true" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="EnvDTE" #>
<#
// set this to either "CSharp" or "VisualBasic"
var language = "CSharp";
// you can move the .tt file to a different folder in your project to have the entity context's namespace change accordingly
// or, you could just set it manually here
var contextNamespace = this.Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint") ?? "Default.Namespace";
var solutionPath = this.DTE.Solution.FileName;
this.SetFileExtensionForLanguage(language);
this.HookAssemblyResolution();
var generator = Type.GetType("BrightstarDB.CodeGeneration.Generator, BrightstarDB.CodeGeneration");
var generateMethod = generator.GetMethod("Generate");
var result = (string)generateMethod.Invoke(null, new[] { language, solutionPath, contextNamespace, "EntityContext", null, null, null });
this.WriteLine(result);
#>
<#+
private DTE dte;
private DTE DTE
{
get
{
if (this.dte == null)
{
var hostServiceProvider = (IServiceProvider)this.Host;
this.dte = (DTE)hostServiceProvider.GetService(typeof(DTE));
}
return this.dte;
}
}
private void SetFileExtensionForLanguage(string language)
{
switch (language)
{
case "CSharp":
this.Host.SetFileExtension(".g.cs");
break;
case "VisualBasic":
this.Host.SetFileExtension(".g.vb");
break;
default:
this.Host.SetFileExtension(".g");
break;
}
}
private void HookAssemblyResolution()
{
var templateDirectory = Path.GetDirectoryName(this.Host.TemplateFile);
var currentDirectory = templateDirectory;
string packagesDirectory = null;
while (Path.GetPathRoot(currentDirectory) != currentDirectory)
{
var packagesDirectories = Directory.GetDirectories(currentDirectory, "packages");
if (packagesDirectories.Length == 0)
{
currentDirectory = Path.GetDirectoryName(currentDirectory);
continue;
}
else if (packagesDirectories.Length > 1)
{
throw new InvalidOperationException("More than one packages directory detected - aborting.");
}
else
{
packagesDirectory = packagesDirectories[0];
break;
}
}
if (packagesDirectory == null)
{
throw new InvalidOperationException("Could not find packages directory relative to the template's directory (" + templateDirectory + ").");
}
this.Debug("packages directory: {0}", packagesDirectory);
var packageDirectories = Directory.GetDirectories(packagesDirectory, "BrightstarDB.CodeGeneration.T4.*");
if (packageDirectories.Length == 0)
{
throw new InvalidOperationException("Could not find BrightstarDB.CodeGeneration.T4 package in packages directory (" + packagesDirectory + ").");
}
else if (packageDirectories.Length > 1)
{
throw new InvalidOperationException("More than 1 version of the BrightstarDB.CodeGeneration.T4 package was found in the packages directory (" + packagesDirectory + "). You must choose 1 version and remove the others.");
}
var packageDirectory = packageDirectories.First();
this.Debug("package directory: {0}", packageDirectory);
var assembliesDirectory = Path.Combine(packageDirectory, "tools");
this.Debug("assemblies directory: {0}", assembliesDirectory);
if (!Directory.Exists(assembliesDirectory))
{
throw new InvalidOperationException("Package directory " + packageDirectory + " was found but no 'tools' subdirectory was found within.");
}
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
{
this.Debug("resolve: {0}", e.Name);
var matches = Directory.GetFiles(assembliesDirectory, e.Name + ".dll", SearchOption.TopDirectoryOnly);
if (matches.Length == 0)
{
this.Debug("Failed to find assembly in assemblies directory ({0}) named '{1}'.", assembliesDirectory, e.Name);
return null;
}
else if (matches.Length > 1)
{
this.Debug("Found more than one potential match for '{0}' in assemblies directory ({0}). Bailing.", e.Name, assembliesDirectory);
return null;
}
var match = matches.First();
this.Debug("match: {0}", match);
return Assembly.LoadFrom(match);
};
}
private void Debug(string format, params object[] args)
{
//this.WriteLine("// " + string.Format(format, args));
}
#>