Beispiel #1
0
        public async Task <IManualWeb> BuildManual()
        {
            var webBuilder = _serviceProvider.GetRequiredService <IWebBuilder>();

            var resourcesDirectory = Path.Combine(_options.ContentDirectory, "resources");

            if (await Task.Run(() => Directory.Exists(resourcesDirectory)))
            {
                webBuilder.RegisterDirectory("/resources", resourcesDirectory);
            }

            webBuilder.RegisterMvc("/", new
            {
                controller = "Home",
                action     = "Index"
            });

            var languages = await _manualTranslations.GetLanguages();

            foreach (var language in languages)
            {
                webBuilder.RegisterMvc($"/prince/{language}/template", new
                {
                    controller = "Prince",
                    action     = "Template",
                    language
                });

                webBuilder.RegisterMvc($"/prince/{language}/output.pdf", new
                {
                    controller = "Prince",
                    action     = "Pdf",
                    language
                });
            }

            // Register our static files.
            webBuilder.RegisterDirectory("/Users/pknopf/git/docgen/src/DocGen.Web.Manual/Internal/Resources/wwwroot");

            CoversheetConfig coversheet = null;
            var sections = new ManualSectionStore();

            foreach (var markdownFile in await Task.Run(() => Directory.GetFiles(_options.ContentDirectory, "*.md")))
            {
                var content = await Task.Run(() => File.ReadAllText(markdownFile));

                var yaml = _yamlParser.ParseYaml(content);
                if (yaml.Yaml == null)
                {
                    yaml = new YamlParseResult(JsonConvert.DeserializeObject("{}"), yaml.Markdown);
                }

                var type = "Content";
                if (!string.IsNullOrEmpty((string)yaml.Yaml.Type))
                {
                    type = yaml.Yaml.Type;
                }

                switch (type)
                {
                case "Coversheet":
                    if (coversheet != null)
                    {
                        throw new DocGenException("Multiple coversheets detected");
                    }
                    coversheet = new CoversheetConfig();
                    coversheet.ProductImage = yaml.Yaml.ProductImage;
                    coversheet.ProductLogo  = yaml.Yaml.ProductLogo;
                    coversheet.Model        = yaml.Yaml.Model;
                    coversheet.Text         = yaml.Yaml.Text;
                    break;

                case "Content":
                    var order = (int?)yaml.Yaml.Order;
                    var title = (string)yaml.Yaml.Title;
                    if (string.IsNullOrEmpty(title))
                    {
                        throw new DocGenException($"The file {markdownFile} needs a title.");
                    }
                    foreach (var language in languages)
                    {
                        sections.AddMarkdown(language,
                                             _translator.Translate(language, title)
                                             , order ?? 0,
                                             _markdownTransformer.TransformMarkdown(
                                                 yaml.Markdown,
                                                 DocgenDefaults.GetDefaultPipeline(),
                                                 x => _translator.Translate(language, x)),
                                             markdownFile);
                    }
                    break;

                default:
                    throw new DocGenException("Unknown contente type");
                }
            }

            if (coversheet == null)
            {
                throw new DocGenException("You must provide a coversheet");
            }

            webBuilder.RegisterServices(services => {
                services.AddMvc();
                services.Configure <RazorViewEngineOptions>(options =>
                {
                    options.FileProviders.Add(new PhysicalFileProvider("/Users/pknopf/git/docgen/src/DocGen.Web.Manual/Internal/Resources"));
                    var templateDirectory = Path.Combine(_options.ContentDirectory, "templates");
                    if (Directory.Exists(templateDirectory))
                    {
                        options.FileProviders.Add(new PhysicalFileProvider(templateDirectory));
                    }
                });
                services.AddSingleton(_translator);
                services.AddSingleton(_manualTranslations);
                services.AddSingleton(sections);
                services.AddSingleton(coversheet);
                services.AddSingleton(_symbolGlossaryStore);
                // These regitrations are so that our controllers can inject core DocGen services.
                DocGen.Core.Services.Register(services);
            });

            return(new ManualWeb(webBuilder));
        }
