From 1fc9502ef9efb2a11ef4efc2a52dbc5ec45c677b Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 12 Apr 2023 19:58:41 +0530 Subject: [PATCH] feat(gradle-plugin): implemented logic for gradle plugin --- gradle-plugin/build.gradle.kts | 48 +++++++++++++++++- gradle-plugin/src/main/java/.gitignore | 1 + .../src/main/java/AndroidIDEBuildInfo.kt.in | 25 ++++++++++ .../gradle/AndroidIDEGradlePlugin.kt | 5 ++ .../androidide/gradle/LogSenderPlugin.kt | 50 +++++++++++++++++++ .../gradle/LogSenderPluginExtension.kt | 31 ++++++++++++ .../com/itsaky/androidide/gradle/common.kt | 24 +++++++++ settings.gradle.kts | 2 +- 8 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 gradle-plugin/src/main/java/.gitignore create mode 100644 gradle-plugin/src/main/java/AndroidIDEBuildInfo.kt.in create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPluginExtension.kt create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/common.kt diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index 0fa97a619..ca9d4b66e 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.utils.addToStdlib.ifTrue + /* * This file is part of AndroidIDE. * @@ -21,6 +23,50 @@ plugins { id("com.gradle.plugin-publish") } +tasks.create("generateBuildInfo") { + + val buildInfoFile = "src/main/java/AndroidIDEBuildInfo.kt" + val buildInfoFileIn = "${buildInfoFile}.in" + + project + .file(buildInfoFileIn) + .replaceContents( + dest = project.file(buildInfoFile), + candidates = arrayOf("@@BUILD_VERSION@@" to project.version.toString()) + ) +} + +val generatedWarning = "DO NOT EDIT - Automatically generated file" +fun File.replaceContents( + dest: File, + comment: String = "//", + vararg candidates: Pair +) { + val contents = + StringBuilder() + .append(comment) + .append(" ") + .append(generatedWarning) + .append(System.getProperty("line.separator").repeat(2)) + + bufferedReader().use { reader -> + reader.readText().also { text -> + var t = text + for ((old, new) in candidates) { + t = t.replace(old, new) + } + contents.append(t) + } + } + + dest.exists().ifTrue { dest.delete() } + + dest.bufferedWriter().use { writer -> + writer.write(contents.toString()) + writer.flush() + } +} + gradlePlugin { website.set(ProjectConfig.GITHUB_URL) vcsUrl.set(ProjectConfig.GITHUB_URL) @@ -34,4 +80,4 @@ gradlePlugin { tags.set(setOf("androidide")) } } -} \ No newline at end of file +} diff --git a/gradle-plugin/src/main/java/.gitignore b/gradle-plugin/src/main/java/.gitignore new file mode 100644 index 000000000..bdd5e7bba --- /dev/null +++ b/gradle-plugin/src/main/java/.gitignore @@ -0,0 +1 @@ +AndroidIDEBuildInfo.kt diff --git a/gradle-plugin/src/main/java/AndroidIDEBuildInfo.kt.in b/gradle-plugin/src/main/java/AndroidIDEBuildInfo.kt.in new file mode 100644 index 000000000..7323fdd05 --- /dev/null +++ b/gradle-plugin/src/main/java/AndroidIDEBuildInfo.kt.in @@ -0,0 +1,25 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +/** + * Build information. + * + * @author Akash Yadav + */ +object AndroidIDEBuildInfo { + const val BUILD_VERSION = "@@BUILD_VERSION@@" +} \ No newline at end of file diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt index 48f7167df..8962c047a 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt @@ -27,5 +27,10 @@ import org.gradle.api.Project class AndroidIDEGradlePlugin : Plugin { override fun apply(target: Project) { + target.run { + if (plugins.hasPlugin("com.android.application")) { + plugins.apply(LogSenderPlugin::class.java) + } + } } } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt new file mode 100644 index 000000000..9a7809ef1 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt @@ -0,0 +1,50 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +package com.itsaky.androidide.gradle + +import AndroidIDEBuildInfo +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * Plugin to manage LogSender in Android applications. + * + * @author Akash Yadav + */ +class LogSenderPlugin : Plugin { + + companion object { + const val LOGSENDER_DEPENDENCY = + "com.itsaky.androidide:logsender:${AndroidIDEBuildInfo.BUILD_VERSION}" + } + + override fun apply(target: Project) { + target.run { + check(plugins.hasPlugin(APP_PLUGIN)) { + "${javaClass.simpleName} can only be applied to Android application projects." + } + + val extension = extensions.create("logsender", LogSenderPluginExtension::class.java) + + configurations + .getByName("${extension.variant}RuntimeOnly") + .dependencies + .add(dependencies.create(LOGSENDER_DEPENDENCY)) + } + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPluginExtension.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPluginExtension.kt new file mode 100644 index 000000000..827e5cf04 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPluginExtension.kt @@ -0,0 +1,31 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +package com.itsaky.androidide.gradle + +/** + * Extension for [LogSenderPlugin]. + * + * @author Akash Yadav + */ +class LogSenderPluginExtension { + + /** + * The name of the variant to which LogSender dependency should be added. + */ + var variant: String = "debug" +} \ No newline at end of file diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/common.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/common.kt new file mode 100644 index 000000000..a39bd2206 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/common.kt @@ -0,0 +1,24 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +package com.itsaky.androidide.gradle + +/** + * @author Akash Yadav + */ + +const val APP_PLUGIN = "com.android.application" \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index b2088d76d..10a8d02ca 100755 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -55,7 +55,7 @@ include( ":gradle-plugin", ":lexers", ":logger", - ":logsender-api", + ":logsender", ":logsender-sample", ":lookup", ":preferences",