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);

            EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies);

            modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetEFMetadata(
                viewGeneratorModel,
                _entityFrameworkService,
                _modelTypesLocator,
                string.Empty);

            await GenerateView(viewGeneratorModel, modelTypeAndContextModel, outputPath);

            if (modelTypeAndContextModel.ContextProcessingResult.ContextProcessingStatus == ContextProcessingStatus.ContextAddedButRequiresConfig)
            {
                throw new Exception(string.Format("{0} {1}", MessageStrings.ScaffoldingSuccessful_unregistered, MessageStrings.Scaffolding_additionalSteps));
            }
        }
        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);
            }

            var outputPath = ValidateAndGetOutputPath(viewGeneratorModel, outputFileName: viewGeneratorModel.ViewName + Constants.ViewExtension);
            var layoutDependencyInstaller = ActivatorUtilities.CreateInstance <MvcLayoutDependencyInstaller>(_serviceProvider);
            await layoutDependencyInstaller.Execute();

            await GenerateView(viewGeneratorModel, null, outputPath);

            await layoutDependencyInstaller.InstallDependencies();
        }
Пример #3
0
        private static ViewTemplate ValidateViewGeneratorModel(ViewGeneratorModel viewGeneratorModel)
        {
            if (viewGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(viewGeneratorModel));
            }

            if (string.IsNullOrEmpty(viewGeneratorModel.ViewName))
            {
                throw new ArgumentException(MessageStrings.ViewNameRequired);
            }

            var templateName = viewGeneratorModel.TemplateName;

            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentException(MessageStrings.TemplateNameRequired);
            }

            ViewTemplate viewTemplate;

            if (!ViewTemplate.ViewTemplateNames.TryGetValue(templateName, out viewTemplate))
            {
                throw new InvalidOperationException(string.Format(MessageStrings.InvalidViewTemplateName, templateName));
            }

            if (viewTemplate.IsModelRequired && string.IsNullOrEmpty(viewGeneratorModel.ModelClass))
            {
                throw new InvalidOperationException(string.Format(MessageStrings.ModelClassRequiredForTemplate, templateName));
            }

            return(viewTemplate);
        }
Пример #4
0
        protected IEnumerable <string> GetTemplateFoldersForContentVersion(ViewGeneratorModel model)
        {
            if (string.IsNullOrEmpty(model.BootstrapVersion))
            {   // for back-compat
                return(TemplateFolders);
            }

            string contentVersion = ContentVersionFromModel(model);

            // the default content is packaged under the default location (no subfolder)
            if (string.Equals(contentVersion, ViewGenerator.ContentVersionDefault, StringComparison.Ordinal))
            {
                return(TemplateFolders);
            }

            if (string.Equals(contentVersion, ViewGenerator.ContentVersionBootstrap3, StringComparison.Ordinal))
            {
                return(TemplateFoldersUtilities.GetTemplateFolders(
                           containingProject: Constants.ThisAssemblyName,
                           applicationBasePath: ApplicationInfo.ApplicationBasePath,
                           baseFolders: new[] { Path.Combine(ViewGenerator.VersionedContentRelativeBaseDir, ViewGenerator.ContentVersionBootstrap3) },
                           projectContext: _projectContext));
            }

            // this should be caught by ViewGenerator.ValidateViewGeneratorModel(), but best to be safe.
            throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, model.BootstrapVersion, string.Join(", ", ViewGenerator.ValidBootstrapVersions)));
        }
Пример #5
0
 protected ViewGeneratorModel(ViewGeneratorModel copyFrom)
     : base(copyFrom)
 {
     ViewName     = copyFrom.ViewName;
     TemplateName = copyFrom.TemplateName;
     PartialView  = copyFrom.PartialView;
 }
Пример #6
0
 protected ViewGeneratorModel(ViewGeneratorModel copyFrom)
     : base(copyFrom)
 {
     ViewName         = copyFrom.ViewName;
     TemplateName     = copyFrom.TemplateName;
     PartialView      = copyFrom.PartialView;
     BootstrapVersion = copyFrom.BootstrapVersion;
 }
        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);
        }
