Skip to content

Commit

Permalink
feat: avoid kotlin-dsl APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMrMilchmann committed Feb 21, 2023
1 parent 8475a20 commit 0e29b65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
Expand Up @@ -24,14 +24,13 @@ package io.github.themrmilchmann.gradle.ecj
import io.github.themrmilchmann.gradle.ecj.plugins.ECJPlugin
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

public abstract class ECJExtension @Inject constructor(objects: ObjectFactory) {

public val compilerGroupId: Property<String> = objects.property<String>().convention(ECJPlugin.DEFAULT_DEPENDENCY_GROUP)
public val compilerArtifactId: Property<String> = objects.property<String>().convention(ECJPlugin.DEFAULT_DEPENDENCY_ARTIFACT)
public val compilerVersion: Property<String> = objects.property<String>().convention(ECJPlugin.DEFAULT_DEPENDENCY_VERSION)
public val compilerGroupId: Property<String> = objects.property(String::class.java).convention(ECJPlugin.DEFAULT_DEPENDENCY_GROUP)
public val compilerArtifactId: Property<String> = objects.property(String::class.java).convention(ECJPlugin.DEFAULT_DEPENDENCY_ARTIFACT)
public val compilerVersion: Property<String> = objects.property(String::class.java).convention(ECJPlugin.DEFAULT_DEPENDENCY_VERSION)

init {
compilerGroupId.finalizeValueOnRead()
Expand Down
Expand Up @@ -27,7 +27,7 @@ import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.tasks.compile.*
import org.gradle.jvm.toolchain.*
import org.gradle.kotlin.dsl.*
import org.gradle.process.CommandLineArgumentProvider

public class ECJPlugin : Plugin<Project> {

Expand All @@ -49,34 +49,31 @@ public class ECJPlugin : Plugin<Project> {

}

@OptIn(ExperimentalStdlibApi::class)
override fun apply(target: Project): Unit = applyTo(target) project@{
/*
* Make sure that the JavaPlugin is applied before this plugin, since we have to override some properties.
* (Configuration happens in the same order in which the configuration actions are added.)
*/
pluginManager.apply(JavaPlugin::class)
pluginManager.apply(JavaPlugin::class.java)

val ecjExtension = extensions.create<ECJExtension>("ecj")
val ecjExtension = extensions.create("ecj", ECJExtension::class.java)

val ecjConfiguration = configurations.create(ECJ_CONFIGURATION_NAME) {
defaultDependencies {
val compilerGroupId = ecjExtension.compilerGroupId.orNull ?: error("ECJ compilerGroupId may not be null")
val compilerArtifactId = ecjExtension.compilerArtifactId.orNull ?: error("ECJ compilerArtifactId may not be null")
val compilerVersion = ecjExtension.compilerVersion.orNull ?: error("ECJ compilerVersion may not be null")

dependencies {
add(create(group = compilerGroupId, name = compilerArtifactId, version = compilerVersion))
}
dependencies.add(target.dependencies.create("$compilerGroupId:$compilerArtifactId:$compilerVersion"))
}
}

val java = extensions.getByType<JavaPluginExtension>()
val javaToolchains = extensions.getByType<JavaToolchainService>()
val java = extensions.getByType(JavaPluginExtension::class.java)
val javaToolchains = extensions.getByType(JavaToolchainService::class.java)

tasks.withType<JavaCompile> {
tasks.withType(JavaCompile::class.java) {
/* Overwrite the javaCompiler to make sure that it is not inferred from the toolchain. */
javaCompiler.set(this@project.provider { null })
javaCompiler.set(null as JavaCompiler?)

/* ECJ does not support generating JNI headers. Make sure the property is not used. */
options.headerOutputDirectory.set(this@project.provider { null })
Expand All @@ -93,14 +90,9 @@ public class ECJPlugin : Plugin<Project> {
options.isFork = true
options.forkOptions.executable = javaLauncher.executablePath.asFile.absolutePath

val prevJvmArgs = options.forkOptions.jvmArgs
options.forkOptions.jvmArgs = buildList(capacity = (prevJvmArgs?.size ?: 0) + 3) {
if (prevJvmArgs != null) addAll(prevJvmArgs)

add("-cp")
add(ecjConfiguration.asPath)
add(MAIN)
}
options.forkOptions.jvmArgumentProviders.add(CommandLineArgumentProvider {
mutableListOf("-cp", ecjConfiguration.asPath, MAIN)
})
}
}
}
Expand Down

1 comment on commit 0e29b65

@TheMrMilchmann
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failure is expected because the samples are tested against Gradle 8 (which is not yet supported, see #9). I probably should refactor this.

Please sign in to comment.