PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/util/Services/AddinManager.cs

#
C# | 89 lines | 70 code | 11 blank | 8 comment | 4 complexity | b567157896f6632230d537edea318e9d MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2007, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Collections;
  10. using System.Reflection;
  11. using NUnit.Core;
  12. using NUnit.Core.Extensibility;
  13. namespace NUnit.Util
  14. {
  15. public class AddinManager : IService
  16. {
  17. static Logger log = InternalTrace.GetLogger(typeof(AddinManager));
  18. #region Instance Fields
  19. IAddinRegistry addinRegistry;
  20. #endregion
  21. #region Constructor
  22. public AddinManager()
  23. {
  24. }
  25. #endregion
  26. #region Addin Registration
  27. public void RegisterAddins()
  28. {
  29. // Load any extensions in the addins directory
  30. DirectoryInfo dir = new DirectoryInfo( NUnitConfiguration.AddinDirectory );
  31. if ( dir.Exists )
  32. foreach( FileInfo file in dir.GetFiles( "*.dll" ) )
  33. Register( file.FullName );
  34. }
  35. public void Register( string path )
  36. {
  37. try
  38. {
  39. AssemblyName assemblyName = new AssemblyName();
  40. assemblyName.Name = Path.GetFileNameWithoutExtension(path);
  41. assemblyName.CodeBase = path;
  42. Assembly assembly = Assembly.Load(assemblyName);
  43. log.Debug( "Loaded " + Path.GetFileName(path) );
  44. foreach ( Type type in assembly.GetExportedTypes() )
  45. {
  46. if ( type.GetCustomAttributes(typeof(NUnitAddinAttribute), false).Length == 1 )
  47. {
  48. Addin addin = new Addin( type );
  49. if ( addinRegistry.IsAddinRegistered(addin.Name) )
  50. log.Error( "Addin {0} was already registered", addin.Name );
  51. else
  52. {
  53. addinRegistry.Register( addin );
  54. log.Debug( "Registered addin: {0}", addin.Name );
  55. }
  56. }
  57. }
  58. }
  59. catch( Exception ex )
  60. {
  61. // NOTE: Since the gui isn't loaded at this point,
  62. // the trace output will only show up in Visual Studio
  63. log.Error( "Failed to load" + path, ex );
  64. }
  65. }
  66. #endregion
  67. #region IService Members
  68. public void InitializeService()
  69. {
  70. addinRegistry = Services.AddinRegistry;
  71. RegisterAddins();
  72. }
  73. public void UnloadService()
  74. {
  75. }
  76. #endregion
  77. }
  78. }