private async Task GenerateViewsIfRequired(CommandLineGeneratorModel controllerGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, string controllerRootName) { if (!controllerGeneratorModel.IsRestController && !controllerGeneratorModel.NoViews) { var viewGenerator = ActivatorUtilities.CreateInstance <EFModelBasedViewScaffolder>(ServiceProvider); var areaPath = string.IsNullOrEmpty(_areaName) ? string.Empty : Path.Combine("Areas", _areaName); var viewBaseOutputPath = Path.Combine( ApplicationInfo.ApplicationBasePath, areaPath, Constants.ViewsFolderName, controllerRootName); var viewGeneratorModel = new ViewGeneratorModel() { UseDefaultLayout = controllerGeneratorModel.UseDefaultLayout, PartialView = false, LayoutPage = controllerGeneratorModel.LayoutPage, Force = controllerGeneratorModel.Force, RelativeFolderPath = viewBaseOutputPath, ReferenceScriptLibraries = controllerGeneratorModel.ReferenceScriptLibraries, BootstrapVersion = controllerGeneratorModel.BootstrapVersion }; var viewAndTemplateNames = new Dictionary <string, string>(); foreach (var viewTemplate in _views) { var viewName = viewTemplate == "List" ? "Index" : viewTemplate; viewAndTemplateNames.Add(viewName, viewTemplate); } await viewGenerator.GenerateViews(viewAndTemplateNames, viewGeneratorModel, modelTypeAndContextModel, viewBaseOutputPath); } }
protected override IEnumerable <RequiredFileEntity> GetRequiredFiles(ViewGeneratorModel viewGeneratorModel) { List <RequiredFileEntity> requiredFiles = new List <RequiredFileEntity>(); if (viewGeneratorModel.ReferenceScriptLibraries) { requiredFiles.Add(new RequiredFileEntity(@"Views/Shared/_ValidationScriptsPartial.cshtml", @"_ValidationScriptsPartial.cshtml")); } return(requiredFiles); }
/// <summary> /// GenerateView /// </summary> /// <param name="viewGeneratorModel"></param> /// <param name="modelTypeAndContextModel"></param> /// <param name="outputPath"></param> /// <returns></returns> /// <remarks> /// only this method, got from: https://github.com/aspnet/Scaffolding/blob/1.1.3/src/Microsoft.VisualStudio.Web.CodeGenerators.Mvc/View/ViewScaffolderBase.cs /// </remarks> internal async Task GenerateView(ViewGeneratorModel viewGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, string outputPath) { if (viewGeneratorModel.ViewName.EndsWith(Constants.ViewExtension, StringComparison.OrdinalIgnoreCase)) { int viewNameLength = viewGeneratorModel.ViewName.Length - Constants.ViewExtension.Length; viewGeneratorModel.ViewName = viewGeneratorModel.ViewName.Substring(0, viewNameLength); } var templateModel = GetViewGeneratorTemplateModel(viewGeneratorModel, modelTypeAndContextModel); var templateName = viewGeneratorModel.TemplateName + Constants.RazorTemplateExtension; await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel); _logger.LogMessage("Added View : " + outputPath.Substring(ApplicationInfo.ApplicationBasePath.Length)); await AddRequiredFiles(viewGeneratorModel); }
public override async Task GenerateCode(ViewGeneratorModel viewGeneratorModel) { if (viewGeneratorModel == null) { throw new ArgumentNullException(nameof(viewGeneratorModel)); } if (string.IsNullOrEmpty(viewGeneratorModel.ViewName)) { throw new ArgumentException(MessageStrings.ViewNameRequired); } if (string.IsNullOrEmpty(viewGeneratorModel.TemplateName)) { throw new ArgumentException(MessageStrings.TemplateNameRequired); } ModelTypeAndContextModel modelTypeAndContextModel = null; var outputPath = ValidateAndGetOutputPath(viewGeneratorModel, outputFileName: viewGeneratorModel.ViewName + Constants.ViewExtension); if (string.IsNullOrEmpty(viewGeneratorModel.DataContextClass)) { modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetCodeModelMetadata(viewGeneratorModel, _entityFrameworkService, _modelTypesLocator); } else { modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetEFMetadata( viewGeneratorModel, _entityFrameworkService, _modelTypesLocator, string.Empty); } var layoutDependencyInstaller = ActivatorUtilities.CreateInstance <MvcLayoutDependencyInstaller>(_serviceProvider); await layoutDependencyInstaller.Execute(); await GenerateView(viewGeneratorModel, modelTypeAndContextModel, outputPath); await layoutDependencyInstaller.InstallDependencies(); if (modelTypeAndContextModel.ContextProcessingResult.ContextProcessingStatus == ContextProcessingStatus.ContextAddedButRequiresConfig) { throw new Exception(string.Format("{0} {1}", MessageStrings.ScaffoldingSuccessful_unregistered, MessageStrings.Scaffolding_additionalSteps)); } }
/// <summary> /// Method exposed for adding multiple views in one operation. /// Utilised by the ControllerWithContextGenerator which generates 5 views for a MVC controller with context. /// </summary> /// <param name="viewsAndTemplates">Names of views and the corresponding template names</param> /// <param name="viewGeneratorModel">Model for View Generator</param> /// <param name="modelTypeAndContextModel">Model Type and DbContext metadata</param> /// <param name="baseOutputPath">Folder where all views will be generated</param> internal async Task GenerateViews(Dictionary <string, string> viewsAndTemplates, ViewGeneratorModel viewGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, string baseOutputPath) { if (viewsAndTemplates == null) { throw new ArgumentNullException(nameof(viewsAndTemplates)); } if (viewGeneratorModel == null) { throw new ArgumentNullException(nameof(viewsAndTemplates)); } if (modelTypeAndContextModel == null) { throw new ArgumentNullException(nameof(modelTypeAndContextModel)); } if (string.IsNullOrEmpty(baseOutputPath)) { baseOutputPath = ApplicationInfo.ApplicationBasePath; } foreach (var entry in viewsAndTemplates) { var viewName = entry.Key; var templateName = entry.Value; if (viewName.EndsWith(Constants.ViewExtension, StringComparison.OrdinalIgnoreCase)) { int viewNameLength = viewName.Length - Constants.ViewExtension.Length; viewName = viewName.Substring(0, viewNameLength); } var outputPath = Path.Combine(baseOutputPath, viewName + Constants.ViewExtension); bool isLayoutSelected = !viewGeneratorModel.PartialView && (viewGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(viewGeneratorModel.LayoutPage)); var templateModel = GetViewGeneratorTemplateModel(viewGeneratorModel, modelTypeAndContextModel); templateModel.ViewName = viewName; templateName = templateName + Constants.RazorTemplateExtension; await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel); _logger.LogMessage($"Added View : {outputPath.Substring(ApplicationInfo.ApplicationBasePath.Length)}"); } await AddRequiredFiles(viewGeneratorModel); }