Skip to content

Instantly share code, notes, and snippets.

@ghale
Created October 24, 2019 13:09
Show Gist options
  • Save ghale/fc2a7ee19cf9d4a1060d9d41817eda2e to your computer and use it in GitHub Desktop.
Save ghale/fc2a7ee19cf9d4a1060d9d41817eda2e to your computer and use it in GitHub Desktop.
/*
* Adds extended data to a build scan so that this data can be searched on in the scans ui.
*
* Add this script to the gradle directory and apply it in your build.gradle
* e.g.
*
* apply from: "gradle/buildScan-extended-data.gradle"
*
*
* Note that this script requires the following to be added to your settings.gradle:
*
* gradle.rootProject {
* ext.buildCacheLocalEnabled = buildCache.local?.enabled
* ext.buildCacheRemoteEnabled = buildCache.remote?.enabled
* }
*/
import java.lang.management.ManagementFactory
import javax.management.ObjectName
tagCleanOrIncremental()
tagAssembleDebug()
tagBuildCacheEnabled()
tagParallel()
tagSystemSpecs()
// Tag whether or not this build has been run with a clean task or is an incremental build
void tagCleanOrIncremental() {
gradle.taskGraph.whenReady { graph ->
if (graph.allTasks.any { task -> task.name == "clean" }) {
buildScan.tag "Clean"
} else {
buildScan.tag "Incremental"
}
}
}
// Tag whether this build was run as "assembleDebug" or "clean assembleDebug" - any other combination of
// tasks on the command line will be ignored
void tagAssembleDebug() {
def taskNames = gradle.startParameter.taskNames
def hasAssembleDebugOnly = taskNames == ["assembleDebug"]
def hasAssembleDebugAndClean = taskNames == ["clean", "assembleDebug"]
if (hasAssembleDebugOnly || hasAssembleDebugAndClean) {
buildScan.tag "assembleDebug"
}
}
// Tag whether the local or remote build caches are enabled
void tagBuildCacheEnabled() {
def buildCacheEnabled = gradle.startParameter.buildCacheEnabled
buildScan.value "Local Build Cache", (buildCacheEnabled && buildCacheLocalEnabled) ? "enabled" : "disabled"
buildScan.value "Remote Build Cache", (buildCacheEnabled && buildCacheRemoteEnabled) ? "enabled" : "disabled"
}
// Tag whether the build was run in parallel
void tagParallel() {
if (gradle.startParameter.parallelProjectExecutionEnabled) {
buildScan.tag "Parallel"
} else {
buildScan.tag "Not Parallel"
}
}
// Tag the number of cpus and amount of physical memory
void tagSystemSpecs() {
def mBeanServer = ManagementFactory.getPlatformMBeanServer()
def memSize = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize")
buildScan.value "Physical Memory", memSize.toString()
def processorCount = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "AvailableProcessors")
buildScan.value "Processor Count", processorCount.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment