-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseCodeGeneratorWithSite.cs
198 lines (178 loc) · 7.16 KB
/
BaseCodeGeneratorWithSite.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
/***************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
***************************************************************************/
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using VSOLE = Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Designer.Interfaces;
namespace Knacka.Se.ProtobufGenerator
{
/// <summary>
/// Base code generator with site implementation
/// </summary>
public abstract class BaseCodeGeneratorWithSite : BaseCodeGenerator, VSOLE.IObjectWithSite
{
private object site = null;
private CodeDomProvider codeDomProvider = null;
private ServiceProvider serviceProvider = null;
#region IObjectWithSite Members
/// <summary>
/// GetSite method of IOleObjectWithSite
/// </summary>
/// <param name="riid">interface to get</param>
/// <param name="ppvSite">IntPtr in which to stuff return value</param>
void VSOLE.IObjectWithSite.GetSite(ref Guid riid, out IntPtr ppvSite)
{
if (site == null)
{
throw new COMException("object is not sited", VSConstants.E_FAIL);
}
IntPtr pUnknownPointer = Marshal.GetIUnknownForObject(site);
IntPtr 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 VSOLE.IObjectWithSite.SetSite(object pUnkSite)
{
site = pUnkSite;
codeDomProvider = null;
serviceProvider = null;
}
#endregion
/// <summary>
/// Demand-creates a ServiceProvider
/// </summary>
private ServiceProvider SiteServiceProvider
{
get
{
if (serviceProvider == null)
{
serviceProvider = new ServiceProvider(site as VSOLE.IServiceProvider);
Debug.Assert(serviceProvider != null, "Unable to get ServiceProvider from site object.");
}
return serviceProvider;
}
}
/// <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
IVSMDCodeDomProvider 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()
{
CodeDomProvider codeDom = GetCodeProvider();
Debug.Assert(codeDom != null, "CodeDomProvider is NULL.");
string 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()
{
object 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;
}
}
}