Ejemplo n.º 1
0
        private void ShowFileList()
        {
            _logger.LogMessage("File List:");
            var files = IdentityGeneratorFilesConfig.GetFilesToList();

            _logger.LogMessage(string.Join(Environment.NewLine, files));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a folder hierarchy:
        ///     ProjectDir
        ///        \ Areas
        ///            \ IdentityAreaName
        ///                \ Data
        ///                \ Pages
        ///                \ Services
        /// </summary>
        private void EnsureFolderLayout(string identityAreaName, IdentityGeneratorTemplateModel templateModel)
        {
            var areaBasePath = Path.Combine(_applicationInfo.ApplicationBasePath, "Areas");

            if (!_fileSystem.DirectoryExists(areaBasePath))
            {
                _fileSystem.CreateDirectory(areaBasePath);
            }

            var areaPath = Path.Combine(areaBasePath, identityAreaName);

            if (!_fileSystem.DirectoryExists(areaPath))
            {
                _fileSystem.CreateDirectory(areaPath);
            }

            var areaFolders = IdentityGeneratorFilesConfig.GetAreaFolders(
                !templateModel.IsUsingExistingDbContext);

            foreach (var areaFolder in areaFolders)
            {
                var path = Path.Combine(areaPath, areaFolder);
                if (!_fileSystem.DirectoryExists(path))
                {
                    _logger.LogMessage($"Adding folder: {path}", LogMessageLevel.Trace);
                    _fileSystem.CreateDirectory(path);
                }
            }
        }
Ejemplo n.º 3
0
        private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTemplateModel templateModel)
        {
            var filesToGenerate = new List <IdentityGeneratorFile>(IdentityGeneratorFilesConfig.GetFilesToGenerate(NamedFiles, templateModel));

            // Check if we need to add ViewImports and which ones.
            if (!_commandlineModel.UseDefaultUI)
            {
                filesToGenerate.AddRange(IdentityGeneratorFilesConfig.GetViewImports(filesToGenerate, _fileSystem, _applicationInfo.ApplicationBasePath));
            }

            if (IdentityGeneratorFilesConfig.TryGetLayoutPeerFiles(_fileSystem, _applicationInfo.ApplicationBasePath, templateModel, out IReadOnlyList <IdentityGeneratorFile> layoutPeerFiles))
            {
                filesToGenerate.AddRange(layoutPeerFiles);
            }

            if (IdentityGeneratorFilesConfig.TryGetCookieConsentPartialFile(_fileSystem, _applicationInfo.ApplicationBasePath, templateModel, out IdentityGeneratorFile cookieConsentPartialConfig))
            {
                filesToGenerate.Add(cookieConsentPartialConfig);
            }

            var filesToGenerateArray = filesToGenerate.ToArray();

            ValidateFilesToGenerate(filesToGenerateArray);

            return(filesToGenerateArray);
        }
Ejemplo n.º 4
0
        private void ValidateFilesOption(IdentityGeneratorTemplateModel templateModel)
        {
            var errors = new List <string>();

            string contentVersion;

            if (templateModel is IdentityGeneratorTemplateModel2 templateModel2)
            {
                contentVersion = templateModel2.ContentVersion;
            }
            else
            {
                contentVersion = IdentityGenerator.ContentVersionDefault;
            }

            var invalidFiles = NamedFiles.Where(f => !IdentityGeneratorFilesConfig.GetFilesToList(contentVersion).Contains(f));

            if (invalidFiles.Any())
            {
                errors.Add(MessageStrings.InvalidFilesListMessage);
                errors.AddRange(invalidFiles);
            }

            if (errors.Any())
            {
                throw new InvalidOperationException(string.Join(Environment.NewLine, errors));
            }
        }
        private void ValidateFilesOption()
        {
            var errors = new List <string>();

            NamedFiles = _commandlineModel.Files.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            var invalidFiles = NamedFiles.Where(f => !IdentityGeneratorFilesConfig.GetFilesToList().Contains(f));

            if (invalidFiles.Any())
            {
                errors.Add(MessageStrings.InvalidFilesListMessage);
                errors.AddRange(invalidFiles);
            }

            if (errors.Any())
            {
                throw new InvalidOperationException(string.Join(Environment.NewLine, errors));
            }
        }
