| buildscript { |
| repositories { |
| maven { |
| url "https://plugins.gradle.org/m2/" |
| } |
| } |
| dependencies { |
| classpath 'com.google.googlejavaformat:google-java-format:1.6' |
| } |
| } |
| |
| apply plugin: JavaFormatterPlugin |
| |
| check.dependsOn verifyJava |
| |
| /* |
| * 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. |
| */ |
| |
| import com.google.googlejavaformat.java.Formatter |
| import com.google.googlejavaformat.java.ImportOrderer |
| import com.google.googlejavaformat.java.JavaFormatterOptions |
| import com.google.googlejavaformat.java.Main |
| import com.google.googlejavaformat.java.RemoveUnusedImports |
| |
| class JavaFormatterPlugin implements Plugin<Project> { |
| void apply(Project project) { |
| project.task('formatJava') { |
| doLast { |
| Main formatter = new Main(new PrintWriter(System.out, true), new PrintWriter(System.err, true), System.in) |
| Project rootProject = project.getRootProject() |
| for (item in project.sourceSets) { |
| for (File file : item.getAllSource()) { |
| if (!file.getName().endsWith(".java") || file.getAbsolutePath().contains("generated-src")) { |
| continue |
| } |
| if (formatter.format("-a", "-i", file.getAbsolutePath()) != 0) { |
| throw new GradleException("Format java failed: " + file.getAbsolutePath()) |
| } |
| } |
| } |
| } |
| } |
| |
| project.task('verifyJava') { |
| doLast { |
| def options = JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.AOSP).build() |
| Formatter formatter = new Formatter(options) |
| Project rootProject = project.getRootProject() |
| for (item in project.sourceSets) { |
| for (File file : item.getAllSource()) { |
| if (!file.getName().endsWith(".java") || file.getAbsolutePath().contains("generated-src")) { |
| continue |
| } |
| |
| String src = new String(file.bytes, "UTF-8") |
| String formatted = formatter.formatSource(src) |
| formatted = RemoveUnusedImports.removeUnusedImports(formatted, RemoveUnusedImports.JavadocOnlyImports.KEEP) |
| formatted = ImportOrderer.reorderImports(formatted); |
| if (!src.equals(formatted)) { |
| throw new GradleException("File not formatted: " + file.getAbsolutePath() |
| + System.lineSeparator() |
| + "In order to reformat your code, run './gradlew formatJava' (or './gradlew fJ' for short)" |
| + System.lineSeparator() |
| + "See https://github.com/deepjavalibrary/djl/blob/master/docs/development/development_guideline.md#coding-conventions for more details") |
| } |
| } |
| } |
| } |
| } |
| } |
| } |