Exemplo n.º 1
0
        private void CheckForInvalidProjectName()
        {
            var validationService     = new ProjectNameService(GenContext.ToolBox.Repo.ProjectNameValidationConfig, () => new List <string>());
            var projectName           = GenContext.Current.ProjectName;
            var projectNameValidation = validationService.Validate(projectName);

            if (!projectNameValidation.IsValid)
            {
                var message = string.Empty;
                switch (projectNameValidation.Errors.FirstOrDefault()?.ErrorType)
                {
                case ValidationErrorType.ReservedName:
                    message = string.Format(CoreStringRes.ErrorProjectReservedName, projectName);
                    break;

                case ValidationErrorType.Regex:
                    message = string.Format(CoreStringRes.ErrorProjectStartsWith, projectName, projectName[0]);
                    break;
                }

                var title = CoreStringRes.ErrorTitleInvalidProjectName;
                var link  = "https://github.com/microsoft/WindowsTemplateStudio/blob/release/docs/WTSNaming.md";
                var vm    = new InfoDialogViewModel(title, message, link, _styleProvider);
                var info  = new Views.Common.InfoDialog(vm);
                GenContext.ToolBox.Shell.ShowModal(info);

                CancelWizard();
            }
        }
        public void Infer_SuccessfullyAccountsForReservedNames()
        {
            var config = new ProjectNameValidationConfig()
            {
                ReservedNames = new string[]
                {
                    "Prism",
                    "CaliburnMicro",
                    "MVVMLight",
                },
            };

            var validationService = new ProjectNameService(config, null);
            var result            = validationService.Infer("Prism");

            Assert.Equal("Prism1", result);
        }
        public void Infer_SuccessfullyAccountsForExistingNames()
        {
            var existingNames = new List <string>()
            {
                "App"
            };

            Func <IEnumerable <string> > getExistingNames = () => { return(existingNames); };

            var config = new ProjectNameValidationConfig()
            {
                ValidateExistingNames = true,
            };

            var validationService = new ProjectNameService(config, getExistingNames);
            var result            = validationService.Infer("App");

            Assert.Equal("App1", result);
        }
        public void Validate_SuccessfullyAccountsForRegex()
        {
            var config = new ProjectNameValidationConfig()
            {
                Regexs = new RegExConfig[]
                {
                    new RegExConfig()
                    {
                        Name    = "projectStartWith$",
                        Pattern = "^[^\\$]",
                    },
                },
            };

            var validationService = new ProjectNameService(config, null);
            var result            = validationService.Validate("$App");

            Assert.False(result.IsValid);
            Assert.Contains(result.Errors, e => e.ErrorType == ValidationErrorType.Regex);
            Assert.Contains(result.Errors, e => e.ValidatorName == "projectStartWith$");
        }
Exemplo n.º 5
0
        internal static void ValidateUserSelection(UserSelection userSelection, bool isNewProject)
        {
            if (isNewProject)
            {
                var rootDir = Directory.GetParent(Directory.GetParent(GenContext.Current.DestinationPath).FullName).FullName;

                var projectNameService = new ProjectNameService(GenContext.ToolBox.Repo.ProjectNameValidationConfig, () => Fs.GetExistingFolderNames(rootDir));

                var result = projectNameService.Validate(GenContext.Current.ProjectName);
                {
                    if (!result.IsValid)
                    {
                        var errors = string.Join(Environment.NewLine, result.Errors.Select(e => $"Error: {e.ErrorType}; Validator: {e.ValidatorName}."));
                        throw new InvalidDataException(string.Format(StringRes.ErrorProjectNameValidationFailed, GenContext.Current.ProjectName, Environment.NewLine + errors));
                    }
                }
            }

            foreach (var item in userSelection.Items)
            {
                var template = GenContext.ToolBox.Repo.Find(t => t.Identity == item.TemplateId);

                if (template.GetItemNameEditable())
                {
                    Func <IEnumerable <string> > existingNames = () => userSelection.Items.Where(t => t.TemplateId != template.Identity).Select(n => n.Name);

                    var itemNameService = new ItemNameService(GenContext.ToolBox.Repo.ItemNameValidationConfig, existingNames);

                    var validationResult = itemNameService.Validate(item.Name);
                    {
                        if (!validationResult.IsValid)
                        {
                            var errors = string.Join(Environment.NewLine, validationResult.Errors.Select(e => $"Error: {e.ErrorType}; Validator: {e.ValidatorName}."));
                            throw new InvalidDataException(string.Format(StringRes.ErrorNamingValidationFailed, item.Name, Environment.NewLine + errors));
                        }
                    }
                }

                var dependencies = GenContext.ToolBox.Repo.GetDependencies(template, userSelection.Context, new List <ITemplateInfo>());

                foreach (var dependency in dependencies)
                {
                    if (!userSelection.Items.Any(i => i.TemplateId == dependency.Identity))
                    {
                        throw new InvalidDataException(string.Format(StringRes.ErrorDependencyMissing, dependency));
                    }
                }

                var requirements = GenContext.ToolBox.Repo.GetRequirements(template, userSelection.Context);

                if (requirements.Count() > 0 && !userSelection.Items.Any(i => requirements.Select(r => r.Identity).Contains(i.TemplateId)))
                {
                    throw new InvalidDataException(string.Format(StringRes.ErrorRequirementMissing, requirements.Select(r => r.Name).Aggregate((i, j) => $"{i},{j}")));
                }

                var exclusionTemplates = GenContext.ToolBox.Repo.GetExclusions(template, userSelection.Context);

                foreach (var exclusion in exclusionTemplates)
                {
                    if (userSelection.Items.Any(i => i.TemplateId == exclusion.Identity))
                    {
                        throw new InvalidDataException(string.Format(StringRes.ErrorExcludedTemplatesFound, exclusion.Name));
                    }
                }
            }
        }