Ejemplo n.º 6
0
        private async Task AddTemplateFiles(IdentityGeneratorTemplateModel templateModel)
        {
            var templates  = IdentityGeneratorFilesConfig.GetTemplateFiles(templateModel);
            var projectDir = Path.GetDirectoryName(_projectContext.ProjectFullPath);

            foreach (var template in templates)
            {
                _logger.LogMessage($"Adding template: {template.Key}", LogMessageLevel.Trace);
                await _codegeneratorActionService.AddFileFromTemplateAsync(
                    Path.Combine(projectDir, template.Value),
                    template.Key,
                    TemplateFolders,
                    templateModel);
            }

            _connectionStringsWriter.AddConnectionString(
                connectionStringName: $"{templateModel.DbContextClass}Connection",
                dataBaseName: templateModel.ApplicationName,
                useSQLite: templateModel.UseSQLite);
        }
Ejemplo n.º 7
0
        private void ShowFileList(string commandBootstrapVersion)
        {
            string contentVersion = string.Equals(commandBootstrapVersion, "3", StringComparison.Ordinal)
                ? ContentVersionBootstrap3
                : ContentVersionDefault;

            _logger.LogMessage("File List:");

            IEnumerable <string> files = IdentityGeneratorFilesConfig.GetFilesToList(contentVersion);

            _logger.LogMessage(string.Join(Environment.NewLine, files));

            if (_fileSystem is SimulationModeFileSystem simModefileSystem)
            {
                foreach (string fileName in files)
                {
                    simModefileSystem.AddMetadataMessage(fileName);
                }
            }
        }
Ejemplo n.º 8
0
        private void ShowFileList(IdentityGeneratorTemplateModel templateModel)
        {
            _logger.LogMessage("File List:");

            string contentVersion;

            // For back-compat
            if (templateModel is IdentityGeneratorTemplateModel2 templateModel2)
            {
                contentVersion = templateModel2.ContentVersion;
            }
            else
            {
                contentVersion = ContentVersionDefault;
            }

            var files = IdentityGeneratorFilesConfig.GetFilesToList(contentVersion);

            _logger.LogMessage(string.Join(Environment.NewLine, files));
        }
