caipivara
7/18/2016 - 11:09 PM

Android / Gradle - how to create a new Changelog with just the last release notes (useful for Fabric releases)

Android / Gradle - how to create a new Changelog with just the last release notes (useful for Fabric releases)

/**
 * Read the changelog and write in resultFileName the first changelog
 *
 * TODO: create reusable gradle plugin.
 * @param changelogFile
 * @param versionIndicator
 * @param resultFileName
 * @return name of the new changelog, if any error then name of initial changelogFile
 */
task generateLastChangelog() {
  description 'Create CHANGELOG-LAST.md with the last changelog reported on CHANGELOG.md'
  group = 'Reporting'

  def changelogFile = "CHANGELOG.md"
  def resultFileName = "CHANGELOG-LAST.md"
  def versionIndicator = "##"
  def message = ""
  def readEnabled = false
  def changelogLines = new File(changelogFile).readLines()

  for (line in changelogLines) {
    if (line.startsWith(versionIndicator)) {
      if (readEnabled) {
        // second changelog
        break
      }

      readEnabled = true
    }

    if (readEnabled && !line.isEmpty()) {
      message += "$line\n"
    }
  }

  if (message.isEmpty()) {
    return changelogFile
  } else {
    def tempChangelog = new File(resultFileName)
    tempChangelog.write message
    return resultFileName
  }
}

Version 0.7.0 Build 50 20/06

  • This thing
  • This other thing
  • THis other thing

Change log

Version 0.7.0 Build 50 20/06

  • THis thing
  • This other thing
  • THis other thing

Version 0.3.0 Build 40 20/06

  • Something
  • Something 2

Version 0.2.0 Build 30 20/06

  • Something 3
  • Something 4
  • Something 5