Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1094 Allow overriding of CLI actions through a new attribute #1099

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions Engine.UnitTests/CliCommandTest.cs
Expand Up @@ -25,8 +25,36 @@ public void CliActionTreeTest()
Assert.IsTrue(root.GetSubCommand("test action testaction".Split(' ')).Name == "testaction");
Assert.IsTrue(root.GetSubCommand("test action testaction arg".Split(' ')).Name == "testaction");
}

[Test]
public void CliOverrideActionTest()
{
var tree = new CliActionTree();
Assert.IsNull(tree.GetSubCommand(new string[] {"TestGroup", "TestAction"}));
Assert.AreEqual("TestAction", tree.GetSubCommand(new string[] { "TestGroup1", "TestAction" }).Name);
}
}

[Display("TestAction", Group: "TestGroup")]
public class DefaultTestAction : ICliAction
{
public int Execute(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

[Display("TestAction", Group: "TestGroup1")]
[OverrideCliAction(typeof(DefaultTestAction))]
public class DefaultTestAction1 : ICliAction
{
public int Execute(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}


[Display("testaction", Groups: new[] { "test", "action" }, Description:"Runs TestAction")]
public class TestAction : ICliAction
{
Expand Down
39 changes: 38 additions & 1 deletion Engine/Cli/CliActionExecutor.cs
Expand Up @@ -31,13 +31,25 @@ public CliActionTree()
.Where(t => t.CanCreateInstance && t.GetDisplayAttribute() != null).ToList();
Name = "tap";
Root = this;

HashSet<ITypeData> overridenTypes = commands.Where(c => c.HasAttribute<OverrideCliActionAttribute>())
.Select(c => TypeData.FromType(c.GetAttribute<OverrideCliActionAttribute>().OverrideType))
.Cast<ITypeData>()
.ToHashSet();

foreach (var item in commands)
ParseCommand(item, item.GetDisplayAttribute().Group, Root);
{
if (!overridenTypes.Contains(item))
{
ParseCommand(item, item.GetDisplayAttribute().Group, Root);
}
}
}

CliActionTree(CliActionTree parent, string name)
{
Name = name;
Root = parent.Root;
}

private static void ParseCommand(ITypeData type, string[] group, CliActionTree command)
Expand Down Expand Up @@ -111,6 +123,31 @@ internal int GetMaxCommandTreeLength(int levelPadding)
}
}

/// <summary>
/// Used to override a CLI action with a new CLI action.
/// Adding this attribute, removes the old CLI action, and adds a new one in its place.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class OverrideCliActionAttribute : Attribute
{
/// <summary>
/// The type to override.
/// </summary>
public Type OverrideType { get; set; }

/// <summary>
/// Used to override a CLI action with a new CLI action.
/// Adding this attribute, removes the old CLI action, and adds a new one in its place.
/// </summary>
/// <param name="overrideType">
/// The type to override.
/// </param>
public OverrideCliActionAttribute(Type overrideType)
{
OverrideType = overrideType;
}
}

/// <summary>
/// Helper used to execute <see cref="ICliAction"/>s.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Engine/Reflection/ReflectionDataExtensions.cs
Expand Up @@ -97,7 +97,7 @@ public static bool DescendsTo(this ITypeData type, Type basetype)
}

/// <summary>
/// Returns true if a reflection ifno has an attribute of type T.
/// Returns true if a reflection info has an attribute of type T.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="mem"></param>
Expand Down