Exemplo n.º 1
0
        public static List <Hint> GetPropertiesHints(IBuildScript buildScript, IFlubuSession flubuSession)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());
            List <Hint>          hints = new List <Hint>();

            foreach (var property in props)
            {
                var attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                if (attributes.Count == 0)
                {
                    hints.Add(new Hint {
                        Name = property.Name
                    });
                }
                else
                {
                    foreach (var fromArgAttribute in attributes)
                    {
                        hints.Add(new Hint
                        {
                            Name      = fromArgAttribute.ArgKey,
                            Help      = fromArgAttribute.Help,
                            HintColor = ConsoleColor.DarkCyan
                        });
                    }
                }
            }

            return(hints);
        }
Exemplo n.º 2
0
        public List <string> GetPropertiesHelp(IBuildScript buildScript)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());
            List <string>        help  = new List <string>();

            foreach (var property in props)
            {
                var attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                foreach (var fromArgAttribute in attributes)
                {
                    var key  = $"-{fromArgAttribute.ArgKey.Replace("|", "|-")}";
                    var type = property.PropertyType;
                    if (type.IsEnum)
                    {
                        var enumValues = Enum.GetValues(type);

                        key = $"{key}(";
                        foreach (var enumValue in enumValues)
                        {
                            key = $"{key}{enumValue}, ";
                        }

                        key = $"{key.Substring(0, key.Length - 2)})";
                    }

                    if (!string.IsNullOrEmpty(fromArgAttribute.Help))
                    {
                        help.Add($"{key} : {fromArgAttribute.Help}");
                    }
                }
            }

            return(help);
        }
Exemplo n.º 3
0
        public void InjectProperties(IBuildScript buildScript, IFlubuSession flubuSession)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());

            foreach (var property in props)
            {
                InjectPropertiesFromScriptArg(buildScript, flubuSession, property);
                InjectPropertiesFromTaskAttributes(buildScript, flubuSession, property);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Searches methods with Target attribute in specified type and creates targets.
        /// </summary>
        /// <param name="buildScriptType"></param>
        /// <param name="taskSession"></param>
        public static void CreateTargetFromMethodAttributes(IBuildScript buildScript, ITaskSession taskSession)
        {
            #if !NETSTANDARD1_6
            var buildScriptType = buildScript.GetType();
            var methods         = buildScriptType.GetRuntimeMethods().Where(x => x.DeclaringType == buildScriptType).ToList();
            foreach (var methodInfo in methods)
            {
                var attributes = methodInfo.GetCustomAttributes <TargetAttribute>(false).ToList();

                if (attributes.Count == 0)
                {
                    continue;
                }

                foreach (var attribute in attributes)
                {
                    var methodParameters = methodInfo.GetParameters().ToList();

                    if (methodParameters.Count == 0)
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Method '{methodInfo.Name}' must have atleast one parameter which must be of type '{nameof(ITarget)}'");
                    }

                    if (methodParameters[0].ParameterType != typeof(ITarget))
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}' first parameter in method '{methodInfo.Name}' must be of type '{nameof(ITarget)}'");
                    }

                    var target = taskSession.CreateTarget(attribute.TargetName);
                    var attributeParamaters = new List <object>()
                    {
                        target
                    };

                    attributeParamaters.AddRange(attribute.MethodParameters);

                    if (methodParameters.Count != attributeParamaters.Count)
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Method parameters {methodInfo.Name} do not match count of attribute parametrs.");
                    }

                    for (int i = 0; i < methodParameters.Count; i++)
                    {
                        if (i != 0 && methodParameters[i].ParameterType != attributeParamaters[i].GetType())
                        {
                            throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Attribute parameter {i + 1.ToString()} does not match '{methodInfo.Name}' method parameter {i + 1.ToString()}. Expected {methodParameters[i].ParameterType} Actual: {attributeParamaters[i].GetType()}");
                        }
                    }

                    methodInfo.Invoke(buildScript, attributeParamaters.ToArray());
                }
            }
#endif
        }
