Example #1
0
        private ModuleCatalog(IExceptionContext ectx)
        {
            Contracts.AssertValue(ectx);

            _entryPointMap = new Dictionary <string, EntryPointInfo>();
            _componentMap  = new Dictionary <string, ComponentInfo>();
            _components    = new List <ComponentInfo>();

            var moduleClasses = ComponentCatalog.FindLoadableClasses <SignatureEntryPointModule>();
            var entryPoints   = new List <EntryPointInfo>();

            foreach (var lc in moduleClasses)
            {
                var type = lc.LoaderType;

                // Scan for entry points.
                foreach (var methodInfo in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
                {
                    var attr = methodInfo.GetCustomAttributes(typeof(TlcModule.EntryPointAttribute), false).FirstOrDefault() as TlcModule.EntryPointAttribute;
                    if (attr == null)
                    {
                        continue;
                    }

                    var info = new EntryPointInfo(ectx, methodInfo, attr,
                                                  methodInfo.GetCustomAttributes(typeof(ObsoleteAttribute), false).FirstOrDefault() as ObsoleteAttribute);

                    entryPoints.Add(info);
                    if (_entryPointMap.ContainsKey(info.Name))
                    {
                        // Duplicate entry point name. We need to show a warning here.
                        // REVIEW: we will be able to do this once catalog becomes a part of env.
                        continue;
                    }

                    _entryPointMap[info.Name] = info;
                }

                // Scan for components.
                // First scan ourself, and then all nested types, for component info.
                ScanForComponents(ectx, type);
                foreach (var nestedType in type.GetTypeInfo().GetNestedTypes())
                {
                    ScanForComponents(ectx, nestedType);
                }
            }
            _entryPoints = entryPoints.ToArray();
        }
        private static TRes CreateCore <TRes, TArgs, TSig>(IHostEnvironment env, TArgs args, out string loadName, params object[] extraArgs)
            where TRes : class
            where TArgs : class, new()
        {
            env.CheckValue(args, nameof(args));

            var classes = ComponentCatalog.FindLoadableClasses <TArgs, TSig>();

            if (classes.Length == 0)
            {
                throw env.Except("Couldn't find a {0} class that accepts {1} as arguments.", typeof(TRes).Name, typeof(TArgs).FullName);
            }
            if (classes.Length > 1)
            {
                throw env.Except("Found too many {0} classes that accept {1} as arguments.", typeof(TRes).Name, typeof(TArgs).FullName);
            }

            var lc = classes[0];

            loadName = lc.LoadNames[0];
            return(lc.CreateInstance <TRes>(env, args, extraArgs));
        }
Example #3
0
        private void ShowHelp(IndentingTextWriter writer, int?columns = null)
        {
            _env.AssertValue(_component);

            string name = _component.Trim();

            string sig = _kind?.ToLowerInvariant();

            // Note that we don't check IsHidden here. The current policy is when IsHidden is true, we don't
            // show the item in "list all" functionality, but will still show help when explicitly requested.

            var infos = ComponentCatalog.FindLoadableClasses(name)
                        .OrderBy(x => ComponentCatalog.SignatureToString(x.SignatureTypes[0]).ToLowerInvariant());
            var kinds      = new StringBuilder();
            var components = new List <Component>();

            foreach (var info in infos)
            {
                _env.AssertValue(info.SignatureTypes);
                kinds.Clear();
                bool foundSig = false;
                foreach (var signature in info.SignatureTypes)
                {
                    _env.Assert(signature.BaseType == typeof(MulticastDelegate));

                    string kind;
                    if (signature == typeof(SignatureDefault))
                    {
                        kind = "Component";
                        if (sig == null || "default".StartsWithInvariantCulture(sig))
                        {
                            foundSig = true;
                        }
                    }
                    else
                    {
                        kind = ComponentCatalog.SignatureToString(signature);
                        if (sig == null || kind.StartsWithInvariantCultureIgnoreCase(sig))
                        {
                            foundSig = true;
                        }
                    }

                    if (kinds.Length > 0)
                    {
                        kinds.Append(", ");
                    }
                    kinds.Append(kind);
                }
                if (foundSig)
                {
                    string kindsStr = kinds.ToString();
                    var    args     = info.CreateArguments();

                    ShowUsage(writer, kindsStr, info.Summary, info.LoadNames[0], info.LoadNames, args, columns);
                    components.Add(new Component(kindsStr, info, args));
                }
            }

            if (components.Count == 0)
            {
                writer.WriteLine("Unknown component: '{0}'", name);
            }
            else
            {
                Serialize(components);
            }
        }