Datei: NDOPackage/BuildEventHandler.cs

Last 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
New Commit (ed9120d)
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.Windows.Forms;
25 using System.IO;
 
26 using dte=EnvDTE;
 
 
27 using System.Collections.Generic;
28 using NDOEnhancer;
29 using System.Reflection;
30 using MessageBox = System.Windows.Forms.MessageBox;
31
32 namespace NDOVsPackage
33 {
34 ····/// <summary>
35 ····/// Summary description for BuildEventHandler.
36 ····/// </summary>
37 ····internal class BuildEventHandler
38 ····{
39 ········private MessageAdapter············messages = null;
40 ········private BuildEvents················buildEvents;
41
42 ········public BuildEventHandler()
43 ········{
44
45 ············VS.Events.BuildEvents.ProjectBuildDone += BuildEvents_ProjectBuildDone;
46
47 //buildEvents. OnBuildProjConfigDone += OnBuildProjConfigDone;
48 ········}
49
50
51 ········void IncludeFiles(ConfigurationOptions options, Project project, ProjectDescription projectDescription)
52 ········{
53 #if DEBUG
54 ············messages.WriteLine("Including project files...");
55 #endif
56 ············string schemaVersion = string.Empty;
57 #if DEBUG
58 ············projectDescription.MessageAdapter = messages;
59 #endif
60 ············string mappingFile = string.Empty;
61 ············try
62 ············{
63 #if DEBUG
64 ················messages.WriteLine("··mapping...");
65 #endif
66 ················mappingFile = projectDescription.DefaultMappingFileName;
67 ················projectDescription.AddFileToProject(mappingFile);
68 ············}
69 ············catch (Exception ex)
70 ············{
71 ················messages.WriteLine("Warning: Can't add mapping file '" + mappingFile + "' to the project. " + ex.Message);
72 ············}
73 ············try
74 ············{
75 ················NDOMapping mapping = new NDOMapping(mappingFile);
76 ················schemaVersion = mapping.SchemaVersion;
77 ············}
78 ············catch (Exception ex)
79 ············{
80 ················messages.WriteLine("Warning: Can't extract schema version from the mapping file. Error message: " + ex.Message);
81 ············}
82
83 #if DEBUG
84 ············messages.WriteLine("...ready");
85 #endif
86 ········}
87
88
89 ········void CheckProjectDescription(ConfigurationOptions options, ProjectDescription pd, string fileName)
90 ········{
91 ············Dictionary<string, NDOReference> ht = pd.References;··// Build the references
92 ············ProjectDescription storedPd = new ProjectDescription(fileName);
93 //············messages.WriteLine(pd.BinFile + ", " + storedPd.BinFile);
94 //············messages.WriteLine(pd.ProjPath + ", " + storedPd.ProjPath);
95 //············messages.WriteLine(pd.ObjPath + ", " + storedPd.ObjPath);
96 ············bool storeIt = (string.Compare(storedPd.BinFile, pd.BinFile, true) != 0
97 ················|| string.Compare(storedPd.ObjPath, pd.ObjPath, true) != 0
98 ················|| string.Compare(storedPd.KeyFile, pd.KeyFile, true) != 0);
99 ············
100 ············storeIt = storeIt || (pd.References.Count != storedPd.References.Count);
101 //············messages.WriteLine(pd.References.Count.ToString() + ", " + storedPd.References.Count);
102
103 ············foreach ( string key in pd.References.Keys )
104 ············{
105 ················//····················messages.WriteLine("Key " + de.Key + " contained: " + storedPd.References.Contains(de.Key));
106 ················if ( !storedPd.References.ContainsKey( key ) )
107 ················{
108 ····················storeIt = true;
109 ····················continue;
110 ················}
111
112 ················NDOReference r1 = pd.References[key];
113 ················NDOReference r2 = storedPd.References[key];
114
115 ················// If we save the collected data, we should use the previously stored CheckThisDLL settings,
116 ················// except in case we changed the state using the UI
117 ················r1.CheckThisDLL = r2.CheckThisDLL;
118
119 ················//····················messages.WriteLine("··" + s1 + ", " + s2);
120 ················if ( string.Compare( Path.GetFullPath(r1.Path), Path.GetFullPath(r2.Path), true ) != 0)
121 ················{
122 ····················storeIt = true;
123 ················}
124 ············}
125 ············if (storeIt)
126 ············{
127 ················FileInfo fi = new FileInfo(fileName);
128 ················if ((fi.Attributes & FileAttributes.ReadOnly) != 0)
129 ················{
130 ····················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);
131 ····················fi.Attributes &= (~FileAttributes.ReadOnly);
132 ················}
133 ················options.Save(pd);
134 ············}
135 ········}
136
137 ········private void BuildEvents_ProjectBuildDone(ProjectBuildDoneEventArgs eventArgs)
138 ········{
139 ············if (messages == null)
140 ················messages = new MessageAdapter();
141
142 ············if (!eventArgs.IsSuccessful)
143 ················return;
144
145 ············try
146 ············{
147
148 ················// projectName can be like 'path\path\abc.def, where abc.def is the project name in the
149 ················// solution explorer
150 ················Project project = eventArgs.Project;
 
 
 
 
 
151 ················ConfigurationOptions options = new ConfigurationOptions(project);
152 ················if (!options.EnableAddIn)
153 ················{
154 ····················messages.WriteLine("NDO Add-in disabled");
155 ····················return;
156 ················}
157
158 ················messages.WriteLine(String.Format(EnhDate.String, "NDO Extension", new AssemblyName(GetType().Assembly.FullName).Version.ToString()));
159
160 ················ProjectDescription projectDescription = new ProjectDescription( project );
161 ················string projFileName = options.FileName;
162 ················CheckProjectDescription(options, projectDescription, projFileName);
163
164 ················string targetFramework = projectDescription.TargetFramework;
165 ················// .NETCoreApp,Version=v2.0 müsste beim Überprüfen ebenfalls gültig sein.
166 ················//if (!string.IsNullOrEmpty( targetFramework ) && (!targetFramework.StartsWith( ".NETFramework,Version=v4" ) && !targetFramework.StartsWith( ".NETStandard,Version=v2" )))
167 ················//{
168 ················//····messages.ShowError( "Project " + project.Name + " has been built with " + targetFramework + ". NDO requires .NETFramework 4.x or .NET Standard. You need to reconfigure your project." );
169 ················//····messages.WriteInsertedLine( targetFramework );
170 ················//····project.DTE.ExecuteCommand( "Build.Cancel", "" );
171 ················//····messages.ActivateErrorList();
172 ················//····return;
173 ················//}
174
175 ················// ------------------ MsBuild Support -----------------------················
176 if ( eventArgs. IsSuccessful || options. UseMsBuild)
177 ················{
178 ····················IncludeFiles(options, project, projectDescription);
 
 
 
 
 
179 ················}
180 ············}
181 ············catch ( System.Exception ex )
182 ············{················
183 ················messages.WriteLine( "*** Enhancer Add-in Error: ***" );
184 ················if (!(ex is EnhancerEmptyException))
185 ················{
186 messages. WriteLine( ex. ToString( ) ) ;
187 ················}
188 ················if (ex is System.Runtime.InteropServices.COMException)
189 ················{
190 ····················messages.WriteLine("An error in the Visual Studio automation system occured. The error should disappear after a restart of Visual Studio.");
191 ················}
192 ················else
193 ················{
194 ····················messages.WriteLine("");
195 ····················messages.WriteLine("This is possibly a follow-up error. Look at error messages above this line.");
196 ················}················
197 ············}
198 ········}
199
200 ····}
201 }
202 ··········messages.ShowError("An error in the Visual Studio automation system occured. The error should disappear after a restart of Visual Studio.");
203 ················}
204 ················else
205 ················{
206 ····················messages.WriteLine("");
207 ····················messages.WriteLine("This is possibly a follow-up error. Look at error messages above this line.");
208 ················}················
209 ············}
210 ········}
211
212 ········//public void OnBuildProjConfigBegin( string projectName, string projectConfig, string platform, string solutionConfig )
213 ········//{
214 ········//}
215
216 ····}
217 }
218