Ejemplo n.º 9
0
        public async Task <IdentityGeneratorTemplateModel> ValidateAndBuild()
        {
            ValidateCommandLine(_commandlineModel);
            RootNamespace = string.IsNullOrEmpty(_commandlineModel.RootNamespace)
                ? _projectContext.RootNamespace
                : _commandlineModel.RootNamespace;

            ValidateRequiredDependencies(_commandlineModel.UseSqlite);

            var defaultDbContextNamespace = $"{RootNamespace}.Areas.Identity.Data";

            IsUsingExistingDbContext = false;
            if (IsDbContextSpecified)
            {
                var existingDbContext = await FindExistingType(_commandlineModel.DbContext);

                if (existingDbContext == null)
                {
                    // We need to create one with what the user specified.
                    DbContextClass     = GetClassNameFromTypeName(_commandlineModel.DbContext);
                    DbContextNamespace = GetNamespaceFromTypeName(_commandlineModel.DbContext)
                                         ?? defaultDbContextNamespace;
                }
                else
                {
                    ValidateExistingDbContext(existingDbContext);
                    IsGenerateCustomUser     = false;
                    IsUsingExistingDbContext = true;
                    UserType           = FindUserTypeFromDbContext(existingDbContext);
                    DbContextClass     = existingDbContext.Name;
                    DbContextNamespace = existingDbContext.Namespace;
                }
            }
            else
            {
                // --dbContext paramter was not specified. So we need to generate one using convention.
                DbContextClass     = GetDefaultDbContextName();
                DbContextNamespace = defaultDbContextNamespace;
            }

            // if an existing user class was determined from the DbContext, don't try to get it from here.
            // Identity scaffolding must use the user class tied to the existing DbContext (when there is one).
            if (string.IsNullOrEmpty(UserClass))
            {
                if (string.IsNullOrEmpty(_commandlineModel.UserClass))
                {
                    IsGenerateCustomUser = false;
                    UserClass            = "IdentityUser";
                    UserClassNamespace   = "Microsoft.AspNetCore.Identity";
                }
                else
                {
                    var existingUser = await FindExistingType(_commandlineModel.UserClass);

                    if (existingUser != null)
                    {
                        ValidateExistingUserType(existingUser);
                        IsGenerateCustomUser = false;
                        UserType             = existingUser;
                    }
                    else
                    {
                        IsGenerateCustomUser = true;
                        UserClass            = GetClassNameFromTypeName(_commandlineModel.UserClass);
                        UserClassNamespace   = GetNamespaceFromTypeName(_commandlineModel.UserClass)
                                               ?? defaultDbContextNamespace;
                    }
                }
            }

            if (_commandlineModel.UseDefaultUI)
            {
                ValidateDefaultUIOption();
            }

            bool hasExistingLayout = DetermineSupportFileLocation(out string supportFileLocation, out string layout);

            string boostrapVersion = string.IsNullOrEmpty(_commandlineModel.BootstrapVersion) ? IdentityGenerator.DefaultBootstrapVersion : _commandlineModel.BootstrapVersion;

            var templateModel = new IdentityGeneratorTemplateModel2()
            {
                ApplicationName          = _applicationInfo.ApplicationName,
                DbContextClass           = DbContextClass,
                DbContextNamespace       = DbContextNamespace,
                UserClass                = UserClass,
                UserClassNamespace       = UserClassNamespace,
                UseSQLite                = _commandlineModel.UseSqlite,
                IsUsingExistingDbContext = IsUsingExistingDbContext,
                Namespace                = RootNamespace,
                IsGenerateCustomUser     = IsGenerateCustomUser,
                UseDefaultUI             = _commandlineModel.UseDefaultUI,
                GenerateLayout           = !hasExistingLayout,
                Layout = layout,
                LayoutPageNoExtension      = Path.GetFileNameWithoutExtension(layout),
                SupportFileLocation        = supportFileLocation,
                HasExistingNonEmptyWwwRoot = HasExistingNonEmptyWwwRootDirectory,
                BootstrapVersion           = boostrapVersion,
                IsRazorPagesProject        = IsRazorPagesLayout()
            };

            templateModel.ContentVersion = DetermineContentVersion(templateModel);

            ValidateIndividualFileOptions();
            if (!string.IsNullOrEmpty(_commandlineModel.Files))
            {
                NamedFiles = _commandlineModel.Files.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            }
            else if (!string.IsNullOrEmpty(_commandlineModel.ExcludeFiles))
            {
                string contentVersion;
                if (templateModel is IdentityGeneratorTemplateModel2 templateModel2)
                {
                    contentVersion = templateModel2.ContentVersion;
                }
                else
                {
                    contentVersion = IdentityGenerator.ContentVersionDefault;
                }
                IEnumerable <string> excludedFiles = _commandlineModel.ExcludeFiles.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList();
                IEnumerable <string> allFiles      = IdentityGeneratorFilesConfig.GetFilesToList(contentVersion);
                //validate excluded files
                var errors       = new List <string>();
                var invalidFiles = excludedFiles.Where(f => !allFiles.Contains(f));
                if (invalidFiles.Any())
                {
                    errors.Add(MessageStrings.InvalidFilesListMessage);
                    errors.AddRange(invalidFiles);
                }

                if (errors.Any())
                {
                    throw new InvalidOperationException(string.Join(Environment.NewLine, errors));
                }

                //get files to overwrite
                NamedFiles = allFiles.Except(excludedFiles);
            }

            templateModel.FilesToGenerate = DetermineFilesToGenerate(templateModel);

            if (IsFilesSpecified)
            {
                ValidateFilesOption(templateModel);
            }

            if (IsExcludeSpecificed)
            {
                ValidateFilesOption(templateModel);
            }

            return(templateModel);
        }
        public async Task <IdentityGeneratorTemplateModel> ValidateAndBuild()
        {
            ValidateCommandLine(_commandlineModel);
            RootNamespace = string.IsNullOrEmpty(_commandlineModel.RootNamespace)
                ? _projectContext.RootNamespace
                : _commandlineModel.RootNamespace;

            ValidateRequiredDependencies(_commandlineModel.UseSQLite);

            var defaultDbContextNamespace = $"{RootNamespace}.Areas.Identity.Data";

            IsUsingExistingDbContext = false;
            if (IsDbContextSpecified)
            {
                var existingDbContext = await FindExistingType(_commandlineModel.DbContext);

                if (existingDbContext == null)
                {
                    // We need to create one with what the user specified.
                    DbContextClass     = GetClassNameFromTypeName(_commandlineModel.DbContext);
                    DbContextNamespace = GetNamespaceFromTypeName(_commandlineModel.DbContext)
                                         ?? defaultDbContextNamespace;
                }
                else
                {
                    ValidateExistingDbContext(existingDbContext);
                    IsGenerateCustomUser     = false;
                    IsUsingExistingDbContext = true;
                    UserType           = FindUserTypeFromDbContext(existingDbContext);
                    DbContextClass     = existingDbContext.Name;
                    DbContextNamespace = existingDbContext.Namespace;
                }
            }
            else
            {
                // --dbContext paramter was not specified. So we need to generate one using convention.
                DbContextClass     = GetDefaultDbContextName();
                DbContextNamespace = defaultDbContextNamespace;
            }

            if (string.IsNullOrEmpty(_commandlineModel.UserClass))
            {
                IsGenerateCustomUser = false;
                UserClass            = "IdentityUser";
                UserClassNamespace   = "Microsoft.AspNetCore.Identity";
            }
            else
            {
                var existingUser = await FindExistingType(_commandlineModel.UserClass);

                if (existingUser != null)
                {
                    ValidateExistingUserType(existingUser);
                    IsGenerateCustomUser = false;
                    UserType             = existingUser;
                }
                else
                {
                    IsGenerateCustomUser = true;
                    UserClass            = GetClassNameFromTypeName(_commandlineModel.UserClass);
                    UserClassNamespace   = GetNamespaceFromTypeName(_commandlineModel.UserClass)
                                           ?? defaultDbContextNamespace;
                }
            }

            if (_commandlineModel.UseDefaultUI)
            {
                ValidateDefaultUIOption();
            }

            if (IsFilesSpecified)
            {
                ValidateFilesOption();
            }

            var layout = _commandlineModel.Layout;

            if (_commandlineModel.GenerateLayout)
            {
                layout = "~/Pages/Shared/_Layout.chstml";
            }


            var templateModel = new IdentityGeneratorTemplateModel()
            {
                ApplicationName             = _applicationInfo.ApplicationName,
                DbContextClass              = DbContextClass,
                DbContextNamespace          = DbContextNamespace,
                UserClass                   = UserClass,
                UserClassNamespace          = UserClassNamespace,
                UseSQLite                   = _commandlineModel.UseSQLite,
                IsUsingExistingDbContext    = IsUsingExistingDbContext,
                Namespace                   = RootNamespace,
                IsGenerateCustomUser        = IsGenerateCustomUser,
                IsGeneratingIndividualFiles = IsFilesSpecified,
                UseDefaultUI                = _commandlineModel.UseDefaultUI,
                GenerateLayout              = _commandlineModel.GenerateLayout,
                Layout = layout
            };

            var filesToGenerate = new List <IdentityGeneratorFile>(IdentityGeneratorFilesConfig.GetFilesToGenerate(NamedFiles, templateModel));

            // Check if we need to add ViewImports and which ones.
            if (!_commandlineModel.UseDefaultUI)
            {
                filesToGenerate.AddRange(IdentityGeneratorFilesConfig.GetViewImports(filesToGenerate, _fileSystem, _applicationInfo.ApplicationBasePath));
            }

            templateModel.FilesToGenerate = filesToGenerate.ToArray();

            ValidateFilesToGenerate(templateModel.FilesToGenerate);

            return(templateModel);
        }
        public async Task <IdentityGeneratorTemplateModel> ValidateAndBuild()
        {
            ValidateCommandLine(_commandlineModel);
            RootNamespace = string.IsNullOrEmpty(_commandlineModel.RootNamespace)
                ? _projectContext.RootNamespace
                : _commandlineModel.RootNamespace;

            ValidateRequiredDependencies(_commandlineModel.UseSQLite);

            var defaultDbContextNamespace = $"{RootNamespace}.Areas.Identity.Data";

            IsUsingExistingDbContext = false;
            if (IsDbContextSpecified)
            {
                var existingDbContext = await FindExistingType(_commandlineModel.DbContext);

                if (existingDbContext == null)
                {
                    // We need to create one with what the user specified.
                    DbContextClass     = GetClassNameFromTypeName(_commandlineModel.DbContext);
                    DbContextNamespace = GetNamespaceFromTypeName(_commandlineModel.DbContext)
                                         ?? defaultDbContextNamespace;
                }
                else
                {
                    ValidateExistingDbContext(existingDbContext);
                    IsGenerateCustomUser     = false;
                    IsUsingExistingDbContext = true;
                    UserType           = FindUserTypeFromDbContext(existingDbContext);
                    DbContextClass     = existingDbContext.Name;
                    DbContextNamespace = existingDbContext.Namespace;
                }
            }
            else
            {
                // --dbContext paramter was not specified. So we need to generate one using convention.
                DbContextClass     = GetDefaultDbContextName();
                DbContextNamespace = defaultDbContextNamespace;
            }

            // if an existing user class was determined from the DbContext, don't try to get it from here.
            // Identity scaffolding must use the user class tied to the existing DbContext (when there is one).
            if (string.IsNullOrEmpty(UserClass))
            {
                if (string.IsNullOrEmpty(_commandlineModel.UserClass))
                {
                    IsGenerateCustomUser = false;
                    UserClass            = "IdentityUser";
                    UserClassNamespace   = "Microsoft.AspNetCore.Identity";
                }
                else
                {
                    var existingUser = await FindExistingType(_commandlineModel.UserClass);

                    if (existingUser != null)
                    {
                        ValidateExistingUserType(existingUser);
                        IsGenerateCustomUser = false;
                        UserType             = existingUser;
                    }
                    else
                    {
                        IsGenerateCustomUser = true;
                        UserClass            = GetClassNameFromTypeName(_commandlineModel.UserClass);
                        UserClassNamespace   = GetNamespaceFromTypeName(_commandlineModel.UserClass)
                                               ?? defaultDbContextNamespace;
                    }
                }
            }

            if (_commandlineModel.UseDefaultUI)
            {
                ValidateDefaultUIOption();
            }

            if (IsFilesSpecified)
            {
                ValidateFilesOption();
            }

            bool hasExistingLayout = DetermineSupportFileLocation(out string supportFileLocation, out string layout);

            var templateModel = new IdentityGeneratorTemplateModel()
            {
                ApplicationName             = _applicationInfo.ApplicationName,
                DbContextClass              = DbContextClass,
                DbContextNamespace          = DbContextNamespace,
                UserClass                   = UserClass,
                UserClassNamespace          = UserClassNamespace,
                UseSQLite                   = _commandlineModel.UseSQLite,
                IsUsingExistingDbContext    = IsUsingExistingDbContext,
                Namespace                   = RootNamespace,
                IsGenerateCustomUser        = IsGenerateCustomUser,
                IsGeneratingIndividualFiles = IsFilesSpecified,
                UseDefaultUI                = _commandlineModel.UseDefaultUI,
                GenerateLayout              = !hasExistingLayout,
                Layout = layout,
                LayoutPageNoExtension      = Path.GetFileNameWithoutExtension(layout),
                SupportFileLocation        = supportFileLocation,
                HasExistingNonEmptyWwwRoot = HasExistingNonEmptyWwwRootDirectory
            };

            var filesToGenerate = new List <IdentityGeneratorFile>(IdentityGeneratorFilesConfig.GetFilesToGenerate(NamedFiles, templateModel));

            // Check if we need to add ViewImports and which ones.
            if (!_commandlineModel.UseDefaultUI)
            {
                filesToGenerate.AddRange(IdentityGeneratorFilesConfig.GetViewImports(filesToGenerate, _fileSystem, _applicationInfo.ApplicationBasePath));
            }

            if (IdentityGeneratorFilesConfig.TryGetLayoutPeerFiles(_fileSystem, _applicationInfo.ApplicationBasePath, templateModel, out IReadOnlyList <IdentityGeneratorFile> layoutPeerFiles))
            {
                filesToGenerate.AddRange(layoutPeerFiles);
            }

            if (IdentityGeneratorFilesConfig.TryGetCookieConsentPartialFile(_fileSystem, _applicationInfo.ApplicationBasePath, templateModel, out IdentityGeneratorFile cookieConsentPartialConfig))
            {
                filesToGenerate.Add(cookieConsentPartialConfig);
            }

            templateModel.FilesToGenerate = filesToGenerate.ToArray();

            ValidateFilesToGenerate(templateModel.FilesToGenerate);

            return(templateModel);
        }