private async Task GenerateController(ControllerGeneratorModel controllerGeneratorModel,
                                              ITypeSymbol model,
                                              ITypeSymbol dataContext,
                                              string controllerNameSpace,
                                              MvcLayoutDependencyInstaller layoutDependencyInstaller)
        {
            if (string.IsNullOrEmpty(controllerGeneratorModel.ControllerName))
            {
                //Todo: Pluralize model name
                controllerGeneratorModel.ControllerName = model.Name + Constants.ControllerSuffix;
            }

            // Validation successful
            Contract.Assert(model != null, "Validation succeded but model type not set");

            string outputPath = ValidateAndGetOutputPath(controllerGeneratorModel);

            var dbContextFullName = dataContext != null?dataContext.ToDisplayString() : controllerGeneratorModel.DataContextClass;

            var modelTypeFullName = model.ToDisplayString();

            var modelMetadata = await _entityFrameworkService.GetModelMetadata(
                dbContextFullName,
                model);

            await layoutDependencyInstaller.Execute();

            var templateName  = "ControllerWithContext.cshtml";
            var templateModel = new ControllerGeneratorTemplateModel(model, dbContextFullName)
            {
                ControllerName      = controllerGeneratorModel.ControllerName,
                AreaName            = string.Empty, //ToDo
                UseAsync            = controllerGeneratorModel.UseAsync,
                ControllerNamespace = controllerNameSpace,
                ModelMetadata       = modelMetadata
            };

            var appBasePath = _applicationEnvironment.ApplicationBasePath;
            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel);

            _logger.LogMessage("Added Controller : " + outputPath.Substring(appBasePath.Length));

            if (!controllerGeneratorModel.NoViews)
            {
                foreach (var viewTemplate in _views)
                {
                    var viewName = viewTemplate == "List" ? "Index" : viewTemplate;
                    // ToDo: This is duplicated from ViewGenerator.
                    bool isLayoutSelected = controllerGeneratorModel.UseDefaultLayout ||
                                            !String.IsNullOrEmpty(controllerGeneratorModel.LayoutPage);

                    var viewTemplateModel = new ViewGeneratorTemplateModel()
                    {
                        ViewDataTypeName      = modelTypeFullName,
                        ViewDataTypeShortName = model.Name,
                        ViewName                 = viewName,
                        LayoutPageFile           = controllerGeneratorModel.LayoutPage,
                        IsLayoutPageSelected     = isLayoutSelected,
                        IsPartialView            = false,
                        ReferenceScriptLibraries = controllerGeneratorModel.ReferenceScriptLibraries,
                        ModelMetadata            = modelMetadata,
                        JQueryVersion            = "1.10.2"
                    };

                    var viewOutputPath = Path.Combine(
                        appBasePath,
                        Constants.ViewsFolderName,
                        templateModel.ControllerRootName,
                        viewName + Constants.ViewExtension);

                    await _codeGeneratorActionsService.AddFileFromTemplateAsync(viewOutputPath,
                                                                                viewTemplate + Constants.RazorTemplateExtension, TemplateFolders, viewTemplateModel);

                    _logger.LogMessage("Added View : " + viewOutputPath.Substring(appBasePath.Length));
                }
            }
        }
        private async Task GenerateController(ControllerGeneratorModel controllerGeneratorModel,
            ModelType model,
            ModelType dataContext,
            string controllerNameSpace,
            MvcLayoutDependencyInstaller layoutDependencyInstaller)
        {
            if (string.IsNullOrEmpty(controllerGeneratorModel.ControllerName))
            {
                //Todo: Pluralize model name
                controllerGeneratorModel.ControllerName = model.Name + Constants.ControllerSuffix;
            }

            // Validation successful
            Contract.Assert(model != null, "Validation succeded but model type not set");

            string outputPath = ValidateAndGetOutputPath(controllerGeneratorModel);

            var dbContextFullName = dataContext != null ? dataContext.FullName : controllerGeneratorModel.DataContextClass;
            var modelTypeFullName = model.FullName;

            var modelMetadata = await _entityFrameworkService.GetModelMetadata(
                dbContextFullName,
                model);

            await layoutDependencyInstaller.Execute();

            var templateName = "ControllerWithContext.cshtml";
            var templateModel = new ControllerGeneratorTemplateModel(model, dbContextFullName)
            {
                ControllerName = controllerGeneratorModel.ControllerName,
                AreaName = string.Empty, //ToDo
                UseAsync = controllerGeneratorModel.UseAsync,
                ControllerNamespace = controllerNameSpace,
                ModelMetadata = modelMetadata
            };

            var appBasePath = _applicationEnvironment.ApplicationBasePath;
            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel);
            _logger.LogMessage("Added Controller : " + outputPath.Substring(appBasePath.Length));

            if (!controllerGeneratorModel.NoViews)
            {
                foreach (var viewTemplate in _views)
                {
                    var viewName = viewTemplate == "List" ? "Index" : viewTemplate;
                    // ToDo: This is duplicated from ViewGenerator.
                    bool isLayoutSelected = controllerGeneratorModel.UseDefaultLayout ||
                        !String.IsNullOrEmpty(controllerGeneratorModel.LayoutPage);

                    var viewTemplateModel = new ViewGeneratorTemplateModel()
                    {
                        ViewDataTypeName = modelTypeFullName,
                        ViewDataTypeShortName = model.Name,
                        ViewName = viewName,
                        LayoutPageFile = controllerGeneratorModel.LayoutPage,
                        IsLayoutPageSelected = isLayoutSelected,
                        IsPartialView = false,
                        ReferenceScriptLibraries = controllerGeneratorModel.ReferenceScriptLibraries,
                        ModelMetadata = modelMetadata,
                        JQueryVersion = "1.10.2"
                    };

                    var viewOutputPath = Path.Combine(
                        appBasePath,
                        Constants.ViewsFolderName,
                        templateModel.ControllerRootName,
                        viewName + Constants.ViewExtension);

                    await _codeGeneratorActionsService.AddFileFromTemplateAsync(viewOutputPath,
                        viewTemplate + Constants.RazorTemplateExtension, TemplateFolders, viewTemplateModel);

                    _logger.LogMessage("Added View : " + viewOutputPath.Substring(appBasePath.Length));
                }
            }
        }
