Skip to content

Commit 6725217

Browse files
author
Quintin Willison
authored
Merge pull request ably#624 from ably/feature/gradle-conform-reformat-202010
Gradle conform and reformat
2 parents 6e7f5b8 + bd219a9 commit 6725217

12 files changed

Lines changed: 194 additions & 88 deletions

File tree

.editorconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ charset = utf-8
99
indent_style = space
1010
indent_size = 2
1111

12-
[*.java]
13-
indent_style = space
12+
[*.{java,groovy,gradle}]
1413
indent_size = 4
1514

1615
[*.md]

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,29 @@ The Android-specific library AAR is built with:
489489

490490
## Code Standard
491491

492-
We are introducing [Checkstyle](https://checkstyle.org/) to enforce code style and spot for transgressions and illogical constructs.
492+
### Checkstyle
493+
494+
We use [Checkstyle](https://checkstyle.org/) to enforce code style and spot for transgressions and illogical constructs
495+
in our Java source files.
493496
The Gradle build has been configured to run these on `java:assembleRelease`.
494497
It does not run for the Android build yet.
495498

496499
You can run just the Checkstyle rules on their own using:
497500

498501
./gradlew checkstyleMain
499502

503+
### CodeNarc
504+
505+
We use [CodeNarc](https://codenarc.org/) to enforce code style in our Gradle build scripts, which are all written in Groovy.
506+
507+
You can run CodeNarc over all build scripts in this repository using:
508+
509+
./gradlew checkWithCodenarc
510+
511+
For more details see the [`gradle-lint`](gradle-lint) project.
512+
513+
### IDE Support
514+
500515
We have a root [`.editorconfig`](.editorconfig) file, supporting [EditorConfig](https://editorconfig.org/), which should be of assistance within most IDEs. e.g.:
501516

502517
- [VS Code](https://code.visualstudio.com/) using the [EditorConfig plugin](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ android {
5858
main {
5959
java {
6060
srcDirs = ['src/main/java', '../lib/src/main/java']
61-
}
61+
}
6262
}
6363
androidTest {
6464
java {

android/maven.gradle

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ def mavenPassword = hasProperty('ossrhPassword') ? ossrhPassword : ''
1111
* Task which signs and uploads the Android artifacts to Nexus OSSRH.
1212
*/
1313
uploadArchives {
14-
signing {
15-
sign configurations.archives
16-
}
14+
signing {
15+
sign configurations.archives
16+
}
1717
repositories.mavenDeployer {
1818
logger.lifecycle('OSSRH auth with username: ' + mavenUser)
1919

20-
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
20+
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
2121

22-
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
22+
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
2323
authentication(userName: mavenUser, password: mavenPassword)
24-
}
24+
}
2525

26-
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
26+
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
2727
authentication(userName: mavenUser, password: mavenPassword)
28-
}
28+
}
2929
pom.groupId = groupId
3030
pom.artifactId = artifactId
3131
pom.version = version
@@ -36,16 +36,16 @@ uploadArchives {
3636
description 'An Android Realtime and REST client library for [Ably.io](https://www.ably.io), the realtime messaging service.'
3737
packaging 'aar'
3838
inceptionYear '2015'
39-
url 'https://www.github.com/ably/ably-java'
40-
developers {
41-
developer {
42-
name 'Paddy Byers'
43-
email 'paddy@ably.io'
44-
url 'https://github.com/paddybyers'
45-
id 'paddybyers'
46-
}
47-
}
48-
scm {
39+
url 'https://www.github.com/ably/ably-java'
40+
developers {
41+
developer {
42+
name 'Paddy Byers'
43+
email 'paddy@ably.io'
44+
url 'https://github.com/paddybyers'
45+
id 'paddybyers'
46+
}
47+
}
48+
scm {
4949
url 'scm:git:https://github.com/ably/ably-java'
5050
connection 'scm:git:https://github.com/ably/ably-java'
5151
developerConnection 'scm:git:git@github.com:ably/ably-java'
@@ -67,24 +67,24 @@ uploadArchives {
6767
}
6868
}
6969

70-
pom.whenConfigured {
71-
p -> p.dependencies = p.dependencies.findAll {
72-
// Exclude dependency on lib subproject.
70+
pom.whenConfigured { p ->
71+
p.dependencies = p.dependencies.findAll {
72+
// Exclude dependency on lib subproject.
7373
dep -> dep.artifactId != "lib"
7474
}.findAll {
75-
// Exclude Google services since we don't want to impose a particular
76-
// version on users. Ideally we would specify a version range,
77-
// but the Google services Gradle plugin doesn't seem to
78-
// support that.
79-
// TODO: Make sure this works when installing from Maven!
75+
// Exclude Google services since we don't want to impose a particular
76+
// version on users. Ideally we would specify a version range,
77+
// but the Google services Gradle plugin doesn't seem to
78+
// support that.
79+
// TODO: Make sure this works when installing from Maven!
8080
dep -> dep.artifactId != 'play-services-gcm' && dep.artifactId != 'firebase-messaging'
8181
}
8282
}
8383

84-
// Export to local Maven cache
84+
// Export to local Maven cache
8585
// COMMENT OUT THIS LINE AND THE ONE BELOW IN ORDER TO RELEASE TO SONATYPE NEXUS STAGING
8686
// TODO https://github.com/ably/ably-java/issues/566
87-
repository(url: repositories.mavenLocal().url)
87+
repository(url: repositories.mavenLocal().url)
8888

8989
// Export files to local storage
9090
// COMMENT OUT THIS LINE AND THE ONE ABOVE IN ORDER TO RELEASE TO SONATYPE NEXUS STAGING
@@ -112,25 +112,25 @@ tasks.whenTaskAdded { task ->
112112
}
113113

114114
task sourcesJar(type: Jar) {
115-
classifier = 'sources'
116-
from android.sourceSets.main.java.srcDirs
115+
classifier = 'sources'
116+
from android.sourceSets.main.java.srcDirs
117117
}
118118

119119
task javadoc(type: Javadoc) {
120-
source = android.sourceSets.main.java.srcDirs
121-
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
122-
failOnError false
120+
source = android.sourceSets.main.java.srcDirs
121+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
122+
failOnError false
123123
}
124124

125125
afterEvaluate {
126-
javadoc.classpath += files(android.libraryVariants.collect { variant ->
127-
variant.javaCompile.classpath.files
128-
})
126+
javadoc.classpath += files(android.libraryVariants.collect { variant ->
127+
variant.javaCompile.classpath.files
128+
})
129129
}
130130

131131
task javadocJar(type: Jar, dependsOn: javadoc) {
132-
classifier = 'javadoc'
133-
from javadoc.destinationDir
132+
classifier = 'javadoc'
133+
from javadoc.destinationDir
134134
}
135135

136136
artifacts {

build.gradle

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

3+
plugins {
4+
id 'io.codearte.nexus-staging' version '0.21.1'
5+
}
6+
37
repositories {
48
google()
9+
mavenCentral()
510
}
6-
buildscript {
7-
repositories {
8-
google()
9-
mavenCentral()
10-
}
11-
dependencies {
12-
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.21.1'
13-
}
14-
}
15-
apply plugin: 'io.codearte.nexus-staging'
1611

1712
nexusStaging {
18-
packageGroup = "io.ably"
13+
packageGroup = "io.ably"
1914
}

common.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
repositories {
2-
jcenter()
3-
mavenCentral()
2+
jcenter()
3+
mavenCentral()
44
}
55

66
group = 'io.ably'
77
version = '1.2.2'
88
description = """Ably java client library"""
99

1010
tasks.withType(Javadoc) {
11-
// To prevent javadoc warnings with Java 8
12-
options.addStringOption('Xdoclint:none', '-quiet')
11+
// To prevent javadoc warnings with Java 8
12+
options.addStringOption('Xdoclint:none', '-quiet')
1313
}

dependencies.gradle

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// These dependencies have to be in lib/build.gradle for compilation _and_
22
// in java/build.gradle and android/build.gradle for maven.
33
dependencies {
4-
implementation 'org.msgpack:msgpack-core:0.8.11'
5-
implementation 'org.java-websocket:Java-WebSocket:1.4.0'
6-
implementation 'com.google.code.gson:gson:2.8.6'
7-
implementation 'com.davidehrmann.vcdiff:vcdiff-core:0.1.1'
8-
testImplementation 'org.hamcrest:hamcrest-all:1.3'
9-
testImplementation 'junit:junit:4.12'
10-
testImplementation 'org.nanohttpd:nanohttpd:2.3.0'
11-
testImplementation 'org.nanohttpd:nanohttpd-nanolets:2.3.0'
12-
testImplementation 'org.nanohttpd:nanohttpd-websocket:2.3.0'
13-
testImplementation 'org.mockito:mockito-core:1.10.19'
14-
testImplementation 'net.jodah:concurrentunit:0.4.2'
15-
testImplementation 'org.slf4j:slf4j-simple:1.7.30'
4+
implementation 'org.msgpack:msgpack-core:0.8.11'
5+
implementation 'org.java-websocket:Java-WebSocket:1.4.0'
6+
implementation 'com.google.code.gson:gson:2.8.6'
7+
implementation 'com.davidehrmann.vcdiff:vcdiff-core:0.1.1'
8+
testImplementation 'org.hamcrest:hamcrest-all:1.3'
9+
testImplementation 'junit:junit:4.12'
10+
testImplementation 'org.nanohttpd:nanohttpd:2.3.0'
11+
testImplementation 'org.nanohttpd:nanohttpd-nanolets:2.3.0'
12+
testImplementation 'org.nanohttpd:nanohttpd-websocket:2.3.0'
13+
testImplementation 'org.mockito:mockito-core:1.10.19'
14+
testImplementation 'net.jodah:concurrentunit:0.4.2'
15+
testImplementation 'org.slf4j:slf4j-simple:1.7.30'
1616
}

gradle-lint/build.gradle

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
plugins {
2+
id 'codenarc'
3+
4+
// This project needs the groovy plugin so that it will recognise groovy source sets,
5+
// which is a requirement of using the codenarc plugin.
6+
id 'groovy'
7+
}
8+
9+
repositories {
10+
jcenter()
11+
}
12+
13+
sourceSets {
14+
// delegate: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetContainer.html
15+
// a.k.a. NamedDomainObjectContainer<SourceSet>
16+
// TODO: find canonical reference for how the named domain object container translates to Groovy Gradle DSL
17+
// and document that here for future reference.
18+
// But, basically, gradleScripts is a name we've made up for this source set. User-defined, if you will.
19+
20+
/**
21+
* Ably-defined name for the source set that encapsulates all Gradle build script files in this repository.
22+
* The CodeNarc plugin creates an implicit task called codenarcGradleScripts, based on this name, however
23+
* that tasks doesn't pick up the .gradle files. Presumably it filters to process only files with a .groovy
24+
* extension. This is why we are invoking CodeNarc by extending the CodeNarc task - see checkWithCodenarc.
25+
*/
26+
gradleScripts {
27+
// groovy is a method added by the groovy plugin
28+
groovy {
29+
// delegate: https://docs.gradle.org/current/dsl/org.gradle.api.file.SourceDirectorySet.html
30+
// srcDir "${project.rootProject.files('.')}"
31+
srcDir '..'
32+
include '**/*.gradle'
33+
exclude '**/bin/**'
34+
}
35+
}
36+
}
37+
38+
// https://docs.gradle.org/current/dsl/org.gradle.api.plugins.quality.CodeNarcExtension.html
39+
// We could move configFile to the configuration phase of our runCodenarc task and that works ok.
40+
// However, reportFormat is not understood there and the reports block that is documented for that block's
41+
// delegate doesn't seem to have an obvious way to generate output to the console.
42+
// For this reason the decision has been made to configure the overall nature for CodeNarc in this block,
43+
// while the specification of which files it lints is made in our ablyCodenarc task.
44+
codenarc {
45+
configFile = file('src/main/groovy/build-script-rules.groovy')
46+
reportFormat 'console'
47+
}
48+
49+
dependencies {
50+
// To use the groovy plugin we have to tell it where to get Groovy from.
51+
// As this project is checking build scripts used by Gradle projects in this repository,
52+
// including this project, the local Groovy instance is exactly what we need.
53+
implementation localGroovy()
54+
}
55+
56+
/**
57+
* This task is informational, here to help with debugging issues with our use of codenarc.
58+
*/
59+
tasks.register('listBuildScriptsCheckedWithCodenarc') {
60+
group = 'Ably'
61+
description = 'Lists the build script files which will be checked by the checkWithCodenarc task.'
62+
63+
doLast {
64+
sourceSets.each { sourceSet ->
65+
println("$sourceSet:")
66+
boolean noSourceFound = true
67+
sourceSet.allSource.each { file ->
68+
println("\t$file")
69+
noSourceFound = false
70+
}
71+
if (noSourceFound) {
72+
println('\tNo source files captured by this source set.')
73+
}
74+
}
75+
}
76+
}
77+
78+
tasks.register('checkWithCodenarc', CodeNarc) { cn ->
79+
// delegate: https://docs.gradle.org/current/dsl/org.gradle.api.plugins.quality.CodeNarc.html
80+
group = 'Ably'
81+
description = 'Checks the build script files in this repository using CodeNarc.'
82+
83+
sourceSets.each { sourceSet ->
84+
cn.source sourceSet.allSource
85+
}
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ruleset {
2+
ruleset('rulesets/formatting.xml') {
3+
// This rule requires a space before the colon, which is unnecessary and less human-readable - especially in our JSON-rich world.
4+
exclude 'SpaceAroundMapEntryColon'
5+
6+
// Arbitrary, ending up forcing developers to contort code unecessarily - especially for string definitions.
7+
exclude 'LineLength'
8+
}
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)