Skip to content

Commit

Permalink
Day 3 - Handle multi-level subproject tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
bamboo committed Apr 5, 2019
1 parent 8101b14 commit 9797ae0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,19 @@ class InstantExecutionIntegrationTest extends AbstractIntegrationSpec {
then:
noExceptionThrown()
}

def "instant execution for multi-level subproject"() {
given:
settingsFile << """
include 'a:b', 'a:c'
"""
run ":a:b:help", ":a:c:help", "-DinstantExecution"
def firstRunOutput = result.normalizedOutput

when:
run ":a:b:help", ":a:c:help", "-DinstantExecution"

then:
result.normalizedOutput == firstRunOutput
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.gradle.util.Path

import java.io.File
import java.lang.reflect.Method
import java.util.SortedSet
import javax.inject.Inject


Expand Down Expand Up @@ -65,28 +66,52 @@ class InstantExecution(private val host: Host) {
isInstantExecutionEnabled && instantExecutionStateFile.isFile

fun saveInstantExecutionState() {
if (!isInstantExecutionEnabled) {
return
if (isInstantExecutionEnabled) {
saveTasks()
}
}

fun loadInstantExecutionState() {
host.scheduleTasks(loadTasks())
}

private
fun saveTasks() {
KryoBackedEncoder(instantExecutionStateFile.outputStream()).use { encoder ->
val scheduledTasks = host.scheduledTasks
val relevantProjectPaths = scheduledTasks.mapNotNull { task ->
task.project.takeIf { it.parent != null }?.path?.let(Path::path)
}.toSortedSet()
val relevantClassPath = classPathFor(scheduledTasks)
encoder.serializeClassPath(relevantClassPath)
encoder.serializeCollection(relevantProjectPaths) {
encoder.writeString(it.path)
}
saveRelevantProjectsFor(scheduledTasks, encoder)
encoder.serializeCollection(scheduledTasks) { task ->
saveStateOf(task, encoder)
}
}
}

fun loadInstantExecutionState() {
host.scheduleTasks(loadTasks())
private
fun saveRelevantProjectsFor(tasks: List<Task>, encoder: KryoBackedEncoder) {

val relevantProjectPaths = tasks.mapNotNull { task ->
task.project.takeIf { it.parent != null }?.path?.let(Path::path)
}.toSortedSet()

encoder.serializeCollection(fillTheGapsOf(relevantProjectPaths)) {
encoder.writeString(it.path)
}
}

private
fun fillTheGapsOf(paths: SortedSet<Path>): List<Path> {
val pathsWithoutGaps = ArrayList<Path>(paths.size)
paths.forEach { path ->
var parent = path.parent
while (parent !== null && parent !in pathsWithoutGaps) {
pathsWithoutGaps.add(parent)
parent = parent.parent
}
pathsWithoutGaps.add(path)
}
return pathsWithoutGaps
}

private
Expand Down

0 comments on commit 9797ae0

Please sign in to comment.