Пример #8
0
        protected async Task AddRequiredFiles(ViewGeneratorModel viewGeneratorModel)
        {
            IEnumerable <RequiredFileEntity> requiredFiles = GetRequiredFiles(viewGeneratorModel);

            foreach (var file in requiredFiles)
            {
                if (!File.Exists(Path.Combine(ApplicationInfo.ApplicationBasePath, file.OutputPath)))
                {
                    await _codeGeneratorActionsService.AddFileAsync(
                        Path.Combine(ApplicationInfo.ApplicationBasePath, file.OutputPath),
                        Path.Combine(TemplateFolders.First(), file.TemplateName));

                    _logger.LogMessage($"Added additional file :{file.OutputPath}");
                }
            }
        }
Пример #9
0
        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);
        }
Пример #10
0
 private static string ContentVersionFromModel(ViewGeneratorModel model)
 {
     if (string.IsNullOrEmpty(model.BootstrapVersion) ||
         string.Equals(model.BootstrapVersion, ViewGenerator.DefaultBootstrapVersion, StringComparison.Ordinal))
     {
         return(ViewGenerator.ContentVersionDefault);
     }
     else if (string.Equals(model.BootstrapVersion, "3", StringComparison.Ordinal))
     {
         return(ViewGenerator.ContentVersionBootstrap3);
     }
     else
     {
         // this should be caught by ViewGenerator.ValidateViewGeneratorModel(), but best to be safe.
         throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, model.BootstrapVersion, string.Join(", ", ViewGenerator.ValidBootstrapVersions)));
     }
 }
Пример #11
0
        protected ViewGeneratorTemplateModel GetViewGeneratorTemplateModel(ViewGeneratorModel viewGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel)
        {
            bool isLayoutSelected = !viewGeneratorModel.PartialView &&
                                    (viewGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(viewGeneratorModel.LayoutPage));

            ViewGeneratorTemplateModel templateModel = new ViewGeneratorTemplateModel()
            {
                ViewDataTypeName      = modelTypeAndContextModel?.ModelType?.FullName,
                ViewDataTypeShortName = modelTypeAndContextModel?.ModelType?.Name,
                ViewName                 = viewGeneratorModel.ViewName,
                LayoutPageFile           = viewGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = isLayoutSelected,
                IsPartialView            = viewGeneratorModel.PartialView,
                ReferenceScriptLibraries = viewGeneratorModel.ReferenceScriptLibraries,
                ModelMetadata            = modelTypeAndContextModel?.ContextProcessingResult?.ModelMetadata,
                JQueryVersion            = "1.10.2" //Todo
            };

            return(templateModel);
        }
Пример #12
0
        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);
            }

            var outputPath = ValidateAndGetOutputPath(viewGeneratorModel, outputFileName: viewGeneratorModel.ViewName + Constants.ViewExtension);

            await GenerateView(viewGeneratorModel, null, outputPath);
        }
Пример #13
0
        private static ViewTemplate ValidateViewGeneratorModel(ViewGeneratorModel viewGeneratorModel)
        {
            if (viewGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(viewGeneratorModel));
            }

            if (string.IsNullOrEmpty(viewGeneratorModel.ViewName))
            {
                throw new ArgumentException(MessageStrings.ViewNameRequired);
            }

            var templateName = viewGeneratorModel.TemplateName;

            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentException(MessageStrings.TemplateNameRequired);
            }

            ViewTemplate viewTemplate;

            if (!ViewTemplate.ViewTemplateNames.TryGetValue(templateName, out viewTemplate))
            {
                throw new InvalidOperationException(string.Format(MessageStrings.InvalidViewTemplateName, templateName));
            }

            if (viewTemplate.IsModelRequired && string.IsNullOrEmpty(viewGeneratorModel.ModelClass))
            {
                throw new InvalidOperationException(string.Format(MessageStrings.ModelClassRequiredForTemplate, templateName));
            }

            if (!string.IsNullOrEmpty(viewGeneratorModel.BootstrapVersion) && !ValidBootstrapVersions.Contains(viewGeneratorModel.BootstrapVersion))
            {
                throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, viewGeneratorModel.BootstrapVersion, string.Join(", ", ValidBootstrapVersions)));
            }

            return(viewTemplate);
        }
