Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.gradle: selectively run spotless #1370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 37 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ spotless {
// cleanthat()
// formatAnnotations()

// Use a different clang-format binary with -PclangFormat=clang-format-15.
def clangFormatBinary = (String) project.findProperty('clangFormat') ?: 'clang-format'
def clangOutput = new ByteArrayOutputStream()
exec {
commandLine clangFormatBinary, '--version'
standardOutput = clangOutput
if (clangVersionCheck.enabled) {
def clangVersion = (String) getProject().ext.get("clangVersion")
def clangFormatBinary = (String) getProject().ext.get("clangFormatBinary")
// FIXME: enable clangFormat in the future.
// clangFormat(clangVersion).pathToExe(clangFormatBinary).style('file')
}
def clangVersion = (String) (clangOutput.toString() =~ /\d+\.\d+\.\d+/)[0]
// FIXME: enable clangFormat in the future.
// clangFormat(clangVersion).pathToExe(clangFormatBinary).style('file')
}
}

Expand Down Expand Up @@ -97,6 +93,38 @@ application {
'--enable-preview', '--enable-native-access', 'ALL-UNNAMED']
}

tasks.register('clangVersionCheck', Exec) {
description = "Detect the version of clang-format"
group = "Help"
// Use a different clang-format binary with -PclangFormat=clang-format-15.
def bin = (String) project.findProperty('clangFormat') ?: 'clang-format'
def clangOutput = new ByteArrayOutputStream()
exec {
commandLine bin, '--version'
standardOutput = clangOutput
}
// Tasks do not have return values, write info to project.ext properties instead.
project.ext.set("clangFormatBinary", bin)
def ver = (String) (clangOutput.toString() =~ /\d+\.\d+\.\d+/)[0]
project.ext.set("clangVersion", ver)
}

tasks.withType(com.diffplug.gradle.spotless.SpotlessTask).configureEach {
dependsOn clangVersionCheck
}

clangVersionCheck.configure {
onlyIf("Task should only run once") { !project.ext.has("clangFormatBinary") }
enabled = false
// Check the argument to gradle, enable task for build and spotless targets.
getGradle().getStartParameter().getTaskNames().each {
def taskName = it.toString()
if (taskName.equals("build") || taskName.startsWith("spotless") || taskName.startsWith(":spotless")) {
enabled = true
}
}
}

tasks.withType(JavaCompile).configureEach {
// ErrorProne is slow, only enable with ./gradlew build -Perrorprone.
options.errorprone.enabled = project.hasProperty('errorprone')
Expand Down