Skip to content

Commit

Permalink
Add support for the configuration cache (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Sep 4, 2021
2 parents 1c3736e + 93dd798 commit ba3c16f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# ImageGrinder

## [Unreleased]
### Added
- Now supports the Gradle Configuration Cache ([#8](https://github.com/diffplug/image-grinder/pull/8)).
- This required bumping our minimum required Gradle from `5.6` to `6.0`.

## [2.1.3] - 2021-06-12
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ imageGrinder {

Every single file in `srcDir` needs to be an image that ImageGrinder can parse. Each image will be parsed, and wrapped into an [`Img`](https://javadoc.io/doc/com.diffplug.gradle/image-grinder/2.1.3/com/diffplug/gradle/imagegrinder/Img.html). Call its methods to grind it into whatever you need in the `dstDir`.

ImageGrinder uses the gradle [Worker API](https://docs.gradle.org/5.6/userguide/custom_tasks.html#worker_api) introduced in Gradle 5.6 to use all your CPU cores for grinding. It also uses gradle's [incremental task](https://docs.gradle.org/5.6/userguide/custom_tasks.html#incremental_tasks) support to do the minimum amount of grinding required.
ImageGrinder uses the gradle [Worker API](https://docs.gradle.org/6.0/userguide/custom_tasks.html#worker_api) introduced in Gradle 5.6 to use all your CPU cores for grinding. It also uses gradle's [incremental task](https://docs.gradle.org/6.0/userguide/custom_tasks.html#incremental_tasks) support to do the minimum amount of grinding required. And if you're using the [configuration cache](https://docs.gradle.org/6.6/userguide/configuration_cache.html) introduced in Gradle 6.6, that'll work too for near-instant startup times.

## Configuration avoidance

Expand All @@ -75,7 +75,7 @@ tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME) {

- ImageGrinder can only read SVG images.
- ImageGrinder can only write PNG images.
- ImageGrinder needs Gradle 5.6 or higher.
- ImageGrinder needs Gradle 6.0 or higher.

Not much of a grinder, but it does everything we needed. If you need more, we're [happy to take PR's](CONTRIBUTING.md)!

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 DiffPlug
* Copyright (C) 2020-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@ public void apply(Project project) {
@Override
public ImageGrinderTask create(String name) {
ImageGrinderTask task = project.getTasks().create(name, ImageGrinderTask.class);
task.getBuildDir().set(project.getBuildDir());
if (name.startsWith("process")) {
Task processResources = project.getTasks().getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME);
processResources.dependsOn(task);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 DiffPlug
* Copyright (C) 2020-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,16 +21,19 @@
import java.io.Serializable;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.file.FileType;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
Expand Down Expand Up @@ -77,6 +80,9 @@ public ImageGrinderTask(WorkerExecutor workerExecutor) {
this.workerExecutor = workerExecutor;
}

@Internal
public abstract DirectoryProperty getBuildDir();

@Incremental
@PathSensitive(PathSensitivity.RELATIVE)
@InputDirectory
Expand Down Expand Up @@ -104,13 +110,16 @@ public void grinder(Action<Img<?>> grinder) {
this.grinder = grinder;
}

@Inject
public abstract FileSystemOperations getFs();

@TaskAction
public void performAction(InputChanges inputChanges) throws Exception {
Objects.requireNonNull(grinder, "grinder");

File cache = new File(getProject().getBuildDir(), "cache" + getName());
File cache = new File(getBuildDir().getAsFile().get(), "cache" + getName());
if (!inputChanges.isIncremental()) {
getProject().delete(getDstDir().getAsFile().get());
getFs().delete(deleteSpec -> deleteSpec.delete(getDstDir().getAsFile().get()));
map = HashMultimap.create();
} else {
readFromCache(cache);
Expand Down Expand Up @@ -140,7 +149,10 @@ public void performAction(InputChanges inputChanges) throws Exception {

private void remove(File srcFile) {
synchronized (map) {
map.removeAll(srcFile).forEach(getProject()::delete);
Set<File> toDelete = map.removeAll(srcFile);
getFs().delete(spec -> {
spec.delete(toDelete.toArray());
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 DiffPlug
* Copyright (C) 2020-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
public class GradleHarness extends ResourceHarness {
/** A gradleRunner(). */
protected GradleRunner gradleRunner() throws IOException {
return GradleRunner.create().withGradleVersion("5.6").withProjectDir(rootFolder()).withPluginClasspath();
return GradleRunner.create().withGradleVersion("6.0").withProjectDir(rootFolder()).withPluginClasspath();
}

/** Runs the eclipseSvg task and asserts that the given result is applied to the given files. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public void testOnce() throws Exception {
ParsedSVGTest.assertEqual(file("dst/[email protected]"), "refresh32.png");
}

@Test
public void testOnceConfigurationCache() throws Exception {
writeBuild();
write("src/refresh.svg", readTestResource("refresh.svg"));
gradleRunner().withGradleVersion("6.6").withArguments("--configuration-cache", "eclipseSvg").build();
ParsedSVGTest.assertEqual(file("dst/refresh.png"), "refresh16.png");
ParsedSVGTest.assertEqual(file("dst/[email protected]"), "refresh32.png");
}

@Test
public void testUpToDate() throws Exception {
writeBuild();
Expand Down

0 comments on commit ba3c16f

Please sign in to comment.