Datei: NDOPackage/BuildEventHandler.cs

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