Пример #1
0
        // Returns the set of template folders appropriate for templateModel.ContentVersion
        private IEnumerable <string> GetTemplateFoldersForContentVersion(IdentityGeneratorTemplateModel templateModel)
        {
            if (!(templateModel is IdentityGeneratorTemplateModel2 templateModel2))
            {   // for back-compat
                return(TemplateFolders);
            }

            // The default content is packaged under the default location "Identity\*" (no subfolder).
            if (string.Equals(templateModel2.ContentVersion, ContentVersionDefault, StringComparison.Ordinal))
            {
                return(TemplateFolders);
            }

            // For non-default bootstrap versions, the content is packaged under "Identity_Versioned\[Version_Identifier]\*"
            // Note: In the future, if content gets pivoted on things other than bootstrap, this logic will need enhancement.
            if (string.Equals(templateModel2.ContentVersion, ContentVersionBootstrap3, StringComparison.Ordinal))
            {
                return(TemplateFoldersUtilities.GetTemplateFolders(
                           Constants.ThisAssemblyName,
                           _applicationInfo.ApplicationBasePath,
                           new[] {
                    Path.Combine(VersionedContentRelativeBaseDir, $"Bootstrap{templateModel2.BootstrapVersion}")
                },
                           _projectContext));
            }

            // This should get caught by IdentityGeneratorTemplateModelBuilder.ValidateCommandLine() and emit the same error.
            // But best to be safe here.
            // Note: If we start pivoting content on things other than bootstrap version, this error message will need to be reworked.
            throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, templateModel2.BootstrapVersion, string.Join(", ", ValidBootstrapVersions)));
        }
        public async Task GenerateCode(GeneratorModel model)
        {
            var typeLocator = _serviceProvider.GetRequiredService <IModelTypesLocator>();

            var genModel = new Model()
            {
                Namespace = model.Namespace,
                Types     = typeLocator.GetAllTypes().Select(t =>
                                                             new MyType
                {
                    Name       = t.Name,
                    Properties = (t.TypeSymbol as INamedTypeSymbol)
                                 .GetMembers()
                                 .Where(m => m.Kind == SymbolKind.Property)
                                 .OfType <IPropertySymbol>()
                                 .Select(m => new MyProperty {
                        Name = m.Name, Type = m.Type.Name
                    }).ToArray()
                }).ToArray()
            };

            var applicationInfo = _serviceProvider.GetRequiredService <IApplicationInfo>();
            var projectInfo     = _serviceProvider.GetRequiredService <IProjectContext>();

            var templateFolders = TemplateFoldersUtilities
                                  .GetTemplateFolders("AdvancedScaffolder",
                                                      applicationInfo.ApplicationBasePath,
                                                      new string[] { "Adv" }, projectInfo);

            var actionService = _serviceProvider.GetRequiredService <ICodeGeneratorActionsService>();
            await actionService.AddFileFromTemplateAsync(@".\file.txt", "Template.cshtml", templateFolders, genModel);
        }
Пример #3
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)));
        }
Пример #4
0
        internal IEnumerable <string> GetTemplateFoldersForContentVersion()
        {
            string contentVersion   = null;
            string bootstrapVersion = null;

            if (TemplateModel is RazorPageGeneratorTemplateModel2 templateModel2)
            {
                bootstrapVersion = templateModel2.BootstrapVersion;
                contentVersion   = templateModel2.ContentVersion;
            }
            else if (TemplateModel is RazorPageWithContextTemplateModel2 templateWithContextModel2)
            {
                bootstrapVersion = templateWithContextModel2.BootstrapVersion;
                contentVersion   = templateWithContextModel2.ContentVersion;
            }
            else
            {   // for back-compat
                return(TemplateFolders);
            }

            // The default content is packaged under the default location "RazorPageGenerator\*" (no subfolder).
            // Note: In the future, if content gets pivoted on things other than bootstrap, this logic will need enhancement.
            if (string.Equals(contentVersion, ContentVersionDefault, StringComparison.Ordinal))
            {
                return(TemplateFolders);
            }

            // For non-default content versions, the content is packaged under "RazorPageGenerator_Versioned\[Version_Identifier]\*"
            if (string.Equals(contentVersion, ContentVersionBootstrap3, StringComparison.Ordinal))
            {
                return(TemplateFoldersUtilities.GetTemplateFolders(
                           containingProject: Constants.ThisAssemblyName,
                           applicationBasePath: ApplicationInfo.ApplicationBasePath,
                           baseFolders: new[] {
                    Path.Combine(VersionedContentRelativeBaseDir, $"Bootstrap{bootstrapVersion}")
                },
                           projectContext: _projectContext));
            }

            // Note: If we start pivoting content on things other than bootstrap version, this error message will need to be reworked.
            throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, bootstrapVersion, string.Join(", ", ValidBootstrapVersions)));
        }
