Beispiel #1
0
        /// <summary>
        /// Returns true if issues are found, and false otherwise.
        /// </summary>
        public AnalyzeResult Analyze(string templateFolder)
        {
            Debug.Assert(!string.IsNullOrEmpty(templateFolder));

            _reporter.WriteLine("\n");
            WriteMessage($@"Validating '{templateFolder}\.template.config\template.json'");

            string indentPrefix = "    ";

            // validate the folder has a .template.config folder
            if (!Directory.Exists(templateFolder))
            {
                WriteError($"templateFolder not found at '{templateFolder}'", _outputPrefix);
                return(GetResultFromErrorMessage(ErrorWarningType.Error, $"{_outputPrefix}templateFolder not found at '{templateFolder}'"));
            }

            var templateJsonFile = Path.Combine(templateFolder, ".template.config/template.json");

            if (!File.Exists(templateJsonFile))
            {
                WriteWarning($"template.json not found at '{templateJsonFile}'", _outputPrefix);
                return(GetResultFromErrorMessage(ErrorWarningType.Warning, $"{_outputPrefix}template.json not found at '{templateJsonFile}'"));
            }

            JToken template;

            try {
                template = _jsonHelper.LoadJsonFrom(templateJsonFile);
            }
            catch (Exception ex) {
                // TODO: make exception more specific
                WriteError($"Unable to load template from: '{templateJsonFile}'.\n Error: {ex.ToString()}");
                return(GetResultFromErrorMessage(ErrorWarningType.Error, $"Unable to load template from: '{templateJsonFile}'.\n Error: {ex.ToString()}"));
            }

            var templateType = GetTemplateType(templateJsonFile);

            if (templateType == TemplateType.Unknown)
            {
                WriteWarning($"Unable to determine if the template is for a project, or item (file) or solution. Assuming it is a project template", indentPrefix);
            }
            WriteVerboseLine($"Found a template of type: '{templateType}'", indentPrefix);
            var templateRules = GetTemplateRules(templateType, template);
            var analyzeResult = new AnalyzeResult();

            foreach (var rule in templateRules)
            {
                if (!ExecuteRule(rule, template))
                {
                    analyzeResult.Issues.Add(new FoundIssue()
                    {
                        IssueType    = rule.Severity,
                        IssueMessage = $"{indentPrefix} {rule.GetErrorMessage()}"
                    });
                    switch (rule.Severity)
                    {
                    case ErrorWarningType.Error:
                        WriteError(rule.GetErrorMessage(), indentPrefix);
                        break;

                    case ErrorWarningType.Warning:
                        WriteWarning(rule.GetErrorMessage(), indentPrefix);
                        break;

                    default:
                        WriteMessage(rule.GetErrorMessage(), indentPrefix);
                        break;
                    }
                }
            }
            analyzeResult = AnalyzeResult.Combine(analyzeResult, AnalyzeHostFiles(Path.GetDirectoryName(templateJsonFile), indentPrefix));
            analyzeResult = AnalyzeResult.Combine(analyzeResult, AnalyzeCasingForCommonProperties(template, indentPrefix));
            analyzeResult = AnalyzeResult.Combine(analyzeResult, ValidateFilePathsInSources(template, templateFolder));

            if (!analyzeResult.FoundIssues)
            {
                _reporter.WriteLine("√ no issues found", indentPrefix);
            }

            return(analyzeResult);
        }