Exemplo n.º 5
0
        public static void SetPropertiesFromScriptArg(IBuildScript buildScript, ITaskSession taskSession)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());

            foreach (var property in props)
            {
                var attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                foreach (var fromArgAttribute in attributes)
                {
                    if (!taskSession.ScriptArgs.ContainsKey(fromArgAttribute.ArgKey))
                    {
                        continue;
                    }

                    if (property.PropertyType.GetTypeInfo().IsGenericType)
                    {
                        var propertyGenericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
                        if (propertyGenericTypeDefinition == typeof(IList <>) ||
                            propertyGenericTypeDefinition == typeof(List <>) ||
                            propertyGenericTypeDefinition == typeof(IEnumerable <>))
                        {
                            var list = taskSession.ScriptArgs[fromArgAttribute.ArgKey].Split(fromArgAttribute.Seperator)
                                       .ToList();
                            property.SetValue(buildScript, list);
                        }
                    }
                    else
                    {
                        property.SetValue(buildScript, MethodParameterModifier.ParseValueByType(taskSession.ScriptArgs[fromArgAttribute.ArgKey], property.PropertyType));
                    }
                }

                if (taskSession.ScriptArgs.ContainsKey(property.Name))
                {
                    if (property.PropertyType.GetTypeInfo().IsGenericType)
                    {
                        var propertyGenericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
                        if (propertyGenericTypeDefinition == typeof(IList <>) ||
                            propertyGenericTypeDefinition == typeof(List <>) ||
                            propertyGenericTypeDefinition == typeof(IEnumerable <>))
                        {
                            property.SetValue(buildScript, taskSession.ScriptArgs[property.Name].Split(',').ToList());
                        }
                    }
                    else
                    {
                        property.SetValue(buildScript, MethodParameterModifier.ParseValueByType(taskSession.ScriptArgs[property.Name], property.PropertyType));
                    }
                }
            }
        }
Exemplo n.º 6
0
        public static Dictionary <string, IReadOnlyCollection <Hint> > GetEnumHints(IBuildScript buildScript, IFlubuSession flubuSession)
        {
#if !NETSTANDARD1_6
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());
            Dictionary <string, IReadOnlyCollection <Hint> > hints = new Dictionary <string, IReadOnlyCollection <Hint> >();
            foreach (var property in props)
            {
                var type = property.PropertyType;
                if (type.IsEnum)
                {
                    var    attribute = property.GetCustomAttribute <FromArgAttribute>(false);
                    string argKey    = null;

                    if (attribute != null)
                    {
                        argKey = attribute.ArgKey.Split('|')[0];
                    }
                    else
                    {
                        argKey = property.Name;
                    }

                    var enumValues = Enum.GetValues(type);

                    List <Hint> values = new List <Hint>();
                    foreach (var enumValue in enumValues)
                    {
                        values.Add(new Hint
                        {
                            Name     = enumValue.ToString(),
                            HintType = HintType.Value,
                        });
                    }

                    hints.Add(argKey.ToLower(), values);
                }
            }

            return(hints);
#endif
            return(null);
        }
Exemplo n.º 7
0
        public static List <string> GetPropertiesHelp(IBuildScript buildScript)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());
            List <string>        help  = new List <string>();

            foreach (var property in props)
            {
                var attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                foreach (var fromArgAttribute in attributes)
                {
                    if (!string.IsNullOrEmpty(fromArgAttribute.Help))
                    {
                        help.Add($"-{fromArgAttribute.ArgKey} : {fromArgAttribute.Help}");
                    }
                }
            }

            return(help);
        }
Exemplo n.º 8
0
        public static List <string> GetPropertiesKeys(IBuildScript buildScript, ITaskSession taskSession)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());
            List <string>        keys  = new List <string>();

            foreach (var property in props)
            {
                var attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                if (attributes.Count == 0)
                {
                    keys.Add(property.Name);
                }
                else
                {
                    foreach (var fromArgAttribute in attributes)
                    {
                        keys.Add(fromArgAttribute.ArgKey);
                    }
                }
            }

            return(keys);
        }
