Exemplo n.º 1
0
        public object ExecuteByName(string name)
        {
            TargetDefinition definition;
            if (!_targets.Value.TryGetValue(name, out definition))
            {
                definition = new TargetDefinition();
                _targets.Value[name] = definition;
            }

            return ExecuteTarget(name, definition);
        }
Exemplo n.º 2
0
        private object ExecuteTarget(string name, TargetDefinition definition)
        {
            foreach (var dependsOn in definition.DependsOn)
            {
                ExecuteByName(dependsOn);
            }

            if (definition.HasExecuted == false)
            {
                definition.HasExecuted = true;

                Console.WriteLine($"Executing {name}");
                definition.ResultObject = definition.ExecuteMethod(_services);
            }

            return definition.ResultObject;
        }
Exemplo n.º 3
0
        public virtual void AddTargets(IDictionary<string, TargetDefinition> targets)
        {
            foreach (var method in GetType().GetTypeInfo().DeclaredMethods)
            {
                var targetAttribute = method.GetCustomAttributes<TargetAttribute>().SingleOrDefault();
                if (targetAttribute == null)
                {
                    continue;
                }

                var name = targetAttribute.Name;
                if (string.IsNullOrEmpty(name))
                {
                    name = method.Name;
                }

                TargetDefinition definition;
                if (!targets.TryGetValue(name, out definition))
                {
                    definition = new TargetDefinition();
                    targets[name] = definition;
                }

                definition.ExecuteMethod = services =>
                {
                    var targetManager = services.GetRequiredService<ITargetManager>();

                    var parameters = method.GetParameters();
                    var arguments = new object[parameters.Length];
                    for (var index = 0; index < parameters.Length; index++)
                    {
                        arguments[index] = services.GetService(parameters[index].ParameterType);

                        if (arguments[index] == null)
                        {
                            arguments[index] = targetManager.ExecuteByReturnType(parameters[index].ParameterType);
                        }
                    }
                    return method.Invoke(this, arguments);
                };

                definition.ResultType = method.ReturnType;

                if (!string.IsNullOrWhiteSpace(targetAttribute.DependsOn))
                {
                    foreach (var dependsOn in targetAttribute.DependsOn.Split(
                        new[] { ',', ' ' },
                        StringSplitOptions.RemoveEmptyEntries))
                    {
                        definition.DependsOn.Add(dependsOn);
                    }
                }

                if (!string.IsNullOrWhiteSpace(targetAttribute.BeforeTarget))
                {
                    foreach (var beforeTarget in targetAttribute.BeforeTarget.Split(
                        new[] { ',', ' ' },
                        StringSplitOptions.RemoveEmptyEntries))
                    {

                        TargetDefinition beforeDefinition;
                        if (!targets.TryGetValue(beforeTarget, out beforeDefinition))
                        {
                            beforeDefinition = new TargetDefinition();
                            targets[beforeTarget] = beforeDefinition;
                        }
                        beforeDefinition.DependsOn.Add(name);
                    }
                }
            }
        }