Add Gradle plugin to project

This commit is contained in:
Brian Norman
2020-02-06 11:06:14 -06:00
parent a76af607fb
commit 4aef0f08bb
11 changed files with 219 additions and 29 deletions
+9 -11
View File
@@ -1,19 +1,17 @@
plugins {
kotlin("jvm") version "1.3.60" apply false
id("org.jetbrains.dokka") version "0.10.0" apply false
id("nebula.release") version "13.1.1"
kotlin("jvm") version "1.3.60" apply false
id("org.jetbrains.dokka") version "0.10.0" apply false
id("com.gradle.plugin-publish") version "0.10.1" apply false
}
val release = tasks.findByPath(":release")
release?.finalizedBy(project.getTasksByName("publish", true))
allprojects {
group = "com.bnorm.power"
group = "com.bnorm.power"
version = "0.1.0-SNAPSHOT"
}
subprojects {
repositories {
mavenCentral()
jcenter()
}
repositories {
mavenCentral()
jcenter()
}
}
-1
View File
@@ -1 +0,0 @@
kotlin.code.style=official
@@ -0,0 +1,42 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java-gradle-plugin")
kotlin("jvm")
kotlin("kapt")
id("com.gradle.plugin-publish")
}
dependencies {
implementation(kotlin("stdlib"))
implementation(kotlin("gradle-plugin-api"))
kapt("com.google.auto.service:auto-service:1.0-rc6")
compileOnly("com.google.auto.service:auto-service-annotations:1.0-rc6")
}
pluginBundle {
website = "https://github.com/bnorm/kotlin-power-assert"
vcsUrl = "https://github.com/bnorm/kotlin-power-assert.git"
tags = listOf("kotlin", "power-assert")
}
gradlePlugin {
plugins {
create("kotlinPowerAssert") {
id = "com.bnorm.power.kotlin-power-assert"
displayName = "Kotlin Power Assertion Plugin"
description = "Kotlin Compiler Plugin to add power to your assertions"
implementationClass = "com.bnorm.power.PowerAssertGradlePlugin"
}
}
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.register("publish") {
dependsOn("publishPlugins")
}
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
open class PowerAssertGradleExtension {
var enabled: Boolean = true
}
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import org.gradle.api.Plugin
import org.gradle.api.Project
class PowerAssertGradlePlugin : Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
extensions.create("kotlinPowerAssert", PowerAssertGradleExtension::class.java)
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import com.google.auto.service.AutoService
import org.gradle.api.Project
import org.gradle.api.tasks.compile.AbstractCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
@AutoService(KotlinGradleSubplugin::class)
class PowerAssertGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
override fun getCompilerPluginId(): String = "com.bnorm.kotlin-power-assert"
override fun isApplicable(project: Project, task: AbstractCompile): Boolean =
project.plugins.hasPlugin(PowerAssertGradlePlugin::class.java)
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact(
groupId = "com.bnorm.power",
artifactId = "kotlin-power-assert",
version = "0.1.0-SNAPSHOT"
)
override fun apply(
project: Project,
kotlinCompile: AbstractCompile,
javaCompile: AbstractCompile?,
variantData: Any?,
androidProjectHandler: Any?,
kotlinCompilation: KotlinCompilation<KotlinCommonOptions>?
): List<SubpluginOption> {
val extension = project.extensions.findByType(PowerAssertGradleExtension::class.java)
?: PowerAssertGradleExtension()
return listOf(
SubpluginOption(key = "enabled", value = extension.enabled.toString())
)
}
}
+1 -1
View File
@@ -49,7 +49,7 @@ tasks.register("dokkaJar", Jar::class) {
}
signing {
setRequired(provider { gradle.taskGraph.hasTask("release") })
setRequired(provider { gradle.taskGraph.hasTask("publish") })
sign(publishing.publications)
}
@@ -44,7 +44,6 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.impl.IrStringConcatenationImpl
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.getPackageFragment
@@ -74,6 +73,7 @@ class PowerAssertCallTransformer(
private lateinit var file: IrFile
private lateinit var fileSource: String
// TODO this is the only thing keeping this project from being multiplatform
private val constructor = this@PowerAssertCallTransformer.context.ir.symbols.assertionErrorConstructor
override fun lower(irFile: IrFile) {
@@ -138,7 +138,7 @@ class PowerAssertCallTransformer(
require(assertCondition is IrGetValue)
// print(buildString {
// append(callSource).newline()
// append(callSource).appendln()
// val sorted = stack.sortedBy { it.indentation }
//
// val indentations = sorted.map { it.indentation }
@@ -149,7 +149,7 @@ class PowerAssertCallTransformer(
// }
// last = i
// }
// newline()
// appendln()
//
// for (tmp in sorted.asReversed()) {
//
@@ -163,7 +163,7 @@ class PowerAssertCallTransformer(
// }
//
// indent(tmp.indentation - last - 1)
// append(tmp.source).newline()
// append(tmp.source).appendln()
// }
// })
@@ -190,8 +190,8 @@ class PowerAssertCallTransformer(
val indentations = sorted.map { it.indentation }
addArgument(irString(buildString {
newline()
append(callSource).newline()
appendln()
append(callSource).appendln()
var last = -1
for (i in indentations) {
if (i > last) {
@@ -205,7 +205,7 @@ class PowerAssertCallTransformer(
for (tmp in sorted.asReversed()) {
addArgument(irString(buildString {
var last = -1
newline()
appendln()
for (i in indentations) {
if (i == tmp.indentation) break
if (i > last) {
@@ -233,12 +233,4 @@ fun IrFile.info(expression: IrElement): SourceRangeInfo {
return fileEntry.getSourceRangeInfo(expression.startOffset, expression.endOffset)
}
fun StringBuilder.indent(indentation: Int) = apply {
repeat(indentation) {
append(" ")
}
}
fun StringBuilder.newline() = apply {
append("\n")
}
fun StringBuilder.indent(indentation: Int): StringBuilder = append(" ".repeat(indentation))
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import com.google.auto.service.AutoService
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.config.CompilerConfiguration
@AutoService(CommandLineProcessor::class)
class PowerAssertCommandLineProcessor : CommandLineProcessor {
override val pluginId: String = "com.bnorm.kotlin-power-assert"
override val pluginOptions: Collection<CliOption> = listOf(
CliOption(
optionName = "enabled",
valueDescription = "<true|false>",
description = "whether to enable the kotlin-power-assert plugin or not"
)
)
override fun processOption(
option: AbstractCliOption,
value: String,
configuration: CompilerConfiguration
) {
return when (option.optionName) {
"enabled" -> configuration.put(KEY_ENABLED, value.toBoolean())
else -> error("Unexpected config option ${option.optionName}")
}
}
}
@@ -21,6 +21,9 @@ import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
val KEY_ENABLED = CompilerConfigurationKey<Boolean>("whether the plugin is enabled")
@AutoService(ComponentRegistrar::class)
class PowerAssertComponentRegistrar : ComponentRegistrar {
@@ -28,6 +31,9 @@ class PowerAssertComponentRegistrar : ComponentRegistrar {
project: MockProject,
configuration: CompilerConfiguration
) {
if (configuration[KEY_ENABLED] == false) {
return
}
IrGenerationExtension.registerExtension(project, PowerAssertIrGenerationExtension())
}
}
+1
View File
@@ -1,3 +1,4 @@
rootProject.name = "kotlin-power-assert"
include(":kotlin-power-assert")
include(":kotlin-power-assert-gradle")