Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin compiler plugin #470

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
extra["kotlin_plugin_id"] = "io.github.oshai.kotlinlogging.kotlin-ir-plugin"
extra["kotlin_plugin_package_name"] = "io.github.oshai.kotlinlogging.irplugin"
Comment on lines +10 to +11
Copy link

Choose a reason for hiding this comment

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

I would drop the IR naming of the plugin and just call it "kotlin plugin" or something like that. I really should update that template and drop the IR part of the name. There's many things you can do now with a compiler plugin that have nothing to do with IR.

}

plugins {
kotlin("multiplatform") version "2.0.21"
// This version is dependent on the maximum tested version
Expand All @@ -14,9 +19,11 @@ plugins {

id("org.jetbrains.dokka") version "2.0.0"

id("com.diffplug.spotless") version "7.0.1"
id("com.diffplug.spotless")

id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
id("com.gradle.plugin-publish") version "1.2.1" apply false
id("com.github.gmazzo.buildconfig") version "5.3.5" apply false
`maven-publish`
signing
}
Expand Down
4 changes: 4 additions & 0 deletions kotlin-ir-plugin-gradle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Gradle wrapper plugin for Kotlin (IR) compiler plugin for kotlin-logging

Just a Gradle wrapper for the Kotlin (IR) Compiler plugin for kotlin-logging,
see [kotlin-ir-plugin](../kotlin-ir-plugin) for the actual Kotlin (IR) compiler plugin.
52 changes: 52 additions & 0 deletions kotlin-ir-plugin-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id("java-gradle-plugin")
id("com.gradle.plugin-publish") version "1.2.1"
kotlin("jvm")
id("com.github.gmazzo.buildconfig")
id("com.diffplug.spotless")

}
repositories {
mavenCentral()
}

dependencies {
implementation(kotlin("gradle-plugin-api"))
}

buildConfig {
val project = project(":kotlin-ir-plugin")
packageName("${rootProject.extra["kotlin_plugin_package_name"]}")
buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${rootProject.extra["kotlin_plugin_id"]}\"")
buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${project.group}\"")
buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${project.name}\"")
buildConfigField("String", "KOTLIN_PLUGIN_VERSION", "\"${project.version}\"")
}

gradlePlugin {
plugins {
create("kotlinLoggingIrPlugin") {
id = rootProject.extra["kotlin_plugin_id"] as String
displayName = "kotlin-logging IR plugin"
description = "Collects metadata about logging calls from the source code and adds it to the respective calls"
implementationClass = "io.github.oshai.kotlinlogging.irplugin.KotlinLoggingGradlePlugin"
}
}
}

publishing {
repositories {
maven {
name = "localPluginRepository"
url = uri(rootProject.layout.buildDirectory.dir("local-plugin-repository"))
}
}
}

// Static code analysis tools
spotless {
kotlin {
target("src/**/*.kt")
ktfmt("0.47").googleStyle()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 io.github.oshai.kotlinlogging.irplugin

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property

// Based on https://github.com/bnorm/kotlin-ir-plugin-template
Copy link

Choose a reason for hiding this comment

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

If you want to look at something a bit more recent, you can check out piecemeal, a compiler-plugin I've been slowly working on myself.

open class KotlinLoggingGradleExtension(objects: ObjectFactory) {
val disableAll: Property<String> = objects.property(String::class.java)
val disableTransformingDeprecatedApi: Property<String> = objects.property(String::class.java)
val disableTransformingNotImplementedApi: Property<String> = objects.property(String::class.java)
val disableTransformingEntryExitApi: Property<String> = objects.property(String::class.java)
val disableTransformingThrowingCatchingApi: Property<String> =
objects.property(String::class.java)
val disableCollectingCallSiteInformation: Property<String> = objects.property(String::class.java)
Comment on lines +24 to +30
Copy link

Choose a reason for hiding this comment

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

I would make these boolean properties and then convert to and from a String when creating the SubpluginOptions. That will make it more clear how to configure the Gradle options.

Also, if you want to provide a default, you can use .convention(false) after the property.

objectFactory.property(Boolean::class.java).convention(false)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 io.github.oshai.kotlinlogging.irplugin

import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption

// Based on https://github.com/bnorm/kotlin-ir-plugin-template
class KotlinLoggingGradlePlugin : KotlinCompilerPluginSupportPlugin {
override fun apply(target: Project): Unit =
with(target) { extensions.create("kotlinlogging", KotlinLoggingGradleExtension::class.java) }

override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true

override fun getCompilerPluginId(): String = BuildConfig.KOTLIN_PLUGIN_ID

override fun getPluginArtifact(): SubpluginArtifact =
SubpluginArtifact(
groupId = BuildConfig.KOTLIN_PLUGIN_GROUP,
artifactId = BuildConfig.KOTLIN_PLUGIN_NAME,
version = BuildConfig.KOTLIN_PLUGIN_VERSION,
)

override fun applyToCompilation(
kotlinCompilation: KotlinCompilation<*>
): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
val extension = project.extensions.getByType(KotlinLoggingGradleExtension::class.java)
return project.provider {
listOf(
SubpluginOption(key = "disableAll", value = extension.disableAll.get()),
SubpluginOption(
key = "disableTransformingDeprecatedApi",
value = extension.disableTransformingDeprecatedApi.get(),
),
SubpluginOption(
key = "disableTransformingNotImplementedApi",
value = extension.disableTransformingNotImplementedApi.get(),
),
SubpluginOption(
key = "disableTransformingEntryExitApi",
value = extension.disableTransformingEntryExitApi.get(),
),
SubpluginOption(
key = "disableTransformingThrowingCatchingApi",
value = extension.disableTransformingThrowingCatchingApi.get(),
),
SubpluginOption(
key = "disableCollectingCallSiteInformation",
value = extension.disableCollectingCallSiteInformation.get(),
),
)
}
}
}
Loading
Loading