Esempio n. 1
0
        // TODO: rework this to use the TemplateModel, as opposed to the RazorPageGeneratorModel (command line model)
        internal async Task GenerateView(RazorPageGeneratorModel razorGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, string outputPath)
        {
            IEnumerable <string> templateFolders = GetTemplateFoldersForContentVersion();

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

            var templateName          = razorGeneratorModel.TemplateName + Constants.RazorTemplateExtension;
            var pageModelTemplateName = razorGeneratorModel.TemplateName + "PageModel" + Constants.RazorTemplateExtension;
            var pageModelOutputPath   = outputPath + ".cs";
            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, templateFolders, TemplateModel);

            _logger.LogMessage("Added RazorPage : " + outputPath.Substring(ApplicationInfo.ApplicationBasePath.Length));
            if (!razorGeneratorModel.NoPageModel)
            {
                await _codeGeneratorActionsService.AddFileFromTemplateAsync(pageModelOutputPath, pageModelTemplateName, templateFolders, TemplateModel);

                _logger.LogMessage("Added PageModel : " + pageModelOutputPath.Substring(ApplicationInfo.ApplicationBasePath.Length));
            }

            await AddRequiredFiles(razorGeneratorModel);
        }
Esempio n. 2
0
        internal async Task GenerateViews(RazorPageGeneratorModel razorPageGeneratorModel)
        {
            if (razorPageGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorPageGeneratorModel));
            }

            if (razorPageGeneratorModel.NoPageModel)
            {
                // Throw not supported exception.
                throw new ArgumentException(MessageStrings.PageModelFlagNotSupported);
            }

            IDictionary <string, string> viewAndTemplateNames = new Dictionary <string, string>();

            foreach (string viewTemplate in Views)
            {
                string viewName = viewTemplate == "List" ? "Index" : viewTemplate;
                viewAndTemplateNames.Add(viewName, viewTemplate);
            }

            ModelTypeAndContextModel modelTypeAndContextModel = null;
            string outputPath = ValidateAndGetOutputPath(razorPageGeneratorModel, string.Empty);

            EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies);

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

            await BaseGenerateViews(viewAndTemplateNames, razorPageGeneratorModel, modelTypeAndContextModel, outputPath);
        }
        public override async Task GenerateCode(RazorPageGeneratorModel razorGeneratorModel)
        {
            if (razorGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorGeneratorModel));
            }

            if (string.IsNullOrEmpty(razorGeneratorModel.RazorPageName))
            {
                throw new ArgumentException(MessageStrings.RazorPageNameRequired);
            }

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

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

            await GenerateView(razorGeneratorModel, null, outputPath);

            await layoutDependencyInstaller.InstallDependencies();
        }
 protected RazorPageGeneratorModel(RazorPageGeneratorModel copyFrom)
     : base(copyFrom)
 {
     RazorPageName = copyFrom.RazorPageName;
     TemplateName  = copyFrom.TemplateName;
     PartialView   = copyFrom.PartialView;
 }
        protected override IEnumerable<RequiredFileEntity> GetRequiredFiles(RazorPageGeneratorModel viewGeneratorModel)
        {
            List<RequiredFileEntity> requiredFiles = new List<RequiredFileEntity>();

            if (viewGeneratorModel.ReferenceScriptLibraries)
            {
                requiredFiles.Add(new RequiredFileEntity(Path.Combine("Pages","_ValidationScriptsPartial.cshtml"), @"_ValidationScriptsPartial.cshtml"));
            }

            return requiredFiles;
        }
Esempio n. 6
0
 private static string DetermineContentVersion(RazorPageGeneratorModel razorGeneratorModel)
 {
     if (string.Equals(razorGeneratorModel.BootstrapVersion, DefaultBootstrapVersion, StringComparison.Ordinal))
     {
         return(ContentVersionDefault);
     }
     else if (string.Equals(razorGeneratorModel.BootstrapVersion, "3", StringComparison.Ordinal))
     {
         return(ContentVersionBootstrap3);
     }
     else
     {
         throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, razorGeneratorModel.BootstrapVersion, string.Join(", ", ValidBootstrapVersions)));
     }
 }
