/// <summary>
        /// Parse a release notes
        /// </summary>
        /// <param name="rawReleaseNotes">Raw release notes</param>
        /// <param name="settings">Settings used for converting</param>
        /// <returns>A parsed release notes</returns>
        public static ReleaseNotes Parse(string rawReleaseNotes, SemanticReleaseNotesConverterSettings settings = null)
        {
            if (string.IsNullOrWhiteSpace(rawReleaseNotes))
            {
                throw new ArgumentNullException("rawReleaseNotes");
            }

            var releaseNotes = new ReleaseNotes();

            var rawLines = rawReleaseNotes.Replace("\r", string.Empty).Split('\n');

            for (int i = 0; i < rawLines.Length; i++)
            {
                var rawLine   = rawLines[i];
                var nextInput = string.Empty;
                if (i + 1 < rawLines.Length)
                {
                    nextInput = rawLines[i + 1];
                }

                // Process the line
                for (int j = 0; j < ParserParts.Count; j++)
                {
                    if (ParserParts[j].Parse(rawLine, releaseNotes, nextInput))
                    {
                        break;
                    }
                }
            }

            return(releaseNotes);
        }
示例#2
0
        /// <summary>
        /// Format a release notes
        /// </summary>
        /// <param name="writer">TextWriter which will be used to writes the formatted release notes</param>
        /// <param name="releaseNotes">Release notes to format</param>
        /// <param name="settings">Settings used for formatting</param>
        public static void Format(TextWriter writer, ReleaseNotes releaseNotes, SemanticReleaseNotesConverterSettings settings = null)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.Write(Format(releaseNotes, settings));
        }
        /// <summary>
        /// Parse a release notes from a stream
        /// </summary>
        /// <param name="reader">Reader of release notes</param>
        /// <param name="settings">Settings used for converting</param>
        /// <returns>A parsed release notes</returns>
        public static ReleaseNotes Parse(TextReader reader, SemanticReleaseNotesConverterSettings settings = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(Parse(reader.ReadToEnd()));
        }
示例#4
0
        private static string GetLiquidTemplate(SemanticReleaseNotesConverterSettings settings)
        {
            string templateName = "GroupBySections.liquid";

            if (settings.GroupBy == GroupBy.Categories)
            {
                templateName = "GroupByCategories.liquid";
            }

            return(GetEmbeddedResource(templateName));
        }
示例#5
0
        private static List <Category> GetCategories(ReleaseNotes releaseNotes, SemanticReleaseNotesConverterSettings settings)
        {
            var categories = new Dictionary <string, List <Item> >();

            ProcessCategories(categories, releaseNotes.Items, settings);

            foreach (var section in releaseNotes.Sections)
            {
                ProcessCategories(categories, section.Items, settings);
            }

            return(categories.Select(x => new Category {
                Name = x.Key, Items = x.Value
            }).ToList());
        }
示例#6
0
        /// <summary>
        ///Format a release notes
        /// </summary>
        /// <param name="releaseNotes">Release notes to format</param>
        /// <param name="settings">Settings used for formatting</param>
        /// <returns>Formatted release notes</returns>
        public static string Format(ReleaseNotes releaseNotes, SemanticReleaseNotesConverterSettings settings = null)
        {
            if (releaseNotes == null)
            {
                throw new ArgumentNullException("releaseNotes");
            }

            if (settings == null)
            {
                settings = SemanticReleaseNotesConverterSettings.Default;
            }

            // template selection
            string template = settings.LiquidTemplate ?? GetLiquidTemplate(settings);

            // liquid rendering
            var liquidTemplate = Template.Parse(template);

            // process categories
            List <Category> processedCategories = GetCategories(releaseNotes, settings);

            var itemsWithoutCategory = new List <Item>(releaseNotes.Items.Where(i => !i.Categories.Any()));

            releaseNotes.Sections.ForEach(s => itemsWithoutCategory.AddRange(s.Items.Where(i => !i.Categories.Any())));

            string result = liquidTemplate.Render(Hash.FromAnonymousObject(new { release_notes = releaseNotes, lcb = "{", rcb = "}", categories = processedCategories, items_without_categories = itemsWithoutCategory })).Trim();

            if (settings.OutputFormat == OutputFormat.Markdown)
            {
                return(result);
            }

            // convert to HTML
            var head = string.Empty;

            if (settings.IncludeStyle)
            {
                head = string.Format(HtmlHead, string.IsNullOrEmpty(settings.CustomStyle) ? GetEmbeddedResource("DefaultStyle.css") : settings.CustomStyle, Environment.NewLine);
            }

            return(string.Format(HtmlEnvelope, head, CommonMarkConverter.Convert(result, DefaultCommonMarkSettings).Trim(), Environment.NewLine));
        }
