Skip to content

Commit

Permalink
Fix #2: exclude build directory by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Mar 19, 2024
1 parent 4529f1c commit b398da2
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "dev.yumi"
version = "1.1.1"
version = "1.1.2"
val javaVersion = 17

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

public class TestClass {
public static void main(String[] args) {
System.out.println("Hello world.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> projectCreationYear;

//region File selection

/**
* The filter to apply to the source files.
* <p>
Expand All @@ -54,28 +67,29 @@ public class YumiLicenserGradleExtension implements PatternFilterable {
public PatternFilterable patternFilterable;

@PackageScope
final LicenseHeader header = new LicenseHeader(new ArrayList<>());
final List<String> excludedSourceSets = new ArrayList<>();

@PackageScope
final Property<Integer> projectCreationYear;
final Property<Boolean> excludeBuildDirectory;

@PackageScope
final TextResourceFactory textResources;
//endregion
//endregion

@PackageScope
final List<String> 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",
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -152,20 +168,31 @@ 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<Integer> getProjectCreationYear() {
return this.projectCreationYear;
}

/**
* {@return the delegated filterable pattern}
*/
@Contract(pure = true)
public PatternFilterable asPatternFilterable() {
return this.patternFilterable;
}
Expand Down Expand Up @@ -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<Boolean> getExcludeBuildDirectory() {
return this.excludeBuildDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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(),
Expand Down

0 comments on commit b398da2

Please sign in to comment.