Skip to content

Commit

Permalink
Use new plugin format for existing plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Oct 25, 2023
1 parent 718e9dd commit 0aac1a2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 105 deletions.
13 changes: 0 additions & 13 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@ plugins {
alias(libs.plugins.kotlinx.serialization)
}

gradlePlugin {
plugins {
register("intellij-theme-generator") {
id = "intellij-theme-generator"
implementationClass = "org.jetbrains.jewel.buildlogic.theme.IntelliJThemeGeneratorPlugin"
}
register("android-studio-releases-generator") {
id = "android-studio-releases-generator"
implementationClass = "org.jetbrains.jewel.buildlogic.demodata.AndroidStudioReleasesGeneratorPlugin"
}
}
}

kotlin {
sourceSets {
all {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import com.squareup.kotlinpoet.ClassName
import io.gitlab.arturbosch.detekt.Detekt
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.jetbrains.jewel.buildlogic.demodata.AndroidStudioReleasesGeneratorTask
import org.jetbrains.jewel.buildlogic.demodata.STUDIO_RELEASES_OUTPUT_CLASS_NAME
import org.jetbrains.jewel.buildlogic.demodata.StudioVersionsGenerationExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.tasks.BaseKotlinCompile

val extension = extensions.findByType(StudioVersionsGenerationExtension::class.java)
?: extensions.create("androidStudioReleasesGenerator", StudioVersionsGenerationExtension::class.java)

val task =
tasks.register<AndroidStudioReleasesGeneratorTask>("generateAndroidStudioReleasesList") {
val className = ClassName.bestGuess(STUDIO_RELEASES_OUTPUT_CLASS_NAME)
outputFile.set(
extension.targetDir.file(
className.packageName.replace(".", "/")
.plus("/${className.simpleName}.kt")
)
)
dataUrl.set(extension.dataUrl)
resourcesDirs.set(extension.resourcesDirs)
}
tasks.withType<BaseKotlinCompile> {
dependsOn(task)
}
tasks.withType<Detekt> {
dependsOn(task)
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
extensions.getByType<KotlinJvmProjectExtension>().apply {
sourceSets["main"].kotlin.srcDir(extension.targetDir)
}
}
40 changes: 40 additions & 0 deletions buildSrc/src/main/kotlin/intellij-theme-generator.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import com.squareup.kotlinpoet.ClassName
import io.gitlab.arturbosch.detekt.Detekt
import org.gradle.kotlin.dsl.container
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.gradle.util.internal.GUtil
import org.jetbrains.jewel.buildlogic.theme.IntelliJThemeGeneratorTask
import org.jetbrains.jewel.buildlogic.theme.ThemeGeneration
import org.jetbrains.jewel.buildlogic.theme.ThemeGeneratorContainer
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.tasks.BaseKotlinCompile

val extension = ThemeGeneratorContainer(container<ThemeGeneration> { ThemeGeneration(it, project) })
extensions.add("intelliJThemeGenerator", extension)

extension.all {
val task = tasks.register<IntelliJThemeGeneratorTask>("generate${GUtil.toCamelCase(name)}Theme") {
outputFile.set(targetDir.file(this@all.themeClassName.map {
val className = ClassName.bestGuess(it)
className.packageName.replace(".", "/")
.plus("/${className.simpleName}.kt")
}))
themeClassName.set(this@all.themeClassName)
ideaVersion.set(this@all.ideaVersion)
themeFile.set(this@all.themeFile)
}
tasks.withType<BaseKotlinCompile> {
dependsOn(task)
}
tasks.withType<Detekt> {
dependsOn(task)
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
extensions.getByType<KotlinJvmProjectExtension>().apply {
sourceSets["main"].kotlin.srcDir(targetDir)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package org.jetbrains.jewel.buildlogic.demodata
import com.squareup.kotlinpoet.ClassName
import gradle.kotlin.dsl.accessors._c011fd04eb69b06af6f445fec200c5f6.main
import gradle.kotlin.dsl.accessors._c011fd04eb69b06af6f445fec200c5f6.sourceSets
import io.gitlab.arturbosch.detekt.Detekt
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
Expand All @@ -16,51 +14,11 @@ import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.setProperty
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.tasks.BaseKotlinCompile
import java.io.File
import java.net.URL

abstract class AndroidStudioReleasesGeneratorPlugin : Plugin<Project> {

final override fun apply(target: Project) {
with(target) {
val extension = extensions.findByType(StudioVersionsGenerationExtension::class.java)
?: extensions.create("androidStudioReleasesGenerator", StudioVersionsGenerationExtension::class.java)

val task =
tasks.register<AndroidStudioReleasesGeneratorTask>("generateAndroidStudioReleasesList") {
val className = ClassName.bestGuess(OUTPUT_CLASS_NAME)
outputFile.set(
extension.targetDir.file(
className.packageName.replace(".", "/")
.plus("/${className.simpleName}.kt")
)
)
dataUrl.set(extension.dataUrl)
resourcesDirs.set(extension.resourcesDirs)
}
tasks.withType<BaseKotlinCompile> {
dependsOn(task)
}
tasks.withType<Detekt> {
dependsOn(task)
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
extensions.getByType<KotlinJvmProjectExtension>().apply {
sourceSets["main"].kotlin.srcDir(extension.targetDir)
}
}
}
}
}

open class StudioVersionsGenerationExtension(project: Project) {

val targetDir: DirectoryProperty = project.objects.directoryProperty()
Expand All @@ -73,7 +31,7 @@ open class StudioVersionsGenerationExtension(project: Project) {
.convention("https://jb.gg/android-studio-releases-list.json")
}

private const val OUTPUT_CLASS_NAME = "org.jetbrains.jewel.samples.ideplugin.releasessample.AndroidStudioReleases"
internal const val STUDIO_RELEASES_OUTPUT_CLASS_NAME = "org.jetbrains.jewel.samples.ideplugin.releasessample.AndroidStudioReleases"

open class AndroidStudioReleasesGeneratorTask : DefaultTask() {

Expand Down Expand Up @@ -107,7 +65,7 @@ open class AndroidStudioReleasesGeneratorTask : DefaultTask() {
val releases = URL(url).openStream()
.use { json.decodeFromStream<ApiAndroidStudioReleases>(it) }

val className = ClassName.bestGuess(OUTPUT_CLASS_NAME)
val className = ClassName.bestGuess(STUDIO_RELEASES_OUTPUT_CLASS_NAME)
val file = AndroidStudioReleasesReader.readFrom(releases, className, url, lookupDirs)

val outputFile = outputFile.get().asFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,21 @@
package org.jetbrains.jewel.buildlogic.theme

import com.squareup.kotlinpoet.ClassName
import io.gitlab.arturbosch.detekt.Detekt
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromStream
import org.gradle.api.DefaultTask
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.container
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.gradle.util.internal.GUtil
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.tasks.BaseKotlinCompile
import java.net.URL

abstract class IntelliJThemeGeneratorPlugin : Plugin<Project> {

final override fun apply(target: Project) {
with(target) {
val extension = ThemeGeneratorContainer(container<ThemeGeneration> { ThemeGeneration(it, project) })
extensions.add("intelliJThemeGenerator", extension)

extension.all {
val task = tasks.register<IntelliJThemeGeneratorTask>("generate${GUtil.toCamelCase(name)}Theme") {
outputFile.set(targetDir.file(this@all.themeClassName.map {
val className = ClassName.bestGuess(it)
className.packageName.replace(".", "/")
.plus("/${className.simpleName}.kt")
}))
themeClassName.set(this@all.themeClassName)
ideaVersion.set(this@all.ideaVersion)
themeFile.set(this@all.themeFile)
}
tasks.withType<BaseKotlinCompile> {
dependsOn(task)
}
tasks.withType<Detekt> {
dependsOn(task)
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
extensions.getByType<KotlinJvmProjectExtension>().apply {
sourceSets["main"].kotlin.srcDir(targetDir)
}
}
}
}
}
}

class ThemeGeneratorContainer(container: NamedDomainObjectContainer<ThemeGeneration>) : NamedDomainObjectContainer<ThemeGeneration> by container

class ThemeGeneration(val name: String, project: Project) {
Expand Down
6 changes: 2 additions & 4 deletions samples/ide-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import SupportedIJVersion.*

plugins {
jewel
alias(libs.plugins.composeDesktop)
Expand All @@ -11,8 +9,8 @@ intellij {
pluginName.set("Jewel Demo")
plugins.set(listOf("org.jetbrains.kotlin"))
val versionRaw = when (supportedIJVersion()) {
IJ_232 -> libs.versions.idea232.get()
IJ_233 -> libs.versions.idea233.get()
SupportedIJVersion.IJ_232 -> libs.versions.idea232.get()
SupportedIJVersion.IJ_233 -> libs.versions.idea233.get()
}
version.set(versionRaw)
}
Expand Down

0 comments on commit 0aac1a2

Please sign in to comment.