From 9e33127397e7831e69b13269fc6e5162f7c4616f Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Mon, 11 Aug 2025 13:03:01 +0000 Subject: [PATCH 1/2] codegen metadata --- .stats.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.stats.yml b/.stats.yml index 1c85ee4a..a098c3d4 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 111 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai%2Fopenai-6a1bfd4738fff02ef5becc3fdb2bf0cd6c026f2c924d4147a2a515474477dd9a.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai%2Fopenai-9cadfad609f94f20ebf74fdc06a80302f1a324dc69700a309a8056aabca82fd2.yml openapi_spec_hash: 3eb8d86c06f0bb5e1190983e5acfc9ba -config_hash: a67c5e195a59855fe8a5db0dc61a3e7f +config_hash: 68337b532875626269c304372a669f67 From 2b86cdd904d657b3eaa48373fec6b737b2071d16 Mon Sep 17 00:00:00 2001 From: D Gardner Date: Tue, 12 Aug 2025 17:46:25 +0100 Subject: [PATCH 2/2] format-code: faster formatting and linting (parallel) --- CONTRIBUTING.md | 15 ++- buildSrc/build.gradle.kts | 1 - .../src/main/kotlin/openai.java.gradle.kts | 103 ++++++++++++++++-- .../src/main/kotlin/openai.kotlin.gradle.kts | 82 ++++++++++++-- scripts/format | 4 +- 5 files changed, 179 insertions(+), 26 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b2884477..cd2ec20b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -125,7 +125,9 @@ The project uses: ## Linting and formatting -This repository uses [Spotless](https://github.com/diffplug/spotless) with Palantir Java Format for code formatting and various linting tools. +This repository uses [Ktfmt](https://github.com/facebook/ktfmt) and +[Palantir Java Format](https://github.com/facebook/ktfmt) for code formatting and various linting +tools. To check formatting and run lints: @@ -141,10 +143,14 @@ To fix all formatting issues automatically: $ ./scripts/format ``` -You can also check formatting directly with Gradle: +You can also check and fix all formatting directly with Gradle: ```sh -$ ./gradlew spotlessCheck # Check formatting +$ ./gradlew lint +``` + +```sh +$ ./gradlew format ``` ## Building @@ -211,7 +217,8 @@ Some useful Gradle tasks: $ ./gradlew tasks # List all available tasks $ ./gradlew build # Build all modules $ ./gradlew test # Run all tests -$ ./gradlew spotlessApply # Format code +$ ./gradlew lint # Check all code formatting +$ ./gradlew format # Format all code $ ./gradlew publishToMavenLocal # Publish to local Maven repository $ ./gradlew dependencies # Show dependency tree ``` diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 778c89de..c6dc92ec 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -10,7 +10,6 @@ repositories { } dependencies { - implementation("com.diffplug.spotless:spotless-plugin-gradle:7.0.2") implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20") implementation("com.vanniktech:gradle-maven-publish-plugin:0.28.0") } diff --git a/buildSrc/src/main/kotlin/openai.java.gradle.kts b/buildSrc/src/main/kotlin/openai.java.gradle.kts index dfbacb86..fd1d0e7a 100644 --- a/buildSrc/src/main/kotlin/openai.java.gradle.kts +++ b/buildSrc/src/main/kotlin/openai.java.gradle.kts @@ -1,24 +1,13 @@ -import com.diffplug.gradle.spotless.SpotlessExtension import org.gradle.api.tasks.testing.logging.TestExceptionFormat plugins { `java-library` - id("com.diffplug.spotless") } repositories { mavenCentral() } -configure { - java { - importOrder() - removeUnusedImports() - palantirJavaFormat() - toggleOffOn() - } -} - java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) @@ -53,3 +42,95 @@ tasks.withType().configureEach { exceptionFormat = TestExceptionFormat.FULL } } + +val palantir by configurations.creating + +dependencies { + palantir("com.palantir.javaformat:palantir-java-format:2.73.0") +} + +fun createPalantirTask(taskName: String) = tasks.registering(JavaExec::class) { + group = "Verification" + description = if (taskName == "lint") { + "Checks if Java source files need to be formatted." + } else { + "Formats Java source files." + } + + classpath = palantir + mainClass = "com.palantir.javaformat.java.Main" + + // Avoid an `IllegalAccessError` on Java 9+. + jvmArgs( + "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", + ) + + // Use paths relative to the current module. + val argumentFile = + project.layout.buildDirectory.file("palantir-$taskName-args.txt").get().asFile + val lastRunTimeFile = + project.layout.buildDirectory.file("palantir-$taskName-last-run.txt").get().asFile + + // Read the time when this task was last executed for this module (if ever). + val lastRunTime = lastRunTimeFile.takeIf { it.exists() }?.readText()?.toLongOrNull() ?: 0L + + // Use a `fileTree` relative to the module's source directory. + val javaFiles = project.fileTree("src") { include("**/*.java") } + + // Determine if any files need to be formatted or linted and continue only if there is at least + // one file. + onlyIf { javaFiles.any { it.lastModified() > lastRunTime } } + + inputs.files(javaFiles) + + doFirst { + // Create the argument file and set the preferred formatting style. + argumentFile.parentFile.mkdirs() + argumentFile.writeText("--palantir\n") + + if (taskName == "lint") { + // For lint, do a dry run, so no files are modified. Set the exit code to 1 (instead of + // the default 0) if any files need to be formatted, indicating that linting has failed. + argumentFile.appendText("--dry-run\n") + argumentFile.appendText("--set-exit-if-changed\n") + } else { + // `--dry-run` and `--replace` (for in-place formatting) are mutually exclusive. + argumentFile.appendText("--replace\n") + } + + // Write the modified files to the argument file. + javaFiles.filter { it.lastModified() > lastRunTime } + .forEach { argumentFile.appendText("${it.absolutePath}\n") } + } + + doLast { + // Record the last execution time for later up-to-date checking. + lastRunTimeFile.writeText(System.currentTimeMillis().toString()) + } + + // Pass the argument file using the @ symbol + args = listOf("@${argumentFile.absolutePath}") + + outputs.upToDateWhen { javaFiles.none { it.lastModified() > lastRunTime } } +} + +val formatJava by createPalantirTask("format") +val lintJava by createPalantirTask("lint") + +tasks.register("format") { + group = "Verification" + description = "Formats all source files." + + dependsOn(formatJava) +} + +tasks.register("lint") { + group = "Verification" + description = "Checks if any source files need to be formatted." + + dependsOn(lintJava) +} diff --git a/buildSrc/src/main/kotlin/openai.kotlin.gradle.kts b/buildSrc/src/main/kotlin/openai.kotlin.gradle.kts index 0405b0e5..fec5d5fd 100644 --- a/buildSrc/src/main/kotlin/openai.kotlin.gradle.kts +++ b/buildSrc/src/main/kotlin/openai.kotlin.gradle.kts @@ -1,4 +1,3 @@ -import com.diffplug.gradle.spotless.SpotlessExtension import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinVersion @@ -27,14 +26,81 @@ kotlin { } } -configure { - kotlin { - ktfmt().kotlinlangStyle() - toggleOffOn() - } -} - tasks.withType().configureEach { systemProperty("junit.jupiter.execution.parallel.enabled", true) systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent") } + +val ktfmt by configurations.creating + +dependencies { + ktfmt("com.facebook:ktfmt:0.56") +} + +fun createKtfmtTask(taskName: String) = tasks.registering(JavaExec::class) { + group = "Verification" + description = if (taskName == "lint") { + "Checks if Kotlin source files need to be formatted." + } else { + "Formats Kotlin source files." + } + + classpath = ktfmt + mainClass = "com.facebook.ktfmt.cli.Main" + + // Use paths relative to the current module. + val argumentFile = project.layout.buildDirectory.file("ktfmt-$taskName-args.txt").get().asFile + val lastRunTimeFile = + project.layout.buildDirectory.file("ktfmt-$taskName-last-run.txt").get().asFile + + // Read the time when this task was last executed for this module (if ever). + val lastRunTime = lastRunTimeFile.takeIf { it.exists() }?.readText()?.toLongOrNull() ?: 0L + + // Use a `fileTree` relative to the module's source directory. + val kotlinFiles = project.fileTree("src") { include("**/*.kt") } + + // Determine if any files need to be formatted or linted and continue only if there is at least + // one file (otherwise Ktfmt will fail). + onlyIf { kotlinFiles.any { it.lastModified() > lastRunTime } } + + inputs.files(kotlinFiles) + + doFirst { + // Create the argument file and set the preferred formatting style. + argumentFile.parentFile.mkdirs() + argumentFile.writeText("--kotlinlang-style\n") + + if (taskName == "lint") { + // For lint, do a dry run, so no files are modified. Set the exit code to 1 (instead of + // the default 0) if any files need to be formatted, indicating that linting has failed. + argumentFile.appendText("--dry-run\n") + argumentFile.appendText("--set-exit-if-changed\n") + } + + // Write the modified files to the argument file. + kotlinFiles.filter { it.lastModified() > lastRunTime } + .forEach { argumentFile.appendText("${it.absolutePath}\n") } + } + + doLast { + // Record the last execution time for later up-to-date checking. + lastRunTimeFile.writeText(System.currentTimeMillis().toString()) + } + + // Pass the argument file using the @ symbol + args = listOf("@${argumentFile.absolutePath}") + + outputs.upToDateWhen { kotlinFiles.none { it.lastModified() > lastRunTime } } +} + +val formatKotlin by createKtfmtTask("format") +val lintKotlin by createKtfmtTask("lint") + +// The "format" and "lint" tasks are registered by the "openai.java" plugin included above. +tasks.named("format") { + dependsOn(formatKotlin) +} + +tasks.named("lint") { + dependsOn(lintKotlin) +} diff --git a/scripts/format b/scripts/format index 456a69db..f76f2cc3 100755 --- a/scripts/format +++ b/scripts/format @@ -4,5 +4,5 @@ set -e cd "$(dirname "$0")/.." -echo "==> Running spotlessApply" -./gradlew spotlessApply +echo "==> Running code formatter" +./gradlew format