-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
149 lines (126 loc) · 5.09 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
149 lines (126 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.net.URL
import java.nio.file.Files
import java.nio.file.StandardCopyOption
val projectVersion: String by project
allprojects {
group = "com.github.fingerprintjs"
version = projectVersion
}
plugins {
alias(libs.plugins.google.osdetector)
}
// Register formatting tasks for each subproject
val googleJavaFormatExeFile = layout.buildDirectory.file("google-java-format.exe")
val googleJavaFormatOsSuffixMap = mapOf(
"osx" to "darwin",
"linux" to "linux",
"windows" to "windows",
)
val googleJavaFormatArchSuffixMap = mapOf(
"x86_64" to "x86-64",
"aarch_64" to "arm64"
)
val googleJavaFormatBinarySuffixMap = mapOf(
"osx" to "",
"linux" to "",
"windows" to ".exe"
)
tasks.register("downloadGoogleJavaFormat") {
val osSuffix = googleJavaFormatOsSuffixMap[osdetector.os]
val archSuffix = googleJavaFormatArchSuffixMap[osdetector.arch]
val binarySuffix = googleJavaFormatBinarySuffixMap[osdetector.os]
val googleJavaFormatVersion = libs.versions.google.java.format.get()
val downloadUrl = "https://github.com/google/google-java-format/releases/download/v$googleJavaFormatVersion/google-java-format_${osSuffix}-${archSuffix}${binarySuffix}"
outputs.file(googleJavaFormatExeFile)
onlyIf {
System.getenv("JITPACK")?.lowercase() != "true"
}
doLast {
val file = googleJavaFormatExeFile.get().asFile
if (!file.exists()) {
println("Downloading google-java-format for $osSuffix-$archSuffix")
URL(downloadUrl).openStream().use { input ->
Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
file.setExecutable(true)
} else {
println("google-java-format already downloaded")
}
}
}
fun Project.registerFormatTasks() {
// Resolve lazily: files may be deleted/regenerated between configuration and execution
// (e.g., openApiGenerate removes and rewrites sources before format runs).
fun srcJavaFiles(): List<String> = fileTree("src") {
include("**/*.java")
}.files.map { it.absolutePath }
tasks.register("format") {
dependsOn(rootProject.tasks.named("downloadGoogleJavaFormat"))
onlyIf {
System.getenv("JITPACK")?.lowercase() != "true"
}
doLast {
val inputFiles = srcJavaFiles()
// First, fix the imports
exec {
executable = googleJavaFormatExeFile.get().asFile.toPath().toString()
args = listOf("--replace", "--fix-imports-only") + inputFiles
}
// Second, format the code. This ensures that the removal of unused imports does not
// introduce the need to run another formatting pass to pass the formatting checks.
exec {
executable = googleJavaFormatExeFile.get().asFile.toPath().toString()
args = listOf("--replace", "--skip-javadoc-formatting", "--skip-reflowing-long-strings") + inputFiles
}
}
}
tasks.register("checkFormat") {
dependsOn(rootProject.tasks.named("downloadGoogleJavaFormat"))
doLast {
println("The following source files need to be formatted:")
exec {
executable = googleJavaFormatExeFile.get().asFile.toPath().toString()
args = listOf("--dry-run", "--set-exit-if-changed", "--skip-javadoc-formatting", "--skip-reflowing-long-strings") + srcJavaFiles()
}
}
}
}
subprojects {
registerFormatTasks()
plugins.withType<org.gradle.api.plugins.JavaPlugin> {
tasks.withType<JavaCompile> {
// Target java 11
options.release = 11
// Enable all warnings and fail the build for any warnings
options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror"))
}
// Print Java toolchain for compilation
tasks.withType<JavaCompile>().configureEach {
doFirst {
println("Compiling with Java: ${javaCompiler.get().metadata.installationPath} (version: ${javaCompiler.get().metadata.languageVersion})")
}
}
// Print Java toolchain used for running tests
tasks.withType<Test>().configureEach {
doFirst {
val launcher = javaLauncher.orNull
if (launcher != null) {
println("Testing with Java: ${launcher.metadata.installationPath} (version: ${launcher.metadata.languageVersion})")
} else {
println("Testing with Java: launcher not set")
}
}
}
// Print Java toolchain used for running
tasks.withType<JavaExec>().configureEach {
doFirst {
val launcher = javaLauncher.orNull
if (launcher != null) {
println("Running JavaExec with Java: ${launcher.metadata.installationPath} (version: ${launcher.metadata.languageVersion})")
} else {
println("Running JavaExec with Java: launcher not set")
}
}
}
}
}