示例#3
0
        public async Task GenerateCode([NotNull] ControllerGeneratorModel controllerGeneratorModel)
        {
            // Validate model
            string      validationMessage;
            ITypeSymbol model, dataContext;

            if (!ValidationUtil.TryValidateType(controllerGeneratorModel.ModelClass, "model", _modelTypesLocator, out model, out validationMessage))
            {
                throw new ArgumentException(validationMessage);
            }

            if (string.IsNullOrWhiteSpace(controllerGeneratorModel.DataContextClass))
            {
                throw new ArgumentException("Please provide a name for DataContext");
            }
            ValidationUtil.TryValidateType(controllerGeneratorModel.DataContextClass, "dataContext", _modelTypesLocator, out dataContext, out validationMessage);

            if (string.IsNullOrEmpty(controllerGeneratorModel.ControllerName))
            {
                //Todo: Pluralize model name
                controllerGeneratorModel.ControllerName = model.Name + Constants.ControllerSuffix;
            }

            // Validation successful
            Contract.Assert(model != null, "Validation succeded but model type not set");

            var outputPath = Path.Combine(
                _applicationEnvironment.ApplicationBasePath,
                Constants.ControllersFolderName,
                controllerGeneratorModel.ControllerName + ".cs");

            if (File.Exists(outputPath) && !controllerGeneratorModel.Force)
            {
                throw new InvalidOperationException(string.Format(
                                                        CultureInfo.CurrentCulture,
                                                        "View file {0} exists, use -f option to overwrite",
                                                        outputPath));
            }

            var dbContextFullName = dataContext != null?dataContext.FullNameForSymbol() : controllerGeneratorModel.DataContextClass;

            var modelTypeFullName = model.FullNameForSymbol();

            var modelMetadata = await _entityFrameworkService.GetModelMetadata(
                dbContextFullName,
                model);

            var templateName  = "ControllerWithContext.cshtml";
            var templateModel = new ControllerGeneratorTemplateModel(model, dbContextFullName)
            {
                ControllerName = controllerGeneratorModel.ControllerName,
                AreaName       = string.Empty, //ToDo
                UseAsync       = controllerGeneratorModel.UseAsync,
                ModelMetadata  = modelMetadata
            };

            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel);

            if (!controllerGeneratorModel.NoViews)
            {
                foreach (var viewTemplate in _views)
                {
                    var viewName = viewTemplate == "List" ? "Index" : viewTemplate;
                    // ToDo: This is duplicated from ViewGenerator.
                    var viewTemplateModel = new ViewGeneratorTemplateModel()
                    {
                        ViewDataTypeName         = modelTypeFullName,
                        ViewName                 = viewName,
                        LayoutPageFile           = controllerGeneratorModel.LayoutPage,
                        IsLayoutPageSelected     = controllerGeneratorModel.UseLayout,
                        IsPartialView            = false,
                        ReferenceScriptLibraries = controllerGeneratorModel.ReferenceScriptLibraries,
                        ModelMetadata            = modelMetadata,
                        JQueryVersion            = "1.10.2"
                    };

                    var viewOutputPath = Path.Combine(
                        _applicationEnvironment.ApplicationBasePath,
                        Constants.ViewsFolderName,
                        model.Name,
                        viewName + ".cshtml");

                    await _codeGeneratorActionsService.AddFileFromTemplateAsync(viewOutputPath,
                                                                                viewTemplate + ".cshtml", TemplateFolders, viewTemplateModel);
                }
            }
        }