Пример #5
0
        // Returns the root directory of the template folders appropriate for templateModel.ContentVersion
        private string GetTemplateFolderRootForContentVersion(IdentityGeneratorTemplateModel templateModel)
        {
            string relativePath = null;

            if (templateModel is IdentityGeneratorTemplateModel2 templateModel2)
            {
                if (string.Equals(templateModel2.ContentVersion, ContentVersionDefault, StringComparison.Ordinal))
                {
                    relativePath = DefaultContentRelativeBaseDir;
                }
                else if (string.Equals(templateModel2.ContentVersion, ContentVersionBootstrap3, StringComparison.Ordinal))
                {
                    // Note: In the future, if content gets pivoted on things other than bootstrap, this logic will need enhancement.
                    relativePath = Path.Combine(VersionedContentRelativeBaseDir, $"Bootstrap{templateModel2.BootstrapVersion}");
                }

                if (string.IsNullOrEmpty(relativePath))
                {
                    // This should get caught by IdentityGeneratorTemplateModelBuilder.ValidateCommandLine() and emit the same error.
                    // But best to be safe here.
                    // Note: If we start pivoting content on things other than bootstrap version, this error message will need to be reworked.
                    throw new InvalidOperationException(string.Format(MessageStrings.InvalidBootstrapVersionForScaffolding, templateModel2.BootstrapVersion, string.Join(", ", ValidBootstrapVersions)));
                }
            }
            else
            {
                relativePath = DefaultContentRelativeBaseDir;
            }

            return(TemplateFoldersUtilities.GetTemplateFolders(
                       Constants.ThisAssemblyName,
                       _applicationInfo.ApplicationBasePath,
                       new[] {
                relativePath
            },
                       _projectContext
                       ).First());
        }
        public void GenerateCode(AdvancedGeneratorModel model)
        {
            var types = modelTypesLocator.GetAllTypes();

            if (!string.IsNullOrEmpty(model.TypeFilter))
            {
                var regex = new Regex(model.TypeFilter);
                types = types.Where(t => regex.IsMatch(t.Name));
            }

            var templateModel = new TemplateModel
            {
                Namespace = model.Namespace,
                Types     = types.Select(t =>
                                         new TemplateModelType
                {
                    Name       = t.Name,
                    Properties = (t.TypeSymbol as INamedTypeSymbol)
                                 .GetMembers()
                                 .Where(m => m.Kind == SymbolKind.Property)
                                 .OfType <IPropertySymbol>()
                                 .Select(m => new TemplateModelProperty {
                        Name = m.Name, Type = m.Type.Name
                    }).ToArray()
                }).ToArray()
            };

            var templateFolders = TemplateFoldersUtilities
                                  .GetTemplateFolders(this.GetType().Assembly.GetName().Name,
                                                      applicationInfo.ApplicationBasePath,
                                                      new string[] { "Advanced" },
                                                      projectContext);

            var outputFilePath = Path.Combine(applicationInfo.ApplicationBasePath, model.OutputFile);

            codeGeneratorActionsService.AddFileFromTemplateAsync(outputFilePath, TemplateName, templateFolders, templateModel);
        }