From b398da2cd205581ccf176e0434ed308bbace45ae Mon Sep 17 00:00:00 2001 From: LambdAurora Date: Tue, 19 Mar 2024 11:16:30 +0100 Subject: [PATCH] Fix #2: exclude build directory by default. --- CHANGELOG.md | 4 ++ build.gradle.kts | 2 +- .../IgnoreBuildDirectoryFunctionalTest.java | 36 +++++++++++++ .../gradle/licenser/test/ScenarioRunner.java | 6 ++- .../ignore_build_directory/build.gradle | 12 +++++ .../build/TestClass.java | 7 +++ .../licenser/YumiLicenserGradleExtension.java | 54 ++++++++++++++----- .../licenser/task/ApplyLicenseTask.java | 4 +- .../licenser/task/CheckLicenseTask.java | 4 +- .../task/SourceDirectoryBasedTask.java | 15 ++++-- 10 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 src/functionalTest/java/dev/yumi/gradle/licenser/test/IgnoreBuildDirectoryFunctionalTest.java create mode 100644 src/functionalTest/resources/scenarios/ignore_build_directory/build.gradle create mode 100644 src/functionalTest/resources/scenarios/ignore_build_directory/build/TestClass.java diff --git a/CHANGELOG.md b/CHANGELOG.md index f66a9dc..2f6042b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,3 +15,7 @@ ## 1.1.1 - Fixed Kotlin multiplatform support when the plugin is applied after the licenser ([#1](https://github.com/YumiProject/yumi-gradle-licenser/issues/1#issuecomment-1931569894)). + +## 1.1.2 + +- Fixed default inclusion of build directories for verification ([#2](https://github.com/YumiProject/yumi-gradle-licenser/issues/2)). diff --git a/build.gradle.kts b/build.gradle.kts index 8b51af9..33cff50 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "dev.yumi" -version = "1.1.1" +version = "1.1.2" val javaVersion = 17 repositories { diff --git a/src/functionalTest/java/dev/yumi/gradle/licenser/test/IgnoreBuildDirectoryFunctionalTest.java b/src/functionalTest/java/dev/yumi/gradle/licenser/test/IgnoreBuildDirectoryFunctionalTest.java new file mode 100644 index 0000000..ce9f434 --- /dev/null +++ b/src/functionalTest/java/dev/yumi/gradle/licenser/test/IgnoreBuildDirectoryFunctionalTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2024 Yumi Project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package dev.yumi.gradle.licenser.test; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A functional test which tests the proper exclusion of build directories even though files may be included in a source set. + */ +public class IgnoreBuildDirectoryFunctionalTest { + @TempDir + File projectDir; + + @Test + public void canRunTask() throws IOException { + var runner = new ScenarioRunner("ignore_build_directory", projectDir.toPath()); + runner.setup(); + + runner.runCheck(); + } +} diff --git a/src/functionalTest/java/dev/yumi/gradle/licenser/test/ScenarioRunner.java b/src/functionalTest/java/dev/yumi/gradle/licenser/test/ScenarioRunner.java index dedbb79..63fe4db 100644 --- a/src/functionalTest/java/dev/yumi/gradle/licenser/test/ScenarioRunner.java +++ b/src/functionalTest/java/dev/yumi/gradle/licenser/test/ScenarioRunner.java @@ -87,7 +87,11 @@ public void setup() throws IOException { Files.copy(ScenarioRunner.class.getResourceAsStream("/HEADER"), this.projectDir.resolve("HEADER")); this.copy("build.gradle"); - this.recurseCopy("src"); + + if (Files.exists(this.getScenarioPath("/src"))) + this.recurseCopy("src"); + if (Files.exists(this.getScenarioPath("/build"))) + this.recurseCopy("build"); } public BuildResult run(String... args) { diff --git a/src/functionalTest/resources/scenarios/ignore_build_directory/build.gradle b/src/functionalTest/resources/scenarios/ignore_build_directory/build.gradle new file mode 100644 index 0000000..a84b4ee --- /dev/null +++ b/src/functionalTest/resources/scenarios/ignore_build_directory/build.gradle @@ -0,0 +1,12 @@ +plugins { + id 'dev.yumi.gradle.licenser' + id 'java' +} + +sourceSets.main.java { + srcDirs = srcDirs + layout.buildDirectory.get().asFile +} + +license { + rule rootProject.file("HEADER") +} diff --git a/src/functionalTest/resources/scenarios/ignore_build_directory/build/TestClass.java b/src/functionalTest/resources/scenarios/ignore_build_directory/build/TestClass.java new file mode 100644 index 0000000..23b38fa --- /dev/null +++ b/src/functionalTest/resources/scenarios/ignore_build_directory/build/TestClass.java @@ -0,0 +1,7 @@ +package test; + +public class TestClass { + public static void main(String[] args) { + System.out.println("Hello world."); + } +} diff --git a/src/main/java/dev/yumi/gradle/licenser/YumiLicenserGradleExtension.java b/src/main/java/dev/yumi/gradle/licenser/YumiLicenserGradleExtension.java index bd39521..fcf5aaf 100644 --- a/src/main/java/dev/yumi/gradle/licenser/YumiLicenserGradleExtension.java +++ b/src/main/java/dev/yumi/gradle/licenser/YumiLicenserGradleExtension.java @@ -41,10 +41,23 @@ * Represents the Yumi Licenser Gradle extension to configure the plugin in buildscripts. * * @author LambdAurora - * @version 1.1.0 + * @version 1.1.2 * @since 1.0.0 */ public class YumiLicenserGradleExtension implements PatternFilterable { + //region Properties + + @PackageScope + final LicenseHeader header = new LicenseHeader(new ArrayList<>()); + + @PackageScope + final HeaderCommentManager headerCommentManager = new HeaderCommentManager(); + + @PackageScope + final Property projectCreationYear; + + //region File selection + /** * The filter to apply to the source files. *

