Skip to content

Commit 5e3bd3d

Browse files
committed
perf(gradle): optimize configuration phase performance
Signed-off-by: ale5000 <15793015+ale5000-git@users.noreply.github.com>
1 parent 688b9f1 commit 5e3bd3d

File tree

2 files changed

+80
-36
lines changed

2 files changed

+80
-36
lines changed

build.gradle

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private void initialize() {
2727
defaultTasks 'tasks'
2828

2929
// Define the custom build directory
30-
project.buildDir = project.file('output')
30+
layout.buildDirectory = 'output'
3131

3232
// Load properties from module.prop
3333
def moduleProp = project.file('zip-content/module.prop')
@@ -43,24 +43,32 @@ private void initialize() {
4343
ext.isAlpha = version.endsWith('-alpha')
4444
//ext.isBeta = version.endsWith('-beta')
4545

46-
println "Project: ${name}"
47-
println "Version: ${version}"
48-
println "OS: ${System.getProperty('os.name')}"
49-
5046
if(JavaVersion.current() < JavaVersion.VERSION_17) {
5147
throw new GradleException('Java 17 or later is required')
5248
}
5349
}
5450

55-
private String getProjectId() {
56-
project.findProperty('projectId') ?: { throw new InvalidUserDataException('projectId is empty or null.') }()
51+
private static String getScriptExt() {
52+
System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows') ? '.bat' : '.sh'
53+
}
54+
55+
private Provider<String> getProjectId() {
56+
project.provider {
57+
project.findProperty('projectId') ?: {
58+
throw new InvalidUserDataException('projectId is empty or null.')
59+
}()
60+
}
61+
}
62+
63+
private Provider<String> getGitCommitHash() {
64+
providers.exec {
65+
commandLine 'git', 'rev-parse', '--short=8', 'HEAD'
66+
}.standardOutput.asText.map{ it.trim() }.orElse('unknown')
5767
}
5868

59-
private String getGitCommitHash() {
60-
try {
61-
'git rev-parse --short=8 HEAD'.execute().text.trim()
62-
} catch (ignored) {
63-
'unknown'
69+
private Provider<String> getZipName(String variant = '*') {
70+
getProjectId().zip(getGitCommitHash()) { id, gitCommitHash ->
71+
"${id}-${project.version}-g${gitCommitHash}-${variant}-by-ale5000-signed.zip"
6472
}
6573
}
6674

@@ -96,20 +104,24 @@ private void configureSigning(def android, File keystorePropsFile) {
96104
android.buildTypes.release.signingConfig android.signingConfigs.config
97105
}
98106

99-
static String getScriptExt() {
100-
System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows') ? '.bat' : '.sh'
101-
}
102-
103107
initialize()
104108

105109
/* ===BLOCKS=== */
106110

107111
base {
108-
archivesName = getProjectId()
112+
archivesName.set(getProjectId())
109113
}
110114

111115
/* ==TASKS=== */
112116

117+
gradle.taskGraph.whenReady {
118+
logger.lifecycle '='*36
119+
logger.lifecycle "Project: ${project.name}"
120+
logger.lifecycle "Version: ${project.version}"
121+
logger.lifecycle "OS: ${System.getProperty('os.name')}"
122+
logger.lifecycle '='*36
123+
}
124+
113125
tasks.register('cleanRecoveryOutput', Delete) {
114126
group = '- Cleanup'
115127
description = 'Deletes the recovery simulator output directory.'
@@ -149,30 +161,62 @@ tasks.register('buildOta', Exec) {
149161
group = '- Flashable ZIP'
150162
description = 'Build the flashable zip [Full edition].'
151163

164+
['LICENSES', 'conf', 'docs', 'lib', 'zip-content'].each { folder ->
165+
inputs.dir(layout.projectDirectory.dir(folder)).withPropertyName("inputDir_${folder}")
166+
}
167+
inputs.files(
168+
layout.projectDirectory.dir('tools').asFileTree.matching { include '*.jar' },
169+
layout.projectDirectory.files('CHANGELOG.rst', 'build.sh'),
170+
layout.projectDirectory.asFileTree.matching { include 'LICENSE*.rst' }
171+
).withPropertyName('inputFiles')
172+
173+
outputs.file(layout.buildDirectory.file(getZipName('full')))
174+
175+
workingDir = layout.projectDirectory
176+
executable = layout.projectDirectory.file("build${getScriptExt()}")
177+
178+
environment([
179+
'BUILD_TYPE': 'full',
180+
'NO_PAUSE' : '1'
181+
])
182+
152183
doFirst {
153184
println 'Building the flashable zip with Gradle...'
154-
environment BUILD_TYPE: 'full'
155-
environment NO_PAUSE: '1'
156-
executable "${projectDir}/build" + getScriptExt()
157185
}
158186
}
159187

160188
tasks.register('buildOtaOSS', Exec) {
161189
group = '- Flashable ZIP'
162190
description = 'Build the flashable zip [OSS edition].'
163191

192+
['LICENSES', 'conf', 'docs', 'lib', 'zip-content'].each { folder ->
193+
inputs.dir(layout.projectDirectory.dir(folder)).withPropertyName("inputDir_${folder}")
194+
}
195+
inputs.files(
196+
layout.projectDirectory.dir('tools').asFileTree.matching { include '*.jar' },
197+
layout.projectDirectory.files('CHANGELOG.rst', 'build.sh'),
198+
layout.projectDirectory.asFileTree.matching { include 'LICENSE*.rst' }
199+
).withPropertyName('inputFiles')
200+
201+
outputs.file(layout.buildDirectory.file(getZipName('oss')))
202+
203+
workingDir = layout.projectDirectory
204+
executable = layout.projectDirectory.file("build${getScriptExt()}")
205+
206+
environment([
207+
'BUILD_TYPE': 'oss',
208+
'NO_PAUSE' : '1'
209+
])
210+
164211
doFirst {
165212
println 'Building the flashable zip (open-source components only) with Gradle...'
166-
environment BUILD_TYPE: 'oss'
167-
environment NO_PAUSE: '1'
168-
executable "${projectDir}/build" + getScriptExt()
169213
}
170214
}
171215

172216
tasks.named('assemble') {
173217
group = null
174218
description = 'Alias of "buildOtaOSS" task.'
175-
finalizedBy buildOtaOSS
219+
dependsOn tasks.named('buildOtaOSS')
176220
}
177221

178222
tasks.named('build') {
@@ -210,7 +254,7 @@ publishing {
210254
publications {
211255
maven(MavenPublication) {
212256
groupId = 'com.github.micro5k'
213-
artifactId = project.ext.projectId
257+
artifactId = getProjectId().get()
214258
version = project.ext.isAlpha ? "${project.version}-SNAPSHOT" : project.version
215259
pom {
216260
name = project.name
@@ -225,14 +269,13 @@ tasks.withType(PublishToMavenLocal).configureEach {
225269
mustRunAfter 'build', 'buildOta', 'buildOtaOSS'
226270

227271
doFirst {
228-
def projectId = project.ext.projectId
229-
def commit = getGitCommitHash()
230-
def zipFiles = fileTree(dir: project.buildDir).matching {
231-
include "${projectId}-${version}-g${commit}-*-by-ale5000-signed.zip"
232-
}.files
233-
234-
if(zipFiles.size() < 1 || zipFiles.size() > 2) {
235-
throw new GradleException("Publishing failed: Expected 1 or 2 artifacts in '${project.buildDir}', but found ${zipFiles.size()}. Check if build tasks were executed.")
272+
def zipFiles = layout.buildDirectory.asFileTree.matching {
273+
include getZipName().get()
274+
}
275+
276+
def count = zipFiles.size()
277+
if(count !in 1..2) {
278+
throw new GradleException("Publishing failed: Expected 1 or 2 artifacts, but found ${count}. Check if build tasks were executed.")
236279
}
237280

238281
// Add artifacts to the publication just before execution

gradle.properties

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# SPDX-License-Identifier: CC0-1.0
33

44
systemProp.file.encoding=UTF-8
5-
org.gradle.jvmargs=-Dfile.encoding=UTF-8
6-
org.gradle.warning.mode=all
7-
org.gradle.parallel=true
5+
org.gradle.jvmargs=-Dfile.encoding=UTF-8 -XX:+UseParallelGC
86
org.gradle.caching=true
7+
org.gradle.configuration-cache=false
8+
org.gradle.parallel=true
9+
org.gradle.warning.mode=all

0 commit comments

Comments
 (0)