PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

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