LUCENE-9564: add spotless and gjf (automatic code formatter)

This commit is contained in:
Dawid Weiss 2020-12-17 13:11:54 +01:00 committed by GitHub
parent 2eeec2f6be
commit c94b035df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 0 deletions

View File

@ -26,6 +26,7 @@ plugins {
id "de.undercouch.download" version "4.0.2" apply false id "de.undercouch.download" version "4.0.2" apply false
id "net.ltgt.errorprone" version "1.2.1" apply false id "net.ltgt.errorprone" version "1.2.1" apply false
id "com.palantir.docker" version "0.25.0" apply false id "com.palantir.docker" version "0.25.0" apply false
id 'com.diffplug.spotless' version "5.8.2" apply false
} }
apply from: file('gradle/defaults.gradle') apply from: file('gradle/defaults.gradle')
@ -145,6 +146,7 @@ apply from: file('gradle/validation/ecj-lint.gradle')
apply from: file('gradle/validation/gradlew-scripts-tweaked.gradle') apply from: file('gradle/validation/gradlew-scripts-tweaked.gradle')
apply from: file('gradle/validation/validate-log-calls.gradle') apply from: file('gradle/validation/validate-log-calls.gradle')
apply from: file('gradle/validation/check-broken-links.gradle') apply from: file('gradle/validation/check-broken-links.gradle')
apply from: file('gradle/validation/spotless.gradle')
// Source or data regeneration tasks // Source or data regeneration tasks
apply from: file('gradle/generation/jflex.gradle') apply from: file('gradle/generation/jflex.gradle')

View File

@ -22,6 +22,7 @@ configure(rootProject) {
["Workflow", "help/workflow.txt", "Typical workflow commands."], ["Workflow", "help/workflow.txt", "Typical workflow commands."],
["Ant", "help/ant.txt", "Ant-gradle migration help."], ["Ant", "help/ant.txt", "Ant-gradle migration help."],
["Tests", "help/tests.txt", "Tests, filtering, beasting, etc."], ["Tests", "help/tests.txt", "Tests, filtering, beasting, etc."],
["Formatting", "help/formatting.txt", "Code formatting conventions."],
["Jvms", "help/jvms.txt", "Using alternative or EA JVM toolchains."], ["Jvms", "help/jvms.txt", "Using alternative or EA JVM toolchains."],
["Deps", "help/dependencies.txt", "Declaring, inspecting and excluding dependencies."], ["Deps", "help/dependencies.txt", "Declaring, inspecting and excluding dependencies."],
["ForbiddenApis", "help/forbiddenApis.txt", "How to add/apply rules for forbidden APIs."], ["ForbiddenApis", "help/forbiddenApis.txt", "How to add/apply rules for forbidden APIs."],

View File

@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* LUCENE-9564: This adds automatic (and enforced) code formatting.
*/
def resources = scriptResources(buildscript)
allprojects { prj ->
plugins.withType(JavaPlugin) {
prj.apply plugin: 'com.diffplug.spotless'
spotless {
java {
licenseHeaderFile file("${resources}/asl-header.txt"), '^(\\s*package)'
lineEndings 'UNIX'
endWithNewline()
googleJavaFormat('1.9')
switch (project.path) {
// Disable for everything else for now.
default:
target 'non-existing/**'
break
}
}
}
}
}
// Add an alias to 'spotlessApply' simply called 'tidy' and add
// spotlessCheck to check.
allprojects { prj ->
task tidy() {
description "Applies formatters and cleanups to sources."
group "verification"
}
tasks.matching { task -> task.name == "spotlessApply" }.configureEach { v ->
tidy.dependsOn v
}
tasks.matching { task -> task.name == "spotlessCheck" }.configureEach { v ->
check.dependsOn v
}
}

View File

@ -0,0 +1,16 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

20
help/formatting.txt Normal file
View File

@ -0,0 +1,20 @@
Code formatting
===============
Starting with (LUCENE-9564) Java code is enforced to comply with
google-java-format conventions. In theory you shouldn't worry about
what the convention actually looks like - write the code in any way
you like and then run:
./gradlew tidy
prior to running your regular precommit checks. This will reformat
your code so that it complies with the convention and passes gradle
'check' task.
IMPORTANT: There is *no* way to mark sections of the code as excluded
from formatting. This is by design and cannot be altered. In vast
majority of cases the formatter will do a great job of cleaning up the
code. Occasionally you may want to rewrite the code (introduce a local
variable orreshape code paths) so that it's easier to read after
automatic formatting.