Skip to content

Commit

Permalink
fix exportSignatures plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Oct 17, 2024
1 parent 35df01a commit 937b23d
Show file tree
Hide file tree
Showing 25 changed files with 209 additions and 126 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ plugins {

id "me.champeau.jmh" version "0.6.8"
id "jacoco"

id "versioning"
id "srcinfo"
}

import java.text.SimpleDateFormat
Expand Down Expand Up @@ -87,6 +90,10 @@ subprojects { project ->
apply from: "$rootDir/gradle-scripts/subproject-dependencies.gradle"
apply from: "$rootDir/gradle-scripts/subproject-config.gradle"

if (project.file("build.gradle").exists()) {
apply from: "$rootDir/gradle-scripts/subproject-srcinfo-plugin.gradle"
}

group='com.commercetools.sdk'

version = globalVersion
Expand Down
19 changes: 0 additions & 19 deletions buildSrc/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@
* <td>{@link com.commercetools.api.models.cart.CartChangeTaxRoundingModeAction}</td>
* </tr>
* <tr>
* <td>{@link io.sphere.sdk.carts.commands.updateactions.FreezeCart}</td>
* <td>{@link com.commercetools.api.models.cart.CartFreezeCartAction}</td>
* </tr>
* <tr>
* <td>{@link io.sphere.sdk.carts.commands.updateactions.Recalculate}</td>
* <td>{@link com.commercetools.api.models.cart.CartRecalculateAction}</td>
* </tr>
Expand Down Expand Up @@ -441,6 +445,10 @@
* <td>{@link com.commercetools.api.models.cart.CartSetShippingRateInputAction}</td>
* </tr>
* <tr>
* <td>{@link io.sphere.sdk.carts.commands.updateactions.UnfreezeCart}</td>
* <td>{@link com.commercetools.api.models.cart.CartUnfreezeCartAction}</td>
* </tr>
* <tr>
* <td>{@link io.sphere.sdk.carts.commands.updateactions.UpdateItemShippingAddress}</td>
* <td>{@link com.commercetools.api.models.cart.CartUpdateItemShippingAddressAction}</td>
* </tr>
Expand Down
36 changes: 36 additions & 0 deletions common-plugins/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id 'java-gradle-plugin'
id 'idea'
id 'org.jetbrains.kotlin.jvm' version "2.0.0"
}

repositories {
mavenCentral()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}

group = 'com.commercetools.sdk.plugins'
version = '1.0'

gradlePlugin {
plugins.register('srcinfo-plugin') {
id = 'srcinfo'
implementationClass = 'com.commercetools.sdk.plugins.SrcInfoPlugin'
}
plugins.register("versioning-plugin") {
id = 'versioning'
implementationClass = 'com.commercetools.sdk.plugins.VersioningPlugin'
}
}

dependencies {
implementation project(path: ':javaparser', configuration: 'shadow')
implementation "com.google.code.gson:gson:2.11.0"
implementation 'com.squareup:javapoet:1.13.0'
implementation 'com.google.guava:guava:33.3.1-jre'
}
19 changes: 19 additions & 0 deletions common-plugins/javaparser/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

group = 'com.commercetools.sdk.plugins'
version = '1.0'

repositories {
mavenCentral()
}

shadowJar {
relocate 'com.github.javaparser', 'shadow.javaparser'
}

dependencies {
implementation 'com.github.javaparser:javaparser-core:3.26.2'
}
1 change: 1 addition & 0 deletions common-plugins/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("javaparser")
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.commercetools.sdk.plugins

import com.github.javaparser.StaticJavaParser
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.body.ConstructorDeclaration
import com.github.javaparser.ast.body.MethodDeclaration
import com.github.javaparser.ast.body.TypeDeclaration
import shadow.javaparser.StaticJavaParser
import shadow.javaparser.ast.CompilationUnit
import shadow.javaparser.ast.body.ClassOrInterfaceDeclaration
import shadow.javaparser.ast.body.ConstructorDeclaration
import shadow.javaparser.ast.body.MethodDeclaration
import shadow.javaparser.ast.body.TypeDeclaration
import com.squareup.javapoet.*
import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaPluginExtension
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.commercetools.sdk.plugins

