Skip to content

Commit

Permalink
Automate plugin publishing (#712)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored Sep 27, 2019
1 parent 03fc28a commit 9419ba4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.iml
.gradle
build
!**/src/main/**/build
local.properties
out
userHome
18 changes: 1 addition & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
}

plugins {
id("com.github.triplet.gradle.build")
id("com.github.ben-manes.versions") version "0.25.0"
}

Expand All @@ -20,23 +21,6 @@ tasks.register<Delete>("clean") {
delete("build")
}

tasks.register("ciBuild") {
val isMaster = System.getenv("CIRCLE_BRANCH") == "master"
val isPr = System.getenv("CIRCLE_PULL_REQUEST") != null
val isSnapshot = project("plugin").version.toString().contains("snapshot", true)

fun allTasks(name: String) = allprojects.mapNotNull { it.tasks.findByName(name) }
if (isMaster && !isPr) { // Release build
if (isSnapshot) {
dependsOn(allTasks("build"), allTasks("publish"))
} else {
dependsOn(allTasks("build"))
}
} else {
dependsOn(allTasks("check"))
}
}

allprojects {
repositories.deps()

Expand Down
4 changes: 4 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ tasks.withType<ValidateTaskProperties>().configureEach {
enableStricterValidation = true
failOnWarning = true
}

dependencies {
implementation("org.ajoberstar.grgit:grgit-gradle:3.1.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.triplet.gradle.build

import org.ajoberstar.grgit.Grgit
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

class GPPBuildPlugin : Plugin<Project> {
private val isMaster get() = System.getenv("CIRCLE_BRANCH") == "master"
private val isPr get() = System.getenv("CIRCLE_PULL_REQUEST") != null

override fun apply(project: Project) {
check(project === project.rootProject) { "Cannot apply build plugin to subprojects." }

project.tasks.register("ciBuild") {
if (isReleaseBuild()) {
val buildTasks = allTasks("build")

dependsOn(buildTasks)
if (isSnapshotBuild()) {
dependsOn(allTasks("publish").mustRunAfter(buildTasks))
} else if (isPublishBuild()) {
dependsOn(allTasks("publishPlugins").mustRunAfter(buildTasks))
}
} else {
dependsOn(allTasks("check"))
}
}
}

private fun isReleaseBuild() = isMaster && !isPr

private fun Task.isSnapshotBuild() =
project.allprojects.any { it.version.toString().contains("snapshot", true) }

private fun Task.isPublishBuild() = Grgit.open { dir = project.rootDir }.use {
if (!it.status().isClean) return@use false

val latestCommit = it.head()
val latestTag = it.tag.list().maxBy { it.commit.dateTime } ?: return@use false

latestCommit.id == latestTag.commit.id
}

private fun Task.allTasks(name: String) =
project.allprojects.mapNotNull { it.tasks.findByName(name) }

private fun List<Task>.mustRunAfter(tasks: List<Task>) = onEach { it.mustRunAfter(tasks) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.github.triplet.gradle.build.GPPBuildPlugin

0 comments on commit 9419ba4

Please sign in to comment.