Datei: NDOPackage/BuildEventHandler.cs
Last Commit (4a7e8ab)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Globalization; |
25 | using System.Text.RegularExpressions; |
26 | using System.Xml; |
27 | using System.Collections; |
28 | using System.Windows.Forms; |
29 | using System.IO; |
30 | using System.Runtime.InteropServices; |
31 | using EnvDTE; |
32 | using EnvDTE80; |
33 | using VSLangProj; |
34 | using System.Collections.Generic; |
35 | using NDOEnhancer; |
36 | using System.Reflection; |
37 | |
38 | namespace NETDataObjects. NDOVSPackage |
39 | { |
40 | ····/// <summary> |
41 | ····/// Summary description for BuildEventHandler. |
42 | ····/// </summary> |
43 | ····internal class BuildEventHandler |
44 | ····{ |
45 | ········private MessageAdapter············messages = null; |
46 | ········private BuildEvents················buildEvents; |
47 | ········private _DTE····················m_applicationObject; |
48 | |
49 | public BuildEventHandler( _DTE applicationObject ) |
50 | ········{ |
51 | ············m_applicationObject = applicationObject; |
52 | ············ApplicationObject.VisualStudioApplication = applicationObject; |
53 | |
54 | Events events = applicationObject. Events; |
55 | ············buildEvents = events.BuildEvents; |
56 | |
57 | ············buildEvents.OnBuildBegin············+= OnBuildBegin; |
58 | ············buildEvents.OnBuildDone················+= OnBuildDone; |
59 | ············buildEvents.OnBuildProjConfigDone····+= OnBuildProjConfigDone; |
60 | //············buildEvents.OnBuildProjConfigBegin····+= OnBuildProjConfigBegin;··// If needed, activate this line and the according method |
61 | ········} |
62 | |
63 | ········public void OnBuildBegin( vsBuildScope scope, vsBuildAction action ) |
64 | ········{ |
65 | ············if (messages == null) |
66 | ············{ |
67 | ················messages = new MessageAdapter(); |
68 | ············} |
69 | ············messages.Success = true; |
70 | ········} |
71 | |
72 | ········public void OnBuildDone( vsBuildScope scope, vsBuildAction action ) |
73 | ········{ |
74 | ············if (!messages.Success) |
75 | ············{ |
76 | ················messages.WriteLine("····Build failed"); |
77 | ············} |
78 | ········} |
79 | |
80 | ········void IncludeFiles(ConfigurationOptions options, Project project, ProjectDescription projectDescription) |
81 | ········{ |
82 | #if DEBUG |
83 | ············messages.WriteLine("Including project files..."); |
84 | #endif |
85 | ············string schemaVersion = string.Empty; |
86 | #if DEBUG |
87 | ············projectDescription.MessageAdapter = messages; |
88 | #endif |
89 | ············string mappingFile = string.Empty; |
90 | ············try |
91 | ············{ |
92 | #if DEBUG |
93 | ················messages.WriteLine("··mapping..."); |
94 | #endif |
95 | ················mappingFile = projectDescription.DefaultMappingFileName; |
96 | ················projectDescription.AddFileToProject(mappingFile); |
97 | ············} |
98 | ············catch (Exception ex) |
99 | ············{ |
100 | ················messages.WriteLine("Warning: Can't add mapping file '" + mappingFile + "' to the project. " + ex.Message); |
101 | ············} |
102 | ············try |
103 | ············{ |
104 | ················NDOMapping mapping = new NDOMapping(mappingFile); |
105 | ················schemaVersion = mapping.SchemaVersion; |
106 | ············} |
107 | ············catch (Exception ex) |
108 | ············{ |
109 | ················messages.WriteLine("Warning: Can't extract schema version from the mapping file. Error message: " + ex.Message); |
110 | ············} |
111 | //············if (options.GenerateSQL) |
112 | //············{ |
113 | //················string sqlFileName = string.Empty; |
114 | //#if DEBUG |
115 | //················messages.WriteLine( "··main sql script..." ); |
116 | //#endif |
117 | //················try |
118 | //················{ |
119 | //····················sqlFileName = Path.ChangeExtension( projectDescription.BinFile, ".ndo.sql" ); |
120 | //····················projectDescription.AddFileToProject( sqlFileName ); |
121 | //················} |
122 | //················catch (Exception ex) |
123 | //················{ |
124 | //····················messages.WriteLine( "Warning: Can't add schema file '" + sqlFileName + "' to the project. " + ex.Message ); |
125 | //················} |
126 | |
127 | //#if DEBUG |
128 | //················messages.WriteLine( "··diff sql script..." ); |
129 | //#endif |
130 | //················try |
131 | //················{ |
132 | //····················sqlFileName = Path.ChangeExtension( projectDescription.BinFile, ".ndodiff." + schemaVersion + ".sql" ); |
133 | //····················projectDescription.AddFileToProject( sqlFileName ); |
134 | //················} |
135 | //················catch (Exception ex) |
136 | //················{ |
137 | //····················messages.WriteLine( "Warning: Can't add schema diff file '" + sqlFileName + "' to the project. " + ex.Message ); |
138 | //················} |
139 | //············} |
140 | #if DEBUG |
141 | ············messages.WriteLine("...ready"); |
142 | #endif |
143 | ········} |
144 | |
145 | |
146 | ········void CheckProjectDescription(ConfigurationOptions options, ProjectDescription pd, string fileName) |
147 | ········{ |
148 | ············Dictionary<string, NDOReference> ht = pd.References;··// Build the references |
149 | ············ProjectDescription storedPd = new ProjectDescription(fileName); |
150 | //············messages.WriteLine(pd.BinFile + ", " + storedPd.BinFile); |
151 | //············messages.WriteLine(pd.ProjPath + ", " + storedPd.ProjPath); |
152 | //············messages.WriteLine(pd.ObjPath + ", " + storedPd.ObjPath); |
153 | ············bool storeIt = (string.Compare(storedPd.BinFile, pd.BinFile, true) != 0 |
154 | ················|| string.Compare(storedPd.ObjPath, pd.ObjPath, true) != 0 |
155 | ················|| string.Compare(storedPd.KeyFile, pd.KeyFile, true) != 0); |
156 | ············ |
157 | ············storeIt = storeIt || (pd.References.Count != storedPd.References.Count); |
158 | //············messages.WriteLine(pd.References.Count.ToString() + ", " + storedPd.References.Count); |
159 | |
160 | ············foreach ( string key in pd.References.Keys ) |
161 | ············{ |
162 | ················//····················messages.WriteLine("Key " + de.Key + " contained: " + storedPd.References.Contains(de.Key)); |
163 | ················if ( !storedPd.References.ContainsKey( key ) ) |
164 | ················{ |
165 | ····················storeIt = true; |
166 | ····················continue; |
167 | ················} |
168 | |
169 | ················NDOReference r1 = pd.References[key]; |
170 | ················NDOReference r2 = storedPd.References[key]; |
171 | |
172 | ················// If we save the collected data, we should use the previously stored CheckThisDLL settings, |
173 | ················// except in case we changed the state using the UI |
174 | ················r1.CheckThisDLL = r2.CheckThisDLL; |
175 | |
176 | ················//····················messages.WriteLine("··" + s1 + ", " + s2); |
177 | ················if ( string.Compare( Path.GetFullPath(r1.Path), Path.GetFullPath(r2.Path), true ) != 0) |
178 | ················{ |
179 | ····················storeIt = true; |
180 | ················} |
181 | ············} |
182 | ············if (storeIt) |
183 | ············{ |
184 | ················FileInfo fi = new FileInfo(fileName); |
185 | ················if ((fi.Attributes & FileAttributes.ReadOnly) != 0) |
186 | ················{ |
187 | ····················MessageBox.Show("The NDO project file '" + fileName + "' is write protected, probably due to your Source Code Control system. NDO needs to update this file now. NDO tries to remove the write protect attribute in order to update the file.", "NDO Add-in", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
188 | ····················fi.Attributes &= (~FileAttributes.ReadOnly); |
189 | ················} |
190 | ················options.Save(pd); |
191 | ············} |
192 | ········} |
193 | |
194 | void PostProcess( ProjectDescription pd ) |
195 | ········{ |
196 | ········} |
197 | |
198 | ········public void OnBuildProjConfigDone( string projectName, string projectConfig, string platform, string solutionConfig, bool success ) |
199 | ········{ |
200 | ············if (messages == null) |
201 | ················messages = new MessageAdapter(); |
202 | |
203 | if ( ! success ) |
204 | ············{ |
205 | ················//messages.DumpTasks(); |
206 | ················return; |
207 | ············} |
208 | |
209 | ············try |
210 | ············{ |
211 | ················Solution solution = m_applicationObject.Solution; |
212 | ················ |
213 | ················// projectName can be like 'path\path\abc.def, where abc.def is the project name in the |
214 | ················// solution explorer |
215 | Project project = null; |
216 | ················try |
217 | ················{ |
218 | ····················project = new ProjectIterator(solution)[projectName]; |
219 | ················} |
220 | ················catch (Exception ex) |
221 | ················{ |
222 | ····················messages.WriteLine(ex.ToString()); |
223 | ················} // project remains null, exception will be thrown later |
224 | ················if (project == null) |
225 | ················{ |
226 | ····················messages.WriteLine("NDO: Project " + projectName + " skipped."); |
227 | ····················return; |
228 | ················} |
229 | ················ConfigurationOptions options = new ConfigurationOptions(project); |
230 | ················if (!options.EnableAddIn) |
231 | ················{ |
232 | ····················messages.WriteLine("NDO Add-in disabled"); |
233 | ····················return; |
234 | ················} |
235 | |
236 | ················messages.WriteLine(String.Format(EnhDate.String, "NDO Extension", new AssemblyName(GetType().Assembly.FullName).Version.ToString())); |
237 | |
238 | ProjectDescription projectDescription = new ProjectDescription( solution, project) ; |
239 | ················string projFileName = options.FileName; |
240 | ················CheckProjectDescription(options, projectDescription, projFileName); |
241 | |
242 | ················string targetFramework = projectDescription.TargetFramework; |
243 | ················// .NETCoreApp,Version=v2.0 müsste beim Überprüfen ebenfalls gültig sein. |
244 | ················//if (!string.IsNullOrEmpty( targetFramework ) && (!targetFramework.StartsWith( ".NETFramework,Version=v4" ) && !targetFramework.StartsWith( ".NETStandard,Version=v2" ))) |
245 | ················//{ |
246 | ················//····messages.ShowError( "Project " + project.Name + " has been built with " + targetFramework + ". NDO requires .NETFramework 4.x or .NET Standard. You need to reconfigure your project." ); |
247 | ················//····messages.WriteInsertedLine( targetFramework ); |
248 | ················//····project.DTE.ExecuteCommand( "Build.Cancel", "" ); |
249 | ················//····messages.ActivateErrorList(); |
250 | ················//····return; |
251 | ················//} |
252 | |
253 | ················// ------------------ MsBuild Support ----------------------- |
254 | ················if (messages.Success || options.UseMsBuild) |
255 | ················{ |
256 | ····················IncludeFiles(options, project, projectDescription); |
257 | ················} |
258 | ················else |
259 | ················{ |
260 | ····················project.DTE.ExecuteCommand("Build.Cancel", ""); |
261 | ····················messages.ActivateErrorList(); |
262 | ················} |
263 | ············} |
264 | ············catch ( System.Exception ex ) |
265 | ············{················ |
266 | ················messages.WriteLine( "*** Enhancer Add-in Error: ***" ); |
267 | ················if (!(ex is EnhancerEmptyException)) |
268 | ················{ |
269 | ····················messages.ShowError( ex.ToString()); |
270 | ················} |
271 | ················if (ex is System.Runtime.InteropServices.COMException) |
272 | ················{ |
273 | ····················messages.ShowError("An error in the Visual Studio automation system occured. The error should disappear after a restart of Visual Studio."); |
274 | ················} |
275 | ················else |
276 | ················{ |
277 | ····················messages.WriteLine(""); |
278 | ····················messages.WriteLine("This is possibly a follow-up error. Look at error messages above this line."); |
279 | ················}················ |
280 | ············} |
281 | ········} |
282 | |
283 | ········//public void OnBuildProjConfigBegin( string projectName, string projectConfig, string platform, string solutionConfig ) |
284 | ········//{ |
285 | ········//} |
286 | |
287 | ····} |
288 | } |
289 |
New Commit (3030986)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Globalization; |
25 | using System.Text.RegularExpressions; |
26 | using System.Xml; |
27 | using System.Collections; |
28 | using System.Windows.Forms; |
29 | using System.IO; |
30 | using System.Runtime.InteropServices; |
31 | using dte=EnvDTE; |
32 | using EnvDTE80; |
33 | using VSLangProj; |
34 | using System.Collections.Generic; |
35 | using NDOEnhancer; |
36 | using System.Reflection; |
37 | |
38 | namespace NDOVsPackage |
39 | { |
40 | ····/// <summary> |
41 | ····/// Summary description for BuildEventHandler. |
42 | ····/// </summary> |
43 | ····internal class BuildEventHandler |
44 | ····{ |
45 | ········private MessageAdapter············messages = null; |
46 | ········private BuildEvents················buildEvents; |
47 | |
48 | public BuildEventHandler( ) |
49 | ········{ |
50 | |
51 | VS. Events. BuildEvents. ProjectBuildDone += BuildEvents_ProjectBuildDone; |
52 | |
53 | ············buildEvents.OnBuildProjConfigDone····+= OnBuildProjConfigDone; |
54 | ········} |
55 | |
56 | |
57 | ········void IncludeFiles(ConfigurationOptions options, Project project, ProjectDescription projectDescription) |
58 | ········{ |
59 | #if DEBUG |
60 | ············messages.WriteLine("Including project files..."); |
61 | #endif |
62 | ············string schemaVersion = string.Empty; |
63 | #if DEBUG |
64 | ············projectDescription.MessageAdapter = messages; |
65 | #endif |
66 | ············string mappingFile = string.Empty; |
67 | ············try |
68 | ············{ |
69 | #if DEBUG |
70 | ················messages.WriteLine("··mapping..."); |
71 | #endif |
72 | ················mappingFile = projectDescription.DefaultMappingFileName; |
73 | ················projectDescription.AddFileToProject(mappingFile); |
74 | ············} |
75 | ············catch (Exception ex) |
76 | ············{ |
77 | ················messages.WriteLine("Warning: Can't add mapping file '" + mappingFile + "' to the project. " + ex.Message); |
78 | ············} |
79 | ············try |
80 | ············{ |
81 | ················NDOMapping mapping = new NDOMapping(mappingFile); |
82 | ················schemaVersion = mapping.SchemaVersion; |
83 | ············} |
84 | ············catch (Exception ex) |
85 | ············{ |
86 | ················messages.WriteLine("Warning: Can't extract schema version from the mapping file. Error message: " + ex.Message); |
87 | ············} |
88 | |
89 | #if DEBUG |
90 | ············messages.WriteLine("...ready"); |
91 | #endif |
92 | ········} |
93 | |
94 | |
95 | ········void CheckProjectDescription(ConfigurationOptions options, ProjectDescription pd, string fileName) |
96 | ········{ |
97 | ············Dictionary<string, NDOReference> ht = pd.References;··// Build the references |
98 | ············ProjectDescription storedPd = new ProjectDescription(fileName); |
99 | //············messages.WriteLine(pd.BinFile + ", " + storedPd.BinFile); |
100 | //············messages.WriteLine(pd.ProjPath + ", " + storedPd.ProjPath); |
101 | //············messages.WriteLine(pd.ObjPath + ", " + storedPd.ObjPath); |
102 | ············bool storeIt = (string.Compare(storedPd.BinFile, pd.BinFile, true) != 0 |
103 | ················|| string.Compare(storedPd.ObjPath, pd.ObjPath, true) != 0 |
104 | ················|| string.Compare(storedPd.KeyFile, pd.KeyFile, true) != 0); |
105 | ············ |
106 | ············storeIt = storeIt || (pd.References.Count != storedPd.References.Count); |
107 | //············messages.WriteLine(pd.References.Count.ToString() + ", " + storedPd.References.Count); |
108 | |
109 | ············foreach ( string key in pd.References.Keys ) |
110 | ············{ |
111 | ················//····················messages.WriteLine("Key " + de.Key + " contained: " + storedPd.References.Contains(de.Key)); |
112 | ················if ( !storedPd.References.ContainsKey( key ) ) |
113 | ················{ |
114 | ····················storeIt = true; |
115 | ····················continue; |
116 | ················} |
117 | |
118 | ················NDOReference r1 = pd.References[key]; |
119 | ················NDOReference r2 = storedPd.References[key]; |
120 | |
121 | ················// If we save the collected data, we should use the previously stored CheckThisDLL settings, |
122 | ················// except in case we changed the state using the UI |
123 | ················r1.CheckThisDLL = r2.CheckThisDLL; |
124 | |
125 | ················//····················messages.WriteLine("··" + s1 + ", " + s2); |
126 | ················if ( string.Compare( Path.GetFullPath(r1.Path), Path.GetFullPath(r2.Path), true ) != 0) |
127 | ················{ |
128 | ····················storeIt = true; |
129 | ················} |
130 | ············} |
131 | ············if (storeIt) |
132 | ············{ |
133 | ················FileInfo fi = new FileInfo(fileName); |
134 | ················if ((fi.Attributes & FileAttributes.ReadOnly) != 0) |
135 | ················{ |
136 | ····················MessageBox.Show("The NDO project file '" + fileName + "' is write protected, probably due to your Source Code Control system. NDO needs to update this file now. NDO tries to remove the write protect attribute in order to update the file.", "NDO Add-in", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
137 | ····················fi.Attributes &= (~FileAttributes.ReadOnly); |
138 | ················} |
139 | ················options.Save(pd); |
140 | ············} |
141 | ········} |
142 | |
143 | private void BuildEvents_ProjectBuildDone( ProjectBuildDoneEventArgs eventArgs) |
144 | ········{ |
145 | ············if (messages == null) |
146 | ················messages = new MessageAdapter(); |
147 | |
148 | if ( !eventArgs. IsSuccessful) |
149 | ················return; |
150 | |
151 | ············try |
152 | ············{ |
153 | |
154 | ················// projectName can be like 'path\path\abc.def, where abc.def is the project name in the |
155 | ················// solution explorer |
156 | Project project = eventArgs. Project; |
157 | ················if (project == null) |
158 | ················{ |
159 | ····················messages.WriteLine("NDO: Project " + projectName + " skipped."); |
160 | ····················return; |
161 | ················} |
162 | ················ConfigurationOptions options = new ConfigurationOptions(project); |
163 | ················if (!options.EnableAddIn) |
164 | ················{ |
165 | ····················messages.WriteLine("NDO Add-in disabled"); |
166 | ····················return; |
167 | ················} |
168 | |
169 | ················messages.WriteLine(String.Format(EnhDate.String, "NDO Extension", new AssemblyName(GetType().Assembly.FullName).Version.ToString())); |
170 | |
171 | ProjectDescription projectDescription = new ProjectDescription( project ) ; |
172 | ················string projFileName = options.FileName; |
173 | ················CheckProjectDescription(options, projectDescription, projFileName); |
174 | |
175 | ················string targetFramework = projectDescription.TargetFramework; |
176 | ················// .NETCoreApp,Version=v2.0 müsste beim Überprüfen ebenfalls gültig sein. |
177 | ················//if (!string.IsNullOrEmpty( targetFramework ) && (!targetFramework.StartsWith( ".NETFramework,Version=v4" ) && !targetFramework.StartsWith( ".NETStandard,Version=v2" ))) |
178 | ················//{ |
179 | ················//····messages.ShowError( "Project " + project.Name + " has been built with " + targetFramework + ". NDO requires .NETFramework 4.x or .NET Standard. You need to reconfigure your project." ); |
180 | ················//····messages.WriteInsertedLine( targetFramework ); |
181 | ················//····project.DTE.ExecuteCommand( "Build.Cancel", "" ); |
182 | ················//····messages.ActivateErrorList(); |
183 | ················//····return; |
184 | ················//} |
185 | |
186 | ················// ------------------ MsBuild Support ----------------------- |
187 | ················if (messages.Success || options.UseMsBuild) |
188 | ················{ |
189 | ····················IncludeFiles(options, project, projectDescription); |
190 | ················} |
191 | ················else |
192 | ················{ |
193 | ····················project.DTE.ExecuteCommand("Build.Cancel", ""); |
194 | ····················messages.ActivateErrorList(); |
195 | ················} |
196 | ············} |
197 | ············catch ( System.Exception ex ) |
198 | ············{················ |
199 | ················messages.WriteLine( "*** Enhancer Add-in Error: ***" ); |
200 | ················if (!(ex is EnhancerEmptyException)) |
201 | ················{ |
202 | ····················messages.ShowError( ex.ToString()); |
203 | ················} |
204 | ················if (ex is System.Runtime.InteropServices.COMException) |
205 | ················{ |
206 | ····················messages.ShowError("An error in the Visual Studio automation system occured. The error should disappear after a restart of Visual Studio."); |
207 | ················} |
208 | ················else |
209 | ················{ |
210 | ····················messages.WriteLine(""); |
211 | ····················messages.WriteLine("This is possibly a follow-up error. Look at error messages above this line."); |
212 | ················}················ |
213 | ············} |
214 | ········} |
215 | |
216 | ········//public void OnBuildProjConfigBegin( string projectName, string projectConfig, string platform, string solutionConfig ) |
217 | ········//{ |
218 | ········//} |
219 | |
220 | ····} |
221 | } |
222 |