Updated PackageAction to use PluginTypeResolver and added unit test to ensure they are found.
This commit is contained in:
82
src/Umbraco.Tests/PackageActionFactoryTests.cs
Normal file
82
src/Umbraco.Tests/PackageActionFactoryTests.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using umbraco.cms.businesslogic.packager;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace Umbraco.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class PackageActionFactoryTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Initialize()
|
||||
{
|
||||
//for testing, we'll specify which assemblies are scanned for the PluginTypeResolver
|
||||
PluginTypeResolver.Current.AssembliesToScan = new[]
|
||||
{
|
||||
this.GetType().Assembly
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures all IPackageActions are found
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Find_Package_Actions()
|
||||
{
|
||||
var actions = PackageAction.PackageActions;
|
||||
Assert.AreEqual(2, actions.Count());
|
||||
}
|
||||
|
||||
#region Classes for tests
|
||||
public class PackageAction1 : IPackageAction
|
||||
{
|
||||
public bool Execute(string packageName, XmlNode xmlData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string Alias()
|
||||
{
|
||||
return "pa1";
|
||||
}
|
||||
|
||||
public bool Undo(string packageName, XmlNode xmlData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public XmlNode SampleXml()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class PackageAction2 : IPackageAction
|
||||
{
|
||||
public bool Execute(string packageName, XmlNode xmlData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string Alias()
|
||||
{
|
||||
return "pa2";
|
||||
}
|
||||
|
||||
public bool Undo(string packageName, XmlNode xmlData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public XmlNode SampleXml()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@
|
||||
<Compile Include="DataTypeFactoryTests.cs" />
|
||||
<Compile Include="MacroEngineFactoryTests.cs" />
|
||||
<Compile Include="MediaFactoryTests.cs" />
|
||||
<Compile Include="PackageActionFactoryTests.cs" />
|
||||
<Compile Include="PluginTypeResolverExtensions.cs" />
|
||||
<Compile Include="PluginTypeResolverTests.cs" />
|
||||
<Compile Include="TestHelper.cs" />
|
||||
|
||||
@@ -55,5 +55,15 @@ namespace umbraco.cms
|
||||
return resolver.ResolveTypes<IMediaFactory>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all available IPackageAction in application
|
||||
/// </summary>
|
||||
/// <param name="resolver"></param>
|
||||
/// <returns></returns>
|
||||
internal static IEnumerable<Type> ResolvePackageActions(this PluginTypeResolver resolver)
|
||||
{
|
||||
return resolver.ResolveTypes<IPackageAction>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
using System.Xml;
|
||||
|
||||
using Umbraco.Core;
|
||||
using umbraco.BasePages;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using umbraco.cms.businesslogic.web;
|
||||
@@ -11,79 +11,84 @@ using umbraco.cms.businesslogic.workflow;
|
||||
using umbraco.interfaces;
|
||||
|
||||
|
||||
namespace umbraco.cms.businesslogic.packager {
|
||||
namespace umbraco.cms.businesslogic.packager
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Package actions are executed on packge install / uninstall.
|
||||
/// </summary>
|
||||
public class PackageAction {
|
||||
private static readonly List<IPackageAction> _packageActions = new List<IPackageAction>();
|
||||
/// <summary>
|
||||
/// Package actions are executed on packge install / uninstall.
|
||||
/// </summary>
|
||||
public class PackageAction
|
||||
{
|
||||
internal static readonly List<IPackageAction> PackageActions = new List<IPackageAction>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="PackageAction"/> class.
|
||||
/// </summary>
|
||||
static PackageAction(){
|
||||
RegisterPackageActions();
|
||||
}
|
||||
|
||||
private static void RegisterPackageActions()
|
||||
{
|
||||
var typeFinder = new Umbraco.Core.TypeFinder2();
|
||||
var types = typeFinder.FindClassesOfType<IPackageAction>();
|
||||
foreach (var t in types)
|
||||
{
|
||||
var typeInstance = Activator.CreateInstance(t) as IPackageAction;
|
||||
if (typeInstance != null)
|
||||
{
|
||||
_packageActions.Add(typeInstance);
|
||||
|
||||
if (HttpContext.Current != null)
|
||||
HttpContext.Current.Trace.Write("registerPackageActions", " + Adding package action '" + typeInstance.Alias());
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="PackageAction"/> class.
|
||||
/// </summary>
|
||||
static PackageAction()
|
||||
{
|
||||
RegisterPackageActions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the package action with the specified action alias.
|
||||
/// </summary>
|
||||
/// <param name="packageName">Name of the package.</param>
|
||||
/// <param name="actionAlias">The action alias.</param>
|
||||
/// <param name="actionXml">The action XML.</param>
|
||||
public static void RunPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) {
|
||||
private static void RegisterPackageActions()
|
||||
{
|
||||
PackageActions.AddRange(
|
||||
PluginTypeResolver.Current.CreateInstances<IPackageAction>(
|
||||
PluginTypeResolver.Current.ResolvePackageActions()));
|
||||
}
|
||||
|
||||
foreach (IPackageAction ipa in _packageActions) {
|
||||
try {
|
||||
if (ipa.Alias() == actionAlias) {
|
||||
/// <summary>
|
||||
/// Runs the package action with the specified action alias.
|
||||
/// </summary>
|
||||
/// <param name="packageName">Name of the package.</param>
|
||||
/// <param name="actionAlias">The action alias.</param>
|
||||
/// <param name="actionXml">The action XML.</param>
|
||||
public static void RunPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml)
|
||||
{
|
||||
|
||||
ipa.Execute(packageName, actionXml);
|
||||
}
|
||||
} catch (Exception ipaExp) {
|
||||
BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error loading package action '{0}' for package {1}: {2}",
|
||||
ipa.Alias(), packageName, ipaExp));
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (IPackageAction ipa in PackageActions)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ipa.Alias() == actionAlias)
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Undos the package action with the specified action alias.
|
||||
/// </summary>
|
||||
/// <param name="packageName">Name of the package.</param>
|
||||
/// <param name="actionAlias">The action alias.</param>
|
||||
/// <param name="actionXml">The action XML.</param>
|
||||
public static void UndoPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) {
|
||||
ipa.Execute(packageName, actionXml);
|
||||
}
|
||||
}
|
||||
catch (Exception ipaExp)
|
||||
{
|
||||
BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error loading package action '{0}' for package {1}: {2}",
|
||||
ipa.Alias(), packageName, ipaExp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (IPackageAction ipa in _packageActions) {
|
||||
try {
|
||||
if (ipa.Alias() == actionAlias) {
|
||||
/// <summary>
|
||||
/// Undos the package action with the specified action alias.
|
||||
/// </summary>
|
||||
/// <param name="packageName">Name of the package.</param>
|
||||
/// <param name="actionAlias">The action alias.</param>
|
||||
/// <param name="actionXml">The action XML.</param>
|
||||
public static void UndoPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml)
|
||||
{
|
||||
|
||||
ipa.Undo(packageName, actionXml);
|
||||
}
|
||||
} catch (Exception ipaExp) {
|
||||
BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error undoing package action '{0}' for package {1}: {2}",
|
||||
ipa.Alias(), packageName, ipaExp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
foreach (IPackageAction ipa in PackageActions)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ipa.Alias() == actionAlias)
|
||||
{
|
||||
|
||||
ipa.Undo(packageName, actionXml);
|
||||
}
|
||||
}
|
||||
catch (Exception ipaExp)
|
||||
{
|
||||
BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error undoing package action '{0}' for package {1}: {2}",
|
||||
ipa.Alias(), packageName, ipaExp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user