Browse Source

Add option to preserve working dir after build

Useful for troubleshooting and experimentation
(e.g. re-run tool in CLI with different args).

To enable:
* Add 'compose.preserve.working.dir=true' to gradle.properties;
* Add '-Pcompose.preserve.working.dir=true' to CLI args.
pull/224/head
Alexey Tsvetkov 4 years ago committed by Alexey Tsvetkov
parent
commit
b915b44d04
  1. 16
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/ComposeProjectProperties.kt
  2. 45
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt
  3. 44
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJvmToolOperationTask.kt

16
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/ComposeProjectProperties.kt

@ -0,0 +1,16 @@
package org.jetbrains.compose.desktop.application.internal
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
internal object ComposeProperties {
fun isVerbose(providers: ProviderFactory): Provider<Boolean> = providers
.gradleProperty("compose.desktop.verbose")
.orElse("false")
.map { "true".equals(it, ignoreCase = true) }
fun preserveWorkingDir(providers: ProviderFactory): Provider<Boolean> = providers
.gradleProperty("compose.preserve.working.dir")
.orElse("false")
.map { "true".equals(it, ignoreCase = true) }
}

45
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt

@ -8,6 +8,8 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.process.ExecResult
import org.gradle.process.ExecSpec
import org.gradle.work.ChangeType
import org.gradle.work.InputChanges
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.compose.desktop.application.internal.*
import java.io.File
@ -17,6 +19,9 @@ abstract class AbstractJPackageTask @Inject constructor(
@get:Input
val targetFormat: TargetFormat,
) : AbstractJvmToolOperationTask("jpackage") {
@get:OutputDirectory
val destinationDir: DirectoryProperty = objects.directoryProperty()
@get:InputFiles
val files: ConfigurableFileCollection = objects.fileCollection()
@ -216,18 +221,34 @@ abstract class AbstractJPackageTask @Inject constructor(
}
}
override fun prepareWorkingDir(tmpDir: File) {
super.prepareWorkingDir(tmpDir)
launcherMainJar.asFile.orNull?.let { sourceFile ->
val targetFile = tmpDir.resolve(sourceFile.name)
sourceFile.copyTo(targetFile)
}
val myFiles = files
fileOperations.copy {
it.from(myFiles)
it.into(tmpDir)
override fun prepareWorkingDir(inputChanges: InputChanges) {
fileOperations.delete(destinationDir)
if (inputChanges.isIncremental) {
logger.debug("Updating working dir incrementally: $workingDir")
val allChanges = inputChanges.getFileChanges(files).asSequence() +
inputChanges.getFileChanges(launcherMainJar)
allChanges.forEach { fileChange ->
val sourceFile = fileChange.file
val targetFile = workingDir.resolve(sourceFile.name)
if (fileChange.changeType == ChangeType.REMOVED) {
fileOperations.delete(targetFile)
logger.debug("Deleted: $targetFile")
} else {
sourceFile.copyTo(targetFile, overwrite = true)
logger.debug("Updated: $targetFile")
}
}
} else {
logger.debug("Updating working dir non-incrementally: $workingDir")
fileOperations.delete(workingDir)
workingDir.mkdirs()
fileOperations.copy {
it.from(files)
it.from(launcherMainJar)
it.into(workingDir)
}
}
}

44
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJvmToolOperationTask.kt

@ -1,7 +1,6 @@
package org.jetbrains.compose.desktop.application.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.internal.file.FileOperations
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
@ -11,12 +10,12 @@ import org.gradle.api.tasks.*
import org.gradle.process.ExecOperations
import org.gradle.process.ExecResult
import org.gradle.process.ExecSpec
import org.gradle.work.InputChanges
import org.jetbrains.compose.desktop.application.internal.ComposeProperties
import org.jetbrains.compose.desktop.application.internal.OS
import org.jetbrains.compose.desktop.application.internal.cliArg
import org.jetbrains.compose.desktop.application.internal.currentOS
import org.jetbrains.compose.desktop.application.internal.notNullProperty
import java.io.File
import java.nio.file.Files
import javax.inject.Inject
abstract class AbstractJvmToolOperationTask(private val toolName: String) : DefaultTask() {
@ -29,13 +28,13 @@ abstract class AbstractJvmToolOperationTask(private val toolName: String) : Defa
@get:Inject
protected abstract val fileOperations: FileOperations
@get:LocalState
protected val workingDir: File = project.buildDir.resolve("compose/tmp/$name")
@get:Input
@get:Optional
val freeArgs: ListProperty<String> = objects.listProperty(String::class.java)
@get:OutputDirectory
val destinationDir: DirectoryProperty = objects.directoryProperty()
@get:Internal
val javaHome: Property<String> = objects.notNullProperty<String>().apply {
set(providers.systemProperty("java.home"))
@ -43,23 +42,25 @@ abstract class AbstractJvmToolOperationTask(private val toolName: String) : Defa
@get:Internal
val verbose: Property<Boolean> = objects.notNullProperty<Boolean>().apply {
val composeVerbose = providers
.gradleProperty("compose.desktop.verbose")
.map { "true".equals(it, ignoreCase = true) }
set(providers.provider { logger.isDebugEnabled }.orElse(composeVerbose))
set(providers.provider { logger.isDebugEnabled }.orElse(ComposeProperties.isVerbose(providers)))
}
protected open fun prepareWorkingDir(inputChanges: InputChanges) {
fileOperations.delete(workingDir)
workingDir.mkdirs()
}
protected open fun makeArgs(tmpDir: File): MutableList<String> = arrayListOf<String>().apply {
freeArgs.orNull?.forEach { add(it) }
}
protected open fun prepareWorkingDir(tmpDir: File) {}
protected open fun configureExec(exec: ExecSpec) {}
protected open fun checkResult(result: ExecResult) {
result.assertNormalExitValue()
}
@TaskAction
fun run() {
fun run(inputChanges: InputChanges) {
val javaHomePath = javaHome.get()
val jtool = File(javaHomePath).resolve("bin/${executableName(toolName)}")
@ -68,24 +69,21 @@ abstract class AbstractJvmToolOperationTask(private val toolName: String) : Defa
"Ensure JAVA_HOME or buildSettings.javaHome is set to JDK 14 or newer"
}
fileOperations.delete(destinationDir)
val tmpDir = Files.createTempDirectory("compose-${toolName}").toFile().apply {
deleteOnExit()
}
try {
val args = makeArgs(tmpDir)
prepareWorkingDir(tmpDir)
val composeBuildDir = project.buildDir.resolve("compose").apply { mkdirs() }
val argsFile = composeBuildDir.resolve("${name}.args.txt")
argsFile.writeText(args.joinToString("\n"))
prepareWorkingDir(inputChanges)
val args = makeArgs(workingDir)
val argsFile = workingDir.parentFile.resolve("${name}.args.txt")
argsFile.writeText(args.joinToString("\n"))
try {
execOperations.exec { exec ->
configureExec(exec)
exec.executable = jtool.absolutePath
exec.setArgs(listOf("@${argsFile.absolutePath}"))
}.also { checkResult(it) }
} finally {
tmpDir.deleteRecursively()
if (!ComposeProperties.preserveWorkingDir(providers).get()) {
fileOperations.delete(workingDir)
}
}
}
}
Loading…
Cancel
Save