Datei: NDOEnhancer/NDOEnhancer/EnhancerMain.cs

Last Commit (3c5cca3)
1 //
2 // Copyright (c) 2002-2024 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.IO;
25 using System.Reflection;
26 using System.Linq;
 
27 //using NDO.Provider;
28 using System.Globalization;
29 using Microsoft.Extensions.DependencyInjection;
30 using Microsoft.Extensions.Hosting;
31 using Microsoft.Extensions.Logging;
32 using NDO.ProviderFactory;
33 using NDOInterfaces;
34 //using NDO.SqlPersistenceHandling;
35
36 namespace NDOEnhancer
37 {
38 ····[Serializable]
39 ····public class EnhancerMain
40 ····{
41 ····
42 ········static bool verboseMode;
43 ········ProjectDescription projectDescription;
44 ········private IServiceProvider serviceProvider;
45
46 ········public static int Main( string[] args )
47 ········{
48 ············int result = 0;
49 ············// Make the culture invariant, otherwise .NET tries to load .resources assemblies.
50 ············System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
51 ············System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
52
53 ············try
54 ············{
55 ················if (args.Length < 2)
56 ····················throw new Exception( "usage: NDOEnhancer <file_name> <target_framework>\n" );
57
58 ················string ndoProjFilePath = args[0];
59 ················string targetFramework = args[1];
60
61 #if DEBUG
62 ····················Console.WriteLine( "Domain base directory is: " + AppDomain.CurrentDomain.BaseDirectory );
63 ····················Console.WriteLine( "Running as " + ( IntPtr.Size * 8 ) + " bit app." );
64 #endif
65
66 ····················new EnhancerMain().InternalStart( ndoProjFilePath, targetFramework );
67 ············}
68 ············catch (Exception ex)
69 ············{
70 ················Console.Error.WriteLine( "Error: " + ex.ToString() );
71 ················result = -1;
72 ············}
73 ············return result;
74 ········}
75
76 ········void Build( Action<IServiceCollection> configure = null )
77 ········{
78 ············var builder = Host.CreateDefaultBuilder();
79 ············builder.ConfigureServices( services =>
80 ············{
81 ················services.AddLogging( b =>
82 ················{
83 ····················b.ClearProviders();
84 ····················b.AddConsole();
85 ················} );
86
87 ················services.AddSingleton<INDOProviderFactory, NDOProviderFactory>();
88 ················services.AddSingleton<IProviderPathFinder, ProviderPathFinder>();
89 ················if (configure != null)
90 ····················configure( services );
91 ············} );
92
93 ············var host = builder.Build();
94 ············this.serviceProvider = host.Services;
95 ········}
96
97 ········void CopyFile(string source, string dest)
98 ········{
99 ············if (verboseMode)
100 ················Console.WriteLine("Copying: " + source + "->" + dest);
101
102 ············File.Copy(source, dest, true);
103 ········}
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106 ········public void InternalStart(string ndoProjFilePath, string targetFramework)
107 ········{
108 ············ConfigurationOptions options;
109
110 ············if (!File.Exists(ndoProjFilePath))
111 {
112 ················throw new Exception("Can't find file '" + ndoProjFilePath + "'");
113 ············}
114
115 #if DEBUG
116 ············Console.WriteLine( $"Loading Project Description from {ndoProjFilePath}..." );
117 #endif
118 ············this.projectDescription = new ProjectDescription( ndoProjFilePath, targetFramework );
119
120 ············AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
121 ············//NDOContainer.Instance.RegisterType<IProviderPathFinder,ProviderPathFinder>();
122 ············//// This is needed as parameter for ProviderPathFinder
123 ············//NDOContainer.Instance.RegisterInstance( this.projectDescription );
124
125 ············options = projectDescription.ConfigurationOptions;
126
127 ············if (!options.EnableAddIn)
128 ················return;
129
130 #if DEBUG
131 ············Console.WriteLine("Loading Project Description ready.");
132
133 ············verboseMode = true;
134 #else
135 ············verboseMode = options.VerboseMode;
136
137 ············// In Debug Mode the base directory is printed in the Main method
138 ············if (verboseMode)
139 ············{
140 ················Console.WriteLine( "Domain base directory is: " + AppDomain.CurrentDomain.BaseDirectory );
141 ············}
142 #endif
143 ············// We should consider using the following code to determine the version:
144 ············//public static string? GetInformationalVersion() =>
145 ············//····Assembly
146 ············//········.GetEntryAssembly()
147 ············//········?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
148 ············//········?.InformationalVersion;
149 ············// The PatchNdoVersion tool is able to enter the informational version.
150
151
152 ············Console.WriteLine( EnhDate.String, "NDO Enhancer", new AssemblyName( GetType().Assembly.FullName ).Version.ToString() );
153 ············if (verboseMode)
154 ················Console.WriteLine( $"Parameters: {ndoProjFilePath}, {targetFramework}" );
155
156 ············if (options.EnableEnhancer)
157 ················this.projectDescription.References.Add(projectDescription.AssemblyName, new NDOReference(projectDescription.AssemblyName, projectDescription.BinFile, true));
158
159 ············MessageAdapter messages = new MessageAdapter();
160
161 ············var basePath = Path.GetDirectoryName( this.projectDescription.BinFile );
162 ············var loadContext = new ManagedLoadContext( basePath, verboseMode );
163 ············using (loadContext.EnterContextualReflection())
164 ············{
165 ················new Enhancer( this.projectDescription, messages, NDOProviderFactory.Instance ).DoIt();
166 ················loadContext.Unload();
167 ············}
168
169 ············if (options.EnableEnhancer)
170 ············{
171 ················string tempDir = Path.Combine(this.projectDescription.ObjPath, "ndotemp");
172 ················string enhObjFile = Path.Combine(tempDir, Path.GetFileName(this.projectDescription.BinFile));
173 ················string enhPdbFile = Path.ChangeExtension(enhObjFile, ".pdb");
174 ················string binPdbFile = Path.ChangeExtension(this.projectDescription.BinFile, ".pdb");
175 ················string objFile = Path.Combine(this.projectDescription.ObjPath, Path.GetFileName(this.projectDescription.BinFile));
176 ················string objPdbFile = Path.Combine(this.projectDescription.ObjPath, Path.GetFileName(binPdbFile));
177 ················bool objPathDifferent = String.Compare(objFile, this.projectDescription.BinFile, true) != 0;
178 ················if (File.Exists( enhObjFile ))
179 ················{
 
 
180 ····················if (objPathDifferent)
181 ························CopyFile( enhObjFile, objFile );
182
183 ····················if (File.Exists( enhPdbFile ))
184 ····················{
185 ························CopyFile( enhPdbFile, binPdbFile );
186
187 ························if (objPathDifferent)
188 ····························CopyFile( enhPdbFile, objPdbFile );
189 ························try
190 ························{
191 ····························File.Delete( enhPdbFile );
192 ························}
193 ························catch (Exception ex)
194 ························{
195 ····························if (verboseMode)
196 ································Console.WriteLine( "Warning: Ignored Exception: " + ex.ToString() );
197 ························}
198 ····················}
199 ················}
200
201 ················Console.WriteLine( "Enhancer ready" );
202 ············}
203 ········}
204
205 ········string GetPackageLibPath( string assyName )
206 ········{
207 ············if (this.projectDescription == null)
208 ················throw new Exception( "Project Description is not defined" );
209
210 ············var packageName = assyName.ToLowerInvariant();
211 ············if (assyName.Equals( "NDO", StringComparison.OrdinalIgnoreCase ))
212 ················packageName = "ndo.dll";
213
214 ············var path = new NugetProps(this.projectDescription).DefaultNugetPackageFolder;
215 ············if (path == null)
216 ················return null;
217
218 ············return Path.Combine( path, new ProjectAssets(this.projectDescription).GetPackageDir( packageName ) );
219 ········}
220
221 ········/// <summary>
222 ········/// This is called, if an assembly can't be found in the bin directory
223 ········/// </summary>
224 ········/// <param name="sender"></param>
225 ········/// <param name="args"></param>
226 ········/// <returns></returns>
227 ········Assembly OnAssemblyResolve( object sender, ResolveEventArgs args )
228 ········{
229 ············if (verboseMode)
230 ················Console.WriteLine( $"AssemblyResolve: {args?.Name}" );
231
232 ············var assyName = args?.Name?.Split(',').FirstOrDefault();
233 ············if (assyName == null)
234 ················return null;
235
236 ············// Assemblies of ProjectReferences are copied to the bin directory
237 ············var binPath = Path.Combine( Path.GetDirectoryName( this.projectDescription.BinFile ), assyName + ".dll" );
238 ············if (File.Exists( binPath ))
239 ············{
240 ················if (verboseMode)
241 ····················Console.WriteLine( "Location: " + binPath );
242 ················return Assembly.LoadFrom( binPath );
243 ············}
244
245 ············string path = GetPackageLibPath( assyName );
246
247 ············if (path == null)
248 ················return null;
249
250 ············Console.WriteLine( "Location: " + path );
251 ············if (File.Exists( path ))
252 ············{
253 ················return Assembly.LoadFrom( path );
254 ············}
255
256 ············return null;
257 ········}
258
259
260 ········public EnhancerMain()
261 ········{
262 ········}
263 ····}
264 }
265
New Commit (dbc6a64)
1 //
2 // Copyright (c) 2002-2024 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.IO;
25 using System.Reflection;
26 using System.Linq;
27 using System.Text;
28 //using NDO.Provider;
29 using System.Globalization;
30 using Microsoft.Extensions.DependencyInjection;
31 using Microsoft.Extensions.Hosting;
32 using Microsoft.Extensions.Logging;
33 using NDO.ProviderFactory;
34 using NDOInterfaces;
35 //using NDO.SqlPersistenceHandling;
36
37 namespace NDOEnhancer
38 {
39 ····[Serializable]
40 ····public class EnhancerMain
41 ····{
42 ····
43 ········static bool verboseMode;
44 ········ProjectDescription projectDescription;
45 ········private IServiceProvider serviceProvider;
46
47 ········public static int Main( string[] args )
48 ········{
49 ············int result = 0;
50 ············// Make the culture invariant, otherwise .NET tries to load .resources assemblies.
51 ············System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
52 ············System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
53
54 ············try
55 ············{
56 ················if (args.Length < 2)
57 ····················throw new Exception( "usage: NDOEnhancer <file_name> <target_framework>\n" );
58
59 ················string ndoProjFilePath = args[0];
60 ················string targetFramework = args[1];
61
62 #if DEBUG
63 ····················Console.WriteLine( "Domain base directory is: " + AppDomain.CurrentDomain.BaseDirectory );
64 ····················Console.WriteLine( "Running as " + ( IntPtr.Size * 8 ) + " bit app." );
65 #endif
66
67 ····················new EnhancerMain().InternalStart( ndoProjFilePath, targetFramework );
68 ············}
69 ············catch (Exception ex)
70 ············{
71 ················Console.Error.WriteLine( "Error: " + ex.ToString() );
72 ················result = -1;
73 ············}
74 ············return result;
75 ········}
76
77 ········void Build( Action<IServiceCollection> configure = null )
78 ········{
79 ············var builder = Host.CreateDefaultBuilder();
80 ············builder.ConfigureServices( services =>
81 ············{
82 ················services.AddLogging( b =>
83 ················{
84 ····················b.ClearProviders();
85 ····················b.AddConsole();
86 ················} );
87
88 ················services.AddSingleton<INDOProviderFactory, NDOProviderFactory>();
89 ················services.AddSingleton<IProviderPathFinder, ProviderPathFinder>();
90 ················if (configure != null)
91 ····················configure( services );
92 ············} );
93
94 ············var host = builder.Build();
95 ············this.serviceProvider = host.Services;
96 ········}
97
98 ········void CopyFile(string source, string dest)
99 ········{
100 ············if (verboseMode)
101 ················Console.WriteLine("Copying: " + source + "->" + dest);
102
103 ············File.Copy(source, dest, true);
104 ········}
105
106
107 ········/// <summary>
108 ········/// Creates a default .ndoproj file, if the project doesn't have one.
109 ········/// </summary>
110 ········void CreateNDOProjFile( string fileName, string targetFramework )
111 ········{
112 ············var binFile = Path.ChangeExtension( Path.GetFileName( fileName ), ".dll" );
113 ············string xml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
114 <Enhancer>
115 ··<Options>
116 ····<EnableAddIn>True</EnableAddIn>
117 ····<EnableEnhancer>True</EnableEnhancer>
118 ····<VerboseMode>False</VerboseMode>
119 ····<NewMapping>False</NewMapping>
120 ····<GenerateSQL>False</GenerateSQL>
121 ····<DefaultConnection>
122 ····</DefaultConnection>
123 ····<TargetMappingFileName>NDOMapping.xml</TargetMappingFileName>
124 ····<GenerateChangeEvents>False</GenerateChangeEvents>
125 ····<UseTimeStamps>False</UseTimeStamps>
126 ····<Utf8Encoding>True</Utf8Encoding>
127 ····<SQLScriptLanguage>
128 ····</SQLScriptLanguage>
129 ····<SchemaVersion>
130 ····</SchemaVersion>
131 ····<IncludeTypecodes>False</IncludeTypecodes>
132 ····<DatabaseOwner>
133 ····</DatabaseOwner>
134 ····<GenerateConstraints>False</GenerateConstraints>
135 ····<DropExistingElements>False</DropExistingElements>
136 ··</Options>
137 ··<ProjectDescription>
138 ····<BinPath>bin/Debug/{targetFramework}/{binFile}</BinPath>
139 ····<ObjPath>obj/Debug/{targetFramework}</ObjPath>
140 ····<AssemblyName />
141 ····<Debug>True</Debug>
142 ····<KeyFile>
143 ····</KeyFile>
144 ····<References>
145 ····</References>
146 ··</ProjectDescription>
147 </Enhancer>";
148 ············File.WriteAllText( fileName, xml, new UTF8Encoding( true ) );
149 ········}
150
151 ········public void InternalStart(string ndoProjFilePath, string targetFramework)
152 ········{
153 ············ConfigurationOptions options;
154
155 ············if (!File.Exists(ndoProjFilePath))
156 CreateNDOProjFile( ndoProjFilePath, targetFramework ) ;
 
 
157
158 #if DEBUG
159 ············Console.WriteLine( $"Loading Project Description from {ndoProjFilePath}..." );
160 #endif
161 ············this.projectDescription = new ProjectDescription( ndoProjFilePath, targetFramework );
162
163 ············AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
164 ············//NDOContainer.Instance.RegisterType<IProviderPathFinder,ProviderPathFinder>();
165 ············//// This is needed as parameter for ProviderPathFinder
166 ············//NDOContainer.Instance.RegisterInstance( this.projectDescription );
167
168 ············options = projectDescription.ConfigurationOptions;
169
170 ············if (!options.EnableAddIn)
171 ················return;
172
173 #if DEBUG
174 ············Console.WriteLine("Loading Project Description ready.");
175
176 ············verboseMode = true;
177 #else
178 ············verboseMode = options.VerboseMode;
179
180 ············// In Debug Mode the base directory is printed in the Main method
181 ············if (verboseMode)
182 ············{
183 ················Console.WriteLine( "Domain base directory is: " + AppDomain.CurrentDomain.BaseDirectory );
184 ············}
185 #endif
186 ············// We should consider using the following code to determine the version:
187 ············//public static string? GetInformationalVersion() =>
188 ············//····Assembly
189 ············//········.GetEntryAssembly()
190 ············//········?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
191 ············//········?.InformationalVersion;
192 ············// The PatchNdoVersion tool is able to enter the informational version.
193
194
195 ············Console.WriteLine( EnhDate.String, "NDO Enhancer", new AssemblyName( GetType().Assembly.FullName ).Version.ToString() );
196 ············if (verboseMode)
197 ················Console.WriteLine( $"Parameters: {ndoProjFilePath}, {targetFramework}" );
198
199 ············if (options.EnableEnhancer)
200 ················this.projectDescription.References.Add(projectDescription.AssemblyName, new NDOReference(projectDescription.AssemblyName, projectDescription.BinFile, true));
201
202 ············MessageAdapter messages = new MessageAdapter();
203
204 ············var basePath = Path.GetDirectoryName( this.projectDescription.BinFile );
205 ············var loadContext = new ManagedLoadContext( basePath, verboseMode );
206 ············using (loadContext.EnterContextualReflection())
207 ············{
208 ················new Enhancer( this.projectDescription, messages, NDOProviderFactory.Instance ).DoIt();
209 ················loadContext.Unload();
210 ············}
211
212 ············if (options.EnableEnhancer)
213 ············{
214 ················string tempDir = Path.Combine(this.projectDescription.ObjPath, "ndotemp");
215 ················string enhObjFile = Path.Combine(tempDir, Path.GetFileName(this.projectDescription.BinFile));
216 ················string enhPdbFile = Path.ChangeExtension(enhObjFile, ".pdb");
217 ················string binPdbFile = Path.ChangeExtension(this.projectDescription.BinFile, ".pdb");
218 ················string objFile = Path.Combine(this.projectDescription.ObjPath, Path.GetFileName(this.projectDescription.BinFile));
219 ················string objPdbFile = Path.Combine(this.projectDescription.ObjPath, Path.GetFileName(binPdbFile));
220 ················bool objPathDifferent = String.Compare(objFile, this.projectDescription.BinFile, true) != 0;
221 ················if (File.Exists( enhObjFile ))
222 ················{
223 ····················CopyFile( enhObjFile, this.projectDescription.BinFile );
224
225 ····················if (objPathDifferent)
226 ························CopyFile( enhObjFile, objFile );
227
228 ····················if (File.Exists( enhPdbFile ))
229 ····················{
230 ························CopyFile( enhPdbFile, binPdbFile );
231
232 ························if (objPathDifferent)
233 ····························CopyFile( enhPdbFile, objPdbFile );
234 ························try
235 ························{
236 ····························File.Delete( enhPdbFile );
237 ························}
238 ························catch (Exception ex)
239 ························{
240 ····························if (verboseMode)
241 ································Console.WriteLine( "Warning: Ignored Exception: " + ex.ToString() );
242 ························}
243 ····················}
244 ················}
245
246 ················Console.WriteLine( "Enhancer ready" );
247 ············}
248 ········}
249
250 ········string GetPackageLibPath( string assyName )
251 ········{
252 ············if (this.projectDescription == null)
253 ················throw new Exception( "Project Description is not defined" );
254
255 ············var packageName = assyName.ToLowerInvariant();
256 ············if (assyName.Equals( "NDO", StringComparison.OrdinalIgnoreCase ))
257 ················packageName = "ndo.dll";
258
259 ············var path = new NugetProps(this.projectDescription).DefaultNugetPackageFolder;
260 ············if (path == null)
261 ················return null;
262
263 ············return Path.Combine( path, new ProjectAssets(this.projectDescription).GetPackageDir( packageName ) );
264 ········}
265
266 ········/// <summary>
267 ········/// This is called, if an assembly can't be found in the bin directory
268 ········/// </summary>
269 ········/// <param name="sender"></param>
270 ········/// <param name="args"></param>
271 ········/// <returns></returns>
272 ········Assembly OnAssemblyResolve( object sender, ResolveEventArgs args )
273 ········{
274 ············if (verboseMode)
275 ················Console.WriteLine( $"AssemblyResolve: {args?.Name}" );
276
277 ············var assyName = args?.Name?.Split(',').FirstOrDefault();
278 ············if (assyName == null)
279 ················return null;
280
281 ············// Assemblies of ProjectReferences are copied to the bin directory
282 ············var binPath = Path.Combine( Path.GetDirectoryName( this.projectDescription.BinFile ), assyName + ".dll" );
283 ············if (File.Exists( binPath ))
284 ············{
285 ················if (verboseMode)
286 ····················Console.WriteLine( "Location: " + binPath );
287 ················return Assembly.LoadFrom( binPath );
288 ············}
289
290 ············string path = GetPackageLibPath( assyName );
291
292 ············if (path == null)
293 ················return null;
294
295 ············Console.WriteLine( "Location: " + path );
296 ············if (File.Exists( path ))
297 ············{
298 ················return Assembly.LoadFrom( path );
299 ············}
300
301 ············return null;
302 ········}
303
304
305 ········public EnhancerMain()
306 ········{
307 ········}
308 ····}
309 }
310