1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.IO;
|
---|
6 | using System.Reflection;
|
---|
7 | using System.Web.Mvc;
|
---|
8 | using System.Diagnostics;
|
---|
9 |
|
---|
10 | namespace HLWebPluginHost.PluginLib {
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Gathers all view locations and registers the plugins
|
---|
14 | /// </summary>
|
---|
15 | public class PluginHelper {
|
---|
16 |
|
---|
17 | /// <summary>
|
---|
18 | /// Gathers all view locations and adds them to the custom view engine
|
---|
19 | /// </summary>
|
---|
20 | internal static void InitializePluginsAndLoadViewLocations() {
|
---|
21 | Assembly[] pluginAssemblies = GetPluginAssemblies();
|
---|
22 |
|
---|
23 | List<string> viewLocations = new List<string>();
|
---|
24 |
|
---|
25 | foreach (Assembly plugin in pluginAssemblies) {
|
---|
26 | var pluginAttribute = plugin.GetCustomAttributes(typeof(HLWebPluginViewLocations), false).FirstOrDefault() as HLWebPluginViewLocations;
|
---|
27 | if (pluginAttribute != null)
|
---|
28 | viewLocations.AddRange(pluginAttribute.viewLocations);
|
---|
29 | }
|
---|
30 |
|
---|
31 | //The PluginViewEngine is used to locate views in the assemlbies
|
---|
32 | ViewEngines.Engines.Clear();
|
---|
33 | ViewEngines.Engines.Add(new PluginViewEngine(viewLocations.ToArray()));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// Builds dynamic links defined by the plugins
|
---|
38 | /// </summary>
|
---|
39 | /// <returns>List of plugin links</returns>
|
---|
40 | public static List<PluginAction> GetPluginActions() {
|
---|
41 |
|
---|
42 | // todo: Load this links once on Application_Start() and cache them.
|
---|
43 | // Currently the controller calls this everytime it renders the master page
|
---|
44 | Assembly[] pluginAssemblies = GetPluginAssemblies();
|
---|
45 |
|
---|
46 | List<PluginAction> pluginLinks = new List<PluginAction>();
|
---|
47 |
|
---|
48 | foreach (Assembly plugin in pluginAssemblies) {
|
---|
49 | var pluginAttribute = plugin.GetCustomAttributes(typeof(HLWebPluginViewLocations), false).FirstOrDefault() as HLWebPluginViewLocations;
|
---|
50 |
|
---|
51 | if (pluginAttribute.addLink) {
|
---|
52 | pluginLinks.Add(new PluginAction() {
|
---|
53 | Name = pluginAttribute.name,
|
---|
54 | Action = pluginAttribute.action,
|
---|
55 | Controller = pluginAttribute.controller
|
---|
56 | });
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | return pluginLinks;
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static Assembly[] GetPluginAssemblies() {
|
---|
64 | IEnumerable<Assembly> loadedPluginAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetCustomAttributes(typeof(HLWebPluginViewLocations), false).Count() > 0).AsEnumerable();
|
---|
65 | Assembly[] distinctLoadedPluginAssemblies = loadedPluginAssemblies.Distinct(new DLLComparer()).ToArray();
|
---|
66 |
|
---|
67 | return distinctLoadedPluginAssemblies;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public class PluginAction {
|
---|
71 | public string Name { get; set; }
|
---|
72 | public string Controller { get; set; }
|
---|
73 | public string Action { get; set; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private class DLLComparer : IEqualityComparer<Assembly> {
|
---|
77 | #region IEqualityComparer<Assembly> Members
|
---|
78 |
|
---|
79 | public bool Equals(Assembly x, Assembly y) {
|
---|
80 | return x.ManifestModule.ScopeName == y.ManifestModule.ScopeName;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public int GetHashCode(Assembly obj) {
|
---|
84 | return obj.ManifestModule.ScopeName.GetHashCode();
|
---|
85 | }
|
---|
86 |
|
---|
87 | #endregion
|
---|
88 | }
|
---|
89 | }
|
---|
90 | } |
---|