Esempio n. 7
0
        protected async Task AddRequiredFiles(RazorPageGeneratorModel razorGeneratorModel)
        {
            IEnumerable <RequiredFileEntity> requiredFiles = GetRequiredFiles(razorGeneratorModel);

            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}");
                }
            }
        }
        public override async Task GenerateCode(RazorPageGeneratorModel razorGeneratorModel)
        {
            if (razorGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorGeneratorModel));
            }

            if (string.IsNullOrEmpty(razorGeneratorModel.RazorPageName))
            {
                throw new ArgumentException(MessageStrings.RazorPageNameRequired);
            }

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

            if (razorGeneratorModel.NoPageModel)
            {
                // Throw not supported exception.
                throw new ArgumentException(MessageStrings.PageModelFlagNotSupported);
            }

            ModelTypeAndContextModel modelTypeAndContextModel = null;
            var outputPath = ValidateAndGetOutputPath(razorGeneratorModel, outputFileName: razorGeneratorModel.RazorPageName + Constants.ViewExtension);

            EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies);

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

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

            await GenerateView(razorGeneratorModel, modelTypeAndContextModel, outputPath);

            await layoutDependencyInstaller.InstallDependencies();

            if (modelTypeAndContextModel.ContextProcessingResult.ContextProcessingStatus == ContextProcessingStatus.ContextAddedButRequiresConfig)
            {
                throw new Exception(string.Format("{0} {1}", MessageStrings.ScaffoldingSuccessful_unregistered, MessageStrings.Scaffolding_additionalSteps));
            }
        }
Esempio n. 9
0
        protected async Task AddRequiredFiles(RazorPageGeneratorModel razorGeneratorModel)
        {
            IEnumerable <RequiredFileEntity> requiredFiles = GetRequiredFiles(razorGeneratorModel);

            string templateFolderRoot = GetTemplateFoldersForContentVersion().First();

            foreach (var file in requiredFiles)
            {
                if (ShouldFileBeAdded(file))
                {
                    await _codeGeneratorActionsService.AddFileAsync(
                        Path.Combine(ApplicationInfo.ApplicationBasePath, file.OutputPath),
                        Path.Combine(templateFolderRoot, file.TemplateName));

                    _logger.LogMessage($"Added additional file :{file.OutputPath}");
                }
            }
        }
Esempio n. 10
0
        public override async Task GenerateCode(RazorPageGeneratorModel razorGeneratorModel)
        {
            if (razorGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorGeneratorModel));
            }

            if (string.IsNullOrEmpty(razorGeneratorModel.RazorPageName))
            {
                throw new ArgumentException(MessageStrings.RazorPageNameRequired);
            }

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

            var outputPath = ValidateAndGetOutputPath(razorGeneratorModel, outputFileName: razorGeneratorModel.RazorPageName + Constants.ViewExtension);

            await GenerateView(razorGeneratorModel, null, outputPath);
        }
Esempio n. 11
0
        public async Task GenerateCode(RazorPageGeneratorModel razorPageGeneratorModel)
        {
            if (razorPageGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorPageGeneratorModel));
            }

            if (string.IsNullOrEmpty(razorPageGeneratorModel.ModelClass))
            {
                if (string.IsNullOrEmpty(razorPageGeneratorModel.RazorPageName))
                {
                    throw new ArgumentException(MessageStrings.RazorPageNameRequired);
                }

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

                RazorPageScaffolderBase scaffolder = ActivatorUtilities.CreateInstance <EmptyRazorPageScaffolder>(_serviceProvider);
                await scaffolder.GenerateCode(razorPageGeneratorModel);
            }
            else
            {
                EFModelBasedRazorPageScaffolder scaffolder = ActivatorUtilities.CreateInstance <EFModelBasedRazorPageScaffolder>(_serviceProvider);

                if (!string.IsNullOrEmpty(razorPageGeneratorModel.TemplateName) && !string.IsNullOrEmpty(razorPageGeneratorModel.RazorPageName))
                {   // Razor page using EF
                    await scaffolder.GenerateCode(razorPageGeneratorModel);
                }
                else
                {   // Razor page CRUD using EF
                    await scaffolder.GenerateViews(razorPageGeneratorModel);
                }
            }
        }
