/// <summary>
        /// Initalizes the manager with the specified namespaces in the specified assembly
        /// </summary>
        /// <param name="assembly">Assembly in which is searched</param>
        /// <param name="namespaces">Namespace to be registered in the manager</param>
        public void Initialize(Assembly assembly, params string[] namespaces)
        {
            if (Namespaces == null)
            {
                Namespaces = new List <string>();
            }

            Namespaces.AddRange(namespaces);

            var commands = assembly.GetTypes().Where(
                t => t.GetCustomAttribute <CommandAttribute>() != null && Namespaces.Contains(t.Namespace)).ToList();

            foreach (var command in commands)
            {
                command.GetMethod(
                    "Register",
                    BindingFlags.Static |
                    BindingFlags.Public |
                    BindingFlags.FlattenHierarchy)
                .Invoke(null, new[] { command });
                commandHandler[(string)command.GetCustomAttribute <CommandAttribute>().Tag] += (e)
                                                                                               => InitializeCommand(command, e);
            }

            InitializeOneTimeCommand(Namespaces.ToArray(), assembly.GetTypes());
            IsInitialized = true;
        }
Пример #2
0
        public void LoadFile(string filename)
        {
            XmlDocument document = new XmlDocument();

            document.Load(filename);

            XmlNode compositeNode = document.SelectSingleNode(CompositeXPath);
            string  rootNamespace = GetNodeValue(compositeNode, "@rootNamespace");

            RootNamespace = rootNamespace;

            XmlNodeList types = document.SelectNodes(TypesXPath);

            foreach (XmlNode typeNode in types)
            {
                string typeName     = GetNodeValue(typeNode, "@name");
                string className    = GetNodeValue(typeNode, "@className");
                string theNamespace = GetNodeValue(typeNode, "@namespace");
                if (!Namespaces.Contains(theNamespace))
                {
                    Namespaces.Add(theNamespace);
                }
                CompositeType compositeType = new CompositeType(typeName, className, theNamespace);

                Type foundType = FindType(className, theNamespace);

                foreach (PropertyInfo pi in foundType.GetProperties())
                {
                    if (pi.CanRead && pi.CanWrite)
                    {
                        CompositeProperty property = new CompositeProperty(typeName, pi.Name, pi.PropertyType);
                        compositeType.Properties.Add(property);
                    }
                }

                XmlNodeList hiddenNodes = typeNode.SelectNodes(PropertiesXPath);
                foreach (XmlNode propertyNode in hiddenNodes)
                {
                    string propertyName = GetNodeValue(propertyNode, "@name");
                    string alias        = GetNodeValue(propertyNode, "@alias");
                    bool   isReadOnly   = GetBooleanNodeValue(propertyNode, "@isReadOnly");
                    bool   isHidden     = GetBooleanNodeValue(propertyNode, "@isHidden");

                    CompositeProperty property = compositeType.Properties.FindCompositeProperty(typeName, propertyName);

                    if (property != null)
                    {
                        if (!String.IsNullOrEmpty(alias))
                        {
                            property.Alias = alias;
                        }
                        property.IsReadOnly = isReadOnly;
                        property.IsHidden   = isHidden;
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Adds to top new namespace into Namespaces property.
 /// </summary>
 /// <param name="ns"></param>
 /// <returns>true if added.</returns>
 public bool AddTopNamespace(string ns)
 {
     if (!String.IsNullOrWhiteSpace(ns) && !Namespaces.Contains(ns))
     {
         Namespaces.Insert(0, ns);
         return(true);
     }
     return(false);
 }
Пример #4
0
 private void AddAdditionalNamespaces(Type inputType)
 {
     if (!Assemblies.Contains(inputType.Assembly))
     {
         Assemblies.Add(inputType.Assembly);
     }
     if (!Namespaces.Contains(inputType.Namespace))
     {
         Namespaces.Add(inputType.Namespace);
     }
 }
Пример #5
0
        private void AddAssemblies(IEnumerable <Assembly> asseblies, IEnumerable <string> namespaces)
        {
            foreach (var additional in asseblies)
            {
                if (!Assemblies.Contains(additional))
                {
                    Assemblies.Add(additional);
                }
            }

            foreach (var namespaceAdditional in namespaces)
            {
                if (!Namespaces.Contains(namespaceAdditional))
                {
                    Namespaces.Add(namespaceAdditional);
                }
            }
        }
Пример #6
0
        protected override void ProcessRecord()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddDefaultCodeGen();
            HashSet <string> paths = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

            paths.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            paths.AddRange(AssemblyFolders);

            var  getAssemblyTypes = new GetAssemblyTypes(paths);
            Type converterType    = getAssemblyTypes.LoadTypeByName(Converter);

            if (!services.TryAddConverter(converterType))
            {
                throw new Exception($"codegen: Error loading converter type: ${converterType}");
            }
            var provider = services.BuildServiceProvider();


            Regex regex = null;
            IConvertObject <Type> converter = provider.GetRequiredService(converterType) as IConvertObject <Type>;

            if (!string.IsNullOrEmpty(Pattern))
            {
                regex = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            }

            getAssemblyTypes.LoadAssemblyTypes(TargetAssembly, type => {
                if (!type.IsAnonymousType() && !type.IsInterface && type.IsPublic)
                {
                    bool match = (Namespaces?.Count() ?? 0) == 0 || Namespaces.Contains(type.Namespace, StringComparer.InvariantCultureIgnoreCase);
                    if (match)
                    {
                        match = match && (regex == null || regex.IsMatch(type.FullName));
                    }
                    if (match)
                    {
                        var data = converter.Convert(type);
                        WriteObject(data);
                    }
                }
            });
        }
Пример #7
0
            private string ReadName()
            {
                int pos1 = CurrentIndex;

                if (IsNameLeadChar)
                {
                    ReadChar();
                }
                else
                {
                    return("");
                }
                while (IsNameChar)
                {
                    ReadChar();
                }
                int pos2 = -1;

                while (_c == '.' || _c == '/') // It is observed that a slash is used as the most left hand separator of components
                {
                    pos2 = CurrentIndex;
                    ReadChar();
                    if (IsNameLeadChar)
                    {
                        ReadChar();
                    }
                    else
                    {
                        throw new Exception("Name char is missing.");
                    }
                    while (IsNameChar)
                    {
                        ReadChar();
                    }
                }
                return(pos2 > 0 && Namespaces.Contains(Substring(pos1, pos2)) ? Substring(pos2 + 1) : Substring(pos1));
            }
Пример #8
0
        public ParserValue Get(ParserValue obj, string token)
        {
            if (!Enabled)
            {
                return(ParserValue.Empty);
            }

            if (obj.IsNull)
            {
                if (token == "#")
                {
                    obj = new StaticClass(token, ShortCut);
                }
                else if (TypeBinding.ContainsKey(token))
                {
                    Type type = (Type)TypeBinding[token];
                    obj = new StaticClass(token, type);
                }
                else if (CurrentScope.Contains(token))
                {
                    obj = CurrentScope.Get(token);
                }
                else if (Namespaces.Contains(token))
                {
                    obj = new ScopeClass(token, (ResourceScope)Namespaces[token]);
                }
                else
                {
                    Fail("Can't find field or type: " + token);
                }
                return(obj);
            }
            ParserValue result = ParserValue.Empty;

            if (obj is StaticClass)
            {
                Type type = (obj as StaticClass).Type;
                if (GetStaticField(type, token, out result))
                {
                    return(result);
                }
                obj = CurrentScope.Get((obj as StaticClass).Name);
            }
            else if (obj is ScopeClass)
            {
                ResourceScope scope = (obj as ScopeClass).Scope;
                if (scope.Contains(token))
                {
                    return(scope.Get(token));
                }
                string newScopeName = scope.Name + "." + token;
                if (Namespaces.Contains(newScopeName))
                {
                    return(new ScopeClass(newScopeName, (ResourceScope)Namespaces[newScopeName]));
                }
                else
                {
                    obj = CurrentScope.Get(scope.Name);
                }
            }
            if (!GetField(obj, token, out result))
            {
                Fail("Couldn't get field/property " + token + " for type " + obj.Type.ToString());
            }
            return(result);
        }