1. Why Build Systems?

[THEORY]

[PREDICT]

Read the code carefully before you run it.

prediction.kt
fun main() {
    val cache = mutableMapOf<String, Int>()

    fun build(name: String, input: Int): Int {
        val cached = cache[name]
        return if (cached == input) {
            println("$name: UP-TO-DATE")
            cached
        } else {
            println("$name: rebuilding")
            cache[name] = input
            input * 2
        }
    }

    build("moduleA", 5)
    build("moduleA", 5)
    build("moduleA", 7)
}

What do the three calls of build() print — which one uses the cached result?

[TASK]

A build system takes source code through several steps to a runnable artifact: first it is compiled, then the compiled classes are packed into an archive, and only after that can the application run. Call compile(), packageJar() and run() in main() in exactly the order a build system would execute them.

task.kt
val buildLog = mutableListOf<String>() fun compile() { buildLog.add("compile") println("Kotlin source code is compiled to .class files") } fun packageJar() { buildLog.add("packageJar") println("Compiled classes are packed into a .jar file") } fun run() { buildLog.add("run") println("Application is started from the .jar file") } fun main() { // TODO: call compile(), packageJar() and run() in the correct order }

Before you get a hint: what have you tried, and where exactly are you stuck?

[COMPREHENSION CHECK]

1. What does a build system automate according to the definition in this lesson?

2. Which point is NOT one of the typical responsibilities of a build system?