• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6518072733

14 Oct 2023 01:54PM UTC coverage: 12.718% (+0.2%) from 12.469%
6518072733

push

github

luttje
add release workflow (squash)

185 of 1687 branches covered (0.0%)

Branch coverage included in aggregate %.

813 of 6160 relevant lines covered (13.2%)

7714.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.85
/Core/Key2Joy.Core/Mapping/Actions/ActionsRepository.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Reflection;
5
using Key2Joy.Contracts.Mapping;
6
using Key2Joy.Contracts.Mapping.Actions;
7
using Key2Joy.Plugins;
8

9
namespace Key2Joy.Mapping.Actions;
10

11
public static class ActionsRepository
12
{
13
    private static Dictionary<string, MappingTypeFactory<AbstractAction>> actions;
14

15
    /// <summary>
16
    /// Loads all actions in the assembly, optionally merging it with additional action types.
17
    /// </summary>
18
    /// <param name="additionalActionTypeFactories"></param>
19
    public static void Buffer(IReadOnlyList<MappingTypeFactory<AbstractAction>> additionalActionTypeFactories = null)
20
    {
21
        static TypeExposedMethod MethodInfoToTypeExposed(MethodInfo m, ExposesScriptingMethodAttribute a)
22
        {
23
            return new TypeExposedMethod(a.FunctionName, m.Name, m.DeclaringType);
29✔
24
        }
25

26
        actions = Assembly.GetExecutingAssembly()
1✔
27
            .GetTypes()
1✔
28
            .Where(t => t.GetCustomAttribute(typeof(ActionAttribute), false) != null)
222✔
29
            .ToDictionary(
1✔
30
                t => t.FullName,
30✔
31
                t => new MappingTypeFactory<AbstractAction>(
30✔
32
                    t.FullName,
30✔
33
                    t.GetCustomAttribute<ActionAttribute>(),
30✔
34
                    t.GetMethods()
30✔
35
                        .Where(m => m.GetCustomAttributes(typeof(ExposesScriptingMethodAttribute), false).Length > 0)
790✔
36
                        .SelectMany(m => m.GetCustomAttributes<ExposesScriptingMethodAttribute>()
25✔
37
                            .Select(a => MethodInfoToTypeExposed(m, a))
54✔
38
                        )
30✔
39
                )
30✔
40
            );
1✔
41

42
        if (additionalActionTypeFactories == null)
1!
43
        {
44
            return;
1✔
45
        }
46

47
        foreach (var actionFactory in additionalActionTypeFactories)
×
48
        {
49
            if (actions.ContainsKey(actionFactory.FullTypeName))
×
50
            {
51
                Console.WriteLine("Action {0} already exists in the action buffer. Overwriting.", actionFactory.FullTypeName);
×
52
            }
53

54
            actions.Add(actionFactory.FullTypeName, actionFactory);
×
55
        }
56
    }
×
57

58
    /// <summary>
59
    /// Gets all action type factories
60
    /// </summary>
61
    /// <returns></returns>
62
    public static IDictionary<string, MappingTypeFactory<AbstractAction>> GetAllActions() => actions;
1✔
63

64
    /// <summary>
65
    /// Gets all action attributes
66
    /// </summary>
67
    /// <returns></returns>
68
    public static IList<ActionAttribute> GetAllActionAttributes() => actions.Values.Select(x => x.Attribute as ActionAttribute).ToList();
×
69

70
    /// <summary>
71
    /// Gets the attribute for the provided action
72
    /// </summary>
73
    /// <param name="action"></param>
74
    /// <returns></returns>
75
    public static ActionAttribute GetAttributeForAction(AbstractAction action)
76
    {
77
        var realTypeName = MappingTypeHelper.GetTypeFullName(actions, action);
×
78
        realTypeName = MappingTypeHelper.EnsureSimpleTypeName(realTypeName);
×
79
        return actions[realTypeName].Attribute as ActionAttribute;
×
80
    }
81

82
    /// <summary>
83
    /// Gets a specific action factory by its type
84
    /// </summary>
85
    /// <param name="type"></param>
86
    /// <returns></returns>
87
    internal static MappingTypeFactory<AbstractAction> GetAction(Type type) => actions[type.FullName];
×
88

89
    /// <summary>
90
    /// Gets all action types and their attribute annotations depending on the specified visibility
91
    /// </summary>
92
    /// <param name="forTopLevel"></param>
93
    /// <returns></returns>
94
    public static SortedDictionary<ActionAttribute, MappingTypeFactory<AbstractAction>> GetAllActions(bool forTopLevel) => new SortedDictionary<ActionAttribute, MappingTypeFactory<AbstractAction>>(
×
95
            actions
×
96
                .Where(kvp =>
×
97
                {
×
98
                    if (kvp.Value.Attribute is not ActionAttribute actionAttribute
×
99
                    || actionAttribute.Visibility == MappingMenuVisibility.Never)
×
100
                    {
×
101
                        return false;
×
102
                    }
×
103

×
104
                    if (forTopLevel)
×
105
                    {
×
106
                        return actionAttribute.Visibility is MappingMenuVisibility.Always
×
107
                            or MappingMenuVisibility.OnlyTopLevel;
×
108
                    }
×
109

×
110
                    return actionAttribute.Visibility is MappingMenuVisibility.Always or MappingMenuVisibility.UnlessTopLevel;
×
111
                })
×
112
                .ToDictionary(t => t.Value.Attribute as ActionAttribute, t => t.Value)
×
113
            );
×
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc