|
1 |
| -import com.diffplug.gradle.spotless.SpotlessExtension |
2 | 1 | import org.gradle.api.tasks.testing.logging.TestExceptionFormat
|
3 | 2 |
|
4 | 3 | plugins {
|
5 | 4 | `java-library`
|
6 |
| - id("com.diffplug.spotless") |
7 | 5 | }
|
8 | 6 |
|
9 | 7 | repositories {
|
10 | 8 | mavenCentral()
|
11 | 9 | }
|
12 | 10 |
|
13 |
| -configure<SpotlessExtension> { |
14 |
| - java { |
15 |
| - importOrder() |
16 |
| - removeUnusedImports() |
17 |
| - palantirJavaFormat() |
18 |
| - toggleOffOn() |
19 |
| - } |
20 |
| -} |
21 |
| - |
22 | 11 | java {
|
23 | 12 | toolchain {
|
24 | 13 | languageVersion.set(JavaLanguageVersion.of(21))
|
@@ -53,3 +42,86 @@ tasks.withType<Test>().configureEach {
|
53 | 42 | exceptionFormat = TestExceptionFormat.FULL
|
54 | 43 | }
|
55 | 44 | }
|
| 45 | + |
| 46 | +val palantir by configurations.creating |
| 47 | +dependencies { |
| 48 | + palantir("com.palantir.javaformat:palantir-java-format:2.73.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.") |
0 commit comments