public static IEnumerable <AreaDescriptor> DiscoverAreaControllerActions(Compilation compilation)
        {
            var allClasses = compilation.SyntaxTrees
                             .SelectMany(st => st.GetRoot().DescendantNodes().OfType <TypeDeclarationSyntax>())
                             .Select(st => compilation.GetSemanticModel(st.SyntaxTree).GetDeclaredSymbol(st));

            var controllerTypes = DiscoverControllers(allClasses);

            var controllersByArea = controllerTypes.GroupBy(GetAreaName);

            foreach (var areaControllers in controllersByArea)
            {
                var area = new AreaDescriptor(areaControllers.Key);

                foreach (var controllerSymbol in areaControllers)
                {
                    var controller = DiscoverControllerActions(controllerSymbol, area);

                    if (controller.Actions.Any())
                    {
                        area.Controllers.Add(controller);
                    }
                }

                if (area.Controllers.Any())
                {
                    yield return(area);
                }
            }
        }
Ejemplo n.º 2
0
        public static IEnumerable <AreaDescriptor> DiscoverAreaControllerActions(Compilation compilation, List <TypeDeclarationSyntax> possibleControllers)
        {
            var allClasses = possibleControllers
                             .Select(st => compilation.GetSemanticModel(st.SyntaxTree).GetDeclaredSymbol(st));

            var disposableDispose = (IMethodSymbol)compilation.GetSpecialType(SpecialType.System_IDisposable).GetMembers(nameof(IDisposable.Dispose)).First();

            var controllerTypes = DiscoverControllers(allClasses);

            var controllersByArea = controllerTypes.GroupBy(GetAreaName);

            foreach (var areaControllers in controllersByArea)
            {
                var area = new AreaDescriptor(areaControllers.Key);

                foreach (var controllerSymbol in areaControllers)
                {
                    var controller = DiscoverControllerActions(controllerSymbol, area, disposableDispose);

                    if (controller.Actions.Any())
                    {
                        area.Controllers.Add(controller);
                    }
                }

                if (area.Controllers.Any())
                {
                    yield return(area);
                }
            }
        }
Ejemplo n.º 3
0
        public static void WriteAreaActions(IndentedTextWriter writer, AreaDescriptor area)
        {
            CodeGenerator.WriteAreaClassStart(writer, $"{area.Name}UrlActions", $"{area.Name}Actions");

            foreach (var controller in area.Controllers)
            {
                WriteControllerActions(writer, controller);
            }

            CodeGenerator.WriteAreaClassEnd(writer);
        }
        public static void WriteAreaActions(IndentedTextWriter writer, AreaDescriptor area)
        {
            writer.WriteLine($"public static {area.Name}UrlActions {area.Name}Actions(this IUrlHelper urlHelper)");
            writer.Indent++;
            writer.WriteLine($"=> new {area.Name}UrlActions(urlHelper);");
            writer.Indent--;
            writer.WriteLineNoTabs("");

            writer.WriteLine($"public class {area.Name}UrlActions");
            writer.WriteLine("{");
            writer.Indent++;

            writer.WriteLine("private readonly IUrlHelper urlHelper;");
            writer.WriteLine($"public {area.Name}UrlActions(IUrlHelper urlHelper)");
            writer.WriteLine("{");
            writer.Indent++;
            writer.WriteLine("this.urlHelper = urlHelper;");
            writer.Indent--;
            writer.WriteLine("}\n");

            var first = true;

            foreach (var controller in area.Controllers)
            {
                if (!first)
                {
                    writer.WriteLineNoTabs("");
                }
                first = false;

                WriteControllerActions(writer, controller);
            }

            writer.Indent--;
            writer.WriteLine("}\n");
        }
 public ControllerDescriptor(AreaDescriptor area, string controllerName)
 {
     Area    = area;
     Name    = controllerName;
     Actions = new List <ActionDescriptor>();
 }
        public static ControllerDescriptor DiscoverControllerActions(ITypeSymbol controllerSymbol, AreaDescriptor area)
        {
            var controllerName = Regex.Replace(controllerSymbol.Name, "Controller$", "");
            var controller     = new ControllerDescriptor(area, controllerName);

            foreach (var actionSymbol in DiscoverActions(controllerSymbol))
            {
                var actionName = Regex.Replace(actionSymbol.Name, "Async$", "");
                var action     = new ActionDescriptor(controller, actionName);

                foreach (var parameterSymbol in actionSymbol.Parameters)
                {
                    action.Parameters.Add(new ParameterDescriptor(
                                              parameterSymbol.Name,
                                              GetParameterType(parameterSymbol.Type),
                                              parameterSymbol.HasExplicitDefaultValue,
                                              parameterSymbol.HasExplicitDefaultValue ? parameterSymbol.ExplicitDefaultValue : null));
                }

                controller.Actions.Add(action);
            }

            return(controller);
        }
Ejemplo n.º 7
0
        public static ControllerDescriptor DiscoverControllerActions(INamedTypeSymbol controllerSymbol, AreaDescriptor area, IMethodSymbol disposableDispose)
        {
            var controllerName = RouteDiscoverer.DiscoverControllerName(controllerSymbol);
            var controller     = new ControllerDescriptor(area, controllerName);

            foreach (var actionSymbol in DiscoverActions(controllerSymbol, disposableDispose))
            {
                var actionName = RouteDiscoverer.DiscoverActionName(actionSymbol);
                var action     = new ActionDescriptor(controller, actionName);

                foreach (var parameter in RouteDiscoverer.DiscoverMethodParameters(actionSymbol))
                {
                    action.Parameters.Add(parameter);
                }

                controller.Actions.Add(action);
            }

            return(controller);
        }