-
Notifications
You must be signed in to change notification settings - Fork 50
/
build.gradle
317 lines (272 loc) · 10.4 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
buildscript {
ext.kotlin_version = '1.6.21'
ext.dokka_version = '1.4.32'
/**
* Properties and environment variables needed to publish.
*/
ext.jfrogUsername = (project.hasProperty('jfrog.username') ?
project.property("jfrog.username") : '')
ext.jfrogPassword = (project.hasProperty('jfrog.password') ?
project.property("jfrog.password") : '')
ext["signing.keyId"] = (project.hasProperty('signing.keyId') ?
project.property("signing.keyId") : '')
ext["signing.password"] = (project.hasProperty('signing.password') ?
project.property("signing.password") : '')
ext["signing.secretKeyRingFile"] = (project.hasProperty('signing.secretKeyRingFile') ?
project.property("signing.secretKeyRingFile") : '')
ext["ossrhUsername"] = (project.hasProperty('ossrhUsername') ?
project.property("ossrhUsername") : '')
ext["ossrhPassword"] = (project.hasProperty('ossrhPassword') ?
project.property("ossrhPassword") : '')
ext["sonatypeStagingProfileId"] = (project.hasProperty('sonatypeStagingProfileId') ?
project.property("sonatypeStagingProfileId") : '')
ext.getPropertyValue = { propertyKey ->
def property = System.getenv(propertyKey)
if (property == null) {
logger.log(LogLevel.INFO, "Could not locate $propertyKey as environment variable. " +
"Trying local.properties")
Properties properties = new Properties()
if (project.rootProject.file('local.properties').exists()) {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
property = properties.getProperty(propertyKey)
}
}
if (property == null) {
logger.log(LogLevel.WARN, "$propertyKey unavailable.")
}
return property
}
ext.getShortCommitSha = {
def gitSha = System.getenv("CIRCLE_SHA1")
if(gitSha != null) return gitSha.substring(0, 7) else return ""
}
ext.isPreRelease = (project.hasProperty("preRelease") && project.property("preRelease").toBoolean() == true)
ext.audioSwitchVersion = "${versionMajor}.${versionMinor}.${versionPatch}" +
(isPreRelease ? "-SNAPSHOT" : '')
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
}
}
plugins {
id "com.diffplug.spotless" version '6.19.0'
id "org.jetbrains.dokka" version "$dokka_version"
id "io.github.gradle-nexus.publish-plugin" version "1.0.0"
id "maven-publish"
}
apply plugin: "com.diffplug.spotless"
spotless {
format 'misc', {
target '**/*.gradle', '**/*.md', '**/.gitignore'
targetExclude 'docs/**'
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
java {
target '**/*.java'
googleJavaFormat().aosp()
}
kotlin {
target '**/*.kt'
ktlint()
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
nexusPublishing {
repositories {
sonatype {
username = ossrhUsername
password = ossrhPassword
stagingProfileId = sonatypeStagingProfileId
useStaging = !isPreRelease
}
}
clientTimeout = Duration.ofSeconds(300)
connectTimeout = Duration.ofSeconds(60)
}
/*
* Utility GradleBuild task that enables defining custom tasks derived from gradle modules in a
* root level gradle file.
*
* TODO: Replace this approach in favor of pushing tasks down into their respective modules.
*/
class RootGradleBuild extends GradleBuild {
private static final String ROOT_PROJECT_NAME = "audioswitch-root"
RootGradleBuild() {
super()
buildName = ROOT_PROJECT_NAME
}
}
/*
* Checks if release tag matches version and current commit
*/
def matchesVersion(versionTag) {
def properties = new Properties()
file("${rootDir}/gradle.properties").withInputStream { properties.load(it) }
def releaseTag = "${properties.getProperty("versionMajor")}." +
"${properties.getProperty("versionMinor")}." +
"${properties.getProperty("versionPatch")}"
return releaseTag == versionTag
}
task validateReleaseTag {
description = 'Validate the release tag matches the release version ' +
'present on commit'
group = 'Git'
doLast {
def circleTag = System.getenv("CIRCLE_TAG")
def tagsMatch = (matchesVersion(circleTag) || isPreRelease) ? ("true") : ("false")
exec {
workingDir "${rootDir}"
commandLine tagsMatch
}
}
}
task incrementVersion(type: RootGradleBuild) {
description = 'Increment the SDK version after a release'
group = 'Git'
doLast {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine "bash", "-c", "git remote show origin | grep HEAD | cut -d: -f2-"
standardOutput stdOut
}
def gitBranch = stdOut.toString().replaceAll("\\s","")
def circleTag = System.getenv("CIRCLE_TAG")
def githubToken = System.getenv("GITHUB_TOKEN")
def repoSlug = "${System.env.CIRCLE_PROJECT_USERNAME}/${System.env.CIRCLE_PROJECT_REPONAME}"
def gitRef = "https://${githubToken}@github.com/${repoSlug}.git"
def nextVersionPatch = versionPatch.toInteger() + 1
def remote = "upstream"
if (!buildDir.exists()) {
buildDir.mkdir()
}
exec {
workingDir "${rootDir}"
commandLine "git", "remote", "add", "${remote}", "${gitRef}"
// Ignore exit value because remote may have been added in previous task
ignoreExitValue true
}
exec {
workingDir "${rootDir}"
commandLine "git", "checkout", "${gitBranch}"
}
/*
* Only update the version on upstream branch if the version matches tag. It is possible
* these values do not match if a job is performed on an earlier commit and a PR
* with a version update occurs later in history.
*/
if (matchesVersion(circleTag)) {
exec {
workingDir "${rootDir}"
commandLine "echo", "Incrementing from versionPatch ${versionPatch} to " +
"${nextVersionPatch}"
}
exec {
workingDir "${rootDir}"
commandLine "sed",
"s@versionPatch=.*@versionPatch=${nextVersionPatch}@",
"gradle.properties"
standardOutput new FileOutputStream("${buildDir}/gradle.properties")
}
exec {
workingDir "${rootDir}"
commandLine "mv", "${buildDir}/gradle.properties", "gradle.properties"
}
exec {
workingDir "${rootDir}"
commandLine "git", "commit", "gradle.properties", "-m", "\"Bump patch version [skip ci]\""
}
exec {
workingDir "${rootDir}"
commandLine "git", "push", "${remote}", "${gitBranch}"
}
}
}
}
task sonatypeAudioSwitchReleaseUpload(type: RootGradleBuild) {
description = 'Publish an AudioSwitch release or pre-release'
group = 'Publishing'
dependsOn validateReleaseTag
buildFile = file('build.gradle')
tasks = ['assembleRelease', 'publishAudioSwitchReleasePublicationToSonatypeRepository', 'closeAndReleaseSonatypeStagingRepository']
startParameter.projectProperties += gradle.startParameter.projectProperties + [
'signing.keyId': "${getPropertyValue("SIGNING_KEY_ID")}",
'signing.password' : "${getPropertyValue("SIGNING_PASSWORD")}",
'signing.secretKeyRingFile' : "${getPropertyValue("SIGNING_SECRET_KEY_RING_FILE")}",
'ossrhUsername' : "${getPropertyValue("OSSRH_USERNAME")}",
'ossrhPassword' : "${getPropertyValue("OSSRH_PASSWORD")}",
'sonatypeStagingProfileId' : "${getPropertyValue("SONATYPE_STAGING_PROFILE_ID")}"
]
}
task publishDocs {
description = 'Publish AudioSwitch KDocs to gh-pages branch'
group = 'Publishing'
dependsOn 'audioswitch:dokkaHtml'
dependsOn validateReleaseTag
def releaseVersion = System.getenv("CIRCLE_TAG") == null ?
("") :
(System.getenv("CIRCLE_TAG"))
def pinLatestDocsCommand = ["ln", "-sfn", "${releaseVersion}", "docs/latest"]
def githubToken = System.getenv("GITHUB_TOKEN")
def repoSlug = "${System.env.CIRCLE_PROJECT_USERNAME}/${System.env.CIRCLE_PROJECT_REPONAME}"
def gitRef = "https://${githubToken}@github.com/${repoSlug}.git"
def remote = "upstream"
def pushNullFile = new FileOutputStream("/dev/null")
doLast {
exec {
workingDir "${rootDir}"
commandLine "git", "remote", "add", "${remote}", "${gitRef}"
// Ignore exit value because remote may have been added in previous task
ignoreExitValue true
}
exec {
workingDir "${rootDir}"
commandLine "git", "fetch", "${remote}"
}
exec {
workingDir "${rootDir}"
commandLine "git", "checkout", "-b", "gh-pages", "remotes/${remote}/gh-pages"
}
exec {
workingDir "${rootDir}"
commandLine "mkdir", "docs"
ignoreExitValue true
}
exec {
workingDir "${rootDir}"
commandLine "cp", "-r", "audioswitch/build/dokka/html/.", "docs/${releaseVersion}"
}
exec {
workingDir "${rootDir}"
commandLine pinLatestDocsCommand
}
exec {
workingDir "${rootDir}"
commandLine "git", "add", "docs/${releaseVersion}", "docs/latest"
}
exec {
workingDir "${rootDir}"
commandLine "git", "commit", "-m", "\"${releaseVersion} release docs [skip ci]\""
}
exec {
workingDir "${rootDir}"
commandLine "git", "push", "--quiet", "${remote}", "gh-pages"
standardOutput pushNullFile
}
exec {
workingDir "${rootDir}"
commandLine "git", "checkout", "${releaseVersion}"
}
}
}