Пример #14
0
        public async Task GenerateCode(ViewGeneratorModel viewGeneratorModel)
        {
            var viewTemplate = ValidateViewGeneratorModel(viewGeneratorModel);

            ViewScaffolderBase scaffolder = null;

            if (viewTemplate.Name == ViewTemplate.EmptyViewTemplate.Name)
            {
                scaffolder = ActivatorUtilities.CreateInstance <EmptyViewScaffolder>(_serviceProvider);
            }
            else if (string.IsNullOrEmpty(viewGeneratorModel.DataContextClass))
            {
                scaffolder = ActivatorUtilities.CreateInstance <ModelBasedViewScaffolder>(_serviceProvider);
            }
            else
            {
                scaffolder = ActivatorUtilities.CreateInstance <EFModelBasedViewScaffolder>(_serviceProvider);
            }

            if (scaffolder != null)
            {
                await scaffolder.GenerateCode(viewGeneratorModel);
            }
        }
Пример #15
0
        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);

            modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetCodeModelMetadata(viewGeneratorModel, _codeModelService, _modelTypesLocator);

            await GenerateView(viewGeneratorModel, modelTypeAndContextModel, outputPath);
        }
Пример #16
0
        public 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;
            var outputPath = ValidateAndGetOutputPath(viewGeneratorModel, outputFileName: viewGeneratorModel.ViewName + Constants.ViewExtension);

            if (string.IsNullOrEmpty(viewGeneratorModel.DataContextClass))
            {
                modelTypeAndContextModel = await ValidateModelAndGetCodeModelMetadata(viewGeneratorModel);
            }
            else
            {
                modelTypeAndContextModel = await ValidateModelAndGetMetadata(viewGeneratorModel);
            }

            var layoutDependencyInstaller = ActivatorUtilities.CreateInstance <MvcLayoutDependencyInstaller>(_serviceProvider);
            await layoutDependencyInstaller.Execute();

            if (viewGeneratorModel.ViewName.EndsWith(Constants.ViewExtension, StringComparison.OrdinalIgnoreCase))
            {
                int viewNameLength = viewGeneratorModel.ViewName.Length - Constants.ViewExtension.Length;
                viewGeneratorModel.ViewName = viewGeneratorModel.ViewName.Substring(0, viewNameLength);
            }

            bool isLayoutSelected = !viewGeneratorModel.PartialView &&
                                    (viewGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(viewGeneratorModel.LayoutPage));

            var templateModel = new ViewGeneratorTemplateModel()
            {
                ViewDataTypeName      = modelTypeAndContextModel.ModelType.FullName,
                ViewDataTypeShortName = modelTypeAndContextModel.ModelType.Name,
                ViewName                 = viewGeneratorModel.ViewName,
                LayoutPageFile           = viewGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = isLayoutSelected,
                IsPartialView            = viewGeneratorModel.PartialView,
                ReferenceScriptLibraries = viewGeneratorModel.ReferenceScriptLibraries,
                ModelMetadata            = modelTypeAndContextModel.ContextProcessingResult.ModelMetadata,
                JQueryVersion            = "1.10.2" //Todo
            };

            var templateName = viewGeneratorModel.TemplateName + Constants.RazorTemplateExtension;
            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel);

            _logger.LogMessage("Added View : " + outputPath.Substring(ApplicationInfo.ApplicationBasePath.Length));

            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;
            }

            IEnumerable <string> templateFolders = GetTemplateFoldersForContentVersion(viewGeneratorModel);

            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);
        }
Пример #18
0
 protected override IEnumerable <RequiredFileEntity> GetRequiredFiles(ViewGeneratorModel viewGeneratorModel)
 {
     return(RequiredFiles);
 }
Пример #19
0
 protected abstract IEnumerable <RequiredFileEntity> GetRequiredFiles(ViewGeneratorModel viewGeneratorModel);
Пример #20
0
 public abstract Task GenerateCode(ViewGeneratorModel viewGeneratorModel);