|
| 1 | +/* |
| 2 | + * This file is part of indra, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2020-2022 KyoriPowered |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package net.kyori.indra.licenser.spotless; |
| 25 | + |
| 26 | +import com.diffplug.gradle.spotless.FormatExtension; |
| 27 | +import com.diffplug.gradle.spotless.SpotlessExtension; |
| 28 | +import com.diffplug.spotless.generic.LicenseHeaderStep; |
| 29 | +import com.diffplug.spotless.kotlin.KotlinConstants; |
| 30 | +import net.kyori.indra.licenser.spotless.internal.SpotlessLicenserExtensionImpl; |
| 31 | +import net.kyori.mammoth.ProjectPlugin; |
| 32 | +import org.gradle.api.Action; |
| 33 | +import org.gradle.api.Project; |
| 34 | +import org.gradle.api.plugins.ExtensionContainer; |
| 35 | +import org.gradle.api.plugins.PluginContainer; |
| 36 | +import org.gradle.api.tasks.TaskContainer; |
| 37 | +import org.jetbrains.annotations.NotNull; |
| 38 | + |
| 39 | +/** |
| 40 | + * A plugin to provide user-friendly configuration for the Spotless license header steps. |
| 41 | + * |
| 42 | + * @since 2.2.0 |
| 43 | + */ |
| 44 | +public class SpotlessLicenserPlugin implements ProjectPlugin { |
| 45 | + private static final String JAVA_LICENSE_HEADER_DELIMITER = "package "; |
| 46 | + |
| 47 | + private static final String HEADER_FILE_NAME = "license_header.txt"; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void apply( |
| 51 | + final @NotNull Project project, |
| 52 | + final @NotNull PluginContainer plugins, |
| 53 | + final @NotNull ExtensionContainer extensions, |
| 54 | + final @NotNull TaskContainer tasks |
| 55 | + ) { |
| 56 | + // Register our own extension |
| 57 | + final SpotlessLicenserExtensionImpl extension = (SpotlessLicenserExtensionImpl) extensions.create(SpotlessLicenserExtension.class, "indraSpotlessLicenser", SpotlessLicenserExtensionImpl.class); |
| 58 | + |
| 59 | + // Default licenser configuration |
| 60 | + extension.licenseHeaderFile().convention(project.getResources().getText().fromFile(project.getRootProject().file(HEADER_FILE_NAME), "UTF-8")); |
| 61 | + |
| 62 | + // Apply spotless |
| 63 | + plugins.apply("com.diffplug.spotless"); |
| 64 | + final SpotlessExtension spotless = extensions.getByType(SpotlessExtension.class); |
| 65 | + |
| 66 | + // Apply license header config to individual languages |
| 67 | + plugins.withId("java", $ -> { |
| 68 | + spotless.java(java -> { |
| 69 | + addStep(project, java, extension, "java", JAVA_LICENSE_HEADER_DELIMITER); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + plugins.withId("org.jetbrains.kotlin.jvm", $ -> { |
| 74 | + spotless.kotlin(kotlin -> { |
| 75 | + addStep(project, kotlin, extension, "kotlin", KotlinConstants.LICENSE_HEADER_DELIMITER); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + plugins.withId("groovy", $ -> { |
| 80 | + spotless.groovy(groovy -> { |
| 81 | + addStep(project, groovy, extension, "groovy", JAVA_LICENSE_HEADER_DELIMITER); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // TODO: scala -- scala doesn't support its own delimiter |
| 86 | + } |
| 87 | + |
| 88 | + private static void addStep(final Project project, final FormatExtension format, final SpotlessLicenserExtensionImpl indraExtension, final String name, final String delimiter) { |
| 89 | + final LicenseHeaderStep step = LicenseHeaderStep.headerDelimiter(indraExtension.createHeaderSupplier(name), ""); |
| 90 | + format.addStep(step.withYearMode(LicenseHeaderStep.YearMode.PRESERVE).build()); // add with dummy settings |
| 91 | + final FormatExtension.LicenseHeaderConfig config = format.new LicenseHeaderConfig(step); |
| 92 | + config.delimiter(delimiter); // replace the step with a properly configured one |
| 93 | + |
| 94 | + // Then apply any extra steps after evaluation |
| 95 | + project.afterEvaluate(p -> { |
| 96 | + for (final Action<FormatExtension.LicenseHeaderConfig> configStep : indraExtension.extraConfigSteps()) { |
| 97 | + configStep.execute(config); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
0 commit comments