Skip to content

Commit

Permalink
Issue-36: Support sort asc or desc
Browse files Browse the repository at this point in the history
Issue-36: Support sort asc or desc
  • Loading branch information
asarkar authored Apr 14, 2022
2 parents b902c6c + 73a0377 commit 5ccb1cb
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ If you are the fiddling type, you can customize the plugin as follows:

```
buildTimeTracker {
barPosition = TRAILING or LEADING, default is TRAILING
barPosition = BarPosition.TRAILING or BarPosition.LEADING, default is TRAILING
// Deprecated: Will be removed in v5, use sortBy
sort = false or true, default is false
output = CONSOLE or CSV, default is CONSOLE
sortBy = Sort.ASC, Sort.DESC, or Sort.NONE, default is NONE
output = Output.CONSOLE or Output.CSV, default is CONSOLE
maxWidth = 120, default is 80
minTaskDuration = Duration.ofSeconds(1), don't show tasks that take less than a second to execute
showBars = false or true, default is true
Expand All @@ -35,6 +37,8 @@ buildTimeTracker {

> If you are using Kotlin build script, set the configuration properties using `property.set()` method.
> `BarPosition`, `Sort`, and `Output` are enums, so, they need to be imported or fully-qualified.
:information_source: Due to a
[Gradle limitation](https://docs.gradle.org/6.5.1/userguide/upgrading_version_5.html#apis_buildlistener_buildstarted_and_gradle_buildstarted_have_been_deprecated)
and the ill-thought-out [Configuration Cache](https://github.com/gradle/gradle/issues/18520) design, the build duration
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pluginImplementationClass = com.asarkar.gradle.buildtimetracker.BuildTimeTracker
pluginDeclarationName = buildTimeTrackerPlugin

projectGroup = com.asarkar.gradle
projectVersion = 4.1.0
projectVersion = 4.2.0

junitVersion = latest.release
assertjVersion = latest.release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BuildTimeTrackerPlugin @Inject constructor(private val registry: BuildEven

private fun copyParams(src: BuildTimeTrackerPluginExtension, dest: BuildTimeTrackerPluginParams) {
dest.barPosition = src.barPosition.get()
dest.sort = src.sort.get()
dest.sortBy = if (src.sort.get()) Sort.DESC else src.sortBy.get()
dest.output = src.output.get()
dest.maxWidth = src.maxWidth.get()
dest.minTaskDuration = src.minTaskDuration.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ enum class Output {
CONSOLE, CSV
}

enum class Sort {
ASC, DESC, NONE
}

open class BuildTimeTrackerPluginExtension(private val project: Project) {
val barPosition: Property<BarPosition> = project.objects.property(BarPosition::class.java)
.convention(Constants.DEFAULT_BAR_POSITION)

@Deprecated("Will be removed in v5, use sortBy")
val sort: Property<Boolean> = project.objects.property(Boolean::class.java)
.convention(Constants.DEFAULT_SORT)
.convention(false)
val sortBy: Property<Sort> = project.objects.property(Sort::class.java)
.convention(Constants.DEFAULT_SORT_BY)
val output: Property<Output> = project.objects.property(Output::class.java)
.convention(Constants.DEFAULT_OUTPUT)
val maxWidth: Property<Int> = project.objects.property(Int::class.java)
Expand All @@ -38,7 +46,7 @@ open class BuildTimeTrackerPluginExtension(private val project: Project) {

open class BuildTimeTrackerPluginParams(var reportsDir: File) : java.io.Serializable {
var barPosition = Constants.DEFAULT_BAR_POSITION
var sort = Constants.DEFAULT_SORT
var sortBy = Constants.DEFAULT_SORT_BY
var output = Constants.DEFAULT_OUTPUT
var maxWidth = Constants.DEFAULT_MAX_WIDTH
var minTaskDuration: Duration = Duration.ofSeconds(Constants.DEFAULT_MIN_TASK_DURATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package com.asarkar.gradle.buildtimetracker

object Constants {
const val PLUGIN_EXTENSION_NAME = "buildTimeTracker"
const val EXTRA_EXTENSION_NAME = "extra"
const val LOGGER_KEY = "logger"
val DEFAULT_BAR_POSITION = BarPosition.TRAILING
const val DEFAULT_SORT = false
val DEFAULT_OUTPUT = Output.CONSOLE
val DEFAULT_SORT_BY = Sort.NONE
const val DEFAULT_MAX_WIDTH = 80
const val DEFAULT_MIN_TASK_DURATION = 1L
const val DEFAULT_SHOW_BARS = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ abstract class TimingRecorder : BuildService<TimingRecorder.Params>, OperationCo
val buildDuration = Duration.between(buildStarted, Instant.now()).seconds
Printer.newInstance(params)
.use { printer ->
val durations = when (params.sortBy) {
Sort.NONE -> taskDurations
Sort.DESC -> taskDurations.sortedBy { -it.second }
Sort.ASC -> taskDurations.sortedBy { it.second }
}
val input = PrinterInput(
buildDuration,
if (params.sort) taskDurations.sortedBy { -it.second } else taskDurations,
durations,
params.maxWidth,
params.showBars,
params.barPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class BuildTimeTrackerPluginFunctionalTest {
import ${Thread::class.qualifiedName}
import ${Output::class.qualifiedName}
import ${Duration::class.qualifiedName}
import ${Sort::class.qualifiedName}
plugins {
id("${props.getProperty("pluginId")}")
Expand Down Expand Up @@ -82,7 +83,7 @@ class BuildTimeTrackerPluginFunctionalTest {

println(buildFile.readText())

val result = run()
val result = run(taskName)

assertThat(result.task(taskName)?.outcome == SUCCESS)
val lines = result.output
Expand Down Expand Up @@ -110,7 +111,7 @@ class BuildTimeTrackerPluginFunctionalTest {

println(buildFile.readText())

val result = run()
val result = run(taskName)

assertThat(result.task(taskName)?.outcome == SUCCESS)
val lines = result.output
Expand Down Expand Up @@ -140,7 +141,7 @@ class BuildTimeTrackerPluginFunctionalTest {

println(buildFile.readText())

val result = run()
val result = run(taskName)
val csvFile = testProjectDir.resolve(Constants.CSV_FILENAME)
assertThat(result.task(taskName)?.outcome == SUCCESS)
assertThat(Files.exists(csvFile)).isTrue
Expand All @@ -166,7 +167,7 @@ class BuildTimeTrackerPluginFunctionalTest {

println(buildFile.readText())

val result = run()
val result = run(taskName)
val csvFile = testProjectDir.resolve(Constants.CSV_FILENAME)
assertThat(result.task(taskName)?.outcome == SUCCESS)
assertThat(Files.exists(csvFile)).isTrue
Expand All @@ -175,10 +176,142 @@ class BuildTimeTrackerPluginFunctionalTest {
assertThat(lines.first()).isEqualTo(":$taskName,0S,0%,")
}

private fun run(): BuildResult {
@Test
fun testSort() {
newBuildFile("build.gradle.kts")
Files.newBufferedWriter(buildFile, APPEND).use {
it.write(
"""
tasks.register("a") {
doLast {
Thread.sleep(1100)
println("Hello, World!")
}
}
tasks.register("b") {
doLast {
Thread.sleep(2100)
println("Hi there!")
}
}
${Constants.PLUGIN_EXTENSION_NAME} {
minTaskDuration.set(Duration.ofMillis(100))
sort.set(true)
}
""".trimIndent()
)
}

println(buildFile.readText())

val result = run("a", "b")

assertThat(result.task(taskName)?.outcome == SUCCESS)
val lines = result.output
.lines()
.filter { it.isNotEmpty() }
assertThat(lines).hasSizeGreaterThanOrEqualTo(4)
assertThat(lines[0]).isEqualTo("> Task :a")
assertThat(lines[1]).isEqualTo("Hello, World!")
assertThat(lines[2]).isEqualTo("> Task :b")
assertThat(lines[3]).isEqualTo("Hi there!")
assertThat(lines[4]).isEqualTo("== Build time summary ==")
assertThat(lines[5]).isEqualTo(":b | 2S | 67% | ${Printer.BLOCK_CHAR.toString().repeat(2)}")
assertThat(lines[6]).isEqualTo(":a | 1S | 33% | ${Printer.BLOCK_CHAR}")
}

@Test
fun testSortByDesc() {
newBuildFile("build.gradle.kts")
Files.newBufferedWriter(buildFile, APPEND).use {
it.write(
"""
tasks.register("a") {
doLast {
Thread.sleep(1100)
println("Hello, World!")
}
}
tasks.register("b") {
doLast {
Thread.sleep(2100)
println("Hi there!")
}
}
${Constants.PLUGIN_EXTENSION_NAME} {
minTaskDuration.set(Duration.ofMillis(100))
sortBy.set(Sort.DESC)
}
""".trimIndent()
)
}

println(buildFile.readText())

val result = run("a", "b")

assertThat(result.task(taskName)?.outcome == SUCCESS)
val lines = result.output
.lines()
.filter { it.isNotEmpty() }
assertThat(lines).hasSizeGreaterThanOrEqualTo(4)
assertThat(lines[0]).isEqualTo("> Task :a")
assertThat(lines[1]).isEqualTo("Hello, World!")
assertThat(lines[2]).isEqualTo("> Task :b")
assertThat(lines[3]).isEqualTo("Hi there!")
assertThat(lines[4]).isEqualTo("== Build time summary ==")
assertThat(lines[5]).isEqualTo(":b | 2S | 67% | ${Printer.BLOCK_CHAR.toString().repeat(2)}")
assertThat(lines[6]).isEqualTo(":a | 1S | 33% | ${Printer.BLOCK_CHAR}")
}

@Test
fun testSortByAsc() {
newBuildFile("build.gradle.kts")
Files.newBufferedWriter(buildFile, APPEND).use {
it.write(
"""
tasks.register("a") {
doLast {
Thread.sleep(1100)
println("Hello, World!")
}
}
tasks.register("b") {
doLast {
Thread.sleep(2100)
println("Hi there!")
}
}
${Constants.PLUGIN_EXTENSION_NAME} {
minTaskDuration.set(Duration.ofMillis(100))
sortBy.set(Sort.ASC)
}
""".trimIndent()
)
}

println(buildFile.readText())

val result = run("a", "b")

assertThat(result.task(taskName)?.outcome == SUCCESS)
val lines = result.output
.lines()
.filter { it.isNotEmpty() }
assertThat(lines).hasSizeGreaterThanOrEqualTo(4)
assertThat(lines[0]).isEqualTo("> Task :a")
assertThat(lines[1]).isEqualTo("Hello, World!")
assertThat(lines[2]).isEqualTo("> Task :b")
assertThat(lines[3]).isEqualTo("Hi there!")
assertThat(lines[4]).isEqualTo("== Build time summary ==")
assertThat(lines[5]).isEqualTo(":a | 1S | 33% | ${Printer.BLOCK_CHAR}")
assertThat(lines[6]).isEqualTo(":b | 2S | 67% | ${Printer.BLOCK_CHAR.toString().repeat(2)}")
}

private fun run(vararg tasks: String): BuildResult {
return GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
.withArguments(taskName, "--warning-mode=all", "--stacktrace")
.withArguments(*tasks, "--warning-mode=all", "--stacktrace")
.withPluginClasspath()
.withDebug(false)
.forwardOutput()
Expand Down

0 comments on commit 5ccb1cb

Please sign in to comment.