This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseGeneratorWithSite.cs
201 lines (176 loc) · 8.11 KB
/
BaseGeneratorWithSite.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//-----------------------------------------------------------------------
// <copyright company="CoApp Project">
// Copyright (c) 2011 Garrett Serack. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CoApp.AnyGen {
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using EnvDTE;
using Microsoft.CSharp;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Designer.Interfaces;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using VSLangProj;
using CodeNamespace = System.CodeDom.CodeNamespace;
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
/// <summary>
/// Base code generator with site implementation
/// </summary>
public abstract class BaseCodeGeneratorWithSite : BaseCodeGenerator, IObjectWithSite {
private CodeDomProvider _codeDomProvider;
private ServiceProvider _serviceProvider;
private object _site;
/// <summary>
/// Demand-creates a ServiceProvider
/// </summary>
private ServiceProvider SiteServiceProvider {
get {
if (_serviceProvider == null) {
_serviceProvider = new ServiceProvider(_site as IServiceProvider);
Debug.Assert(_serviceProvider != null, "Unable to get ServiceProvider from site object.");
}
return _serviceProvider;
}
}
/// <summary>
/// GetSite method of IOleObjectWithSite
/// </summary>
/// <param name = "riid">interface to get</param>
/// <param name = "ppvSite">IntPtr in which to stuff return value</param>
public void GetSite(ref Guid riid, out IntPtr ppvSite) {
if (_site == null) {
throw new COMException("object is not sited", VSConstants.E_FAIL);
}
var pUnknownPointer = Marshal.GetIUnknownForObject(_site);
var intPointer = IntPtr.Zero;
Marshal.QueryInterface(pUnknownPointer, ref riid, out intPointer);
if (intPointer == IntPtr.Zero) {
throw new COMException("site does not support requested interface", VSConstants.E_NOINTERFACE);
}
ppvSite = intPointer;
}
/// <summary>
/// SetSite method of IOleObjectWithSite
/// </summary>
/// <param name = "pUnkSite">site for this object to use</param>
void IObjectWithSite.SetSite(object pUnkSite) {
_site = pUnkSite;
_codeDomProvider = null;
_serviceProvider = null;
}
/// <summary>
/// Method to get a service by its GUID
/// </summary>
/// <param name = "serviceGuid">GUID of service to retrieve</param>
/// <returns>An object that implements the requested service</returns>
protected object GetService(Guid serviceGuid) {
return SiteServiceProvider.GetService(serviceGuid);
}
/// <summary>
/// Method to get a service by its Type
/// </summary>
/// <param name = "serviceType">Type of service to retrieve</param>
/// <returns>An object that implements the requested service</returns>
protected object GetService(Type serviceType) {
return SiteServiceProvider.GetService(serviceType);
}
/// <summary>
/// Returns a CodeDomProvider object for the language of the project containing
/// the project item the generator was called on
/// </summary>
/// <returns>A CodeDomProvider object</returns>
protected virtual CodeDomProvider GetCodeProvider() {
if (_codeDomProvider == null) {
//Query for IVSMDCodeDomProvider/SVSMDCodeDomProvider for this project type
var provider = GetService(typeof (SVSMDCodeDomProvider)) as IVSMDCodeDomProvider;
if (provider != null) {
_codeDomProvider = provider.CodeDomProvider as CodeDomProvider;
}
else {
//In the case where no language specific CodeDom is available, fall back to C#
_codeDomProvider = CodeDomProvider.CreateProvider("C#");
}
}
return _codeDomProvider;
}
/// <summary>
/// Gets the default extension of the output file from the CodeDomProvider
/// </summary>
/// <returns></returns>
protected override string GetDefaultExtension() {
var codeDom = GetCodeProvider();
Debug.Assert(codeDom != null, "CodeDomProvider is NULL.");
var extension = codeDom.FileExtension;
if (extension != null && extension.Length > 0) {
extension = "." + extension.TrimStart(".".ToCharArray());
}
return extension;
}
/// <summary>
/// Returns the EnvDTE.ProjectItem object that corresponds to the project item the code
/// generator was called on
/// </summary>
/// <returns>The EnvDTE.ProjectItem of the project item the code generator was called on</returns>
protected ProjectItem GetProjectItem() {
var p = GetService(typeof (ProjectItem));
Debug.Assert(p != null, "Unable to get Project Item.");
return (ProjectItem) p;
}
/// <summary>
/// Returns the EnvDTE.Project object of the project containing the project item the code
/// generator was called on
/// </summary>
/// <returns>
/// The EnvDTE.Project object of the project containing the project item the code generator was called on
/// </returns>
protected Project GetProject() {
return GetProjectItem().ContainingProject;
}
/// <summary>
/// Returns the VSLangProj.VSProjectItem object that corresponds to the project item the code
/// generator was called on
/// </summary>
/// <returns>The VSLangProj.VSProjectItem of the project item the code generator was called on</returns>
protected VSProjectItem GetVSProjectItem() {
return (VSProjectItem) GetProjectItem().Object;
}
/// <summary>
/// Returns the VSLangProj.VSProject object of the project containing the project item the code
/// generator was called on
/// </summary>
/// <returns>
/// The VSLangProj.VSProject object of the project containing the project item
/// the code generator was called on
/// </returns>
protected VSProject GetVSProject() {
return (VSProject) GetProject().Object;
}
protected byte[] GenerateExceptionMessage(Exception ex) {
return GenerateMessage(ex.ToString());
}
protected byte[] GenerateMessage(string format, params object[] args ) {
var code = new CodeCompileUnit();
var codeNamespace = new CodeNamespace(FileNameSpace);
var codeProvider = new CSharpCodeProvider();
code.Namespaces.Add(codeNamespace);
codeNamespace.Comments.Add(new CodeCommentStatement(String.Format(format,args)));
var stringcode = WriteCode(codeProvider, code);
return Encoding.UTF8.GetBytes(stringcode);
}
protected static string WriteCode(CodeDomProvider pProvider, CodeCompileUnit code) {
var stringwriter = new StringWriter();
pProvider.GenerateCodeFromCompileUnit(code, stringwriter, null);
var stringcode = stringwriter.ToString();
stringwriter.Close();
return stringcode;
}
}
}