Esempio n. 12
0
 protected override IEnumerable <RequiredFileEntity> GetRequiredFiles(RazorPageGeneratorModel razorGeneratorModel)
 {
     return(RequiredFiles);
 }
Esempio n. 13
0
        protected RazorPageWithContextTemplateModel GetRazorPageWithContextTemplateModel(RazorPageGeneratorModel razorGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel)
        {
            bool isLayoutSelected = !razorGeneratorModel.PartialView &&
                                    (razorGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(razorGeneratorModel.LayoutPage));

            var namespaceName = string.IsNullOrEmpty(razorGeneratorModel.NamespaceName)
                ? GetDefaultPageModelNamespaceName(razorGeneratorModel.RelativeFolderPath)
                : razorGeneratorModel.NamespaceName;

            if (string.IsNullOrEmpty(razorGeneratorModel.BootstrapVersion))
            {
                razorGeneratorModel.BootstrapVersion = RazorPageScaffolderBase.DefaultBootstrapVersion;
            }

            RazorPageWithContextTemplateModel2 templateModel = new RazorPageWithContextTemplateModel2(modelTypeAndContextModel.ModelType, modelTypeAndContextModel.DbContextFullName)
            {
                NamespaceName            = namespaceName,
                NoPageModel              = razorGeneratorModel.NoPageModel,
                PageModelClassName       = razorGeneratorModel.RazorPageName + "Model",
                ViewDataTypeName         = modelTypeAndContextModel?.ModelType?.FullName,
                ViewDataTypeShortName    = modelTypeAndContextModel?.ModelType?.Name,
                ContextTypeName          = modelTypeAndContextModel?.DbContextFullName,
                RazorPageName            = razorGeneratorModel.RazorPageName,
                LayoutPageFile           = razorGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = isLayoutSelected,
                IsPartialView            = razorGeneratorModel.PartialView,
                ReferenceScriptLibraries = razorGeneratorModel.ReferenceScriptLibraries,
                ModelMetadata            = modelTypeAndContextModel?.ContextProcessingResult?.ModelMetadata,
                JQueryVersion            = "1.10.2", //Todo
                BootstrapVersion         = razorGeneratorModel.BootstrapVersion,
                ContentVersion           = DetermineContentVersion(razorGeneratorModel)
            };

            return(templateModel);
        }
Esempio n. 14
0
        protected RazorPageGeneratorTemplateModel GetRazorPageViewGeneratorTemplateModel(RazorPageGeneratorModel razorGeneratorModel)
        {
            bool isLayoutSelected = !razorGeneratorModel.PartialView &&
                                    (razorGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(razorGeneratorModel.LayoutPage));

            var namespaceName = string.IsNullOrEmpty(razorGeneratorModel.NamespaceName)
                ? GetDefaultPageModelNamespaceName(razorGeneratorModel.RelativeFolderPath)
                : razorGeneratorModel.NamespaceName;

            if (string.IsNullOrEmpty(razorGeneratorModel.BootstrapVersion))
            {
                razorGeneratorModel.BootstrapVersion = RazorPageScaffolderBase.DefaultBootstrapVersion;
            }

            RazorPageGeneratorTemplateModel2 templateModel = new RazorPageGeneratorTemplateModel2()
            {
                NamespaceName            = namespaceName,
                NoPageModel              = razorGeneratorModel.NoPageModel,
                PageModelClassName       = razorGeneratorModel.RazorPageName + "Model",
                RazorPageName            = razorGeneratorModel.RazorPageName,
                LayoutPageFile           = razorGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = isLayoutSelected,
                IsPartialView            = razorGeneratorModel.PartialView,
                ReferenceScriptLibraries = razorGeneratorModel.ReferenceScriptLibraries,
                JQueryVersion            = "1.10.2", //Todo
                BootstrapVersion         = razorGeneratorModel.BootstrapVersion,
                ContentVersion           = DetermineContentVersion(razorGeneratorModel)
            };

            return(templateModel);
        }
