Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

PluginDescriptor.cs

using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Nop.Core;
using Nop.Core.Infrastructure;

namespace Nop.Services.Plugins;

/// 
/// Represents a plugin descriptor
/// 
public partial class PluginDescriptor : PluginDescriptorBaseInfo, IDescriptor, IComparable
{
    #region Ctor

    public PluginDescriptor()
    {
        SupportedVersions = new List();
        LimitedToStores = new List();
        LimitedToCustomerRoles = new List();
        DependsOn = new List();
    }

    #endregion

    #region Methods

    /// 
    /// Get plugin descriptor from the description text
    /// 
    /// Description text
    /// Plugin descriptor
    public static PluginDescriptor GetPluginDescriptorFromText(string text)
    {
        if (string.IsNullOrEmpty(text))
            return new PluginDescriptor();

        //get plugin descriptor from the JSON file
        var descriptor = JsonConvert.DeserializeObject(text);

        //nopCommerce 2.00 didn't have 'SupportedVersions' parameter, so let's set it to "2.00"
        if (!descriptor.SupportedVersions.Any())
            descriptor.SupportedVersions.Add("2.00");

        return descriptor;
    }

    /// 
    /// Get the instance of the plugin
    /// 
    /// Type of the plugin
    /// Plugin instance
    public virtual TPlugin Instance() where TPlugin : class, IPlugin
    {
        //try to resolve plugin as unregistered service
        var instance = EngineContext.Current.ResolveUnregistered(PluginType);

        //try to get typed instance
        var typedInstance = instance as TPlugin;
        if (typedInstance != null)
            typedInstance.PluginDescriptor = this;

        return typedInstance;
    }

    /// 
    /// Compares this instance with a specified PluginDescriptor object
    /// 
    /// The PluginDescriptor to compare with this instance
    /// An integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified parameter
    public int CompareTo(PluginDescriptor other)
    {
        if (DisplayOrder != other.DisplayOrder)
            return DisplayOrder.CompareTo(other.DisplayOrder);

        return string.Compare(SystemName, other.SystemName, StringComparison.InvariantCultureIgnoreCase);
    }

    /// 
    /// Returns the plugin as a string
    /// 
    /// Value of the FriendlyName
    public override string ToString()
    {
        return FriendlyName;
    }

    /// 
    /// Save plugin descriptor to the plugin description file
    /// 
    public virtual void Save()
    {
        //since plugins are loaded before IoC initialization using the default provider,
        //in order to avoid possible problems we use CommonHelper.DefaultFileProvider
        //instead of the main file provider
        var fileProvider = CommonHelper.DefaultFileProvider;

        //get the description file path
        if (OriginalAssemblyFile == null)
            throw new Exception($"Cannot load original assembly path for {SystemName} plugin.");

        var filePath = fileProvider.Combine(fileProvider.GetDirectoryName(OriginalAssemblyFile), NopPluginDefaults.DescriptionFileName);
        if (!fileProvider.FileExists(filePath))
            throw new Exception($"Description file for {SystemName} plugin does not exist. {filePath}");

        //save the file
        var text = JsonConvert.SerializeObject(this, Formatting.Indented);
        fileProvider.WriteAllText(filePath, text, Encoding.UTF8);
    }

    #endregion

    #region Properties

    /// 
    /// Gets or sets the plugin group
    /// 
    [JsonProperty(PropertyName = "Group")]
    public virtual string Group { get; set; }

    /// 
    /// Gets or sets the plugin friendly name
    /// 
    [JsonProperty(PropertyName = "FriendlyName")]
    public virtual string FriendlyName { get; set; }

    /// 
    /// Gets or sets the supported versions of nopCommerce
    /// 
    [JsonProperty(PropertyName = "SupportedVersions")]
    public virtual IList SupportedVersions { get; set; }

    /// 
    /// Gets or sets the author
    /// 
    [JsonProperty(PropertyName = "Author")]
    public virtual string Author { get; set; }

    /// 
    /// Gets or sets the display order
    /// 
    [JsonProperty(PropertyName = "DisplayOrder")]
    public virtual int DisplayOrder { get; set; }

    /// 
    /// Gets or sets the name of the assembly file
    /// 
    [JsonProperty(PropertyName = "FileName")]
    public virtual string AssemblyFileName { get; set; }

    /// 
    /// Gets or sets the description
    /// 
    [JsonProperty(PropertyName = "Description")]
    public virtual string Description { get; set; }

    /// 
    /// Gets or sets the list of store identifiers in which this plugin is available. If empty, then this plugin is available in all stores
    /// 
    [JsonProperty(PropertyName = "LimitedToStores")]
    public virtual IList LimitedToStores { get; set; }

    /// 
    /// Gets or sets the list of customer role identifiers for which this plugin is available. If empty, then this plugin is available for all ones.
    /// 
    [JsonProperty(PropertyName = "LimitedToCustomerRoles")]
    public virtual IList LimitedToCustomerRoles { get; set; }

    /// 
    /// Gets or sets the list of plugins' system name that this plugin depends on
    /// 
    [JsonProperty(PropertyName = "DependsOnSystemNames")]
    public virtual IList DependsOn { get; set; }

    /// 
    /// Gets or sets the value indicating whether plugin is installed
    /// 
    [JsonIgnore]
    public virtual bool Installed { get; set; }

    /// 
    /// Gets or sets the plugin type
    /// 
    [JsonIgnore]
    public virtual Type PluginType { get; set; }

    /// 
    /// Gets or sets the original assembly file
    /// 
    [JsonIgnore]
    public virtual string OriginalAssemblyFile { get; set; }

    /// 
    /// Gets or sets the list of all library files in the plugin directory
    /// 
    [JsonIgnore]
    public virtual IList PluginFiles { get; set; }

    /// 
    /// Gets or sets the assembly that is active in the application
    /// 
    [JsonIgnore]
    public virtual Assembly ReferencedAssembly { get; set; }

    /// 
    /// Gets or sets the value indicating whether need to show the plugin on plugins page
    /// 
    [JsonIgnore]
    public virtual bool ShowInPluginsList { get; set; } = true;

    #endregion
}