Skip to content

Commit

Permalink
Make check license tasks incremental.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Dec 10, 2024
1 parent ca6611e commit 0db4367
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# LambdAurora's ignore file
#
# v0.23
# v0.24

# JetBrains
.idea/
Expand Down Expand Up @@ -50,6 +50,8 @@ logs/
# Languages
## Java
classes/
## Kotlin
.kotlin/
## Python
__pycache__/
venv/
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
- Switched from `PatternFilterable` to `Set<String>` for file matching in `HeaderCommentManager` as only extensions are matched.
- Adapted `LicenseYearSelectionMode` to not consume `Project` arguments anymore.

## 2.1.0

- Made check license tasks incremental.

[#3]: https://github.com/YumiProject/yumi-gradle-licenser/pull/3
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For a project you need to apply the plugin to your project:

```groovy
plugins {
id "dev.yumi.gradle.licenser" version "2.0.+"
id "dev.yumi.gradle.licenser" version "2.1.+"
}
```

Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.yumi"
version = "2.0.0"
version = "2.1.0"
val javaVersion = 17

repositories {
Expand Down Expand Up @@ -79,8 +79,10 @@ tasks.withType<Javadoc>().configureEach {
}

tasks.jar {
val archivesName = base.archivesName.get()

from("LICENSE") {
rename { "${it}_${base.archivesName.get()}" }
rename { "${it}_${archivesName}" }
}
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.configuration-cache=true
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[versions]
jetbrains-annotations = "24.0.1"
jetbrains-annotations = "26.0.1"
jgit = "6.7.0.202309050840-r"
# Gradle-specific
gradle-kotlin = "1.9.22"
# Testing
junit-jupiter = "5.10.0"
junit-launcher = "1.10.0"
junit-jupiter = "5.11.3"
junit-launcher = "1.11.3"

[libraries]
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BaseJavaFunctionalTest {

@Test
public void canRunTask() throws IOException {
var runner = new ScenarioRunner("base_java", projectDir.toPath());
var runner = new ScenarioRunner("base_java", projectDir.toPath(), false);
runner.setup();

Path testClassPath = runner.path("src/main/java/test/TestClass.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -30,7 +31,7 @@ public class ConfigurationCacheFunctionalTest {

@Test
public void canRunTask() throws IOException {
var runner = new ScenarioRunner("base_java", projectDir.toPath());
var runner = new ScenarioRunner("base_java", projectDir.toPath(), true);
runner.setup();

Path testClassPath = runner.path("src/main/java/test/TestClass.java");
Expand All @@ -39,7 +40,7 @@ public void canRunTask() throws IOException {
Path testClassWithOptionalPath = runner.path("src/main/java/test/TestClassWithOptional.java");
Path testPackageInfoPath = runner.path("src/main/java/test/package-info.java");

var result = runner.run("--configuration-cache", "applyLicenses", "--stacktrace");
var result = runner.run();

// Verify the result
assertTrue(
Expand Down Expand Up @@ -81,5 +82,22 @@ public void canRunTask() throws IOException {
runner.runCheck();
// Test the caching.
runner.runCheck();

Files.copy(
ScenarioRunner.TEST_JAR_PATH.resolve("scenarios/cache/TestClassModified.java"),
testClassPath,
StandardCopyOption.REPLACE_EXISTING
);
result = runner.run();
// Verify the result
assertTrue(
result.getOutput().contains("- Updated file " + testClassPath),
"Missing updated file string in output log."
);
result = runner.runCheck();
assertTrue(
result.getOutput().contains("All license header checks passed (1 files)."),
"Missing accurate checks passed string in output log."
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CustomSourceSetFunctionalTest {

@Test
public void canRunTask() throws IOException {
var runner = new ScenarioRunner("custom_sourceset", projectDir.toPath());
var runner = new ScenarioRunner("custom_sourceset", projectDir.toPath(), false);
runner.setup();

var result = runner.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class IgnoreBuildDirectoryFunctionalTest {

@Test
public void canRunTask() throws IOException {
var runner = new ScenarioRunner("ignore_build_directory", projectDir.toPath());
var runner = new ScenarioRunner("ignore_build_directory", projectDir.toPath(), false);
runner.setup();

runner.runCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void canRunMultiplatform() throws IOException {
}

private void run(String name, String path) throws IOException {
var runner = new ScenarioRunner(name, projectDir.toPath());
var runner = new ScenarioRunner(name, projectDir.toPath(), false);
runner.setup();

Path testKotlinPath = runner.path(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ScenarioRunner {
private static final PathResolver TEST_JAR_PATH;
static final PathResolver TEST_JAR_PATH;
private final String name;
private final Path projectDir;
private final boolean forceConfigurationCache;

public ScenarioRunner(String name, Path projectDir) {
public ScenarioRunner(String name, Path projectDir, boolean forceConfigurationCache) {
this.name = name;
this.projectDir = projectDir;
this.forceConfigurationCache = forceConfigurationCache;
}

private Path getScenarioPath(String path) {
Expand All @@ -36,12 +41,13 @@ public Path path(String path) {
return this.projectDir.resolve(path).normalize();
}

private Path copy(String pathStr) throws IOException {
public Path copy(String pathStr, @NotNull CopyOption... options) throws IOException {
Path destinationPath = this.path(pathStr);
Files.createDirectories(destinationPath.getParent());
Files.copy(
this.getScenarioPath('/' + pathStr),
destinationPath
destinationPath,
options
);
return destinationPath.toAbsolutePath();
}
Expand Down Expand Up @@ -95,11 +101,17 @@ public void setup() throws IOException {
}

public BuildResult run(String... args) {
var argsList = new ArrayList<>(List.of(args));

if (this.forceConfigurationCache) {
argsList.add(0, "--configuration-cache");
}

// Run the build
var runner = GradleRunner.create();
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments(args);
runner.withArguments(argsList.toArray(String[]::new));
runner.withProjectDir(this.projectDir.toFile());
return runner.build();
}
Expand All @@ -108,8 +120,8 @@ public BuildResult run() {
return this.run("applyLicenses", "--stacktrace");
}

public void runCheck() {
this.run("checkLicenses", "--stacktrace");
public BuildResult runCheck() {
return this.run("checkLicenses", "--stacktrace");
}

interface PathResolver {
Expand Down
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 again.");
}
}
24 changes: 16 additions & 8 deletions src/main/java/dev/yumi/gradle/licenser/task/ApplyLicenseTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.UntrackedTask;
import org.jetbrains.annotations.ApiStatus;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;

/**
* Represents the task that applies license headers to project files.
*
* @author LambdAurora
* @version 2.0.0
* @version 2.1.0
* @since 1.0.0
*/
@ApiStatus.Internal
@UntrackedTask(because = "Task may rewrite the input files.")
public abstract class ApplyLicenseTask extends SourceDirectoryBasedTask {
@Inject
public ApplyLicenseTask(YumiLicenserGradleExtension extension) {
Expand All @@ -56,7 +56,11 @@ public ApplyLicenseTask(YumiLicenserGradleExtension extension) {

@TaskAction
public void execute() {
this.execute(this.getHeaderCommentManager().get(), new Consumer(this.getLicenseHeader().get()));
this.execute(
this.getHeaderCommentManager().get(),
StreamSupport.stream(this.getSourceFiles().spliterator(), false).map(File::toPath),
new Consumer(this.getLicenseHeader().get())
);
}

/**
Expand Down Expand Up @@ -123,7 +127,9 @@ public void consume(
String read = Files.readString(path, StandardCharsets.UTF_8);
var readComment = headerComment.readHeaderComment(read);

List<String> lines = this.licenseHeader.format(rootDir, projectCreationYear, logger, path, readComment.existing());
List<String> lines = this.licenseHeader.format(
rootDir, projectCreationYear, logger, path, readComment.existing()
);

if (lines != null) {
this.updatedFiles.add(path);
Expand All @@ -142,7 +148,9 @@ public void consume(
end = readComment.separator() + readComment.separator() + end;
}

String content = start + headerComment.writeHeaderComment(lines, readComment.separator()) + end;
String content = start
+ headerComment.writeHeaderComment(lines, readComment.separator())
+ end;

try {
var backupPath = Utils.getBackupPath(buildPath, projectPath, path);
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/dev/yumi/gradle/licenser/task/CheckLicenseTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
import org.gradle.work.InputChanges;
import org.jetbrains.annotations.ApiStatus;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;

/**
* Represents a task that checks the validity of license headers in project files.
*
* @author LambdAurora
* @version 2.0.0
* @version 2.1.0
* @since 1.0.0
*/
@ApiStatus.Internal
Expand All @@ -52,8 +53,17 @@ public CheckLicenseTask(YumiLicenserGradleExtension extension) {
}

@TaskAction
public void execute() {
this.execute(this.getHeaderCommentManager().get(), new Consumer(this.getLicenseHeader().get()));
public void execute(InputChanges inputChanges) {
this.execute(
this.getHeaderCommentManager().get(),
StreamSupport.stream(
inputChanges.getFileChanges(this.getSourceFiles()).spliterator(),
false
).filter(action -> action.getChangeType() != ChangeType.REMOVED)
.map(FileChange::getFile)
.map(File::toPath),
new Consumer(this.getLicenseHeader().get())
);
}

/**
Expand Down
Loading

0 comments on commit 0db4367

Please sign in to comment.