Esempio n. 15
0
 protected abstract IEnumerable <RequiredFileEntity> GetRequiredFiles(RazorPageGeneratorModel razorGeneratorModel);
Esempio n. 16
0
        internal async Task BaseGenerateViews(IDictionary <string, string> viewsAndTemplates, RazorPageGeneratorModel razorPageGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, string baseOutputPath)
        {
            if (viewsAndTemplates == null)
            {
                throw new ArgumentNullException(nameof(viewsAndTemplates));
            }

            if (razorPageGeneratorModel == null)
            {
                throw new ArgumentNullException(nameof(razorPageGeneratorModel));
            }

            if (modelTypeAndContextModel == null)
            {
                throw new ArgumentNullException(nameof(modelTypeAndContextModel));
            }

            if (string.IsNullOrEmpty(baseOutputPath))
            {
                baseOutputPath = ApplicationInfo.ApplicationBasePath;
            }

            foreach (KeyValuePair <string, string> entry in viewsAndTemplates)
            {
                string viewName            = entry.Key;
                string templateName        = entry.Value;
                string outputPath          = Path.Combine(baseOutputPath, viewName + Constants.ViewExtension);
                var    pageModelOutputPath = outputPath + ".cs";

                bool isLayoutSelected = !razorPageGeneratorModel.PartialView &&
                                        (razorPageGeneratorModel.UseDefaultLayout || !string.IsNullOrEmpty(razorPageGeneratorModel.LayoutPage));

                RazorPageWithContextTemplateModel templateModel = GetRazorPageWithContextTemplateModel(razorPageGeneratorModel, modelTypeAndContextModel);
                templateModel.RazorPageName      = viewName;
                templateModel.PageModelClassName = viewName + "Model";
                var pageModelTemplateName = templateName + "PageModel" + Constants.RazorTemplateExtension;
                templateName = templateName + Constants.RazorTemplateExtension;

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

                _logger.LogMessage($"Added Razor Page : {outputPath.Substring(ApplicationInfo.ApplicationBasePath.Length)}");
                await _codeGeneratorActionsService.AddFileFromTemplateAsync(pageModelOutputPath, pageModelTemplateName, TemplateFolders, templateModel);

                _logger.LogMessage($"Added PageModel : {pageModelOutputPath.Substring(ApplicationInfo.ApplicationBasePath.Length)}");
            }

            await AddRequiredFiles(razorPageGeneratorModel);
        }
Esempio n. 17
0
 public abstract Task GenerateCode(RazorPageGeneratorModel razorGeneratorModel);
Esempio n. 18
0
        protected RazorPageGeneratorTemplateModel GetRazorPageViewGeneratorTemplateModel(RazorPageGeneratorModel razorGeneratorModel)
        {
            bool isLayoutSelected = !razorGeneratorModel.PartialView &&
                                    (razorGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(razorGeneratorModel.LayoutPage));

            var namespaceName = string.IsNullOrEmpty(razorGeneratorModel.NamespaceName)
                ? GetDefaultPageModelNamespaceName(razorGeneratorModel.RelativeFolderPath)
                : razorGeneratorModel.NamespaceName;

            RazorPageGeneratorTemplateModel templateModel = new RazorPageGeneratorTemplateModel()
            {
                NamespaceName            = namespaceName,
                NoPageModel              = razorGeneratorModel.NoPageModel,
                PageModelClassName       = razorGeneratorModel.RazorPageName + "Model",
                RazorPageName            = razorGeneratorModel.RazorPageName,
                LayoutPageFile           = razorGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = isLayoutSelected,
                IsPartialView            = razorGeneratorModel.PartialView,
                ReferenceScriptLibraries = razorGeneratorModel.ReferenceScriptLibraries,
                JQueryVersion            = "1.10.2" //Todo
            };

            return(templateModel);
        }