public void Execute(TagHelperDescriptorProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var compilation = context.GetCompilation();

        if (compilation == null)
        {
            // No compilation, nothing to do.
            return;
        }

        var symbols = ComponentSymbols.Create(compilation);

        var types   = new List <INamedTypeSymbol>();
        var visitor = new ComponentTypeVisitor(symbols, types);

        var targetAssembly = context.Items.GetTargetAssembly();

        if (targetAssembly is not null)
        {
            visitor.Visit(targetAssembly.GlobalNamespace);
        }
        else
        {
            visitor.Visit(compilation.Assembly.GlobalNamespace);
            foreach (var reference in compilation.References)
            {
                if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                {
                    visitor.Visit(assembly.GlobalNamespace);
                }
            }
        }

        for (var i = 0; i < types.Count; i++)
        {
            var type = types[i];

            // Components have very simple matching rules.
            // 1. The type name (short) matches the tag name.
            // 2. The fully qualified name matches the tag name.
            var shortNameMatchingDescriptor = CreateShortNameMatchingDescriptor(symbols, type);
            context.Results.Add(shortNameMatchingDescriptor);
            var fullyQualifiedNameMatchingDescriptor = CreateFullyQualifiedNameMatchingDescriptor(symbols, type);
            context.Results.Add(fullyQualifiedNameMatchingDescriptor);

            foreach (var childContent in shortNameMatchingDescriptor.GetChildContentProperties())
            {
                // Synthesize a separate tag helper for each child content property that's declared.
                context.Results.Add(CreateChildContentDescriptor(symbols, shortNameMatchingDescriptor, childContent));
                context.Results.Add(CreateChildContentDescriptor(symbols, fullyQualifiedNameMatchingDescriptor, childContent));
            }
        }
    }
Пример #2
0
        public void Execute(TagHelperDescriptorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var compilation = context.GetCompilation();

            if (compilation == null)
            {
                // No compilation, nothing to do.
                return;
            }

            // We need to see private members too
            compilation = WithMetadataImportOptionsAll(compilation);

            var symbols = ComponentSymbols.Create(compilation);

            var types   = new List <INamedTypeSymbol>();
            var visitor = new ComponentTypeVisitor(symbols, types);

            // Visit the primary output of this compilation, as well as all references.
            visitor.Visit(compilation.Assembly);
            foreach (var reference in compilation.References)
            {
                // We ignore .netmodules here - there really isn't a case where they are used by user code
                // even though the Roslyn APIs all support them.
                if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                {
                    visitor.Visit(assembly);
                }
            }

            for (var i = 0; i < types.Count; i++)
            {
                var type = types[i];

                // Components have very simple matching rules.
                // 1. The type name (short) matches the tag name.
                // 2. The fully qualified name matches the tag name.
                var shortNameMatchingDescriptor = CreateShortNameMatchingDescriptor(symbols, type);
                context.Results.Add(shortNameMatchingDescriptor);
                var fullyQualifiedNameMatchingDescriptor = CreateFullyQualifiedNameMatchingDescriptor(symbols, type);
                context.Results.Add(fullyQualifiedNameMatchingDescriptor);

                foreach (var childContent in shortNameMatchingDescriptor.GetChildContentProperties())
                {
                    // Synthesize a separate tag helper for each child content property that's declared.
                    context.Results.Add(CreateChildContentDescriptor(symbols, shortNameMatchingDescriptor, childContent));
                    context.Results.Add(CreateChildContentDescriptor(symbols, fullyQualifiedNameMatchingDescriptor, childContent));
                }
            }
        }
Пример #3
0
        public void Execute(TagHelperDescriptorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var compilation = context.GetCompilation();

            if (compilation == null)
            {
                // No compilation, nothing to do.
                return;
            }

            var componentSymbol = compilation.GetTypeByMetadataName(TagHelperTypes.IComponent);

            if (componentSymbol == null || componentSymbol.TypeKind == TypeKind.Error)
            {
                // Could not find attributes we care about in the compilation. Nothing to do.
                return;
            }

            var types   = new List <INamedTypeSymbol>();
            var visitor = new ComponentTypeVisitor(componentSymbol, types);

            // We always visit the global namespace.
            visitor.Visit(compilation.Assembly.GlobalNamespace);

            foreach (var reference in compilation.References)
            {
                if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                {
                    if (IsTagHelperAssembly(assembly))
                    {
                        visitor.Visit(assembly.GlobalNamespace);
                    }
                }
            }

            for (var i = 0; i < types.Count; i++)
            {
                var type       = types[i];
                var descriptor = CreateDescriptor(type);

                if (descriptor != null)
                {
                    context.Results.Add(descriptor);
                }
            }
        }
Пример #4
0
        public void Execute(TagHelperDescriptorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var compilation = context.GetCompilation();

            if (compilation == null)
            {
                // No compilation, nothing to do.
                return;
            }

            var componentSymbol = compilation.GetTypeByMetadataName(BlazorApi.IComponent.MetadataName);

            if (componentSymbol == null)
            {
                // No definition for IComponent, nothing to do.
                return;
            }

            var types   = new List <INamedTypeSymbol>();
            var visitor = new ComponentTypeVisitor(componentSymbol, types);

            // Visit the primary output of this compilation, as well as all references.
            visitor.Visit(compilation.Assembly);
            foreach (var reference in compilation.References)
            {
                // We ignore .netmodules here - there really isn't a case where they are used by user code
                // even though the Roslyn APIs all support them.
                if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                {
                    visitor.Visit(assembly);
                }
            }

            for (var i = 0; i < types.Count; i++)
            {
                var type = types[i];
                context.Results.Add(CreateDescriptor(type));
            }
        }