Skip to content

Commit

Permalink
(#363) Fix doubloons on macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve committed Nov 5, 2023
1 parent 144b9e4 commit 665136a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/Lanceur.Core/Models/MacroQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Lanceur.Core.Models
{
public abstract class MacroQueryResult : SelfExecutableQueryResult
{
#region Methods

public abstract SelfExecutableQueryResult Clone();

#endregion Methods
}
}
3 changes: 2 additions & 1 deletion src/Lanceur.Infra/Managers/MacroManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using Lanceur.Core.Repositories;
using Lanceur.Infra.Utils;
using Lanceur.SharedKernel.Mixins;

namespace Lanceur.Infra.Managers
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public QueryResult Handle(QueryResult item)
return null;
}

var macro = MacroInstances[alias.GetMacroName()];
var macro = MacroInstances[alias.GetMacroName()].Clone();
macro.Name = alias.Name;
macro.Parameters = alias.Parameters;
return macro;
Expand Down
8 changes: 4 additions & 4 deletions src/Lanceur.Infra/Managers/MacroManagerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class MacroManagerCache
{
#region Fields

private static Dictionary<string, SelfExecutableQueryResult> _macroInstances;
private static Dictionary<string, MacroQueryResult> _macroInstances;
private readonly Assembly _asm;
private readonly IDbRepository _dataService;

Expand All @@ -34,7 +34,7 @@ internal MacroManagerCache(Assembly asm, IAppLoggerFactory logFactory, IDbReposi

protected IAppLogger Log { get; }

protected Dictionary<string, SelfExecutableQueryResult> MacroInstances
protected Dictionary<string, MacroQueryResult> MacroInstances
{
get
{
Expand All @@ -54,11 +54,11 @@ private void LoadMacros()
var found = from t in _asm.GetTypes()
where t.GetCustomAttributes<MacroAttribute>().Any()
select t;
var macroInstances = new Dictionary<string, SelfExecutableQueryResult>();
var macroInstances = new Dictionary<string, MacroQueryResult>();
foreach (var type in found)
{
var instance = Activator.CreateInstance(type);
if (instance is not SelfExecutableQueryResult alias) continue;
if (instance is not MacroQueryResult alias) continue;

var name = alias.Name = (type.GetCustomAttribute(typeof(MacroAttribute)) as MacroAttribute)?.Name;
name = name?.ToUpper().Replace("@", string.Empty) ?? string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/Lanceur.Infra/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public IEnumerable<QueryResult> Search(Cmdline query)

// If there's an exact match, promote it to the top
// of the list.
var orderedResults = SetupAndSort(results).ToArray();
var orderedResults = SetupAndSort(results).ToList();
var match = (from r in orderedResults
where r.Name == query.Name
select r).FirstOrDefault();
Expand Down
23 changes: 23 additions & 0 deletions src/Lanceur.SharedKernel/Mixins/CloningMixin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;

namespace Lanceur.SharedKernel.Mixins;

public static class CloningMixin
{
#region Methods

/// <summary>
/// Clone object by serialising/deserialising it
/// </summary>
/// <param name="obj">The object to clone</param>
/// <typeparam name="T">The type of the object to clone</typeparam>
/// <returns>A new instance of the object with the same state</returns>
public static T CloneObject<T>(this T obj)
where T: new()
{
var bytes = JsonSerializer.SerializeToUtf8Bytes(obj);
return JsonSerializer.Deserialize<T>(bytes);
}

#endregion Methods
}
5 changes: 4 additions & 1 deletion src/Tests/Lanceur.Tests/Utils/Macros/MultiMacroTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Lanceur.Core;
using Lanceur.Core.Models;
using System.ComponentModel;
using Lanceur.SharedKernel.Mixins;

namespace Lanceur.Tests.Utils.Macros
{
[Macro("multi"), Description("Allow to start multiple alias at once")]
public class MultiMacroTest : SelfExecutableQueryResult
public class MultiMacroTest : MacroQueryResult
{
#region Constructors

Expand All @@ -21,6 +22,8 @@ public MultiMacroTest(string parameters = null)

#region Methods

public override SelfExecutableQueryResult Clone() => this.CloneObject();

public override Task<IEnumerable<QueryResult>> ExecuteAsync(Cmdline cmdline = null)
{
var list = new List<QueryResult>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Lanceur.Core;
using Lanceur.Core.Models;
using Lanceur.SharedKernel.Mixins;
using System.ComponentModel;

namespace Lanceur.Tests.Utils.ReservedAliases
{
[ReservedAlias("anothertest"), Description("description")]
public class ExecutableTestAlias : SelfExecutableQueryResult
public class ExecutableTestAlias : MacroQueryResult
{
#region Constructors

Expand All @@ -22,6 +23,8 @@ public ExecutableTestAlias()

public static ExecutableTestAlias Random() => FromName(Guid.NewGuid().ToString().Substring(0, 8));

public override SelfExecutableQueryResult Clone() => this.CloneObject();

public override Task<IEnumerable<QueryResult>> ExecuteAsync(Cmdline cmdline = null)
{
Parameters = cmdline.Parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using Lanceur.Core.Models;
using Lanceur.SharedKernel.Mixins;

namespace Lanceur.Tests.Utils.ReservedAliases
{
public class ExecutableWithResultsTestAlias : SelfExecutableQueryResult
public class ExecutableWithResultsTestAlias : MacroQueryResult
{
#region Methods

public static ExecutableWithResultsTestAlias FromName(string name) => new() { Name = name, Query = new Cmdline(name) };

public override SelfExecutableQueryResult Clone() => this.CloneObject();

public override Task<IEnumerable<QueryResult>> ExecuteAsync(Cmdline cmdline = null)
{
var result = new List<QueryResult>()
Expand Down

0 comments on commit 665136a

Please sign in to comment.