示例#7
0
        private static void ProcessCategories(Dictionary <string, List <Item> > categories, IEnumerable <Item> items, SemanticReleaseNotesConverterSettings settings)
        {
            foreach (var item in items)
            {
                if (item.Categories.Any())
                {
                    foreach (var category in item.Categories)
                    {
                        var categoryName = category;
                        if (settings.PluralizeCategoriesTitle)
                        {
                            categoryName = categoryName.Pluralize(false);
                        }

                        if (!categories.ContainsKey(categoryName))
                        {
                            categories.Add(categoryName, new List <Item>());
                        }
                        categories[categoryName].Add(item);
                    }
                }
            }
        }
示例#8
0
        private static int Run(string[] args)
        {
            try
            {
                SetDefaults();

                var output = Console.Out;
                Logger.SetLogAction(ConsoleLogAction.Write);
                Logger.SetMinimalLevel(LogLevel.Info);

                Logger.Info("SemanticReleaseNotesParser V{0}", Assembly.GetExecutingAssembly().GetName().Version);

                // Arguments parsing
                var arguments = Arguments.ParseArguments(args);
                if (arguments.Help)
                {
                    arguments.WriteOptionDescriptions(output);
                    return(1);
                }

                // Handle debug
                if (arguments.Debug)
                {
                    Logger.SetMinimalLevel(LogLevel.Debug);
                }

                if (!FileSystem.File.Exists(arguments.ReleaseNotesPath))
                {
                    Logger.Error("Release notes file '{0}' does not exists", arguments.ReleaseNotesPath);
                    return(1);
                }

                // Settings
                string template = null;
                if (!string.IsNullOrEmpty(arguments.TemplatePath))
                {
                    if (!FileSystem.File.Exists(arguments.TemplatePath))
                    {
                        Logger.Error("Template file '{0}' does not exists", arguments.TemplatePath);
                        return(1);
                    }

                    Logger.Debug("Custom template used: '{0}'", arguments.TemplatePath);
                    template = FileSystem.File.ReadAllText(arguments.TemplatePath);
                }

                var settings = new SemanticReleaseNotesConverterSettings
                {
                    OutputFormat             = arguments.OutputFormat,
                    LiquidTemplate           = template,
                    GroupBy                  = arguments.GroupBy,
                    PluralizeCategoriesTitle = arguments.PluralizeCategoriesTitle,
                    IncludeStyle             = arguments.IncludeStyle != null,
                    CustomStyle              = GetCustomStyle(arguments.IncludeStyle)
                };

                // Parsing
                Logger.Debug("Parsing release notes '{0}'", arguments.ReleaseNotesPath);

                var releaseNotes = SemanticReleaseNotesConverter.Parse(FileSystem.File.OpenText(arguments.ReleaseNotesPath), settings);

                // Formatting
                string formattedReleaseNotes = SemanticReleaseNotesConverter.Format(releaseNotes, settings);

                Logger.Debug("Formatted release notes: {0}", formattedReleaseNotes);

                // Select output
                if (arguments.OutputType.HasFlag(OutputType.File))
                {
                    Logger.Debug("File output");
                    FileSystem.File.WriteAllText(arguments.ResultFilePath, formattedReleaseNotes);
                    Logger.Info("File '{0}' generated", arguments.ResultFilePath);
                }

                if (arguments.OutputType.HasFlag(OutputType.Environment))
                {
                    Logger.Debug("Environment output");
                    var buildServer = GetApplicableBuildServer();
                    Logger.Debug("Build server selected: {0}", buildServer.GetType().Name);
                    buildServer.SetEnvironmentVariable("SemanticReleaseNotes", formattedReleaseNotes);
                }

                return(0);
            }
            catch (Exception exception)
            {
                var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
                Logger.Error(error);
                return(1);
            }
        }