From 5f0ee6e759e9530d5d496514789a47b3444350da Mon Sep 17 00:00:00 2001 From: Jerome Dochez Date: Mon, 1 Feb 2021 16:13:40 -0800 Subject: [PATCH] added more kotlin generation if the kotlin plugin is applied. removed unuseful tracing. Test: Existing. --- .../gradle/replicator/codegen/ImportClassPicker.kt | 14 +++----------- code/plugin/build.gradle.kts | 2 +- .../replicator/codegen/plugin/CodegenPlugin.kt | 9 ++++++++- .../replicator/codegen/plugin/GenerateCode.kt | 1 - .../codegen/plugin/JavaLibraryCodegenPlugin.kt | 13 ++++++++----- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/code/codegen/src/main/kotlin/com/android/gradle/replicator/codegen/ImportClassPicker.kt b/code/codegen/src/main/kotlin/com/android/gradle/replicator/codegen/ImportClassPicker.kt index e91f9a4..9986ea3 100644 --- a/code/codegen/src/main/kotlin/com/android/gradle/replicator/codegen/ImportClassPicker.kt +++ b/code/codegen/src/main/kotlin/com/android/gradle/replicator/codegen/ImportClassPicker.kt @@ -111,17 +111,9 @@ open class ImportClassPicker( */ private fun loadModel(kClass: Class<*>): ClassModel<*>? { val selectedType = kClass.kotlin - try { - return ClassModel(selectedType, - selectedType.constructors.first(), - selectedType.declaredFunctions) - } catch (e: Exception) { - println("Caught !") - return null - } catch (e: Error) { - println("Caught !") - return null - } + return ClassModel(selectedType, + selectedType.constructors.first(), + selectedType.declaredFunctions) } /** * Returns true if the class is eligible to used for code generation. It must follows all the constraints diff --git a/code/plugin/build.gradle.kts b/code/plugin/build.gradle.kts index 6882674..f032cb9 100644 --- a/code/plugin/build.gradle.kts +++ b/code/plugin/build.gradle.kts @@ -52,7 +52,7 @@ gradlePlugin { val javaLibraryCodegen by plugins.creating { id = "com.android.gradle.replicator.java-library-codegen-plugin" - version = 0.1 + version = Versions.pluginVersion implementationClass = "com.android.gradle.replicator.codegen.plugin.JavaLibraryCodegenPlugin" } } \ No newline at end of file diff --git a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/CodegenPlugin.kt b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/CodegenPlugin.kt index aeedef6..b404ed4 100644 --- a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/CodegenPlugin.kt +++ b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/CodegenPlugin.kt @@ -149,7 +149,14 @@ class CodegenPlugin: Plugin { // Randomizer values should be set during project replication along the number of java and kotlin files. task.seed.set(Random.nextInt()) - task.nbOfJavaFiles.set(10) + val hasKotlin = project.plugins.map { it.toString() }.find { + it.contains("org.jetbrains.kotlin") + } != null + if (hasKotlin) { + task.nbOfKotlinFiles.set(10) + } else { + task.nbOfJavaFiles.set(10) + } // make sure we depend on our dependencies built artifacts so we have access to their generated classes. projectDependencies.forEach { diff --git a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/GenerateCode.kt b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/GenerateCode.kt index 93953c9..12ad8d9 100644 --- a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/GenerateCode.kt +++ b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/GenerateCode.kt @@ -36,7 +36,6 @@ abstract class GenerateCode: DefaultTask() { // generate files. Main().process( arrayOf( - "-gen", "Java", "-module", path.removeSuffix(":$name").removePrefix(":").replace(':', '_'), "-i", parameters.get().asFile.absolutePath, "-o", outputDirectory.get().asFile.absolutePath diff --git a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/JavaLibraryCodegenPlugin.kt b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/JavaLibraryCodegenPlugin.kt index 00a408d..d9d011d 100644 --- a/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/JavaLibraryCodegenPlugin.kt +++ b/code/plugin/src/main/kotlin/com/android/gradle/replicator/codegen/plugin/JavaLibraryCodegenPlugin.kt @@ -26,15 +26,15 @@ import kotlin.random.Random @Suppress("UnstableApiUsage") class JavaLibraryCodegenPlugin: AbstractCodeGenPlugin() { override fun apply(project: Project) { - println("Java Library plugin applied !") - val topProjectName by lazy { var current = project while (current.parent != null) current = current.parent!! current.name } - val hasKotlinSources = project.pluginManager.hasPlugin("org.jetbrains.kotlin.jvm") + val hasKotlinSources = project.plugins.map { it.toString() }.find { + it.contains("org.jetbrains.kotlin") + } != null val generateTask = project.tasks.register( "generateCodegenParams", @@ -84,9 +84,12 @@ class JavaLibraryCodegenPlugin: AbstractCodeGenPlugin() { // Randomizer values should be set during project replication along the number of java and kotlin files. task.seed.set(Random.nextInt()) - if (hasKotlinSources) + if (hasKotlinSources) { task.nbOfKotlinFiles.set(10) - else task.nbOfJavaFiles.set(10) } + } else { + task.nbOfJavaFiles.set(10) + } + } val generateCodeTask = project.tasks.register( "generateCode",