|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.spotless.groovy; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.gradle.api.GradleException; |
| 21 | +import org.gradle.api.internal.file.UnionFileCollection; |
| 22 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 23 | +import org.gradle.api.tasks.SourceSet; |
| 24 | + |
| 25 | +import com.diffplug.gradle.spotless.FormatExtension; |
| 26 | +import com.diffplug.gradle.spotless.FormatTask; |
| 27 | +import com.diffplug.gradle.spotless.LicenseHeaderStep; |
| 28 | +import com.diffplug.gradle.spotless.SpotlessExtension; |
| 29 | +import com.diffplug.gradle.spotless.java.ImportSorterStep; |
| 30 | + |
| 31 | +// TODO: None of this is actually groovy specific. Spotless' |
| 32 | +// maintainers are not fans of Groovy, but we're happy to take |
| 33 | +// PRs to flesh out this functionality. |
| 34 | +public class GroovyExtension extends FormatExtension { |
| 35 | + public static final String NAME = "groovy"; |
| 36 | + |
| 37 | + public GroovyExtension(SpotlessExtension rootExtension) { |
| 38 | + super(NAME, rootExtension); |
| 39 | + } |
| 40 | + |
| 41 | + public static final String LICENSE_HEADER_DELIMITER = "package "; |
| 42 | + |
| 43 | + public void licenseHeader(String licenseHeader) { |
| 44 | + licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); |
| 45 | + } |
| 46 | + |
| 47 | + public void licenseHeaderFile(Object licenseHeaderFile) { |
| 48 | + licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); |
| 49 | + } |
| 50 | + |
| 51 | + public void importOrder(List<String> importOrder) { |
| 52 | + customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(importOrder)::format); |
| 53 | + } |
| 54 | + |
| 55 | + public void importOrderFile(Object importOrderFile) { |
| 56 | + customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(getProject().file(importOrderFile))::format); |
| 57 | + } |
| 58 | + |
| 59 | + /** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */ |
| 60 | + @Override |
| 61 | + protected void setupTask(FormatTask task) throws Exception { |
| 62 | + if (target == null) { |
| 63 | + JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class); |
| 64 | + if (javaPlugin == null) { |
| 65 | + throw new GradleException("You must apply the java plugin before the spotless plugin if you are using the java extension."); |
| 66 | + } |
| 67 | + UnionFileCollection union = new UnionFileCollection(); |
| 68 | + for (SourceSet sourceSet : javaPlugin.getSourceSets()) { |
| 69 | + union.add(sourceSet.getJava()); |
| 70 | + } |
| 71 | + target = union; |
| 72 | + } |
| 73 | + // LicenseHeaderStep completely blows apart package-info.java - this common-sense check ensures that |
| 74 | + // it skips package-info.java. See https://github.com/diffplug/spotless/issues/1 |
| 75 | + steps.replaceAll(step -> { |
| 76 | + if (LicenseHeaderStep.NAME.equals(step.getName())) { |
| 77 | + return step.filterByFile(file -> !file.getName().equals("package-info.java")); |
| 78 | + } else { |
| 79 | + return step; |
| 80 | + } |
| 81 | + }); |
| 82 | + super.setupTask(task); |
| 83 | + } |
| 84 | +} |
0 commit comments