コード例 #1
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);
        }
コード例 #2
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,
                IsGeneratingIndividualFiles = IsFilesSpecified,
                UseDefaultUI                = _commandlineModel.UseDefaultUI,
                GenerateLayout              = !hasExistingLayout,
                Layout = layout,
                LayoutPageNoExtension      = Path.GetFileNameWithoutExtension(layout),
                SupportFileLocation        = supportFileLocation,
                HasExistingNonEmptyWwwRoot = HasExistingNonEmptyWwwRootDirectory,
                BootstrapVersion           = boostrapVersion,
                IsRazorPagesProject        = IsRazorPagesLayout()
            };

            templateModel.ContentVersion = DetermineContentVersion(templateModel);

            if (!string.IsNullOrEmpty(_commandlineModel.Files))
            {
                NamedFiles = _commandlineModel.Files.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            }
            templateModel.FilesToGenerate = DetermineFilesToGenerate(templateModel);

            if (IsFilesSpecified)
            {
                ValidateFilesOption(templateModel);
            }

            return(templateModel);
        }