Skip to content

Commit

Permalink
Migrate to spotless (#1748)
Browse files Browse the repository at this point in the history
* Use spotless for formatting / license check

* Apply formatting

* README

* Apply to example

* google-java-format 1.8 for now

* google-java-format 1.7 for now (last supporting Java 8)
  • Loading branch information
Anuraag Agrawal authored Oct 5, 2020
1 parent eb4a18f commit ed16964
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 53 deletions.
34 changes: 17 additions & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ Make sure to review the projects [license](LICENSE) and sign the
[CNCF CLA](https://identity.linuxfoundation.org/projects/cncf). A signed CLA will be enforced by an
automatic check once you submit a PR, but you can also sign it after opening your PR.

## Style guideline

We follow the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).
Our build will fail if source code is not formatted according to that style.

To verify code style manually run the following command,
which uses [google-java-format](https://github.com/google/google-java-format) library:
## Checks

`./gradlew verifyGoogleJavaFormat`
Before submitting a PR, you should make sure the style checks and unit tests pass. You can run these
with the `check` task.

or on Windows
```bash
$ ./gradlew check
```

`gradlew.bat verifyGoogleJavaFormat`
## Style guideline

Instead of fixing style inconsistencies by hand, you can run gradle task `googleJavaFormat`
to automatically fix all found issues:
We follow the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).
Our build will fail if source code is not formatted according to that style. To fix any
style failures the above [checks](#checks) show, automatically apply the formatting with

`./gradlew googleJavaFormat`
```bash
$ ./gradlew spotlessApply
```

or on Windows
To verify code style manually run the following command,
which uses [google-java-format](https://github.com/google/google-java-format) library:

`gradlew.bat googleJavaFormat`
`./gradlew spotless`

### Pre-commit hook
To completely delegate code style formatting to the machine,
you can add [git pre-commit hook](https://git-scm.com/docs/githooks).
We provide an example script in `buildscripts/pre-commit` file.
Just copy or symlink it into `.git/hooks` folder.


### Editorconfig
As additional convenience for IntelliJ Idea users, we provide `.editorconfig` file.
Idea will automatically use it to adjust its code formatting settings.
It does not support all required rules, so you still have to run `googleJavaFormat` from time to time.
It does not support all required rules, so you still have to run `spotlessApply` from time to time.

### Javadoc

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ init-git-submodules:

.PHONY: verify-format
verify-format:
./gradlew verGJF
./gradlew spotlessCheck

.PHONY: publish-snapshots
publish-snapshots:
Expand Down
23 changes: 8 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "com.github.sherter.google-java-format" apply false
id "com.diffplug.spotless" version "5.6.1"
id "com.jfrog.artifactory" apply false
id "com.jfrog.bintray" version "1.8.4" apply false // Need *specific* version.
id "net.ltgt.errorprone" apply false
Expand Down Expand Up @@ -137,7 +137,7 @@ configure(opentelemetryProjects) {
apply plugin: 'signing'
apply plugin: 'jacoco'

apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'com.diffplug.spotless'
apply plugin: 'net.ltgt.errorprone'

repositories {
Expand Down Expand Up @@ -299,8 +299,11 @@ configure(opentelemetryProjects) {

jacoco { toolVersion = "0.8.5" }

googleJavaFormat {
toolVersion = '1.7'
spotless {
java {
googleJavaFormat("1.7")
licenseHeaderFile rootProject.file('buildscripts/spotless.license.java'), '(package|import|class|// Includes work from:)'
}
}

configurations {
Expand Down Expand Up @@ -363,17 +366,7 @@ configure(opentelemetryProjects) {
addBooleanOption('Xdoclint:all,-missing', true)
}

afterEvaluate { // Allow subproject to add more source sets.
tasks.googleJavaFormat {
source = sourceSets*.allJava
include '**/*.java'
}

tasks.verifyGoogleJavaFormat {
source = sourceSets*.allJava
include '**/*.java'
}

afterEvaluate {
jar {
inputs.property("moduleName", moduleName)

Expand Down
4 changes: 0 additions & 4 deletions buildscripts/checkstyle.license

This file was deleted.

6 changes: 0 additions & 6 deletions buildscripts/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@

<property name="severity" value="error"/>


<module name="RegexpHeader">
<property name="headerFile" value="${rootDir}/buildscripts/checkstyle.license"/>
<property name="fileExtensions" value="java"/>
</module>

<property name="fileExtensions" value="java, properties, xml"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
Expand Down
2 changes: 1 addition & 1 deletion buildscripts/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env sh

./gradlew googleJavaFormat
./gradlew spotlessApply
5 changes: 5 additions & 0 deletions buildscripts/spotless.license.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

12 changes: 6 additions & 6 deletions examples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
plugins {
id 'com.github.sherter.google-java-format' apply false
id "com.jfrog.artifactory" apply false
id "com.diffplug.spotless"
}

subprojects {
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'com.diffplug.spotless'

group = "io.opentelemetry"
version = "0.1.0-SNAPSHOT"
Expand All @@ -29,10 +28,11 @@ subprojects {
}
}

googleJavaFormat {
toolVersion = '1.7'
spotless {
java {
googleJavaFormat("1.7")
}
}
compileJava.dependsOn(verifyGoogleJavaFormat)

//create a single Jar with all dependencies
task fatJar(type: Jar) {
Expand Down
3 changes: 1 addition & 2 deletions examples/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pluginManagement {
plugins {
id 'com.github.sherter.google-java-format' version "0.8"
id "com.jfrog.artifactory" version "4.13.0"
id "com.diffplug.spotless" version "5.6.1"
id 'com.google.protobuf' version '0.8.8'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

// Includes work from:
/*
* Copyright 2010, Guava Authors
* SPDX-License-Identifier: Apache-2.0
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pluginManagement {
plugins {
id "com.diffplug.spotless" version "5.6.1"
id "com.github.ben-manes.versions" version "0.20.0"
id "com.github.kt3k.coveralls" version "2.0.1"
id 'com.github.sherter.google-java-format' version "0.8"
id "com.google.protobuf" version "0.8.8"
id "com.jfrog.artifactory" version "4.13.0"
id "io.morethan.jmhreport" version "0.9.0"
Expand Down

0 comments on commit ed16964

Please sign in to comment.