import com.github.javaparser.StaticJavaParser
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.body.TypeDeclaration
import shadow.javaparser.StaticJavaParser
import shadow.javaparser.ast.CompilationUnit
import shadow.javaparser.ast.body.ClassOrInterfaceDeclaration
import shadow.javaparser.ast.body.TypeDeclaration
import com.google.common.base.CaseFormat
import org.gradle.api.Plugin
import org.gradle.api.Project
import shadow.javaparser.ast.body.MethodDeclaration
import java.io.File
import java.io.IOException
import java.io.Writer
Expand Down Expand Up @@ -263,22 +264,22 @@ class MigrationInfoPlugin : Plugin<Project> {



// fun classInfo(file: File, info: ClassOrInterfaceDeclaration, hash: String): List<Pair<String, Map<String, String>>> {
// return info.methods.map { methodInfo ->
// "${info.fullyQualifiedName.get()}#${methodInfo.signature}" to mapOf(
// "type" to "method",
// "gitHash" to hash,
// "methodName" to methodInfo.name.toString(),
// "simpleName" to "${info.name}#${methodInfo.name}",
// "name" to "${info.fullyQualifiedName.get()}#${methodInfo.name}",
// "file" to file.toString(),
// "start" to methodInfo.begin.get().line.toString(),
// "end" to methodInfo.end.get().line.toString(),
// "srcUrl" to "https://github.com/commercetools/commercetools-sdk-java-v2/blob/${hash}/${file}#L${methodInfo.begin.get().line}-L${methodInfo.end.get().line}",
// "sdkV1Method" to methodInfo.methodBody()
// )
// }
// }
fun classInfo(file: File, info: ClassOrInterfaceDeclaration, hash: String): List<Pair<String, Map<String, String>>> {
return info.methods.map { methodInfo ->
"${info.fullyQualifiedName.get()}#${methodInfo.signature}" to mapOf(
"type" to "method",
"gitHash" to hash,
"methodName" to methodInfo.name.toString(),
"simpleName" to "${info.name}#${methodInfo.name}",
"name" to "${info.fullyQualifiedName.get()}#${methodInfo.name}",
"file" to file.toString(),
"start" to methodInfo.begin.get().line.toString(),
"end" to methodInfo.end.get().line.toString(),
"srcUrl" to "https://github.com/commercetools/commercetools-sdk-java-v2/blob/${hash}/${file}#L${methodInfo.begin.get().line}-L${methodInfo.end.get().line}",
"sdkV1Method" to methodInfo.methodBody()
)
}
}

fun ClassOrInterfaceDeclaration.v2Class(v2BaseFolder: List<String>): String {

Expand Down Expand Up @@ -326,33 +327,33 @@ class MigrationInfoPlugin : Plugin<Project> {
return v2Classes.joinToString(", ");
}

// fun ClassOrInterfaceDeclaration.v1CreateCommandClassUsage(): Pair<String, String> {
//
// if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
// return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
// }
// val v1PackageName = this.fullyQualifiedName.map { it.replace("." + this.name.toString(), "") }.get()
// val v1ModelPackageName = v1PackageName.replace(".commands", "");
//
// return this.getMethodsByName("of").map { "$v1ModelPackageName.${it.parameters.filter { it.nameAsString.contains("draft", true) }.map { it.typeAsString }.firstOrNull() ?: "" }" to "of(${it.parameters.joinToString(",") { it.typeAsString }})" }.first()
// }
//
// fun ClassOrInterfaceDeclaration.v1UpdateCommandClassUsage(): Pair<String, String> {
//
// if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
// return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
// }
//
// return this.getMethodsByName("of").filter { it.parameters.any { it.typeAsString.startsWith("UpdateAction") } }.map { "" to "of(${it.parameters.joinToString(",") { it.typeAsString.split("<").first() + if (it.isVarArgs) "..." else "" }})" }.first()
// }
//
// fun ClassOrInterfaceDeclaration.v1DeleteCommandClassUsage(): Pair<String, String> {
//
// if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
// return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
// }
// return this.getMethodsByName("of").filter { it.parameters.any { it.typeAsString.startsWith("Versioned") } }.map { "" to "of(${it.parameters.joinToString(",") { it.typeAsString.split("<").first() }})" }.firstOrNull() ?: Pair("", "")
// }
fun ClassOrInterfaceDeclaration.v1CreateCommandClassUsage(): Pair<String, String> {

if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
}
val v1PackageName = this.fullyQualifiedName.map { it.replace("." + this.name.toString(), "") }.get()
val v1ModelPackageName = v1PackageName.replace(".commands", "");

return this.getMethodsByName("of").map { "$v1ModelPackageName.${it.parameters.filter { it.nameAsString.contains("draft", true) }.map { it.typeAsString }.firstOrNull() ?: "" }" to "of(${it.parameters.joinToString(",") { it.typeAsString }})" }.first()
}

fun ClassOrInterfaceDeclaration.v1UpdateCommandClassUsage(): Pair<String, String> {

if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
}

return this.getMethodsByName("of").filter { it.parameters.any { it.typeAsString.startsWith("UpdateAction") } }.map { "" to "of(${it.parameters.joinToString(",") { it.typeAsString.split("<").first() + if (it.isVarArgs) "..." else "" }})" }.first()
}

fun ClassOrInterfaceDeclaration.v1DeleteCommandClassUsage(): Pair<String, String> {

if (commandClassUsageV1Mapping.containsKey(this.fullyQualifiedName.get())) {
return commandClassUsageV1Mapping.get(this.fullyQualifiedName.get()) ?: Pair("", "")
}
return this.getMethodsByName("of").filter { it.parameters.any { it.typeAsString.startsWith("Versioned") } }.map { "" to "of(${it.parameters.joinToString(",") { it.typeAsString.split("<").first() }})" }.firstOrNull() ?: Pair("", "")
}

