Skip to content

Commit

Permalink
Support settings plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Enaium committed Oct 19, 2024
1 parent 45104cf commit fcb9679
Showing 16 changed files with 293 additions and 62 deletions.
105 changes: 89 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,26 +3,93 @@
![GitHub top language](https://img.shields.io/github/languages/top/enaium/jimmer-gradle?style=flat-square&logo=kotlin)
![Gradle Plugin Portal Version](https://img.shields.io/gradle-plugin-portal/v/cn.enaium.jimmer.gradle?style=flat-square&logo=gradle)

Feature:
## Feature

- Generate code for database table, column and association.
- Incremental compile for dto language (apt/ksp).
- implementation (spring-boot-start/sql/sql-kotlin) for dependencies.
- annotationProcessor/ksp for dependencies.
- Add implementation (spring-boot-start/sql/sql-kotlin) for dependencies.
- Add annotationProcessor/ksp for dependencies.
- Easy to add arguments for annotationProcessor/ksp.
- Can use jimmer's catalog in the project.
- Let jimmer generate code when opening the project for the first time.

## version
## Usage

Warning: The version of the extension is not support for `ksp` when the
issue [#1789](https://github.com/google/ksp/issues/1789) doesn't fix.
Warning: You cannot use the method 1 and method 2 at the same time.

### Gradle Project Plugin(Method 1)

In the `build.gradle.kts` file, you can use the following code to apply the plugin.

```kotlin
plugins {
id("cn.enaium.jimmer.gradle") version "latest.release"
}
```

If you also used kotlin, you need to declare the ksp plugin before the jimmer plugin.

```kotlin
plugins {
kotlin("jvm") version "2.0.21"
id("com.google.devtools.ksp") version "2.0.21+"
id("cn.enaium.jimmer.gradle") version "latest.release"
}
```

### Gradle Settings Plugin(Method 2)

In the `settings.gradle.kts` file, you can use the following code to apply the plugin.

```kotlin
plugins {
id("cn.enaium.jimmer.gradle.setting") version "latest.release"
}
```

If you want to modify the extension of the gradle project plugin, then you can use the gradle project plugin.

```kotlin
plugins {
alias(jimmer_catalog.plugins.jimmer)
}
```

If you also used kotlin, you need to declare the ksp plugin before the jimmer plugin.

```kotlin
plugins {
kotlin("jvm") version "2.0.21"
alias(jimmer_catalog.plugins.ksp) version "2.0.21+"
alias(jimmer_catalog.plugins.jimmer)
}
```

then you need to add the ksp dependency in the `build.gradle.kts` file.

```kotlin
dependencies {
ksp(jimmer_catalog.ksp)
}
```

## Jimmer's Version(Both Gradle Project Plugin and Gradle Settings Plugin)

Warning: The version of the extension is not supported for `ksp` when
issue [#1789](https://github.com/google/ksp/issues/1789) isn't fixed, but you can use the latest version or use the
gradle setting
plugin.

In the `build.gradle.kts` file if you use the gradle project plugin, or in the `settings.gradle.kts` file if you use the
gradle settings plugin, you can use the following code to set the version of jimmer.

```kotlin
jimmer {
version.set("0.8.131")//default latest
version.set("latest.release")//default latest
}
```

## generate entity
## Generate Entity(Only Gradle Project Plugin)

![Static Badge](https://img.shields.io/badge/-Kotlin-gray?style=flat-square&logo=kotlin&logoColor=white)
![Static Badge](https://img.shields.io/badge/-Java-gray?style=flat-square&logo=openjdk&logoColor=white)
@@ -72,7 +139,7 @@ jimmer {
}
```

### association
### Association(Only Gradle Project Plugin)

You must add the suffix '_id'(`primaryKey.set("id")`) to the column name if the column is a fake foreign key, otherwise
the column cannot be recognized as a foreign key.
@@ -94,7 +161,7 @@ jimmer {
}
```

### typeMappings
### typeMappings(Only Gradle Project Plugin)

[default](src/main/kotlin/cn/enaium/jimmer/gradle/utility/mapping.kt)

@@ -114,9 +181,9 @@ jimmer {
}
```

## dto incremental compile
## Dto Incremental Compile

Nothing
Open the dto file and press `Ctrl + F9` to compile the dto file.

## implementation for dependencies

@@ -142,7 +209,7 @@ Nothing

```kotlin
plugins {
id("com.google.devtools.ksp")//require
id("com.google.devtools.ksp")//require and must declare before jimmer gradle plugin
}
```

@@ -160,11 +227,11 @@ jimmer {
}
```

## extension
## Gradle Project Plugin Extension

| extension | type | default | description |
|-------------------------------------|------------------------------------------------------|--------------------------|---------------------------------------------|
| `version` | `String` | `+` | Jimmer version. |
| `version` | `String` | `latest.release` | Jimmer version. |
| `keepIsPrefix` | `Boolean` | `false` | Keep 'is' prefix in getter method. |
| `generator` | `cn.enaium.jimmer.gradle.extension.Generator` | | Entity generator. |
| `generator.target` | `cn.enaium.jimmer.gradle.extension.Target` | | Entity generator target. |
@@ -197,4 +264,10 @@ jimmer {
| `entry.fetchers` | `String` | | Generate `fetchers` class name, java only. |
| `immutable.isModuleRequired` | `Boolean` | | Kotlin only. |
| `source.includes` | `List<String>` | | Java only. |
| `source.excludes` | `List<String>` | | Java only. |
| `source.excludes` | `List<String>` | | Java only. |

## Gradle Settings Plugin Extension

| extension | type | default | description |
|-----------|----------|------------------|-----------------|
| `version` | `String` | `latest.release` | Jimmer version. |
11 changes: 8 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -6,15 +6,13 @@ plugins {
}

group = "cn.enaium"
version = "0.0.12"
version = "0.0.13"

repositories {
mavenCentral()
}

dependencies {
compileOnly(libs.ksp)

implementation(libs.kotlinpoet)
implementation(libs.javapoet)
implementation(libs.jetbrainsAnnotations)
@@ -54,6 +52,13 @@ gradlePlugin {
description = "A gradle plugin for jimmer"
tags.set(listOf("orm", "jimmer", "generator"))
}
create("setting") {
id = "cn.enaium.jimmer.gradle.setting"
implementationClass = "cn.enaium.jimmer.gradle.SettingPlugin"
displayName = "setting"
description = "A gradle plugin for jimmer"
tags.set(listOf("orm", "jimmer", "generator"))
}
}
}

4 changes: 1 addition & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[versions]
kotlin = "1.9.23"
kotlin = "2.0.21"
publishing = "1.2.1"
ksp = "1.9.23-1.0.20"
kotlinpoet = "1.16.0"
javapoet = "1.13.0"
testcontainers = "1.19.7"
@@ -12,7 +11,6 @@ jimmer = "0.8.124"
jetbrainsAnnotations = "24.1.0"

[libraries]
ksp = { module = "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" }
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
jetbrainsAnnotations = { module = "org.jetbrains:annotations", version.ref = "jetbrainsAnnotations" }
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Feb 21 13:43:28 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
47 changes: 35 additions & 12 deletions src/main/kotlin/cn/enaium/jimmer/gradle/JimmerPlugin.kt
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import cn.enaium.jimmer.gradle.task.GenerateEntityTask
import cn.enaium.jimmer.gradle.utility.*
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.tasks.JvmConstants

/**
* @author Enaium
@@ -106,7 +107,7 @@ class JimmerPlugin : Plugin<Project> {
}
} else if (afterProject.plugins.hasKsp && extension.language.get() == Language.KOTLIN) {
fun add(key: String, value: String) {
afterProject.extensions.ksp.arg(key, value)
kspArg(afterProject.extensions.getByName("ksp"), key, value)
}

extension.dto.dirs.takeIf { it.isPresent && it.get().isNotEmpty() }?.let {
@@ -135,6 +136,7 @@ class JimmerPlugin : Plugin<Project> {
}

if (afterProject.tasks.hasCompileJava && extension.language.get() == Language.JAVA) {
afterProject.tasks.getByName(PRE_TASK_NAME).dependsOn("compileJava")
afterProject.tasks.compileJava { compile ->
compile.options.compilerArgs.firstOrNull {
it.startsWith("-A$DTO_DIRS=")
@@ -165,8 +167,10 @@ class JimmerPlugin : Plugin<Project> {
}

if (afterProject.tasks.hasKsp && extension.language.get() == Language.KOTLIN) {
afterProject.tasks.getByName(PRE_TASK_NAME).dependsOn(KSP_TASK_NAME)

afterProject.tasks.kspKotlin { kspKotlin ->
afterProject.extensions.ksp.arguments[DTO_DIRS]?.let { dirs ->
kspArguments(afterProject.extensions.getByName("ksp"))[DTO_DIRS]?.let { dirs ->
dirs.split("\\s*[,:;]\\s*".toRegex()).mapNotNull {
when {
it == "" || it == "/" -> null
@@ -187,31 +191,50 @@ class JimmerPlugin : Plugin<Project> {
}

// Add spring-boot-starter,sql,sql-kotlin
if (afterProject.plugins.hasSpringBoot) {
if (afterProject.plugins.hasSpringBoot && !afterProject.hasDependency(JIMMER_SPRINGBOOT_NAME)) {
afterProject.dependencies.implementation(
"org.babyfish.jimmer:jimmer-spring-boot-starter:${extension.version.get()}"
"$JIMMER_GROUP:$JIMMER_SPRINGBOOT_NAME:${extension.version.get()}"
)
} else if (extension.language.get() == Language.JAVA) {
} else if (extension.language.get() == Language.JAVA && !afterProject.hasDependency(JIMMER_SQL_NAME)) {
afterProject.dependencies.implementation(
"org.babyfish.jimmer:jimmer-sql:${extension.version.get()}"
"$JIMMER_GROUP:$JIMMER_SQL_NAME:${extension.version.get()}"
)
} else if (extension.language.get() == Language.KOTLIN) {
} else if (extension.language.get() == Language.KOTLIN
&& !afterProject.hasDependency(JIMMER_SQL_KOTLIN_NAME)
) {
afterProject.dependencies.implementation(
"org.babyfish.jimmer:jimmer-sql-kotlin:${extension.version.get()}"
"$JIMMER_GROUP:$JIMMER_SQL_KOTLIN_NAME:${extension.version.get()}"
)
}

if (extension.language.get() == Language.JAVA) {
if (extension.language.get() == Language.JAVA
&& !afterProject.hasDependency(JIMMER_APT_NAME, JvmConstants.ANNOTATION_PROCESSOR_CONFIGURATION_NAME)
) {
afterProject.dependencies.annotationProcessor(
"org.babyfish.jimmer:jimmer-apt:${extension.version.get()}"
"$JIMMER_GROUP:$JIMMER_APT_NAME:${extension.version.get()}"
)
}
}

if (project.plugins.hasKsp && extension.language.get() == Language.KOTLIN) {
if (project.plugins.hasKsp && extension.language.get() == Language.KOTLIN
&& hasClass("com.google.devtools.ksp.gradle.KspExtension")
) {
project.dependencies.ksp(
"org.babyfish.jimmer:jimmer-ksp:${extension.version.get()}"
"$JIMMER_GROUP:$JIMMER_KSP_NAME:${extension.version.get()}"
)
}
}

private fun kspArg(any: Any, key: String, value: String) {
any.javaClass.methods.firstOrNull {
it.name == "arg" && it.parameterTypes.size == 2
}?.invoke(any, key, value)
}

private fun kspArguments(any: Any): MutableMap<String, String> {
@Suppress("UNCHECKED_CAST")
return any.javaClass.methods.firstOrNull {
it.name == "getArguments"
}?.invoke(any) as MutableMap<String, String>
}
}
53 changes: 53 additions & 0 deletions src/main/kotlin/cn/enaium/jimmer/gradle/SettingPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 Enaium
*
* 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 cn.enaium.jimmer.gradle

import cn.enaium.jimmer.gradle.extension.SettingExtension
import cn.enaium.jimmer.gradle.utility.lib
import org.gradle.api.Plugin
import org.gradle.api.initialization.Settings

/**
* @author Enaium
*/
class SettingPlugin : Plugin<Settings> {
override fun apply(target: Settings) {
val extension = target.extensions.create("jimmer", SettingExtension::class.java)

target.dependencyResolutionManagement.versionCatalogs.apply {
register("jimmer_catalog") {
it.plugin("jimmer", "cn.enaium.jimmer.gradle").version {
// Nothing
}
it.plugin("ksp", "com.google.devtools.ksp").version {
// Nothing
}
it.lib("apt", "apt").version(extension.version.get())
it.lib("bom", "bom").version(extension.version.get())
it.lib("client", "client").version(extension.version.get())
it.lib("coreKotlin", "core-kotlin").version(extension.version.get())
it.lib("core", "core").version(extension.version.get())
it.lib("dtoCompiler", "dto-compiler").version(extension.version.get())
it.lib("ksp", "ksp").version(extension.version.get())
it.lib("mapstructApt", "mapstruct-apt").version(extension.version.get())
it.lib("springBootStarter", "spring-boot-starter").version(extension.version.get())
it.lib("sqlKotlin", "sql-kotlin").version(extension.version.get())
it.lib("sql", "sql").version(extension.version.get())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ open class JimmerExtension @Inject constructor(objects: ObjectFactory) {
val source: Source = objects.newInstance(Source::class.java)
val immutable: Immutable = objects.newInstance(Immutable::class.java)
val language: Property<Language> = objects.property(Language::class.java)
val version: Property<String> = objects.property(String::class.java).convention("+")
val version: Property<String> = objects.property(String::class.java).convention("latest.release")
val keepIsPrefix: Property<Boolean> = objects.property(Boolean::class.java)

fun generator(action: Generator.() -> Unit) {
Loading

0 comments on commit fcb9679

Please sign in to comment.