-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-31: Support Configuration Cache
Issue-31: Support Configuration Cache
- Loading branch information
Showing
7 changed files
with
78 additions
and
199 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 26 additions & 12 deletions
38
src/main/kotlin/com/asarkar/gradle/buildtimetracker/BuildTimeTrackerPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
package com.asarkar.gradle.buildtimetracker | ||
|
||
import com.asarkar.gradle.buildtimetracker.Constants.EXTRA_EXTENSION_NAME | ||
import com.asarkar.gradle.buildtimetracker.Constants.LOGGER_KEY | ||
import com.asarkar.gradle.buildtimetracker.Constants.PLUGIN_EXTENSION_NAME | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.plugins.ExtensionAware | ||
import org.gradle.api.plugins.ReportingBasePlugin | ||
import org.gradle.api.reflect.TypeOf | ||
import org.gradle.build.event.BuildEventsListenerRegistry | ||
import javax.inject.Inject | ||
|
||
class BuildTimeTrackerPlugin : Plugin<Project> { | ||
@Suppress("UnstableApiUsage") | ||
class BuildTimeTrackerPlugin @Inject constructor(private val registry: BuildEventsListenerRegistry) : Plugin<Project> { | ||
override fun apply(project: Project) { | ||
project.pluginManager.apply(ReportingBasePlugin::class.java) | ||
val ext = project.extensions.create( | ||
PLUGIN_EXTENSION_NAME, BuildTimeTrackerPluginExtension::class.java, project | ||
) | ||
(ext as ExtensionAware).extensions.add( | ||
object : TypeOf<Map<String, Any>>() {}, | ||
EXTRA_EXTENSION_NAME, | ||
mapOf<String, Any>(LOGGER_KEY to project.logger) | ||
) | ||
val timingRecorder = TimingRecorder(ext) | ||
project.gradle.addListener(timingRecorder) | ||
val clazz = TimingRecorder::class.java | ||
val timingRecorder = | ||
project.gradle.sharedServices.registerIfAbsent(clazz.simpleName, clazz) { spec -> | ||
val params = BuildTimeTrackerPluginParams(ext.reportsDir.get().asFile) | ||
spec.parameters.getParams().set(params) | ||
} | ||
|
||
project.gradle.projectsEvaluated { | ||
copyParams(ext, timingRecorder.get().parameters.getParams().get()) | ||
} | ||
|
||
registry.onTaskCompletion(timingRecorder) | ||
} | ||
|
||
private fun copyParams(src: BuildTimeTrackerPluginExtension, dest: BuildTimeTrackerPluginParams) { | ||
dest.barPosition = src.barPosition.get() | ||
dest.sort = src.sort.get() | ||
dest.output = src.output.get() | ||
dest.maxWidth = src.maxWidth.get() | ||
dest.minTaskDuration = src.minTaskDuration.get() | ||
dest.showBars = src.showBars.get() | ||
dest.reportsDir = src.reportsDir.get().asFile | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 27 additions & 39 deletions
66
src/main/kotlin/com/asarkar/gradle/buildtimetracker/TimingRecorder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,50 @@ | ||
package com.asarkar.gradle.buildtimetracker | ||
|
||
import org.gradle.BuildAdapter | ||
import org.gradle.BuildResult | ||
import org.gradle.api.Task | ||
import org.gradle.api.execution.TaskExecutionListener | ||
import org.gradle.api.invocation.Gradle | ||
import org.gradle.api.logging.Logger | ||
import org.gradle.api.plugins.ExtensionAware | ||
import org.gradle.api.reflect.TypeOf | ||
import org.gradle.api.tasks.TaskState | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.services.BuildService | ||
import org.gradle.api.services.BuildServiceParameters | ||
import org.gradle.tooling.events.FinishEvent | ||
import org.gradle.tooling.events.OperationCompletionListener | ||
import org.gradle.tooling.events.task.TaskFinishEvent | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.ConcurrentLinkedQueue | ||
|
||
class TimingRecorder(private val ext: BuildTimeTrackerPluginExtension) : TaskExecutionListener, BuildAdapter() { | ||
private val taskStartTimings: MutableMap<String, Instant> = ConcurrentHashMap() | ||
private val taskDurations: MutableCollection<Pair<String, Long>> = ConcurrentLinkedQueue() | ||
private lateinit var buildStarted: Instant | ||
|
||
override fun beforeExecute(task: Task) { | ||
taskStartTimings[task.path] = Instant.now() | ||
@Suppress("UnstableApiUsage") | ||
abstract class TimingRecorder : BuildService<TimingRecorder.Params>, OperationCompletionListener, AutoCloseable { | ||
interface Params : BuildServiceParameters { | ||
fun getParams(): Property<BuildTimeTrackerPluginParams> | ||
} | ||
|
||
override fun afterExecute(task: Task, state: TaskState) { | ||
check(taskStartTimings.contains(task.path)) { "No start timing for task ${task.path}" } | ||
val duration = Duration.between(taskStartTimings[task.path], Instant.now()).seconds | ||
if (duration >= ext.minTaskDuration.get().seconds) { | ||
taskDurations.add(task.path to duration) | ||
private val taskDurations: MutableCollection<Pair<String, Long>> = ConcurrentLinkedQueue() | ||
private val buildStarted: Instant = Instant.now() | ||
|
||
override fun onFinish(event: FinishEvent) { | ||
if (event is TaskFinishEvent) { | ||
val params = parameters.getParams().get() | ||
val duration = Duration.ofMillis(event.result.endTime - event.result.startTime).seconds | ||
if (duration >= params.minTaskDuration.seconds) { | ||
taskDurations.add(event.descriptor.taskPath to duration) | ||
} | ||
} | ||
} | ||
|
||
override fun buildFinished(result: BuildResult) { | ||
override fun close() { | ||
if (taskDurations.isEmpty()) { | ||
val extra = (ext as ExtensionAware).extensions.getByType( | ||
object : TypeOf<Map<String, Any>>() {} | ||
) | ||
(extra[Constants.LOGGER_KEY] as Logger).lifecycle( | ||
"All tasks completed within the minimum threshold: {}s, no build summary to show", | ||
ext.minTaskDuration.get().seconds | ||
) | ||
return | ||
} | ||
val params = parameters.getParams().get() | ||
val buildDuration = Duration.between(buildStarted, Instant.now()).seconds | ||
Printer.newInstance(ext) | ||
Printer.newInstance(params) | ||
.use { printer -> | ||
val input = PrinterInput( | ||
buildDuration, | ||
if (ext.sort.get()) taskDurations.sortedBy { -it.second } else taskDurations, | ||
ext.maxWidth.get(), | ||
ext.showBars.get(), | ||
ext.barPosition.get() | ||
if (params.sort) taskDurations.sortedBy { -it.second } else taskDurations, | ||
params.maxWidth, | ||
params.showBars, | ||
params.barPosition | ||
) | ||
printer.print(input) | ||
} | ||
} | ||
|
||
override fun projectsEvaluated(gradle: Gradle) { | ||
buildStarted = Instant.now() | ||
} | ||
} |