|
| 1 | +/* |
| 2 | +* SPDX-License-Identifier: Apache-2.0 |
| 3 | +* |
| 4 | +* The OpenSearch Contributors require contributions made to |
| 5 | +* this file be licensed under the Apache-2.0 license or a |
| 6 | +* compatible open source license. |
| 7 | +* |
| 8 | +* Modifications Copyright OpenSearch Contributors. See |
| 9 | +* GitHub history for details. |
| 10 | +*/ |
| 11 | + |
| 12 | +/* |
| 13 | + * Licensed to Elasticsearch under one or more contributor |
| 14 | + * license agreements. See the NOTICE file distributed with |
| 15 | + * this work for additional information regarding copyright |
| 16 | + * ownership. Elasticsearch licenses this file to you under |
| 17 | + * the Apache License, Version 2.0 (the "License"); you may |
| 18 | + * not use this file except in compliance with the License. |
| 19 | + * You may obtain a copy of the License at |
| 20 | + * |
| 21 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | + * |
| 23 | + * Unless required by applicable law or agreed to in writing, |
| 24 | + * software distributed under the License is distributed on an |
| 25 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 26 | + * KIND, either express or implied. See the License for the |
| 27 | + * specific language governing permissions and limitations |
| 28 | + * under the License. |
| 29 | + */ |
| 30 | + |
| 31 | +import org.opensearch.gradle.BuildPlugin |
| 32 | + |
| 33 | +/* |
| 34 | + * This script plugin configures formatting for Java source using Spotless |
| 35 | + * for Gradle. Since the act of formatting existing source can interfere |
| 36 | + * with developers' workflows, we don't automatically format all code |
| 37 | + * (yet). Instead, we maintain a list of projects that are excluded from |
| 38 | + * formatting, until we reach a point where we can comfortably format them |
| 39 | + * in one go without too much disruption. |
| 40 | + * |
| 41 | + * Any new sub-projects must not be added to the exclusions list! |
| 42 | + * |
| 43 | + * To perform a reformat, run: |
| 44 | + * |
| 45 | + * ./gradlew spotlessApply |
| 46 | + * |
| 47 | + * To check the current format, run: |
| 48 | + * |
| 49 | + * ./gradlew spotlessJavaCheck |
| 50 | + * |
| 51 | + * This is also carried out by the `precommit` task. |
| 52 | + * |
| 53 | + * For more about Spotless, see: |
| 54 | + * |
| 55 | + * https://github.com/diffplug/spotless/tree/master/plugin-gradle |
| 56 | + */ |
| 57 | + |
| 58 | +org.opensearch.gradle.BuildPlugin { |
| 59 | + plugins.withType(BuildPlugin).whenPluginAdded { |
| 60 | + project.apply plugin: "com.diffplug.spotless" |
| 61 | + |
| 62 | + spotless { |
| 63 | + java { |
| 64 | + // Normally this isn't necessary, but we have Java sources in |
| 65 | + // non-standard places |
| 66 | + target '**/*.java' |
| 67 | + |
| 68 | + removeUnusedImports() |
| 69 | + eclipse().configFile rootProject.file('buildSrc/formatterConfig.xml') |
| 70 | + trimTrailingWhitespace() |
| 71 | + endWithNewline() |
| 72 | + |
| 73 | + custom 'Refuse wildcard imports', { |
| 74 | + // Wildcard imports can't be resolved; fail the build |
| 75 | + if (it =~ /\s+import .*\*;/) { |
| 76 | + throw new AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.") |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // See DEVELOPER_GUIDE.md for details of when to enable this. |
| 81 | + if (System.getProperty('spotless.paddedcell') != null) { |
| 82 | + paddedCell() |
| 83 | + } |
| 84 | + } |
| 85 | + format 'misc', { |
| 86 | + target '*.md', '*.gradle', '**/*.yaml', '**/*.yml', '**/*.svg' |
| 87 | + |
| 88 | + trimTrailingWhitespace() |
| 89 | + endWithNewline() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + precommit.dependsOn 'spotlessJavaCheck' |
| 94 | + } |
| 95 | +} |
0 commit comments