Exemplo n.º 1
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);
        }
        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);
        }