Beispiel #2
0
        public TestCase ParseTestCase(string content)
        {
            var yaml = _yamlParser.ParseYaml(content);

            if (yaml.Yaml == null)
            {
                yaml = new YamlParseResult(Newtonsoft.Json.JsonConvert.DeserializeObject("{}"), yaml.Markdown);
            }

            StringBuilder action   = new StringBuilder();
            StringBuilder expected = new StringBuilder();
            StringBuilder current  = null;

            using (var stringReader = new StringReader(yaml.Markdown))
            {
                string line;
                while ((line = stringReader.ReadLine()) != null)
                {
                    if (line == "# Action")
                    {
                        current = action;
                    }
                    else if (line == "# Expected")
                    {
                        current = expected;
                    }
                    else
                    {
                        if (current == null)
                        {
                            throw new DocGenException($"Content '{line}' should be within a action or expected");
                        }

                        current.AppendLine(line);
                    }
                }
            }

            var    result = new TestCase();
            string number = yaml.Yaml?.Number;

            if (string.IsNullOrEmpty(number))
            {
                throw new DocGenException("You must provider a number");
            }
            if (!TryParseVersion(number, out Version version))
            {
                throw new DocGenException($"Invalid number format {number}");
            }

            result.Number = version;
            string responseType = yaml.Yaml?.ResponseType;

            if (string.IsNullOrEmpty(responseType))
            {
                responseType = "PassFail";
            }
            result.ResponseType = (TestCaseResponseTypeEnum)Enum.Parse(typeof(TestCaseResponseTypeEnum), responseType);
            string validationType = yaml.Yaml?.ValidationType;

            if (string.IsNullOrEmpty(validationType))
            {
                validationType = "Verification";
            }
            result.ValidationType = (TestCaseValidationTypeEnum)Enum.Parse(typeof(TestCaseValidationTypeEnum), validationType);
            string testType = yaml.Yaml?.Type;

            if (string.IsNullOrEmpty(testType))
            {
                testType = "Software";
            }
            result.Type     = (TestCaseTypeEnum)Enum.Parse(typeof(TestCaseTypeEnum), testType);
            result.Action   = action.ToString();
            result.Expected = expected.ToString();

            result.Action   = result.Action.TrimEnd(Environment.NewLine.ToCharArray());
            result.Expected = result.Expected.TrimEnd(Environment.NewLine.ToCharArray());

            if (string.IsNullOrEmpty(result.Action))
            {
                throw new DocGenException("Action is required");
            }
            if (string.IsNullOrEmpty(result.Expected))
            {
                throw new DocGenException("Expected is required");
            }

            return(result);
        }
Beispiel #3
0
        public SoftwareSpecification ParseSoftwareSpecification(string content)
        {
            var yaml = _yamlParser.ParseYaml(content);

            if (yaml.Yaml == null)
            {
                yaml = new YamlParseResult(Newtonsoft.Json.JsonConvert.DeserializeObject("{}"), yaml.Markdown);
            }

            StringBuilder requirement  = new StringBuilder();
            StringBuilder verification = new StringBuilder();
            StringBuilder current      = null;

            using (var stringReader = new StringReader(yaml.Markdown))
            {
                string line;
                while ((line = stringReader.ReadLine()) != null)
                {
                    if (line == "# Requirement")
                    {
                        current = requirement;
                    }
                    else if (line == "# Verification Method")
                    {
                        current = verification;
                    }
                    else
                    {
                        if (current == null)
                        {
                            throw new DocGenException($"Content '{line}' should be within a requirement or verification method");
                        }

                        current.AppendLine(line);
                    }
                }
            }

            var    result = new SoftwareSpecification();
            string number = yaml.Yaml?.Number;

            if (string.IsNullOrEmpty(number))
            {
                throw new DocGenException("You must provider a number");
            }
            if (!TryParseVersion(number, out Version version))
            {
                throw new DocGenException($"Invalid number format {number}");
            }

            result.Number             = version;
            result.Title              = yaml.Yaml?.Title;
            result.Requirement        = requirement.ToString();
            result.VerificationMethod = verification.ToString();

            result.Requirement        = result.Requirement.TrimEnd(Environment.NewLine.ToCharArray());
            result.VerificationMethod = result.VerificationMethod.TrimEnd(Environment.NewLine.ToCharArray());

            if (string.IsNullOrEmpty(result.Title))
            {
                throw new DocGenException("Title is required");
            }
            if (string.IsNullOrEmpty(result.Requirement))
            {
                throw new DocGenException("Requirement is required");
            }
            if (string.IsNullOrEmpty(result.VerificationMethod))
            {
                throw new DocGenException("You must provide a verification method");
            }

            return(result);
        }
