Skip to content

Commit 7287f1f

Browse files
[Backport 2.x] Backport codegen implementation (#1173)
* Generate client code from OpenAPI specification (#366) * OpenAPI generator from scratch Signed-off-by: Thomas Farr <[email protected]> * Write to files and generate object shapes Signed-off-by: Thomas Farr <[email protected]> * Output response shapes and enum shapes Signed-off-by: Thomas Farr <[email protected]> * Fix builder setters for enums Signed-off-by: Thomas Farr <[email protected]> * Better handling of required fields Signed-off-by: Thomas Farr <[email protected]> * Unify templates for ObjectShape/OperationRequest Signed-off-by: Thomas Farr <[email protected]> * Simple http path expression Signed-off-by: Thomas Farr <[email protected]> * Generate client class Signed-off-by: Thomas Farr <[email protected]> * Cleanup Signed-off-by: Thomas Farr <[email protected]> * Generate namespaces remote store client Signed-off-by: Thomas Farr <[email protected]> * Add license header, improve http path builder, fix remote store spec Signed-off-by: Thomas Farr <[email protected]> * Suppress checkstyle unused imports lint for generated code Signed-off-by: Thomas Farr <[email protected]> * Centralize some naming Signed-off-by: Thomas Farr <[email protected]> * Allow renaming operations Signed-off-by: Thomas Farr <[email protected]> * Add generated warning header Signed-off-by: Thomas Farr <[email protected]> * Refactor Signed-off-by: Thomas Farr <[email protected]> * Less code more templates Signed-off-by: Thomas Farr <[email protected]> * Simplify http path part Signed-off-by: Thomas Farr <[email protected]> * Fixes Signed-off-by: Thomas Farr <[email protected]> * Swap to swagger-parser Signed-off-by: Thomas Farr <[email protected]> * Handle x-data-type & array response bodies Signed-off-by: Thomas Farr <[email protected]> * Handle multiple http paths for same operation Signed-off-by: Thomas Farr <[email protected]> * Improve type handling Signed-off-by: Thomas Farr <[email protected]> * Tweak package name determination Signed-off-by: Thomas Farr <[email protected]> * Handle additionalProperties on ErrorCause Signed-off-by: Thomas Farr <[email protected]> * Simplify enum shape Signed-off-by: Thomas Farr <[email protected]> * Change default number type Signed-off-by: Thomas Farr <[email protected]> * Generate parameter-less operation method when no required fields Signed-off-by: Thomas Farr <[email protected]> * Refactor to parse multi-file spec Signed-off-by: Thomas Farr <[email protected]> * spotlessApply Signed-off-by: Thomas Farr <[email protected]> * Use spotless to format code Signed-off-by: Thomas Farr <[email protected]> * Maintain spec location information Signed-off-by: Thomas Farr <[email protected]> * Refactor model transformation and type mapper Signed-off-by: Thomas Farr <[email protected]> * Match existing naming scheme Signed-off-by: Thomas Farr <[email protected]> * Change OperationGroup matching logic to determine which to generate Signed-off-by: Thomas Farr <[email protected]> * Better handle errors thrown by formatter Signed-off-by: Thomas Farr <[email protected]> * Add typedef comment Signed-off-by: Thomas Farr <[email protected]> * Support tagged union Signed-off-by: Thomas Farr <[email protected]> * Add additional reserved keywords Signed-off-by: Thomas Farr <[email protected]> * Improve parsing logic Signed-off-by: Thomas Farr <[email protected]> * Add basic tests and implement proper argument parsing Signed-off-by: Thomas Farr <[email protected]> * Improve tagged union generation and filter global query params Signed-off-by: Thomas Farr <[email protected]> * Change enum naming style Signed-off-by: Thomas Farr <[email protected]> * Handle Time type Signed-off-by: Thomas Farr <[email protected]> * Handle deprecation Signed-off-by: Thomas Farr <[email protected]> * Clear operations to generate Signed-off-by: Thomas Farr <[email protected]> * Change usages of `<stream>.toList()` to `<stream>.collect(Collectors.toList())` Signed-off-by: Thomas Farr <[email protected]> * Remove publishing from build.gradle Signed-off-by: Thomas Farr <[email protected]> * Cleaning up Signed-off-by: Thomas Farr <[email protected]> * spotless Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit bb995ec) * Bump commons-codec:commons-codec from 1.11.0 to 1.17.0 in java-codegen (#1023) Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 13982b1) * Regenerate request/response for the `info` operation (#1026) * Tweak formatting rules used by codegen to allow reformatting javadoc Signed-off-by: Thomas Farr <[email protected]> * Support codegen for request shapes that may be singleton Signed-off-by: Thomas Farr <[email protected]> * Generate info operation shapes Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit dea7e72) * Enforce & fix license headers (#1027) * Enforce license headers Signed-off-by: Thomas Farr <[email protected]> * spotlessApply Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 5d5c6b5) * Add `@Generated` annotation to generated classes (#1029) Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 767c313) * Begin generating client implementations (#1052) * Tweak client generation to extend from existing classes Signed-off-by: Thomas Farr <[email protected]> * Modify existing client classes to be abstract base classes Signed-off-by: Thomas Farr <[email protected]> * Re-run generator Signed-off-by: Thomas Farr <[email protected]> * ./gradlew spotlessApply Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit dbde609) * Bump org.junit:junit-bom from 5.10.2 to 5.10.3 (#1062) * Bump org.junit:junit-bom from 5.10.2 to 5.10.3 Bumps [org.junit:junit-bom](https://github.com/junit-team/junit5) from 5.10.2 to 5.10.3. - [Release notes](https://github.com/junit-team/junit5/releases) - [Commits](junit-team/junit5@r5.10.2...r5.10.3) Signed-off-by: Thomas Farr <[email protected]> --- updated-dependencies: - dependency-name: org.junit:junit-bom dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Update changelog Signed-off-by: dependabot[bot] <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <dependabot[bot]@users.noreply.github.com> (cherry picked from commit af769c8) * Bump org.owasp.dependencycheck from 9.2.0 to 10.0.2 (#1069) * Bump org.owasp.dependencycheck from 9.2.0 to 10.0.2 Bumps org.owasp.dependencycheck from 9.2.0 to 10.0.2. Signed-off-by: Thomas Farr <[email protected]> --- updated-dependencies: - dependency-name: org.owasp.dependencycheck dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * Update changelog Signed-off-by: dependabot[bot] <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <dependabot[bot]@users.noreply.github.com> (cherry picked from commit 848be31) * Add workflow to auto-update codegen (#1065) Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 0cf11db) * Fix handling of OpenAPI schemas with multiple types (#1102) Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit eff1ae1) * Regenerate dangling_indices namespace (#1124) * Rename genericArgs to typeParams Signed-off-by: Thomas Farr <[email protected]> * Fix required path param handling Signed-off-by: Thomas Farr <[email protected]> * Correctly generate AcknowledgedResponseBase Signed-off-by: Thomas Farr <[email protected]> * Generate DeleteDanglingIndexRequest Signed-off-by: Thomas Farr <[email protected]> * Correctly generate DeleteDanglingIndexResponse Signed-off-by: Thomas Farr <[email protected]> * Generate import_dangling_index Signed-off-by: Thomas Farr <[email protected]> * Generate ListDanglingIndicesRequest Signed-off-by: Thomas Farr <[email protected]> * Generate ErrorCause Signed-off-by: Thomas Farr <[email protected]> * Generate NodeStatistics Signed-off-by: Thomas Farr <[email protected]> * Generate DanglingIndex Signed-off-by: Thomas Farr <[email protected]> * Generate ListDanglingIndicesResponse Signed-off-by: Thomas Farr <[email protected]> * Generate DanglingIndicesClient Signed-off-by: Thomas Farr <[email protected]> * Fixes Signed-off-by: Thomas Farr <[email protected]> * Changelog and Upgrading Signed-off-by: Thomas Farr <[email protected]> * Fix javadoc Signed-off-by: Thomas Farr <[email protected]> * Fix tests Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 324f90a) * Bump com.github.jk1.dependency-license-report from 2.8 to 2.9 (#1144) Bumps com.github.jk1.dependency-license-report from 2.8 to 2.9. Signed-off-by: Thomas Farr <[email protected]> --- updated-dependencies: - dependency-name: com.github.jk1.dependency-license-report dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> (cherry picked from commit 08e7e65) * Tweak codegen logic for formatting, documentation and handling of anyOf (#1152) * Re-generate code Signed-off-by: Thomas Farr <[email protected]> * Improve builder doc comments Signed-off-by: Thomas Farr <[email protected]> * Fix list/map isDefined serialize logic Signed-off-by: Thomas Farr <[email protected]> * Add support for deprecated enums Signed-off-by: Thomas Farr <[email protected]> * Improve isDefined logic Signed-off-by: Thomas Farr <[email protected]> * Allow overriding request/response naming Signed-off-by: Thomas Farr <[email protected]> * Support anyOf Signed-off-by: Thomas Farr <[email protected]> * Update spec Signed-off-by: Thomas Farr <[email protected]> * spotlessApply Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 7b954af) * Generate the ml namespace (#1158) * Generate ml.register_model_group Signed-off-by: Thomas Farr <[email protected]> * Start neural search sample Signed-off-by: Thomas Farr <[email protected]> * Re-generate ShardStatistics Signed-off-by: Thomas Farr <[email protected]> * Re-generate ShardFailure Signed-off-by: Thomas Farr <[email protected]> * Re-generate Result Signed-off-by: Thomas Farr <[email protected]> * Re-generate WriteResponseBase Signed-off-by: Thomas Farr <[email protected]> * Generate ml.delete_model_group Signed-off-by: Thomas Farr <[email protected]> * Generate ml.register_model Signed-off-by: Thomas Farr <[email protected]> * Exclude legacy license from ml namespace Signed-off-by: Thomas Farr <[email protected]> * Generate ml.get_task Signed-off-by: Thomas Farr <[email protected]> * Generate ml.delete_task Signed-off-by: Thomas Farr <[email protected]> * Generate ml.delete_model Signed-off-by: Thomas Farr <[email protected]> * Generate ml.deploy_model Signed-off-by: Thomas Farr <[email protected]> * Generate ml.undeploy_model Signed-off-by: Thomas Farr <[email protected]> * Complete neural search sample Signed-off-by: Thomas Farr <[email protected]> * Generate ml.get_model Signed-off-by: Thomas Farr <[email protected]> * Add changelog entry Signed-off-by: Thomas Farr <[email protected]> * note Signed-off-by: Thomas Farr <[email protected]> * Fix tests Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit c6e61e1) * Invert abstractness of generated vs "handwritten" client classes (#1170) * Invert abstractness of generated vs "handwritten" client classes Signed-off-by: Thomas Farr <[email protected]> * Address review comments Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> (cherry picked from commit 42e2928) * Temporarily disable code gen up-to-date check Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 51375b4 commit 7287f1f

File tree

111 files changed

+58738
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+58738
-0
lines changed

.github/workflows/code-generation.yml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Code Generation
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
schedule:
7+
- cron: '0 0 * * 1' # 00:00 UTC Weekly on Mondays
8+
9+
jobs:
10+
up_to_date:
11+
name: Ensure Generated Code Is Up To Date
12+
# if: github.event_name == 'pull_request'
13+
if: false
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout Java Client
17+
uses: actions/checkout@v4
18+
19+
- name: Set up JDK
20+
uses: actions/setup-java@v4
21+
with:
22+
java-version: 11
23+
distribution: 'temurin'
24+
cache: 'gradle'
25+
26+
- name: Run Code Generator
27+
run: ./gradlew :java-codegen:run
28+
29+
- name: Check For Uncommitted Changes
30+
shell: bash -eo pipefail {0}
31+
run: |
32+
output=$(git status --porcelain)
33+
if [ -z "$output" ]; then
34+
echo "Clean working directory"
35+
exit 0
36+
else
37+
echo "Dirty working directory"
38+
echo "$output"
39+
exit 1
40+
fi
41+
42+
43+
update_spec:
44+
name: Update Specification and Re-generate Code
45+
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'schedule' && github.repository == 'opensearch-project/opensearch-java')
46+
runs-on: ubuntu-latest
47+
permissions:
48+
contents: write
49+
pull-requests: write
50+
steps:
51+
- name: Checkout Java Client
52+
uses: actions/checkout@v4
53+
54+
- name: Set up JDK
55+
uses: actions/setup-java@v4
56+
with:
57+
java-version: 11
58+
distribution: 'temurin'
59+
cache: 'gradle'
60+
61+
- name: Download Latest Spec && Run Code Generator
62+
run: ./gradlew :java-codegen:downloadLatestSpec :java-codegen:run
63+
64+
- name: Get Current Date
65+
id: date
66+
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
67+
68+
- name: Generate GitHub App Token
69+
if: github.repository == 'opensearch-project/opensearch-java'
70+
id: github_app_token
71+
uses: tibdex/[email protected]
72+
with:
73+
app_id: ${{ secrets.APP_ID }}
74+
private_key: ${{ secrets.APP_PRIVATE_KEY }}
75+
installation_id: 22958780
76+
77+
- name: Create Pull Request
78+
uses: peter-evans/create-pull-request@v6
79+
with:
80+
token: ${{ steps.github_app_token.outputs.token || secrets.GITHUB_TOKEN }}
81+
commit-message: Re-generate client code using latest OpenSearch API specification (${{ steps.date.outputs.date }})
82+
title: Re-generated client code using latest OpenSearch API specification
83+
body: |
84+
Re-generated client code using latest OpenSearch API specification.
85+
Date: ${{ steps.date.outputs.date }}
86+
branch: code-gen/${{ github.ref_name }}/update
87+
signoff: true
88+
labels: |
89+
autocut

buildSrc/formatterConfig-generated.xml

+362
Large diffs are not rendered by default.

java-client/src/main/java/org/opensearch/client/util/ApiTypeHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.List;
4040
import java.util.Map;
4141
import java.util.Set;
42+
import javax.annotation.Nonnull;
4243
import javax.annotation.Nullable;
4344

4445
/**
@@ -134,6 +135,7 @@ public static <T> boolean isDefined(List<T> list) {
134135
/**
135136
* Returns an unmodifiable view of a list. If {@code list} is {@code null}, an {@link #undefinedList()} is returned.
136137
*/
138+
@Nonnull
137139
public static <T> List<T> unmodifiable(@Nullable List<T> list) {
138140
if (list == null) {
139141
return undefinedList();

java-codegen/build.gradle.kts

+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
/*
10+
* Modifications Copyright OpenSearch Contributors. See
11+
* GitHub history for details.
12+
*/
13+
14+
import com.github.jk1.license.ProjectData
15+
import com.github.jk1.license.render.ReportRenderer
16+
import de.undercouch.gradle.tasks.download.Download
17+
import java.io.FileWriter
18+
19+
buildscript {
20+
repositories {
21+
mavenLocal()
22+
maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots")
23+
mavenCentral()
24+
maven(url = "https://plugins.gradle.org/m2/")
25+
}
26+
dependencies {
27+
"classpath"(group = "org.opensearch.gradle", name = "build-tools", version = "3.0.0-SNAPSHOT")
28+
}
29+
}
30+
31+
plugins {
32+
application
33+
id("com.github.jk1.dependency-license-report") version "2.9"
34+
id("org.owasp.dependencycheck") version "10.0.2"
35+
id("com.diffplug.spotless") version "6.25.0"
36+
id("de.undercouch.download") version "5.6.0"
37+
}
38+
apply(plugin = "opensearch.repositories")
39+
apply(plugin = "org.owasp.dependencycheck")
40+
41+
val runtimeJavaVersion = (System.getProperty("runtime.java")?.toInt())?.let(JavaVersion::toVersion) ?: JavaVersion.current()
42+
logger.quiet("=======================================")
43+
logger.quiet(" Runtime JDK Version : $runtimeJavaVersion")
44+
logger.quiet(" Gradle JDK Version : " + JavaVersion.current())
45+
logger.quiet("=======================================")
46+
47+
java {
48+
targetCompatibility = JavaVersion.VERSION_11
49+
sourceCompatibility = JavaVersion.VERSION_11
50+
51+
withJavadocJar()
52+
withSourcesJar()
53+
54+
toolchain {
55+
languageVersion = JavaLanguageVersion.of(runtimeJavaVersion.majorVersion)
56+
vendor = JvmVendorSpec.ADOPTIUM
57+
}
58+
}
59+
60+
application {
61+
mainClass.set("org.opensearch.client.codegen.CodeGenerator")
62+
applicationDefaultJvmArgs = listOf(
63+
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
64+
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
65+
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
66+
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
67+
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
68+
)
69+
}
70+
71+
val localSpecification = "$projectDir/opensearch-openapi.yaml"
72+
73+
tasks.create<Download>("downloadLatestSpec") {
74+
src("https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml")
75+
dest(localSpecification)
76+
}
77+
78+
tasks.named<JavaExec>("run") {
79+
args = listOf(
80+
"--input", localSpecification,
81+
"--eclipse-config", "$rootDir/buildSrc/formatterConfig-generated.xml",
82+
"--output", "${project(":java-client").projectDir}/src/generated/java/"
83+
)
84+
}
85+
86+
tasks.withType<ProcessResources> {
87+
expand(
88+
"version" to version,
89+
"git_revision" to (if (rootProject.extra.has("gitHashFull")) rootProject.extra["gitHashFull"] else "unknown")
90+
)
91+
}
92+
93+
tasks.withType<Javadoc>().configureEach{
94+
options {
95+
encoding = "UTF-8"
96+
}
97+
}
98+
99+
tasks.withType<Jar> {
100+
doFirst {
101+
if (rootProject.extra.has("gitHashFull")) {
102+
val jar = this as Jar
103+
jar.manifest.attributes["X-Git-Revision"] = rootProject.extra["gitHashFull"]
104+
jar.manifest.attributes["X-Git-Commit-Time"] = rootProject .extra["gitCommitTime"]
105+
} else {
106+
throw GradleException("No git information available")
107+
}
108+
}
109+
110+
manifest {
111+
attributes["Implementation-Title"] = "OpenSearch Java client code generator"
112+
attributes["Implementation-Vendor"] = "OpenSearch"
113+
attributes["Implementation-URL"] = "https://github.com/opensearch-project/opensearch-java/"
114+
attributes["Build-Date"] = rootProject.extra["buildTime"]
115+
}
116+
117+
metaInf {
118+
from("../LICENSE.txt")
119+
from("../NOTICE.txt")
120+
}
121+
}
122+
123+
tasks.withType<Test> {
124+
useJUnitPlatform()
125+
testLogging {
126+
events("passed", "skipped", "failed")
127+
}
128+
}
129+
130+
tasks.build {
131+
dependsOn("spotlessJavaCheck")
132+
}
133+
134+
dependencies {
135+
// Apache 2.0
136+
implementation("io.swagger.parser.v3", "swagger-parser", "2.1.22")
137+
138+
// (New) BSD
139+
implementation("com.samskivert", "jmustache", "1.16")
140+
141+
// Apache 2.0
142+
implementation("commons-cli", "commons-cli", "1.8.0")
143+
implementation("commons-codec", "commons-codec", "1.17.0")
144+
implementation("commons-logging", "commons-logging", "1.3.2")
145+
implementation("org.apache.commons", "commons-lang3", "3.14.0")
146+
implementation("org.apache.commons", "commons-text", "1.12.0")
147+
implementation("org.apache.logging.log4j", "log4j-api", "[2.17.1,3.0)")
148+
implementation("org.apache.logging.log4j", "log4j-core", "[2.17.1,3.0)")
149+
implementation("org.apache.logging.log4j", "log4j-slf4j2-impl", "[2.17.1,3.0)")
150+
151+
// Apache 2.0
152+
implementation("com.fasterxml.jackson.core", "jackson-core", "2.17.1")
153+
implementation("com.fasterxml.jackson.core", "jackson-databind", "2.17.1")
154+
155+
// Apache 2.0
156+
implementation("com.diffplug.spotless", "spotless-lib", "2.45.0")
157+
implementation("com.diffplug.spotless", "spotless-lib-extra", "2.45.0")
158+
159+
// Apache 2.0
160+
// https://search.maven.org/artifact/com.google.code.findbugs/jsr305
161+
implementation("com.google.code.findbugs", "jsr305", "3.0.2")
162+
163+
// Apache 2.0
164+
compileOnly("org.jetbrains", "annotations", "24.1.0")
165+
166+
// Apache 2.0
167+
implementation("org.apache.maven.resolver", "maven-resolver-api", "1.9.20")
168+
implementation("org.apache.maven.resolver", "maven-resolver-supplier", "1.9.20")
169+
170+
// MIT
171+
implementation("org.semver4j", "semver4j", "5.3.0")
172+
173+
// EPL-2.0
174+
testImplementation(platform("org.junit:junit-bom:5.10.3"))
175+
testImplementation("org.junit.jupiter", "junit-jupiter")
176+
testRuntimeOnly("org.junit.platform", "junit-platform-launcher")
177+
}
178+
179+
licenseReport {
180+
renderers = arrayOf(SpdxReporter(rootProject.layout.buildDirectory.file("release/codegen-dependencies.csv").get().getAsFile()))
181+
excludeGroups = arrayOf()
182+
}
183+
184+
class SpdxReporter(val dest: File) : ReportRenderer {
185+
// License names to their SPDX identifier
186+
val spdxIds = mapOf(
187+
"Apache 2" to "Apache-2.0",
188+
"Apache 2.0" to "Apache-2.0",
189+
"Apache-2.0" to "Apache-2.0",
190+
"\"Apache-2.0\";link=\"https://www.apache.org/licenses/LICENSE-2.0.txt\"" to "Apache-2.0",
191+
"\"Apache 2.0\";link=\"http://www.apache.org/licenses/LICENSE-2.0.txt\"" to "Apache-2.0",
192+
"\"Apache License 2.0\";link=\"http://www.apache.org/licenses/LICENSE-2.0.html\"" to "Apache-2.0",
193+
"Apache License 2.0" to "Apache-2.0",
194+
"Apache License, version 2.0" to "Apache-2.0",
195+
"Apache License, Version 2.0" to "Apache-2.0",
196+
"Apache Software License, version 2.0" to "Apache-2.0",
197+
"The Apache License, Version 2.0" to "Apache-2.0",
198+
"The Apache Software License, Version 2.0" to "Apache-2.0",
199+
"BSD Zero Clause License" to "0BSD",
200+
"The (New) BSD License" to "BSD-3-Clause",
201+
"EDL 1.0" to "BSD-3-Clause",
202+
"Eclipse Distribution License - v 1.0" to "BSD-3-Clause",
203+
"Eclipse Distribution License (New BSD License)" to "BSD-3-Clause",
204+
"Eclipse Public License 2.0" to "EPL-2.0",
205+
"Eclipse Public License v. 2.0" to "EPL-2.0",
206+
"Eclipse Public License - v 2.0" to "EPL-2.0",
207+
"GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0",
208+
"COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0",
209+
"Lesser General Public License, version 3 or greater" to "LGPL-3.0+",
210+
"MIT" to "MIT",
211+
"MIT License" to "MIT",
212+
"The MIT License" to "MIT",
213+
"Mozilla Public License, Version 2.0" to "MPL-2.0",
214+
"Public Domain" to "PUBLIC-DOMAIN"
215+
)
216+
217+
private fun quote(str: String) : String {
218+
return if (str.contains(',') || str.contains("\"")) {
219+
"\"" + str.replace("\"", "\"\"") + "\""
220+
} else {
221+
str
222+
}
223+
}
224+
225+
override fun render(data: ProjectData?) {
226+
dest.parentFile.mkdirs()
227+
FileWriter(dest).use { out ->
228+
out.append("name,url,version,revision,license\n")
229+
data?.allDependencies?.forEach { dep ->
230+
val info = com.github.jk1.license.render.LicenseDataCollector.multiModuleLicenseInfo(dep)
231+
232+
val depVersion = dep.version
233+
val depName = dep.group + ":" + dep.name
234+
val depUrl = if (info.moduleUrls.isNotEmpty()) { info.moduleUrls.first() } else { "<NO MODULE URL>" }
235+
236+
val licenseIds = info.licenses.mapNotNull { license ->
237+
license.name?.let {
238+
checkNotNull(spdxIds[it]) { "No SPDX identifier for $license" }
239+
}
240+
}.toSet()
241+
242+
// Combine multiple licenses.
243+
// See https://spdx.github.io/spdx-spec/appendix-IV-SPDX-license-expressions/#composite-license-expressions
244+
val licenseId = licenseIds.joinToString(" OR ")
245+
out.append("${quote(depName)},${quote(depUrl)},${quote(depVersion)},,${quote(licenseId)}\n")
246+
}
247+
}
248+
}
249+
}
250+
251+
tasks.withType<Jar> {
252+
doLast {
253+
ant.withGroovyBuilder {
254+
"checksum"("algorithm" to "md5", "file" to archiveFile.get())
255+
"checksum"("algorithm" to "sha1", "file" to archiveFile.get())
256+
"checksum"("algorithm" to "sha-256", "file" to archiveFile.get(), "fileext" to ".sha256")
257+
"checksum"("algorithm" to "sha-512", "file" to archiveFile.get(), "fileext" to ".sha512")
258+
}
259+
}
260+
}
261+
262+
spotless {
263+
java {
264+
target("**/*.java")
265+
266+
licenseHeaderFile("../LICENSE_HEADER.txt")
267+
268+
// Use the default importOrder configuration
269+
importOrder()
270+
removeUnusedImports()
271+
272+
eclipse().configFile("../buildSrc/formatterConfig-generated.xml")
273+
274+
trimTrailingWhitespace()
275+
endWithNewline()
276+
}
277+
}

0 commit comments

Comments
 (0)