fun ClassOrInterfaceDeclaration.v2CommandClassUsage(): String {

Expand Down Expand Up @@ -486,17 +487,18 @@ class MigrationInfoPlugin : Plugin<Project> {
"io.sphere.sdk.projects.commands.updateactions.ChangeMessages" to "removed",
)

// fun MethodDeclaration.methodBody(): String {
// val methodBody = this.body
// if (!methodBody.isPresent) {
// return ""
// }
// val bodyRange = methodBody.get().tokenRange.get().toString()
// return bodyRange.substring(1, bodyRange.length - 1).trimIndent()
// }
fun MethodDeclaration.methodBody(): String {
val methodBody = this.body
if (!methodBody.isPresent) {
return ""
}
val bodyRange = methodBody.get().tokenRange.get().toString()
return bodyRange.substring(1, bodyRange.length - 1).trimIndent()
}
}

fun String.packageName(): String {
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this)
if (this.isEmpty() or this.contains(".").not()) return ""
val packageFolder = Paths.get(this.replace(".", "/"))
return packageFolder.parent.toString().replace("/", ".");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.commercetools.sdk.plugins;

import com.github.javaparser.StaticJavaParser
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.body.MethodDeclaration
import com.github.javaparser.ast.body.TypeDeclaration
import com.github.javaparser.ast.expr.NormalAnnotationExpr
import shadow.javaparser.ParserConfiguration.LanguageLevel
import shadow.javaparser.ast.CompilationUnit
import shadow.javaparser.ast.body.ClassOrInterfaceDeclaration
import shadow.javaparser.ast.body.MethodDeclaration
import shadow.javaparser.ast.body.TypeDeclaration
import shadow.javaparser.ast.expr.NormalAnnotationExpr
import com.google.gson.stream.JsonWriter
import org.gradle.api.Plugin
import org.gradle.api.Project
import shadow.javaparser.JavaParser
import shadow.javaparser.ParserConfiguration
import java.io.File
import java.io.IOException
import java.io.Writer
Expand All @@ -32,11 +34,14 @@ class SrcInfoPlugin : Plugin<Project> {
private fun createExportTask(project: Project) {
val extension = project.extensions.create("srcInfo", SrcInfoPluginExtension::class.java)
val exportTask = project.task("exportSignatures")
val parserConfiguration = ParserConfiguration()
parserConfiguration.languageLevel = LanguageLevel.JAVA_17
val javaParser = JavaParser(parserConfiguration)
exportTask.group = "documentation"
exportTask.doLast {
val outputFolder = extension.outputFolder.map { o -> Paths.get(o) }.getOrElse(project.buildDir.resolve("src-info").toPath())
val docsUrnOnly = extension.docsUrnOnly.getOrElse(false)
outputFolder.toFile().mkdir()
outputFolder.toFile().mkdirs()
val javaFiles = project.fileTree(mapOf("dir" to extension.baseFolder.getOrElse("src"), "include" to "**/*.java")).files
val fileWriter: Writer = Files.newBufferedWriter(outputFolder.resolve(project.name + ".json"))
val writer = JsonWriter(fileWriter)
Expand All @@ -46,7 +51,7 @@ class SrcInfoPlugin : Plugin<Project> {
writer.beginObject()
javaFiles.forEach { file ->
run {
javaFileInfo(file, project, hash, extension.includePackages.orNull, docsUrnOnly).forEach { entry ->
javaFileInfo(javaParser, file, project, hash, extension.includePackages.orNull, docsUrnOnly).forEach { entry ->
run {
writer.name(entry.key)
writer.beginObject()
Expand All @@ -65,8 +70,9 @@ class SrcInfoPlugin : Plugin<Project> {
}
}

private fun javaFileInfo(file: File, project: Project, hash: String, includePackages: List<String>?, docsUrnOnly: Boolean): Map<String, Map<String, String>> {
val parse: CompilationUnit = StaticJavaParser.parse(file)
private fun javaFileInfo(javaParser: JavaParser, file: File, project: Project, hash: String, includePackages: List<String>?, docsUrnOnly: Boolean): Map<String, Map<String, String>> {
val parse: CompilationUnit = javaParser.parse(file).result.get()

val relativeFile = file.relativeTo(project.rootDir)
if (includePackages != null && includePackages.firstOrNull { s: String -> parse.packageDeclaration.get().nameAsString.startsWith(s) } == null) {
return emptyMap()
Expand Down
8 changes: 0 additions & 8 deletions gradle-scripts/subproject-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ ext {
buildInfoPackage = "io.vrap.rmf.base.client"
}

srcInfo {
includePackages = [
"example",
"commercetools"
]
docsUrnOnly = true
}

tasks.register('integrationTest', Test) {
description = "Runs integration tests."
group = "verification"
Expand Down
2 changes: 0 additions & 2 deletions gradle-scripts/subproject-plugins.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ apply plugin: 'idea'

apply plugin: 'java'
apply plugin: 'java-library' // needed to make sure that transitive deps have 'compile' scope

apply plugin: 'srcinfo'
9 changes: 9 additions & 0 deletions gradle-scripts/subproject-srcinfo-plugin.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: 'srcinfo'

srcInfo {
includePackages = [
"example",
"commercetools"
]
docsUrnOnly = true
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 937b23d

Please sign in to comment.