feat(gradle-plugin): implemented logic for gradle plugin

This commit is contained in:
Akash Yadav
2023-04-12 19:58:41 +05:30
parent 729a6246f9
commit 1fc9502ef9
8 changed files with 184 additions and 2 deletions
+47 -1
View File
@@ -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<String, String>
) {
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"))
}
}
}
}
+1
View File
@@ -0,0 +1 @@
AndroidIDEBuildInfo.kt
@@ -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 <https://www.gnu.org/licenses/>.
*/
/**
* Build information.
*
* @author Akash Yadav
*/
object AndroidIDEBuildInfo {
const val BUILD_VERSION = "@@BUILD_VERSION@@"
}
@@ -27,5 +27,10 @@ import org.gradle.api.Project
class AndroidIDEGradlePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.run {
if (plugins.hasPlugin("com.android.application")) {
plugins.apply(LogSenderPlugin::class.java)
}
}
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<Project> {
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))
}
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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"
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.gradle
/**
* @author Akash Yadav
*/
const val APP_PLUGIN = "com.android.application"
+1 -1
View File
@@ -55,7 +55,7 @@ include(
":gradle-plugin",
":lexers",
":logger",
":logsender-api",
":logsender",
":logsender-sample",
":lookup",
":preferences",