Remove class files when building non-incrementally

This commit is contained in:
Alexey Tsvetkov
2016-03-28 08:41:04 +03:00
parent c8b7764576
commit 3d8ec118cb
6 changed files with 80 additions and 4 deletions
@@ -457,6 +457,11 @@ open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments>() {
outputDir: File,
args: K2JVMCompilerArguments
): ExitCode {
logger.kotlinDebug("Removing all kotlin classes in $outputDir")
// we're free to delete all classes since only we know about that directory
// todo: can be optimized -- compile and remove only files that were not generated
listClassFiles(outputDir.canonicalPath).forEach { it.delete() }
val moduleFile = makeModuleFile(args.moduleName, isTest = false, outputDir = outputDir, sourcesToCompile = sourcesToCompile, javaSourceRoots = getJavaSourceRoots(), classpath = classpath, friendDirs = listOf())
args.module = moduleFile.absolutePath
val messageCollector = GradleMessageCollector(logger)
@@ -1,6 +1,6 @@
package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.findFileByName
import org.jetbrains.kotlin.gradle.util.getFileByName
import org.jetbrains.kotlin.gradle.util.modify
import org.junit.Test
import java.io.File
@@ -44,7 +44,7 @@ class CacheVersionChangedIT : BaseGradleIT() {
versionFile.writeText(modifiedVersion)
// gradle won't call kotlin task if no file is changed
val dummyKtFile = project.projectDir.findFileByName("Dummy.kt")
val dummyKtFile = project.projectDir.getFileByName("Dummy.kt")
dummyKtFile.modify { it + " " }
project.build("build", options = options) {
@@ -0,0 +1,65 @@
package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.findFileByName
import org.jetbrains.kotlin.gradle.util.getFileByName
import org.jetbrains.kotlin.gradle.util.modify
import org.junit.Test
import java.io.File
import kotlin.test.assertNull
import kotlin.test.assertTrue
class ClassFileIsRemovedIT : BaseGradleIT() {
@Test
fun testClassIsRemovedNonIC() {
doTestClassIsRemoved(defaultBuildOptions())
}
@Test
fun testClassIsRemovedIC() {
doTestClassIsRemoved(defaultBuildOptions().copy(incremental = true))
}
private fun doTestClassIsRemoved(buildOptions: BuildOptions) {
doTest(buildOptions) { dummyFile ->
assertTrue(dummyFile.delete(), "Could not delete $dummyFile")
}
}
@Test
fun testClassIsRenamedNonIC() {
doTestClassIsRenamed(defaultBuildOptions())
}
@Test
fun testClassIsRenamedIC() {
doTestClassIsRenamed(defaultBuildOptions().copy(incremental = true))
}
fun doTestClassIsRenamed(buildOptions: BuildOptions) {
doTest(buildOptions) { dummyFile ->
dummyFile.modify { it.replace("Dummy", "ForDummies") }
}
}
fun doTest(buildOptions: BuildOptions, transformDummy: (File)->Unit) {
val project = Project("kotlinInJavaRoot", "2.10")
project.build("build", options = buildOptions) {
assertSuccessful()
}
val dummyFile = project.projectDir.getFileByName("Dummy.kt")
transformDummy(dummyFile)
project.build("build", options = buildOptions) {
assertSuccessful()
val dummyClassFile = project.projectDir.findFileByName("Dummy.class")
assertNull(dummyClassFile, "$dummyClassFile should not exist!")
}
// check that class removal does not trigger rebuild
project.build("build", options = buildOptions) {
assertSuccessful()
assertContains(":compileKotlin UP-TO-DATE", ":compileJava UP-TO-DATE")
}
}
}
@@ -2,9 +2,11 @@ package org.jetbrains.kotlin.gradle.util
import java.io.File
fun File.findFileByName(name: String): File =
fun File.getFileByName(name: String): File =
findFileByName(name) ?: throw AssertionError("Could not find file with name '$name' in $this")
fun File.findFileByName(name: String): File? =
walk().filter { it.isFile && it.name.equals(name, ignoreCase = true) }.firstOrNull()
?: throw AssertionError("Could not find file with name '$name' in $this")
fun File.modify(transform: (String)->String) {
writeText(transform(readText()))
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=512m