Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 11 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down Expand Up @@ -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
```
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
103 changes: 92 additions & 11 deletions buildSrc/src/main/kotlin/openai.java.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<SpotlessExtension> {
java {
importOrder()
removeUnusedImports()
palantirJavaFormat()
toggleOffOn()
}
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
Expand Down Expand Up @@ -53,3 +42,95 @@ tasks.withType<Test>().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)
}
82 changes: 74 additions & 8 deletions buildSrc/src/main/kotlin/openai.kotlin.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import com.diffplug.gradle.spotless.SpotlessExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

Expand Down Expand Up @@ -27,14 +26,81 @@ kotlin {
}
}

configure<SpotlessExtension> {
kotlin {
ktfmt().kotlinlangStyle()
toggleOffOn()
}
}

tasks.withType<Test>().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)
}
4 changes: 2 additions & 2 deletions scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ set -e

cd "$(dirname "$0")/.."

echo "==> Running spotlessApply"
./gradlew spotlessApply
echo "==> Running code formatter"
./gradlew format
Loading