Beispiel #4
0
        public UserNeed ParseUserNeed(string content)
        {
            var yaml = _yamlParser.ParseYaml(content);

            if (yaml.Yaml == null)
            {
                yaml = new YamlParseResult(Newtonsoft.Json.JsonConvert.DeserializeObject("{}"), yaml.Markdown);
            }

            StringBuilder userNeed         = new StringBuilder();
            StringBuilder validationMethod = new StringBuilder();
            StringBuilder current          = null;

            using (var stringReader = new StringReader(yaml.Markdown))
            {
                string line;
                while ((line = stringReader.ReadLine()) != null)
                {
                    if (line == "# User Need")
                    {
                        current = userNeed;
                    }
                    else if (line == "# Validation Method")
                    {
                        current = validationMethod;
                    }
                    else
                    {
                        if (current == null)
                        {
                            throw new DocGenException($"Content '{line}' should be within a user need or validation method");
                        }

                        current.AppendLine(line);
                    }
                }
            }

            var    result = new UserNeed();
            string number = yaml.Yaml?.Number;

            if (string.IsNullOrEmpty(number))
            {
                throw new DocGenException("You must provider a number");
            }
            if (!TryParseVersion(number, out Version version))
            {
                throw new DocGenException($"Invalid number format {number}");
            }

            result.Number           = version;
            result.Title            = yaml.Yaml?.Title;
            result.Category         = yaml.Yaml?.Category;
            result.Description      = userNeed.ToString();
            result.ValidationMethod = validationMethod.ToString();

            result.Description      = result.Description.TrimEnd(Environment.NewLine.ToCharArray());
            result.ValidationMethod = result.ValidationMethod.TrimEnd(Environment.NewLine.ToCharArray());

            if (string.IsNullOrEmpty(result.Title))
            {
                throw new DocGenException("Title is required");
            }
            if (string.IsNullOrEmpty(result.Category))
            {
                throw new DocGenException("Category is required");
            }
            if (string.IsNullOrEmpty(result.Description))
            {
                throw new DocGenException("You must provide description");
            }
            if (string.IsNullOrEmpty(result.ValidationMethod))
            {
                throw new DocGenException("You must provide a validation method");
            }

            return(result);
        }
        public async Task RegenerateTemplate()
        {
            var translations = new List <string>();

            foreach (var markdownFile in await Task.Run(() => Directory.GetFiles(_options.ContentDirectory, "*.md")))
            {
                var content = await Task.Run(() => File.ReadAllText(markdownFile));

                var yaml = _yamlParser.ParseYaml(content);
                if (yaml.Yaml == null)
                {
                    yaml = new YamlParseResult(JsonConvert.DeserializeObject("{}"), yaml.Markdown);
                }

                var type = "Content";
                if (!string.IsNullOrEmpty((string)yaml.Yaml.Type))
                {
                    type = yaml.Yaml.Type;
                }

                switch (type)
                {
                case "Content":
                    var order = (int?)yaml.Yaml.Order;
                    _markdownTransformer.TransformMarkdown(yaml.Markdown,
                                                           DocgenDefaults.GetDefaultPipeline(),
                                                           value =>
                    {
                        if (!translations.Contains(value))
                        {
                            translations.Add(value);
                        }
                        return(value);
                    });
                    break;
                }
            }

            // Add some translations that we have hardcoded in our templates.
            if (!translations.Contains("Table of contents"))
            {
                translations.Add("Table of contents");
            }

            // Now that we have the translations of all of our documents, let's generate the POT file.
            var destination = Path.Combine(_options.ContentDirectory, "translations", "template.pot");
            await Task.Run(() =>
            {
                var parentDirectory = Path.GetDirectoryName(destination) ?? "";
                if (!Directory.Exists(parentDirectory))
                {
                    Directory.CreateDirectory(parentDirectory);
                }
                if (File.Exists(destination))
                {
                    File.Delete(destination);
                }
                using (var file = File.OpenWrite(destination))
                    using (var writer = new StreamWriter(file))
                        SharpGettext.SharpGettext.GeneratePOT(
                            new POTemplateHeader
                        {
                            Language = "en-US"
                        },
                            translations.Select(x => new POTranslation
                        {
                            Text = x
                        }).ToList(),
                            writer);
            });

            // Now that we have our POT files, let's update all of our translations.
            await Task.Run(async() =>
            {
                foreach (var file in Directory.GetFiles(Path.Combine(_options.ContentDirectory, "translations"), "*.po"))
                {
                    await RunAsync("msgmerge", $"-U \"{file}\" \"{destination}\"");
                }
            });
        }