All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.selesse.gradle.git.changelog.ChangelogParser.groovy Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package com.selesse.gradle.git.changelog
import com.google.common.base.Splitter
import com.google.common.collect.LinkedHashMultimap
import com.google.common.collect.Multimap

import java.util.regex.Pattern

class ChangelogParser {
    List headings
    Multimap headingsAndTheirCommits

    ChangelogParser(String changelogString) {
        this.headings = []
        this.headingsAndTheirCommits = LinkedHashMultimap.create()

        parseChangelogString(changelogString)
    }

    def parseChangelogString(String changelogString) {
        def multiLineNewLine = Pattern.compile('\\r?\\n', Pattern.MULTILINE)
        List changelog = Splitter.on(multiLineNewLine)
                .trimResults()
                .omitEmptyStrings()
                .splitToList(changelogString)

        def currentTag = 'None'

        for (int i = 0; i < changelog.size(); i++) {
            def currentLine = changelog.get(i)
            def nextLine = null
            if (i + 1 < changelog.size()) {
                nextLine = changelog.get(i + 1)
            }

            // The pattern is "Title\n-----" for tags
            if (nextLine != null && '-'.multiply(currentLine.length()).equals(nextLine)) {
                currentTag = currentLine
                headings.add(currentTag)
            } else {
                if (!currentLine.matches(/^-+$/)) {
                    headingsAndTheirCommits.put(currentTag, currentLine)
                }
            }
        }
    }

    public static extractTagAndDate(String string) {
        def matches = string =~ ~/([^\s]+) \(([^)]+)\)/

        return [matches[0][1], matches[0][2]]
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy