1+ @file:Suppress(" unused" )
2+
3+ import java.util.*
14import buildlogic.git.getAllCommitsPushed
25import buildlogic.git.getCommitCount
36import buildlogic.git.getWorkingTreeClean
47import com.android.build.api.dsl.ApplicationExtension
8+ import org.gradle.BuildAdapter
9+ import org.gradle.BuildResult
510import org.gradle.api.DefaultTask
11+ import org.gradle.api.Plugin
12+ import org.gradle.api.Project
13+ import org.gradle.api.Transformer
14+ import org.gradle.api.file.RegularFileProperty
15+ import org.gradle.api.provider.Provider
616import org.gradle.api.tasks.Input
17+ import org.gradle.api.tasks.InputFile
718import org.gradle.api.tasks.TaskAction
819import org.gradle.kotlin.dsl.*
920import org.gradle.work.DisableCachingByDefault
1021import org.kohsuke.github.GHReleaseBuilder
1122import org.kohsuke.github.GitHub
1223
13- @DisableCachingByDefault
14- abstract class GithubCreateReleaseTask : DefaultTask () {
24+ abstract class GithubPlugin : Plugin <Project > {
1525
16- init {
17- notCompatibleWithConfigurationCache(" needs to talk to GitHub" )
26+ override fun apply (target : Project ) {
27+ with (target) {
28+ tasks.register<GithubClearReleaseTask >(" githubClearReleases" )
29+ subprojects {
30+ afterEvaluate {
31+ if (extensions.findByType<ApplicationExtension >() != null ) {
32+ tasks.register<GithubCreateReleaseTask >(" githubCreateRelease" )
33+ }
34+ }
35+ }
36+
37+ }
1838 }
39+ }
40+
41+ @DisableCachingByDefault
42+ abstract class GithubCreateReleaseTask : Github () {
1943
20- @get:Input
21- abstract val repo: String
44+ private val debug = true
2245
23- @get:Input
24- abstract val token: String
46+ @Input
47+ val commitCount = project.getCommitCount()
48+
49+ @Input
50+ val workingTreeClean = project.getWorkingTreeClean()
51+
52+ @Input
53+ val allCommitsPushed = project.getAllCommitsPushed()
54+
55+ init {
56+ if (debug || (workingTreeClean && allCommitsPushed)) {
57+ dependsOn(" packageRelease" )
58+ }
59+ }
2560
2661 @TaskAction
2762 fun createRelease () {
28- val commitCount = project.getCommitCount()
29- val workingTreeClean = project.getWorkingTreeClean()
30- val allCommitsPushed = project.getAllCommitsPushed()
31-
3263 val android = project.extensions.getByType<ApplicationExtension >()
3364
34- if (workingTreeClean && allCommitsPushed) {
35- dependsOn(" assembleRelease" )
65+ if (! debug) {
66+ check(workingTreeClean) { " Commit all changes before creating release" }
67+ check(allCommitsPushed) { " Push to remote before creating release" }
3668 }
3769
38- check(workingTreeClean) { " Commit all changes before creating release" }
39- check(allCommitsPushed) { " Push to remote before creating release" }
40-
41- val packageRelease = project.tasks.getByName(" packageRelease" )
42-
43- val outputs = packageRelease.outputs.files
44- val apks = outputs.filter { it.isDirectory }.flatMap { it.listFiles { file -> file.extension == " apk" }!! .toList() }
45-
46- val github = GitHub .connectUsingOAuth(token)
47- val repository = github.getRepository(repo)
70+ val github = GitHub .connectUsingOAuth(token.get())
71+ val repository = github.getRepository(repo.get())
4872
4973 val tagName = " ${android.namespace} -v$commitCount "
5074 val name = " ${project.name} -v$commitCount "
@@ -58,39 +82,60 @@ abstract class GithubCreateReleaseTask : DefaultTask() {
5882
5983 val release = repository.createRelease(tagName).name(name).draft(true ).makeLatest(GHReleaseBuilder .MakeLatest .FALSE ).create()
6084
85+ val packageRelease = project.tasks.getByName(" packageRelease" )
86+ val outputs = packageRelease.outputs.files
87+ val apks = outputs.filter { it.isDirectory }.flatMap { it.listFiles { file -> file.extension == " apk" }.asList() }
6188 apks.forEach {
6289 release.uploadAsset(" ${project.name} -v$commitCount .apk" , it.inputStream(), " application/vnd.android.package-archive" )
6390 }
6491
65- doLast {
66- project.logger.info(" Created release ${release.name} : ${release.htmlUrl} " )
67- }
92+ val logString = " Created release ${release.name} : ${release.htmlUrl} "
93+
94+ project.gradle.addBuildListener(object : BuildAdapter () {
95+ override fun buildFinished (result : BuildResult ) {
96+ println (logString)
97+ }
98+ })
6899 }
69100}
70101
71102@DisableCachingByDefault
72- abstract class GithubClearReleaseTask : DefaultTask () {
73-
74- init {
75- notCompatibleWithConfigurationCache(" needs to talk to GitHub" )
76- }
77-
78- @get:Input
79- abstract val repo: String
80-
81- @get:Input
82- abstract val token: String
103+ abstract class GithubClearReleaseTask : Github () {
83104
84105 @TaskAction
85106 fun clearReleases () {
86-
87- val github = GitHub .connectUsingOAuth(token)
88- val repository = github.getRepository(repo)
107+ val github = GitHub .connectUsingOAuth(token.get())
108+ val repository = github.getRepository(repo.get())
89109
90110 repository.listReleases().filter { it.isDraft }.forEach { release ->
91111 val name = release.name
92112 release.delete()
93- project.logger.info(" Deleted release $name " )
113+ println (" Deleted release $name " )
114+ }
115+ }
116+ }
117+
118+ abstract class Github : DefaultTask () {
119+
120+ init {
121+ notCompatibleWithConfigurationCache(" needs to talk to GitHub" )
122+ }
123+
124+ @get:InputFile
125+ abstract val configurationFile: RegularFileProperty
126+
127+ @Input
128+ val properties: Provider <Properties > = configurationFile.map { Properties ().apply { load(it.asFile.inputStream()) } }
129+
130+ @Input
131+ val repo: Provider <String ?> = properties.map<String ?>(PropertyFileTransformer (" github_repo" ))
132+
133+ @Input
134+ val token: Provider <String > = properties.map<String ?>(PropertyFileTransformer (" github_api_key" ))
135+
136+ class PropertyFileTransformer (val key : String ) : Transformer<String?, Properties> {
137+ override fun transform (`in `: Properties ): String? {
138+ return `in `[key]?.toString()
94139 }
95140 }
96141}
0 commit comments