openai/openai-java
Publicmirrored from https://github.com/openai/openai-javaAvailable
buildSrc/src/main/kotlin/openai.java.gradle.kts
127lines · modecode
| 1 | import org.gradle.api.tasks.testing.logging.TestExceptionFormat |
| 2 | |
| 3 | plugins { |
| 4 | `java-library` |
| 5 | } |
| 6 | |
| 7 | repositories { |
| 8 | mavenCentral() |
| 9 | } |
| 10 | |
| 11 | java { |
| 12 | toolchain { |
| 13 | languageVersion.set(JavaLanguageVersion.of(21)) |
| 14 | } |
| 15 | |
| 16 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 17 | targetCompatibility = JavaVersion.VERSION_1_8 |
| 18 | } |
| 19 | |
| 20 | tasks.withType<JavaCompile>().configureEach { |
| 21 | options.compilerArgs.add("-Werror") |
| 22 | options.release.set(8) |
| 23 | } |
| 24 | |
| 25 | tasks.named<Jar>("jar") { |
| 26 | manifest { |
| 27 | attributes(mapOf( |
| 28 | "Implementation-Title" to project.name, |
| 29 | "Implementation-Version" to project.version |
| 30 | )) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | tasks.withType<Test>().configureEach { |
| 35 | useJUnitPlatform() |
| 36 | |
| 37 | // Run tests in parallel to some degree. |
| 38 | maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1) |
| 39 | forkEvery = 100 |
| 40 | |
| 41 | testLogging { |
| 42 | exceptionFormat = TestExceptionFormat.FULL |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | val palantir by configurations.creating |
| 47 | dependencies { |
| 48 | palantir("com.palantir.javaformat:palantir-java-format:2.89.0") |
| 49 | } |
| 50 | |
| 51 | fun registerPalantir( |
| 52 | name: String, |
| 53 | description: String, |
| 54 | ) { |
| 55 | val javaName = "${name}Java" |
| 56 | tasks.register<JavaExec>(javaName) { |
| 57 | group = "Verification" |
| 58 | this.description = description |
| 59 | |
| 60 | classpath = palantir |
| 61 | mainClass = "com.palantir.javaformat.java.Main" |
| 62 | |
| 63 | // Avoid an `IllegalAccessError` on Java 9+. |
| 64 | jvmArgs( |
| 65 | "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", |
| 66 | "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", |
| 67 | "--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", |
| 68 | "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", |
| 69 | "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", |
| 70 | ) |
| 71 | |
| 72 | // Use paths relative to the current module. |
| 73 | val argumentFile = |
| 74 | project.layout.buildDirectory.file("palantir-$name-args.txt").get().asFile |
| 75 | val lastRunTimeFile = |
| 76 | project.layout.buildDirectory.file("palantir-$name-last-run.txt").get().asFile |
| 77 | |
| 78 | // Read the time when this task was last executed for this module (if ever). |
| 79 | val lastRunTime = lastRunTimeFile.takeIf { it.exists() }?.readText()?.toLongOrNull() ?: 0L |
| 80 | |
| 81 | // Use a `fileTree` relative to the module's source directory. |
| 82 | val javaFiles = project.fileTree("src") { include("**/*.java") } |
| 83 | |
| 84 | // Determine if any files need to be formatted or linted and continue only if there is at least |
| 85 | // one file. |
| 86 | onlyIf { javaFiles.any { it.lastModified() > lastRunTime } } |
| 87 | |
| 88 | inputs.files(javaFiles) |
| 89 | |
| 90 | doFirst { |
| 91 | // Create the argument file and set the preferred formatting style. |
| 92 | argumentFile.parentFile.mkdirs() |
| 93 | argumentFile.writeText("--palantir\n") |
| 94 | |
| 95 | if (name == "lint") { |
| 96 | // For lint, do a dry run, so no files are modified. Set the exit code to 1 (instead of |
| 97 | // the default 0) if any files need to be formatted, indicating that linting has failed. |
| 98 | argumentFile.appendText("--dry-run\n") |
| 99 | argumentFile.appendText("--set-exit-if-changed\n") |
| 100 | } else { |
| 101 | // `--dry-run` and `--replace` (for in-place formatting) are mutually exclusive. |
| 102 | argumentFile.appendText("--replace\n") |
| 103 | } |
| 104 | |
| 105 | // Write the modified files to the argument file. |
| 106 | javaFiles.filter { it.lastModified() > lastRunTime } |
| 107 | .forEach { argumentFile.appendText("${it.absolutePath}\n") } |
| 108 | } |
| 109 | |
| 110 | doLast { |
| 111 | // Record the last execution time for later up-to-date checking. |
| 112 | lastRunTimeFile.writeText(System.currentTimeMillis().toString()) |
| 113 | } |
| 114 | |
| 115 | // Pass the argument file using the @ symbol |
| 116 | args = listOf("@${argumentFile.absolutePath}") |
| 117 | |
| 118 | outputs.upToDateWhen { javaFiles.none { it.lastModified() > lastRunTime } } |
| 119 | } |
| 120 | |
| 121 | tasks.named(name) { |
| 122 | dependsOn(tasks.named(javaName)) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | registerPalantir(name = "format", description = "Formats all Java source files.") |
| 127 | registerPalantir(name = "lint", description = "Verifies all Java source files are formatted.") |