diff --git a/.github/scripts/integration_tests.sh b/.github/scripts/integration_tests.sh new file mode 100644 index 0000000..4548930 --- /dev/null +++ b/.github/scripts/integration_tests.sh @@ -0,0 +1,18 @@ +set -e +SCRIPT_DIR=$(cd $(dirname $0) && pwd -P) +PROJECT_DIR=$SCRIPT_DIR/../../ + +cd $PROJECT_DIR +pwd + +# MAVEN +cd $PROJECT_DIR/kudos-sample/kudos-maven-sample +mvn test -Dkudos.version=0.0.0-SNAPSHOT +cd - + +# GRADLE +cd $PROJECT_DIR/kudos-sample/kudos-gradle-sample +./gradlew test -PKUDOS_VERSION=0.0.0-SNAPSHOT +cd - + +cd - diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2778db5..e61c0a8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -39,5 +39,8 @@ jobs: - name: Publish To Local run: ./gradlew publishToMavenLocal -PVERSION_NAME=0.0.0-SNAPSHOT + - name: Run integration tests + run: bash .github/scripts/integration_tests.sh + - name: Check spotless run: ./gradlew spotlessCheck --no-configuration-cache diff --git a/README.md b/README.md index c37893d..55ab2be 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,9 @@ We kept thinking, is there a way to provide null safety and default values suppo ## Quick Start -### 1. Add the plugin to classpath +### 1. Introduce Kudos into Gradle Projects + +#### 1.1 Add the plugin to classpath ```kotlin // Option 1 @@ -147,7 +149,7 @@ dependencyResolutionManagement { } ```` -### 2. Apply the plugin +#### 1.2 Apply the plugin ```kotlin plugins { @@ -179,6 +181,97 @@ com.kanyun.kudos:kudos-jackson You can add these libraries to your dependencies explicitly if needed. +### 2. Introduce Kudos into Maven Projects + +#### 2.1 Project Dependencies + +Add `kudos-gson` or `kudos-jackson` into the dependencies of your project. + +```xml + + + ... + + ... + + + com.kanyun.kudos + kudos-gson + ${kudos.version} + + + com.google.code.gson + gson + 2.10 + + + + + com.kanyun.kudos + kudos-jackson + ${kudos.version} + + + com.fasterxml.jackson.core + jackson-databind + 2.15.0 + + + ... + +``` + +#### 2.2 Configure the Compiler + +```xml + + ... + + + src/main/java + src/test/java + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + + kudos + + + + + + + + + + + + + + com.kanyun.kudos + kudos-maven-plugin + ${kudos.version} + + + + ... + + + + + +``` + ### 3. Annotate classes with `@Kudos` For types that need to add `Kudos` parsing support, simply add the `@Kudos` annotation, for example: diff --git a/README_zh.md b/README_zh.md index e4558ec..6888b59 100644 --- a/README_zh.md +++ b/README_zh.md @@ -101,7 +101,9 @@ User(id=12, name=Benny Huo, age=0, tel=null) ## 快速上手 -### 1. 添加插件到 classpath +### 1. 将 Kudos 集成到 Gradle 项目中 + +#### 1.1 添加插件到 classpath ```kotlin // 方式 1 @@ -147,7 +149,7 @@ dependencyResolutionManagement { } ```` -### 2. 在项目中启用插件 +#### 1.2 在项目中启用插件 ```kotlin plugins { @@ -179,6 +181,97 @@ com.kanyun.kudos:kudos-jackson 当然,开发者也可以在合适的场景下手动引入这些依赖。 +### 2. 将 Kudos 集成到 Maven 项目中 + +#### 2.1 添加项目依赖 + +将 `kudos-gson` 或者 `kudos-jackson` 添加到你的项目依赖中。 + +```xml + + + ... + + ... + + + com.kanyun.kudos + kudos-gson + ${kudos.version} + + + com.google.code.gson + gson + 2.10 + + + + + com.kanyun.kudos + kudos-jackson + ${kudos.version} + + + com.fasterxml.jackson.core + jackson-databind + 2.15.0 + + + ... + +``` + +#### 2.2 配置编译器插件 + +```xml + + ... + + + src/main/java + src/test/java + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + + kudos + + + + + + + + + + + + + + com.kanyun.kudos + kudos-maven-plugin + ${kudos.version} + + + + ... + + + + + +``` + ### 3. 为特定类启动 Kudos 的支持 对于需要添加 `Kudos` 解析支持的类型,直接添加 `@Kudos` 注解即可,例如: diff --git a/gradle.properties b/gradle.properties index c531966..899e6c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ kotlin.code.style=official -VERSION_NAME=1.8.20-1.0.0 +VERSION_NAME=1.8.20-1.0.1 GROUP=com.kanyun.kudos diff --git a/kudos-compiler/build.gradle.kts b/kudos-compiler/build.gradle.kts index 12e21b4..7dcb700 100644 --- a/kudos-compiler/build.gradle.kts +++ b/kudos-compiler/build.gradle.kts @@ -19,7 +19,6 @@ plugins { kotlin("jvm") java id("com.bennyhuo.kotlin.trimindent") - kotlin("kapt") id("com.github.gmazzo.buildconfig") id("com.bennyhuo.kotlin.plugin.embeddable.test") } @@ -33,9 +32,6 @@ dependencies { compileOnly("org.jetbrains.kotlin:kotlin-stdlib") compileOnly("org.jetbrains.kotlin:kotlin-compiler") - kapt("com.google.auto.service:auto-service:1.0.1") - compileOnly("com.google.auto.service:auto-service-annotations:1.0.1") - implementation("org.jetbrains.kotlin:kotlin-noarg:1.8.20") testImplementation(kotlin("test-junit")) @@ -55,6 +51,6 @@ compileKotlin.kotlinOptions.freeCompilerArgs += listOf( compileKotlin.kotlinOptions.jvmTarget = "1.8" buildConfig { - packageName("$group.gson.extensions") + packageName("$group") buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${project.property("KOTLIN_PLUGIN_ID")}\"") } diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCommandLineProcessor.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCommandLineProcessor.kt index 9d8d277..36e9f6a 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCommandLineProcessor.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCommandLineProcessor.kt @@ -16,10 +16,9 @@ package com.kanyun.kudos.compiler -import com.google.auto.service.AutoService +import com.kanyun.kudos.BuildConfig import com.kanyun.kudos.compiler.options.Option import com.kanyun.kudos.compiler.options.Options -import com.kanyun.kudos.gson.extensions.BuildConfig import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption import org.jetbrains.kotlin.compiler.plugin.CliOption import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor @@ -27,7 +26,6 @@ import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi import org.jetbrains.kotlin.config.CompilerConfiguration @OptIn(ExperimentalCompilerApi::class) -@AutoService(CommandLineProcessor::class) class KudosCommandLineProcessor : CommandLineProcessor { override val pluginId: String = BuildConfig.KOTLIN_PLUGIN_ID diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCompilerPluginRegistrar.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCompilerPluginRegistrar.kt index 2fbe97a..f0090db 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCompilerPluginRegistrar.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosCompilerPluginRegistrar.kt @@ -16,7 +16,6 @@ package com.kanyun.kudos.compiler -import com.google.auto.service.AutoService import com.kanyun.kudos.compiler.k1.KudosComponentContainerContributor import com.kanyun.kudos.compiler.k1.KudosSyntheticResolveExtension import com.kanyun.kudos.compiler.k2.KudosFirExtensionRegistrar @@ -36,7 +35,6 @@ import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension lateinit var logger: Logger @OptIn(ExperimentalCompilerApi::class) -@AutoService(CompilerPluginRegistrar::class) class KudosCompilerPluginRegistrar : CompilerPluginRegistrar() { override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) { diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt index 8f5d49c..9d0793c 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt @@ -83,7 +83,7 @@ class KudosIrClassTransformer( private val defaults = HashSet() fun transform() { - if (Options.isGsonEnabled()) { + if (Options.gson()) { generateJsonAdapter() } generateNoArgConstructor() diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt index 6e5d41d..9243dc5 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt @@ -60,7 +60,7 @@ class KudosDeclarationChecker( } else { checkInitBlock(context, declaration) checkNoArg(descriptor, context, declaration) - if (Options.isGsonEnabled()) { + if (Options.gson()) { checkJsonAdapter(descriptor, context, declaration) } checkPrimaryConstructor(declaration, context) diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt index 2466c52..1a45da7 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt @@ -73,7 +73,7 @@ class KudosFirClassChecker( } else { checkInitBlock(declaration, context, reporter) checkNoArg(declaration, context, reporter) - if (Options.isGsonEnabled()) { + if (Options.gson()) { checkJsonAdapter(declaration, context, reporter) } checkPrimaryConstructor(declaration, context, reporter) diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/options/Options.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/options/Options.kt index 5239cfa..a343b54 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/options/Options.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/options/Options.kt @@ -19,18 +19,18 @@ package com.kanyun.kudos.compiler.options object Options { @JvmField - val isGsonEnabled = Option( - "isGsonEnabled", + val gson = Option( + "gson", false, - "Enable Gson plugin.", + "Whether to enable the support for Gson.", "", ) @JvmField - val isJacksonEnabled = Option( - "isJacksonEnabled", + val jackson = Option( + "jackson", false, - "Enable Jackson plugin.", + "Whether to enable the support for Jackson.", "", ) diff --git a/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor new file mode 100644 index 0000000..456ccdc --- /dev/null +++ b/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor @@ -0,0 +1 @@ +com.kanyun.kudos.compiler.KudosCommandLineProcessor diff --git a/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar b/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar new file mode 100644 index 0000000..373c29d --- /dev/null +++ b/kudos-compiler/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar @@ -0,0 +1 @@ +com.kanyun.kudos.compiler.KudosCompilerPluginRegistrar diff --git a/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt b/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt index 80fc72a..2261572 100644 --- a/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt +++ b/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt @@ -57,7 +57,7 @@ open class TestBase { } private fun gsonDeserialize(): String { - Options.isGsonEnabled.set(true) + Options.gson.set(true) return """ // FILE: Gson.kt diff --git a/kudos-gradle-plugin/build.gradle.kts b/kudos-gradle-plugin/build.gradle.kts index e17f26b..b012bf3 100644 --- a/kudos-gradle-plugin/build.gradle.kts +++ b/kudos-gradle-plugin/build.gradle.kts @@ -26,7 +26,7 @@ dependencies { buildConfig { val compilerPluginProject = project(":kudos-compiler-embeddable") - packageName("${compilerPluginProject.group}.kudos") + packageName("${compilerPluginProject.group}") buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${property("KOTLIN_PLUGIN_ID")}\"") buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${compilerPluginProject.group}\"") buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${compilerPluginProject.name}\"") diff --git a/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt b/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt index 0d6b45c..9e63e8f 100644 --- a/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt +++ b/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt @@ -16,7 +16,7 @@ package com.kanyun.kudos.gradle -import com.kanyun.kudos.kudos.BuildConfig +import com.kanyun.kudos.BuildConfig import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.provider.Provider @@ -70,10 +70,10 @@ open class KudosGradlePlugin : KotlinCompilerPluginSupportPlugin { val kudosExtension = project.extensions.getByType(KudosExtension::class.java) val options = ArrayList() if (kudosExtension.gson) { - options += SubpluginOption("isGsonEnabled", "true") + options += SubpluginOption("gson", "true") } if (kudosExtension.jackson) { - options += SubpluginOption("isJacksonEnabled", "true") + options += SubpluginOption("jackson", "true") } return project.provider { options } } diff --git a/kudos-maven-plugin/build.gradle.kts b/kudos-maven-plugin/build.gradle.kts new file mode 100644 index 0000000..93e281d --- /dev/null +++ b/kudos-maven-plugin/build.gradle.kts @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 Kanyun, Inc. + * + * 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. + */ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +/* + * Copyright (C) 2023 Kanyun, Inc. + * + * 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. + */ +plugins { + kotlin("jvm") + id("com.github.gmazzo.buildconfig") +} + +// embed the source files of kudos compiler plugin. +sourceSets { + main { + java.srcDir(rootProject.file("kudos-compiler/src/main/java")) + resources.srcDir(rootProject.file("kudos-compiler/src/main/resources")) + } +} + +dependencies { + implementation(kotlin("stdlib")) + implementation("org.jetbrains.kotlin:kotlin-noarg:1.8.20") + + compileOnly("org.jetbrains.kotlin:kotlin-compiler") + compileOnly("org.jetbrains.kotlin:kotlin-maven-plugin") + compileOnly("org.apache.maven:maven-core:3.8.6") +} + +buildConfig { + val compilerPluginProject = project(":kudos-compiler-embeddable") + packageName("${compilerPluginProject.group}") + buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${property("KOTLIN_PLUGIN_ID")}\"") + buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${compilerPluginProject.group}\"") + buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${compilerPluginProject.name}\"") + buildConfigField("String", "KOTLIN_PLUGIN_VERSION", "\"${compilerPluginProject.version}\"") +} + + +val compileKotlin: KotlinCompile by tasks +compileKotlin.kotlinOptions.freeCompilerArgs += listOf( + "-Xjvm-default=enable", + "-Xcontext-receivers", + "-opt-in=kotlin.RequiresOptIn" +) +compileKotlin.kotlinOptions.jvmTarget = "1.8" diff --git a/kudos-maven-plugin/gradle.properties b/kudos-maven-plugin/gradle.properties new file mode 100644 index 0000000..34430a9 --- /dev/null +++ b/kudos-maven-plugin/gradle.properties @@ -0,0 +1 @@ +POM_NAME=Kudos-Maven-Plugin diff --git a/kudos-maven-plugin/src/main/java/com/kanyun/kudos/maven/KudosMavenPluginExtension.kt b/kudos-maven-plugin/src/main/java/com/kanyun/kudos/maven/KudosMavenPluginExtension.kt new file mode 100644 index 0000000..b85701e --- /dev/null +++ b/kudos-maven-plugin/src/main/java/com/kanyun/kudos/maven/KudosMavenPluginExtension.kt @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023 Kanyun, Inc. + * + * 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.kanyun.kudos.maven + +import org.apache.maven.plugin.MojoExecution +import org.apache.maven.project.MavenProject +import org.codehaus.plexus.component.annotations.Component +import org.codehaus.plexus.component.annotations.Requirement +import org.codehaus.plexus.logging.Logger +import org.jetbrains.kotlin.maven.KotlinMavenPluginExtension +import org.jetbrains.kotlin.maven.PluginOption + +val KUDOS_COMPILER_PLUGIN_ID = "com.kanyun.kudos" + +@Component(role = KotlinMavenPluginExtension::class, hint = "kudos") +class KudosMavenPluginExtension : KotlinMavenPluginExtension { + @Requirement + lateinit var logger: Logger + + override fun getCompilerPluginId() = KUDOS_COMPILER_PLUGIN_ID + + override fun isApplicable(project: MavenProject, execution: MojoExecution): Boolean { + return true + } + + override fun getPluginOptions(project: MavenProject, execution: MojoExecution): List { + logger.debug("Loaded Maven plugin " + javaClass.name) + return emptyList() + } +} diff --git a/kudos-maven-plugin/src/main/resources/META-INF/plexus/components.xml b/kudos-maven-plugin/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000..861b3dc --- /dev/null +++ b/kudos-maven-plugin/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,33 @@ + + + + + + org.jetbrains.kotlin.maven.KotlinMavenPluginExtension + kudos + com.kanyun.kudos.maven.KudosMavenPluginExtension + + false + + + org.codehaus.plexus.logging.Logger + logger + + + + + diff --git a/kudos-sample/kudos-gradle-sample/build.gradle.kts b/kudos-sample/kudos-gradle-sample/build.gradle.kts new file mode 100644 index 0000000..2aff3cd --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/build.gradle.kts @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 Kanyun, Inc. + * + * 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. + */ +plugins { + java + kotlin("jvm") version "1.8.20" + id("com.kanyun.kudos") +} + +repositories { + mavenLocal() + mavenCentral() + maven("https://s01.oss.sonatype.org/content/repositories/snapshots") +} + +kudos { + gson = true + jackson = true +} + +dependencies { + implementation("com.google.code.gson:gson:2.10") + implementation("com.fasterxml.jackson.core:jackson-databind:2.15.0") + testImplementation("org.jetbrains.kotlin:kotlin-test-junit") + testImplementation("junit:junit:4.13.1") +} diff --git a/kudos-sample/kudos-gradle-sample/gradle.properties b/kudos-sample/kudos-gradle-sample/gradle.properties new file mode 100644 index 0000000..0753426 --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/gradle.properties @@ -0,0 +1,2 @@ +POM_NAME=Kudos-Annotations +KUDOS_VERSION=1.8.20-1.0.1 diff --git a/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.jar b/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..033e24c Binary files /dev/null and b/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.jar differ diff --git a/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.properties b/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..62f495d --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/kudos-sample/kudos-gradle-sample/gradlew b/kudos-sample/kudos-gradle-sample/gradlew new file mode 100755 index 0000000..fcb6fca --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# 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 +# +# https://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. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/kudos-sample/kudos-gradle-sample/gradlew.bat b/kudos-sample/kudos-gradle-sample/gradlew.bat new file mode 100644 index 0000000..6689b85 --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/kudos-sample/kudos-gradle-sample/settings.gradle.kts b/kudos-sample/kudos-gradle-sample/settings.gradle.kts new file mode 100644 index 0000000..e01d4f5 --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/settings.gradle.kts @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + + val KUDOS_VERSION: String by settings + plugins { + id("com.kanyun.kudos") version(KUDOS_VERSION) + } +} diff --git a/kudos-sample/kudos-gradle-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt b/kudos-sample/kudos-gradle-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt new file mode 100644 index 0000000..f304341 --- /dev/null +++ b/kudos-sample/kudos-gradle-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt @@ -0,0 +1,55 @@ +package com.kanyun.kudos.sample.test + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.DeserializationFeature +import com.google.gson.reflect.TypeToken +import com.kanyun.kudos.annotations.Kudos +import com.kanyun.kudos.gson.kudosGson +import com.kanyun.kudos.jackson.kudosObjectMapper +import kotlin.test.assertEquals +import org.junit.Test + +class KudosTest { + + inline fun deserializeByJackson(string: String, expect: String) { + val mapper = kudosObjectMapper() + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + mapper.disable(DeserializationFeature.WRAP_EXCEPTIONS); + try { + val t: T = mapper.readValue(string, object: TypeReference() {}) + assertEquals(t.toString(), expect) + } catch (e: Exception) { + assertEquals(e.toString(), expect) + } + } + + inline fun deserializeByGson(string: String, expect: String) { + val gson = kudosGson() + try { + val t: T = gson.fromJson(string, object: TypeToken() {}.type) + assertEquals(t.toString(), expect) + } catch (e: Exception) { + assertEquals(e.toString(), expect) + } + } + + @Kudos + data class User(val id: Long, val name: String, val age: Int = 10) + + fun gsonSample() { + deserializeByGson("""{}""", "java.lang.NullPointerException: Missing non-null field 'id'.") + deserializeByGson("""{"id": 10}""", "java.lang.NullPointerException: Missing non-null field 'name'.") + } + + fun jacksonSample() { + deserializeByJackson("""{}""", "java.lang.NullPointerException: Missing non-null field 'id'.") + deserializeByJackson("""{"id": 10}""", "java.lang.NullPointerException: Missing non-null field 'name'.") + } + + @Test + fun test() { + println("test Kudos.") + gsonSample() + jacksonSample() + } +} diff --git a/kudos-sample/kudos-maven-sample/.gitignore b/kudos-sample/kudos-maven-sample/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/kudos-sample/kudos-maven-sample/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/kudos-sample/kudos-maven-sample/pom.xml b/kudos-sample/kudos-maven-sample/pom.xml new file mode 100644 index 0000000..741d00d --- /dev/null +++ b/kudos-sample/kudos-maven-sample/pom.xml @@ -0,0 +1,110 @@ + + + + 4.0.0 + + com.kanyun.kudos + kudos-maven-sample + 1.0-SNAPSHOT + jar + + kudos-maven-sample + + + UTF-8 + 1.8.20 + 1.8.20-1.0.1 + official + 4.13.1 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + com.kanyun.kudos + kudos-gson + ${kudos.version} + + + com.kanyun.kudos + kudos-jackson + ${kudos.version} + + + com.google.code.gson + gson + 2.10 + + + com.fasterxml.jackson.core + jackson-databind + 2.15.0 + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + test + + + junit + junit + ${junit.version} + test + + + + + src/main/java + src/test/java + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + kudos + + + + + + + + + + + com.kanyun.kudos + kudos-maven-plugin + ${kudos.version} + + + + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + + + diff --git a/kudos-sample/kudos-maven-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt b/kudos-sample/kudos-maven-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt new file mode 100644 index 0000000..f304341 --- /dev/null +++ b/kudos-sample/kudos-maven-sample/src/test/java/com/kanyun/kudos/sample/test/KudosTest.kt @@ -0,0 +1,55 @@ +package com.kanyun.kudos.sample.test + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.DeserializationFeature +import com.google.gson.reflect.TypeToken +import com.kanyun.kudos.annotations.Kudos +import com.kanyun.kudos.gson.kudosGson +import com.kanyun.kudos.jackson.kudosObjectMapper +import kotlin.test.assertEquals +import org.junit.Test + +class KudosTest { + + inline fun deserializeByJackson(string: String, expect: String) { + val mapper = kudosObjectMapper() + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + mapper.disable(DeserializationFeature.WRAP_EXCEPTIONS); + try { + val t: T = mapper.readValue(string, object: TypeReference() {}) + assertEquals(t.toString(), expect) + } catch (e: Exception) { + assertEquals(e.toString(), expect) + } + } + + inline fun deserializeByGson(string: String, expect: String) { + val gson = kudosGson() + try { + val t: T = gson.fromJson(string, object: TypeToken() {}.type) + assertEquals(t.toString(), expect) + } catch (e: Exception) { + assertEquals(e.toString(), expect) + } + } + + @Kudos + data class User(val id: Long, val name: String, val age: Int = 10) + + fun gsonSample() { + deserializeByGson("""{}""", "java.lang.NullPointerException: Missing non-null field 'id'.") + deserializeByGson("""{"id": 10}""", "java.lang.NullPointerException: Missing non-null field 'name'.") + } + + fun jacksonSample() { + deserializeByJackson("""{}""", "java.lang.NullPointerException: Missing non-null field 'id'.") + deserializeByJackson("""{"id": 10}""", "java.lang.NullPointerException: Missing non-null field 'name'.") + } + + @Test + fun test() { + println("test Kudos.") + gsonSample() + jacksonSample() + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index d73f970..b071f93 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,6 +8,7 @@ pluginManagement { include(":kudos-compiler") include(":kudos-compiler-embeddable") include(":kudos-gradle-plugin") +include(":kudos-maven-plugin") include(":kudos-annotations") include(":kudos-runtime") include(":kudos-gson")