|
// Generate changelog |
|
android.applicationVariants.all { variant -> |
|
def outputDir = file("${buildDir}/changelog") |
|
def inputFile = file("${rootProject.projectDir}/docs/recent_changes.txt") |
|
def outputFile = file("${outputDir}/changelog.json") |
|
|
|
android.sourceSets.main.assets.srcDir outputDir |
|
def generateChangelogTask = task("generate${variant.name.capitalize()}Changelog").doLast { |
|
def versions = [] |
|
inputFile.eachLine { line -> |
|
def match |
|
|
|
if ((match = (line =~ /^v(\d+\.\d+\.\d+(?:-\w+)?) (\d+)$/)).matches()) { |
|
def versionName = match.group(1) |
|
def versionCode = match.group(2).toInteger() |
|
versions.add([ |
|
name: versionName, |
|
code: versionCode, |
|
changes: [] |
|
]) |
|
} else if ((match = (line =~ /^\* (.*)$/)).matches()) { |
|
versions.last().changes.add(match.group(1)) |
|
} else if (line == "") { |
|
// Should be last line of version. |
|
} else { |
|
throw new InvalidUserDataException("Incorrect format") |
|
} |
|
} |
|
outputDir.mkdirs() |
|
outputFile.text = groovy.json.JsonOutput.toJson(versions) |
|
} |
|
|
|
generateChangelogTask.inputs.files inputFile |
|
generateChangelogTask.outputs.files outputFile |
|
|
|
generateChangelogTask.metaClass.outputFile = outputFile |
|
generateChangelogTask.metaClass.inputFile = inputFile |
|
|
|
variant.mergeAssets.dependsOn generateChangelogTask |
|
|
|
if (variant.buildType.isDebuggable()) { |
|
return |
|
} |
|
def validateChangelogTask = task("validate${variant.name.capitalize()}Changelog").doLast { |
|
def json = new groovy.json.JsonSlurper().parseText(file(outputFile).getText()) |
|
def changelogVersionName = json[0].name |
|
def changelogVersionCode = json[0].code |
|
if (changelogVersionName != variant.versionName) { |
|
throw new InvalidUserDataException("Version name (${variant.versionName}) of ${variant.name} does not match last changelog entry (${changelogVersionName})") |
|
} |
|
if (changelogVersionCode % 100 != 0) { |
|
throw new InvalidUserDataException("Version code of changelog entry (${changelogVersionCode}) is invalid. It should be a multiple of 100.") |
|
} |
|
if ((changelogVersionCode / 100 as long) != (variant.versionCode / 100 as long)) { |
|
throw new InvalidUserDataException("Version code (${variant.versionCode}) of ${variant.name} does not match last changelog entry (${changelogVersionCode})") |
|
} |
|
} |
|
|
|
validateChangelogTask.inputs.files outputFile |
|
|
|
validateChangelogTask.dependsOn generateChangelogTask |
|
variant.mergeAssets.dependsOn validateChangelogTask |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate whatsnew to use with https://github.com/Triple-T/gradle-play-publisher |
|
android.applicationVariants.all { variant -> |
|
if (variant.buildType.isDebuggable()) { |
|
return |
|
} |
|
def changelogJsonFile = project.tasks["generate${variant.name.capitalize()}Changelog"].outputFile |
|
def listingDir = file("${buildDir}/listing") |
|
def cultureDir = file("${listingDir}/en-US") |
|
def whatsnewFile = file("${cultureDir}/whatsnew") |
|
def generateWhatsnewTask = task("generate${variant.name.capitalize()}Whatsnew").doLast { |
|
def json = new groovy.json.JsonSlurper().parseText(file(changelogJsonFile).getText()) |
|
assert json[0].changes instanceof List |
|
def changes = json[0].changes.collect { "* ${it}" }.join("\n") |
|
|
|
// Remove lines that exceed the maximum number of characters in a whatsnew message |
|
if (changes.length() > 500) { |
|
changes = changes.substring(0, changes.lastIndexOf('*', 500) - 1) |
|
} |
|
cultureDir.mkdirs() |
|
whatsnewFile.text = changes |
|
} |
|
generateWhatsnewTask.inputs.files changelogJsonFile |
|
generateWhatsnewTask.outputs.files whatsnewFile |
|
generateWhatsnewTask.dependsOn project.tasks["generate${variant.name.capitalize()}Changelog"] |
|
} |