Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/08/10 16:25:23 (15 years ago)
Author:
gkronber
Message:

Implemented changes as suggested by abeham after code review and simplified method PluginValidator.GetPluginDescription(). #863.

Location:
trunk/sources/HeuristicLab.PluginInfrastructure/Manager
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/Manager/PluginDescription.cs

    r2690 r2763  
    6060      internal set { version = value; }
    6161    }
     62    [Obsolete]
    6263    private DateTime buildDate;
    6364    /// <summary>
    6465    /// Gets the build date of the plugin.
    6566    /// </summary>
     67    [Obsolete]
    6668    public DateTime BuildDate {
    6769      get { return buildDate; }
     
    132134    internal void Load() {
    133135      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
    134         throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState);
     136        throw new InvalidOperationException("Can't load a plugin in state " + pluginState);
    135137      pluginState = PluginState.Loaded;
    136138      nTimesLoaded++;
  • trunk/sources/HeuristicLab.PluginInfrastructure/Manager/PluginValidator.cs

    r2750 r2763  
    260260    /// <param name="t"></param>
    261261    private PluginDescription GetPluginDescription(Type pluginType) {
    262       // get all attributes of that type
    263       IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(pluginType);
    264       List<PluginDependency> pluginDependencies = new List<PluginDependency>();
    265       List<PluginFile> pluginFiles = new List<PluginFile>();
    266       string pluginName = null;
    267       string pluginDescription = null;
    268       string pluginVersion = "0.0.0.0";
    269       // iterate through all custom attributes and search for attributed that we are interested in
    270       foreach (CustomAttributeData attributeData in attributes) {
    271         if (IsAttributeDataForType(attributeData, typeof(PluginAttribute))) {
    272           pluginName = (string)attributeData.ConstructorArguments[0].Value;
    273           if (attributeData.ConstructorArguments.Count() > 1) {
    274             pluginVersion = (string)attributeData.ConstructorArguments[1].Value;
    275           }
    276           if (attributeData.ConstructorArguments.Count() > 2) {
    277             pluginDescription = (string)attributeData.ConstructorArguments[2].Value;
    278           } else {
    279             // no description given => use name as description
    280             pluginDescription = pluginName;
    281           }
    282         } else if (IsAttributeDataForType(attributeData, typeof(PluginDependencyAttribute))) {
    283           string name = (string)attributeData.ConstructorArguments[0].Value;
    284           Version version = new Version();
    285           // check if version is given for now
    286           // later when the constructore of PluginDependencyAttribute with only one argument has been removed
    287           // this conditional can be removed as well
    288           if (attributeData.ConstructorArguments.Count > 1) {
    289             try {
    290               version = new Version((string)attributeData.ConstructorArguments[1].Value); // throws FormatException
    291             }
    292             catch (FormatException ex) {
    293               throw new InvalidPluginException("Invalid version format of dependency " + name + " in plugin " + pluginType.ToString(), ex);
    294             }
    295           }
    296           pluginDependencies.Add(new PluginDependency(name, version));
    297         } else if (IsAttributeDataForType(attributeData, typeof(PluginFileAttribute))) {
    298           string pluginFileName = (string)attributeData.ConstructorArguments[0].Value;
    299           PluginFileType fileType = (PluginFileType)attributeData.ConstructorArguments[1].Value;
    300           pluginFiles.Add(new PluginFile(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName)), fileType));
    301         }
    302       }
     262
     263      string pluginName, pluginDescription, pluginVersion;
     264      GetPluginMetaData(pluginType, out pluginName, out pluginDescription, out pluginVersion);
     265      var pluginFiles = GetPluginFilesMetaData(pluginType);
     266      var pluginDependencies = GetPluginDependencyMetaData(pluginType);
    303267
    304268      // minimal sanity check of the attribute values
    305269      if (!string.IsNullOrEmpty(pluginName) &&
    306           pluginFiles.Count > 0 &&                                   // at least on file
     270          pluginFiles.Count() > 0 &&                                 // at least on file
    307271          pluginFiles.Any(f => f.Type == PluginFileType.Assembly)) { // at least on assembly
    308272        // create a temporary PluginDescription that contains the attribute values
     
    317281      } else {
    318282        throw new InvalidPluginException("Invalid metadata in plugin " + pluginType.ToString());
     283      }
     284    }
     285
     286    private static IEnumerable<PluginDependency> GetPluginDependencyMetaData(Type pluginType) {
     287      // get all attributes of type PluginDependency
     288      var dependencyAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
     289                                 where IsAttributeDataForType(attr, typeof(PluginDependencyAttribute))
     290                                 select attr;
     291
     292      foreach (var dependencyAttr in dependencyAttributes) {
     293        string name = (string)dependencyAttr.ConstructorArguments[0].Value;
     294        Version version = new Version("0.0.0.0"); // default version
     295        // check if version is given for now
     296        // later when the constructor of PluginDependencyAttribute with only one argument has been removed
     297        // this conditional can be removed as well
     298        if (dependencyAttr.ConstructorArguments.Count > 1) {
     299          try {
     300            version = new Version((string)dependencyAttr.ConstructorArguments[1].Value); // might throw FormatException
     301          }
     302          catch (FormatException ex) {
     303            throw new InvalidPluginException("Invalid version format of dependency " + name + " in plugin " + pluginType.ToString(), ex);
     304          }
     305        }
     306        yield return new PluginDependency(name, version);
     307      }
     308    }
     309
     310    // not static because we need the PluginDir property
     311    private IEnumerable<PluginFile> GetPluginFilesMetaData(Type pluginType) {
     312      // get all attributes of type PluginFileAttribute
     313      var pluginFileAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
     314                                 where IsAttributeDataForType(attr, typeof(PluginFileAttribute))
     315                                 select attr;
     316      foreach (var pluginFileAttribute in pluginFileAttributes) {
     317        string pluginFileName = (string)pluginFileAttribute.ConstructorArguments[0].Value;
     318        PluginFileType fileType = (PluginFileType)pluginFileAttribute.ConstructorArguments[1].Value;
     319        yield return new PluginFile(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName)), fileType);
     320      }
     321    }
     322
     323    private static void GetPluginMetaData(Type pluginType, out string pluginName, out string pluginDescription, out string pluginVersion) {
     324      // there must be a single attribute of type PluginAttribute
     325      var pluginMetaDataAttr = (from attr in CustomAttributeData.GetCustomAttributes(pluginType)
     326                                where IsAttributeDataForType(attr, typeof(PluginAttribute))
     327                                select attr).Single();
     328
     329      pluginName = (string)pluginMetaDataAttr.ConstructorArguments[0].Value;
     330
     331      // default description and version
     332      pluginVersion = "0.0.0.0";
     333      pluginDescription = pluginName;
     334      if (pluginMetaDataAttr.ConstructorArguments.Count() == 2) {
     335        // if two arguments are given the second argument is the version
     336        pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
     337      } else if (pluginMetaDataAttr.ConstructorArguments.Count() == 3) {
     338        // if three arguments are given the second argument is the description and the third is the version
     339        pluginDescription = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
     340        pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[2].Value;
    319341      }
    320342    }
     
    459481    }
    460482
    461     internal void OnPluginLoaded(PluginInfrastructureEventArgs e) {
     483    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
    462484      if (PluginLoaded != null)
    463485        PluginLoaded(this, e);
Note: See TracChangeset for help on using the changeset viewer.