|
| 1 | +import java.time.LocalDate |
| 2 | +import java.time.format.DateTimeFormatter |
| 3 | + |
| 4 | +/** |
| 5 | + * This file defines the updateCff task. |
| 6 | + * |
| 7 | + * This task hooks into the workflow of the release task, |
| 8 | + * which, according to the source code of the release plugin, |
| 9 | + * seems to be defined like this: |
| 10 | + * |
| 11 | + * tasks = [ |
| 12 | + * "${p}createScmAdapter" as String, |
| 13 | + * "${p}initScmAdapter" as String, |
| 14 | + * "${p}checkCommitNeeded" as String, |
| 15 | + * "${p}checkUpdateNeeded" as String, |
| 16 | + * "${p}checkoutMergeToReleaseBranch" as String, |
| 17 | + * "${p}unSnapshotVersion" as String, |
| 18 | + * "${p}confirmReleaseVersion" as String, |
| 19 | + * -> we insert our task here <- |
| 20 | + * "${p}checkSnapshotDependencies" as String, |
| 21 | + * "${p}runBuildTasks" as String, |
| 22 | + * "${p}preTagCommit" as String, |
| 23 | + * "${p}createReleaseTag" as String, |
| 24 | + * "${p}checkoutMergeFromReleaseBranch" as String, |
| 25 | + * "${p}updateVersion" as String, |
| 26 | + * "${p}commitNewVersion" as String |
| 27 | + * ] |
| 28 | + */ |
| 29 | + |
| 30 | +tasks.register('updateCff') { |
| 31 | + group = 'release' |
| 32 | + description = 'Updates the version in CITATION.cff file' |
| 33 | + |
| 34 | + outputs.file("CITATION.cff") |
| 35 | + |
| 36 | + doLast { |
| 37 | + def version = project.version.toString() |
| 38 | + def today = LocalDate.now().format(DateTimeFormatter.ISO_DATE) |
| 39 | + |
| 40 | + def cffFile = file('CITATION.cff') |
| 41 | + def content = cffFile.text |
| 42 | + |
| 43 | + // Update or insert version |
| 44 | + if (content.contains('version:')) { |
| 45 | + content = content.replaceAll(/(?m)^version:\s*.+$/, "version: ${version}") |
| 46 | + } else { |
| 47 | + content = content + "\nversion: ${version}" |
| 48 | + } |
| 49 | + |
| 50 | + // Update or insert date-released |
| 51 | + if (content.contains('date-released:')) { |
| 52 | + content = content.replaceAll(/(?m)^date-released:\s*.+$/, "date-released: ${today}") |
| 53 | + } else { |
| 54 | + content = content + "\ndate-released: ${today}" |
| 55 | + } |
| 56 | + |
| 57 | + cffFile.text = content |
| 58 | + println "Updated CITATION.cff to version ${version} and date-released ${today}" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Make sure your custom task runs after a specific task in the release sequence |
| 63 | +tasks.named('updateCff') { |
| 64 | + mustRunAfter(tasks.named('confirmReleaseVersion')) |
| 65 | +} |
| 66 | + |
| 67 | +// Ensure subsequent tasks in the release sequence run after your custom task |
| 68 | +tasks.named('checkSnapshotDependencies') { |
| 69 | + dependsOn('updateCff') |
| 70 | +} |
0 commit comments