Exemple #1
0
        public async Task Run(string projectPath)
        {
            var projectRoot = Path.GetDirectoryName(projectPath);

            // Load the project and check for compilation errors
            var workspace = MSBuildWorkspace.Create();

            DumpMsBuildAssemblies("clean workspace");

            var project = await workspace.OpenProjectAsync(projectPath);

            DumpMsBuildAssemblies("project loaded");
            if (workspace.Diagnostics.Count > 0)
            {
                var foundErrors = false;
                foreach (var diag in workspace.Diagnostics)
                {
                    Console.Error.WriteLine($"  {diag.Kind}: {diag.Message}");
                    if (diag.Kind == Microsoft.CodeAnalysis.WorkspaceDiagnosticKind.Failure)
                    {
                        foundErrors = true;
                    }
                }
                if (foundErrors)
                {
                    return;
                }
            }

            // Prep the project Compilation object, and process the Controller public methods list
            var compilation = await project.GetCompilationAsync() as CSharpCompilation;

            SyntaxNodeHelpers.PopulateControllerClassMethodNames(compilation);

            // Analyse the controllers in the project (updating them to be partial), as well as locate all the view files
            var controllers  = _controllerRewriter.RewriteControllers(compilation);
            var allViewFiles = _viewLocators.SelectMany(x => x.Find(projectRoot));

            // Assign view files to controllers
            foreach (var views in allViewFiles.GroupBy(v => new { v.AreaName, v.ControllerName }))
            {
                var controller = controllers
                                 .Where(c => string.Equals(c.Name, views.Key.ControllerName, StringComparison.OrdinalIgnoreCase))
                                 .Where(c => string.Equals(c.Area, views.Key.AreaName, StringComparison.OrdinalIgnoreCase))
                                 .FirstOrDefault();
                if (controller == null)
                {
                    controllers.Add(controller = new ControllerDefinition
                    {
                        Area = views.Key.AreaName,
                        Name = views.Key.ControllerName,
                    });
                }
                foreach (var view in views)
                {
                    controller.Views.Add(view);
                }
            }

            // Generate mappings for area names, to avoid clashes with controller names
            var areaMap = GenerateAreaMap(controllers);

            foreach (var controller in controllers.Where(a => !string.IsNullOrEmpty(a.Area)))
            {
                controller.AreaKey = areaMap[controller.Area];
            }

            // Generate the R4Mvc.generated.cs file
            _generatorService.Generate(projectRoot, controllers);
        }