Exemplo n.º 9
0
        public static void SetPropertiesFromScriptArg(IBuildScript buildScript, IFlubuSession flubuSession)
        {
            var buildScriptType        = buildScript.GetType();
            IList <PropertyInfo> props = new List <PropertyInfo>(buildScriptType.GetProperties());

            foreach (var property in props)
            {
                var    attributes = property.GetCustomAttributes <FromArgAttribute>(false).ToList();
                string argKey     = null;
                foreach (var fromArgAttribute in attributes)
                {
                    var argKeys = fromArgAttribute.ArgKey.Split('|');
                    foreach (var key in argKeys)
                    {
                        if (!flubuSession.ScriptArgs.ContainsKey(key))
                        {
                            continue;
                        }

                        argKey = key;
                        break;
                    }

                    if (argKey == null)
                    {
                        continue;
                    }

                    if (property.PropertyType.GetTypeInfo().IsGenericType)
                    {
                        var propertyGenericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
                        if (propertyGenericTypeDefinition == typeof(IList <>) ||
                            propertyGenericTypeDefinition == typeof(List <>) ||
                            propertyGenericTypeDefinition == typeof(IEnumerable <>))
                        {
                            var list = flubuSession.ScriptArgs[argKey].Split(fromArgAttribute.Seperator)
                                       .ToList();
                            property.SetValue(buildScript, list);
                        }
                    }
                    else
                    {
                        SetPropertyValue(property, buildScript, flubuSession.ScriptArgs[argKey], property.PropertyType, argKey);
                    }
                }

                if (flubuSession.ScriptArgs.ContainsKey(property.Name))
                {
                    if (property.PropertyType.GetTypeInfo().IsGenericType)
                    {
                        var propertyGenericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
                        if (propertyGenericTypeDefinition == typeof(IList <>) ||
                            propertyGenericTypeDefinition == typeof(List <>) ||
                            propertyGenericTypeDefinition == typeof(IEnumerable <>))
                        {
                            property.SetValue(buildScript, flubuSession.ScriptArgs[property.Name].Split(',').ToList());
                        }
                    }
                    else
                    {
                        SetPropertyValue(property, buildScript, flubuSession.ScriptArgs[property.Name], property.PropertyType, property.Name);
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Searches methods with Target attribute in specified type and creates targets.
        /// </summary>
        /// <param name="buildScriptType"></param>
        /// <param name="flubuSession"></param>
        public void CreateTargetFromMethodAttributes(IBuildScript buildScript, IFlubuSession flubuSession)
        {
            #if !NETSTANDARD1_6
            var buildScriptType = buildScript.GetType();
            var methods         = buildScriptType.GetRuntimeMethods().Where(x => x.DeclaringType == buildScriptType).ToList();
            foreach (var methodInfo in methods)
            {
                var attributes = methodInfo.GetCustomAttributes <TargetAttribute>(false).ToList();

                if (attributes.Count == 0)
                {
                    continue;
                }

                foreach (var attribute in attributes)
                {
                    var methodParameters = methodInfo.GetParameters().ToList();

                    if (methodParameters.Count == 0)
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Method '{methodInfo.Name}' must have atleast one parameter which must be of type '{nameof(ITarget)}'");
                    }

                    if (methodParameters[0].ParameterType != typeof(ITarget))
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}' first parameter in method '{methodInfo.Name}' must be of type '{nameof(ITarget)}'");
                    }

                    var target = flubuSession.CreateTarget(attribute.TargetName);
                    var attributeParamaters = new List <object>()
                    {
                        target
                    };

                    attributeParamaters.AddRange(attribute.MethodParameters);

                    if (methodParameters.Count != attributeParamaters.Count)
                    {
                        throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Method parameters {methodInfo.Name} do not match count of attribute parametrs.");
                    }

                    for (int i = 0; i < methodParameters.Count; i++)
                    {
                        if (i != 0 && methodParameters[i].ParameterType != attributeParamaters[i].GetType())
                        {
                            throw new ScriptException($"Failed to create target '{attribute.TargetName}'. Attribute parameter {i + 1.ToString()} does not match '{methodInfo.Name}' method parameter {i + 1.ToString()}. Expected {methodParameters[i].ParameterType} Actual: {attributeParamaters[i].GetType()}");
                        }
                    }

                    var parameterInfos = methodInfo.GetParameters();
                    for (int i = 0; i < parameterInfos.Length; i++)
                    {
                        ParameterInfo parameter       = parameterInfos[i];
                        var           paramAttributes = parameter.GetCustomAttributes <FromArgAttribute>(false).ToList();
                        foreach (var fromArgAttribute in paramAttributes)
                        {
                            if (!flubuSession.Args.ScriptArguments.ContainsKey(fromArgAttribute.ArgKey))
                            {
                                continue;
                            }

                            attributeParamaters[i] = MethodParameterModifier.ParseValueByType(flubuSession.Args.ScriptArguments[fromArgAttribute.ArgKey], parameter.ParameterType);
                        }

                        if (flubuSession.Args.ScriptArguments.ContainsKey(parameter.Name))
                        {
                            object parsedValue = MethodParameterModifier.ParseValueByType(flubuSession.Args.ScriptArguments[parameter.Name], parameter.ParameterType);
                            attributeParamaters[i] = parsedValue;
                        }
                    }

                    methodInfo.Invoke(buildScript, attributeParamaters.ToArray());
                }
            }
#endif
        }