예제 #1
0
        public async Task GenerateCode([NotNull] ControllerGeneratorModel controllerGeneratorModel)
        {
            // Validate model
            string      validationMessage;
            ITypeSymbol model, dataContext;

            // Review: MVC scaffolding used ActiveProject's MSBuild RootNamespace property
            // That's not possible in command line scaffolding - the closest we can get is
            // the name of assembly??
            var appName             = _libraryManager.GetLibraryInformation(_applicationEnvironment.ApplicationName).Name;
            var controllerNameSpace = appName + "." + Constants.ControllersFolderName;

            var layoutDependencyInstaller = _typeActivator.CreateInstance <MvcLayoutDependencyInstaller>(_serviceProvider);

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

                ValidationUtil.TryValidateType(controllerGeneratorModel.DataContextClass, "dataContext", _modelTypesLocator, out dataContext, out validationMessage);
                await GenerateController(controllerGeneratorModel, model, dataContext, controllerNameSpace, layoutDependencyInstaller);
            }
            else
            {
                await layoutDependencyInstaller.Execute();
                await GenerateEmptyController(controllerGeneratorModel, controllerNameSpace);
            }

            await layoutDependencyInstaller.InstallDependencies();
        }
예제 #2
0
        public async Task GenerateCode([NotNull] ViewGeneratorModel viewGeneratorModel)
        {
            // Validate model
            string      validationMessage;
            ITypeSymbol model, dataContext;

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

            if (string.IsNullOrEmpty(viewGeneratorModel.ViewName))
            {
                throw new ArgumentException("The ViewName cannot be empty");
            }

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

            var outputPath = Path.Combine(
                _applicationEnvironment.ApplicationBasePath,
                Constants.ViewsFolderName,
                model.Name,
                viewGeneratorModel.ViewName + ".cshtml");

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

            var templateName = viewGeneratorModel.TemplateName + ".cshtml";

            var dbContextFullName = dataContext.FullNameForSymbol();
            var modelTypeFullName = model.FullNameForSymbol();

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

            var templateModel = new ViewGeneratorTemplateModel()
            {
                ViewDataTypeName         = modelTypeFullName,
                ViewName                 = viewGeneratorModel.ViewName,
                LayoutPageFile           = viewGeneratorModel.LayoutPage,
                IsLayoutPageSelected     = viewGeneratorModel.UseLayout,
                IsPartialView            = viewGeneratorModel.PartialView,
                ReferenceScriptLibraries = viewGeneratorModel.ReferenceScriptLibraries,
                ModelMetadata            = modelMetadata,
                JQueryVersion            = "1.10.2" //Todo
            };

            await _codeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, templateName, TemplateFolders, templateModel);
        }
예제 #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);
                }
            }
        }
예제 #4
0
        public async Task GenerateCode([NotNull] ViewGeneratorModel viewGeneratorModel)
        {
            // Validate model
            string      validationMessage;
            ITypeSymbol model, dataContext;

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

            if (string.IsNullOrEmpty(viewGeneratorModel.ViewName))
            {
                throw new ArgumentException("The ViewName cannot be empty");
            }

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

            if (string.IsNullOrEmpty(viewGeneratorModel.TemplateName))
            {
                throw new ArgumentException("The TemplateName cannot be empty");
            }

            ValidationUtil.TryValidateType(viewGeneratorModel.DataContextClass, "dataContext", _modelTypesLocator, out dataContext, out validationMessage);

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

            var appbasePath = _applicationEnvironment.ApplicationBasePath;
            var outputPath  = Path.Combine(
                appbasePath,
                Constants.ViewsFolderName,
                model.Name,
                viewGeneratorModel.ViewName + Constants.ViewExtension);

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

            var templateName = viewGeneratorModel.TemplateName + Constants.RazorTemplateExtension;

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

            var modelTypeFullName = model.ToDisplayString();

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

            var layoutDependencyInstaller = _typeActivator.CreateInstance <MvcLayoutDependencyInstaller>(_serviceProvider);

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

            await layoutDependencyInstaller.Execute();

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

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

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

            await layoutDependencyInstaller.InstallDependencies();
        }