@@ -54,28 +67,29 @@ public class YumiLicenserGradleExtension implements PatternFilterable { public PatternFilterable patternFilterable; @PackageScope - final LicenseHeader header = new LicenseHeader(new ArrayList<>()); + final List excludedSourceSets = new ArrayList<>(); @PackageScope - final Property projectCreationYear; + final Property excludeBuildDirectory; - @PackageScope - final TextResourceFactory textResources; + //endregion + //endregion - @PackageScope - final List excludedSourceSets = new ArrayList<>(); + //region Utils @PackageScope - final HeaderCommentManager headerCommentManager = new HeaderCommentManager(); + final TextResourceFactory textResources; + + //endregion @Inject public YumiLicenserGradleExtension(final ObjectFactory objects, final Project project) { - this.patternFilterable = new PatternSet(); this.textResources = project.getResources().getText(); this.projectCreationYear = objects.property(Integer.class) .convention(project.provider(() -> Utils.getProjectCreationYear(project))); + this.patternFilterable = new PatternSet(); this.exclude( "**/*.txt", "**/*.json", @@ -103,6 +117,8 @@ public YumiLicenserGradleExtension(final ObjectFactory objects, final Project pr "**/MANIFEST.MF", "**/META-INF/services/**" ); + this.excludeBuildDirectory = objects.property(Boolean.class) + .convention(true); } /** @@ -152,13 +168,23 @@ public void rule(HeaderRule rule) { /** * {@return the license header definition of this project} */ + @Contract(pure = true) public LicenseHeader getLicenseHeader() { return this.header; } + /** + * {@return the header comment manager to attach header comment to specific file formats} + */ + @Contract(pure = true) + public @NotNull HeaderCommentManager getHeaderCommentManager() { + return this.headerCommentManager; + } + /** * {@return the project creation year property} */ + @Contract(pure = true) public Property getProjectCreationYear() { return this.projectCreationYear; } @@ -166,6 +192,7 @@ public Property getProjectCreationYear() { /** * {@return the delegated filterable pattern} */ + @Contract(pure = true) public PatternFilterable asPatternFilterable() { return this.patternFilterable; } @@ -298,9 +325,12 @@ public boolean isSourceSetExcluded(SourceSet sourceSet) { } /** - * {@return the header comment manager to attach header comment to specific file formats} + * {@return the property which excludes the build directory from checks if set to {@code true}} + * + * @since 1.1.2 */ - public @NotNull HeaderCommentManager getHeaderCommentManager() { - return this.headerCommentManager; + @Contract(pure = true) + public @NotNull Property getExcludeBuildDirectory() { + return this.excludeBuildDirectory; } } diff --git a/src/main/java/dev/yumi/gradle/licenser/task/ApplyLicenseTask.java b/src/main/java/dev/yumi/gradle/licenser/task/ApplyLicenseTask.java index 646fd86..f1fe883 100644 --- a/src/main/java/dev/yumi/gradle/licenser/task/ApplyLicenseTask.java +++ b/src/main/java/dev/yumi/gradle/licenser/task/ApplyLicenseTask.java @@ -34,7 +34,7 @@ * Represents the task that applies license headers to project files. * * @author LambdAurora - * @version 1.1.0 + * @version 1.1.2 * @since 1.0.0 */ @ApiStatus.Internal @@ -44,7 +44,7 @@ public class ApplyLicenseTask extends SourceDirectoryBasedTask { @Inject public ApplyLicenseTask(SourceDirectorySet sourceDirectorySet, YumiLicenserGradleExtension extension) { - super(sourceDirectorySet, extension.asPatternFilterable()); + super(sourceDirectorySet, extension.asPatternFilterable(), extension.getExcludeBuildDirectory().get()); this.licenseHeader = extension.getLicenseHeader(); this.headerCommentManager = extension.getHeaderCommentManager(); this.setDescription("Applies the correct license headers to source files in the " diff --git a/src/main/java/dev/yumi/gradle/licenser/task/CheckLicenseTask.java b/src/main/java/dev/yumi/gradle/licenser/task/CheckLicenseTask.java index fec8159..eda7081 100644 --- a/src/main/java/dev/yumi/gradle/licenser/task/CheckLicenseTask.java +++ b/src/main/java/dev/yumi/gradle/licenser/task/CheckLicenseTask.java @@ -31,7 +31,7 @@ * Represents a task that checks the validity of license headers in project files. * * @author LambdAurora - * @version 1.1.0 + * @version 1.1.2 * @since 1.0.0 */ @ApiStatus.Internal @@ -41,7 +41,7 @@ public class CheckLicenseTask extends SourceDirectoryBasedTask { @Inject public CheckLicenseTask(SourceDirectorySet sourceDirectorySet, YumiLicenserGradleExtension extension) { - super(sourceDirectorySet, extension.asPatternFilterable()); + super(sourceDirectorySet, extension.asPatternFilterable(), extension.getExcludeBuildDirectory().get()); this.licenseHeader = extension.getLicenseHeader(); this.headerCommentManager = extension.getHeaderCommentManager(); this.setDescription("Checks whether source files in the " diff --git a/src/main/java/dev/yumi/gradle/licenser/task/SourceDirectoryBasedTask.java b/src/main/java/dev/yumi/gradle/licenser/task/SourceDirectoryBasedTask.java index adb957f..e7f8c0c 100644 --- a/src/main/java/dev/yumi/gradle/licenser/task/SourceDirectoryBasedTask.java +++ b/src/main/java/dev/yumi/gradle/licenser/task/SourceDirectoryBasedTask.java @@ -23,17 +23,19 @@ * Represents a task that acts on a given source directory set. * * @author LambdAurora - * @version 1.1.0 + * @version 1.1.2 * @since 1.0.0 */ @ApiStatus.Internal public abstract class SourceDirectoryBasedTask extends DefaultTask { protected final SourceDirectorySet sourceDirectorySet; protected final PatternFilterable patternFilterable; + protected final boolean excludeBuildDirectory; - protected SourceDirectoryBasedTask(SourceDirectorySet sourceDirectorySet, PatternFilterable patternFilterable) { + protected SourceDirectoryBasedTask(SourceDirectorySet sourceDirectorySet, PatternFilterable patternFilterable, boolean excludeBuildDirectory) { this.sourceDirectorySet = sourceDirectorySet; this.patternFilterable = patternFilterable; + this.excludeBuildDirectory = excludeBuildDirectory; } /** @@ -43,15 +45,20 @@ protected SourceDirectoryBasedTask(SourceDirectorySet sourceDirectorySet, Patter * @param consumer the action to execute on a given file */ void execute(HeaderCommentManager headerCommentManager, SourceConsumer consumer) { + Path buildDir = this.getProject().getLayout().getBuildDirectory().get().getAsFile().toPath(); + this.sourceDirectorySet.matching(this.patternFilterable) .visit(fileVisitDetails -> { if (fileVisitDetails.isDirectory()) return; + Path sourcePath = fileVisitDetails.getFile().toPath(); + + // Exclude the build directory unless it's forcefully included. + if (this.excludeBuildDirectory && sourcePath.startsWith(buildDir)) return; + HeaderComment headerComment = headerCommentManager.findHeaderComment(fileVisitDetails); if (headerComment != null) { - Path sourcePath = fileVisitDetails.getFile().toPath(); - try { consumer.consume( this.getProject(), this.getLogger(), this.getProject().getProjectDir().toPath(),