Skip to content

Commit

Permalink
Merge pull request #618 from diffplug/feat/lazy
Browse files Browse the repository at this point in the history
Execute the configuration actions lazily
  • Loading branch information
nedtwigg authored Jun 19, 2020
2 parents 1cc5f54 + 1e2801d commit aa42229
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.annotation.Nullable;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand Down Expand Up @@ -56,6 +57,7 @@
/** Adds a `spotless{Name}Check` and `spotless{Name}Apply` task. */
public class FormatExtension {
final SpotlessExtensionBase spotless;
final List<Action<FormatExtension>> modernLazyActions = new ArrayList<>();

public FormatExtension(SpotlessExtensionBase spotless) {
this.spotless = Objects.requireNonNull(spotless);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SpotlessExtension(Project project) {
*/
@Deprecated
public void css(Action<CssExtension> closure) {
configure(CssExtension.NAME, CssExtension.class, closure);
format(CssExtension.NAME, CssExtension.class, closure);
}

/**
Expand All @@ -55,7 +55,7 @@ public void css(Action<CssExtension> closure) {
*/
@Deprecated
public void xml(Action<XmlExtension> closure) {
configure(XmlExtension.NAME, XmlExtension.class, closure);
format(XmlExtension.NAME, XmlExtension.class, closure);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,63 +115,63 @@ public void ratchetFrom(String ratchetFrom) {
/** Configures the special java-specific extension. */
public void java(Action<JavaExtension> closure) {
requireNonNull(closure);
configure(JavaExtension.NAME, JavaExtension.class, closure);
format(JavaExtension.NAME, JavaExtension.class, closure);
}

/** Configures the special scala-specific extension. */
public void scala(Action<ScalaExtension> closure) {
requireNonNull(closure);
configure(ScalaExtension.NAME, ScalaExtension.class, closure);
format(ScalaExtension.NAME, ScalaExtension.class, closure);
}

/** Configures the special kotlin-specific extension. */
public void kotlin(Action<KotlinExtension> closure) {
requireNonNull(closure);
configure(KotlinExtension.NAME, KotlinExtension.class, closure);
format(KotlinExtension.NAME, KotlinExtension.class, closure);
}

/** Configures the special Gradle Kotlin DSL specific extension. */
public void kotlinGradle(Action<KotlinGradleExtension> closure) {
requireNonNull(closure);
configure(KotlinGradleExtension.NAME, KotlinGradleExtension.class, closure);
format(KotlinGradleExtension.NAME, KotlinGradleExtension.class, closure);
}

/** Configures the special freshmark-specific extension. */
public void freshmark(Action<FreshMarkExtension> closure) {
requireNonNull(closure);
configure(FreshMarkExtension.NAME, FreshMarkExtension.class, closure);
format(FreshMarkExtension.NAME, FreshMarkExtension.class, closure);
}

/** Configures the special groovy-specific extension. */
public void groovy(Action<GroovyExtension> closure) {
configure(GroovyExtension.NAME, GroovyExtension.class, closure);
format(GroovyExtension.NAME, GroovyExtension.class, closure);
}

/** Configures the special groovy-specific extension for Gradle files. */
public void groovyGradle(Action<GroovyGradleExtension> closure) {
configure(GroovyGradleExtension.NAME, GroovyGradleExtension.class, closure);
format(GroovyGradleExtension.NAME, GroovyGradleExtension.class, closure);
}

/** Configures the special sql-specific extension for SQL files. */
public void sql(Action<SqlExtension> closure) {
configure(SqlExtension.NAME, SqlExtension.class, closure);
format(SqlExtension.NAME, SqlExtension.class, closure);
}

/** Configures the special C/C++-specific extension. */
public void cpp(Action<CppExtension> closure) {
configure(CppExtension.NAME, CppExtension.class, closure);
format(CppExtension.NAME, CppExtension.class, closure);
}

/** Configures the special typescript-specific extension for typescript files. */
public void typescript(Action<TypescriptExtension> closure) {
configure(TypescriptExtension.NAME, TypescriptExtension.class, closure);
format(TypescriptExtension.NAME, TypescriptExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
requireNonNull(closure, "closure");
configure(name, FormatExtension.class, closure);
format(name, FormatExtension.class, closure);
}

/** Makes it possible to remove a format which was created earlier. */
Expand Down Expand Up @@ -200,13 +200,13 @@ public void setEnforceCheck(boolean enforceCheck) {
this.enforceCheck = enforceCheck;
}

protected <T extends FormatExtension> void configure(String name, Class<T> clazz, Action<T> configure) {
T value = maybeCreate(name, clazz);
configure.execute(value);
public <T extends FormatExtension> void format(String name, Class<T> clazz, Action<T> configure) {
T format = maybeCreate(name, clazz);
configure.execute(format);
}

@SuppressWarnings("unchecked")
private <T extends FormatExtension> T maybeCreate(String name, Class<T> clazz) {
protected final <T extends FormatExtension> T maybeCreate(String name, Class<T> clazz) {
FormatExtension existing = formats.get(name);
if (existing != null) {
if (!existing.getClass().equals(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.diffplug.gradle.spotless;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaBasePlugin;
Expand Down Expand Up @@ -46,10 +47,14 @@ public SpotlessExtensionModern(Project project) {

final TaskProvider<?> rootCheckTask, rootApplyTask, rootDiagnoseTask;

@SuppressWarnings("unchecked")
@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
// TODO override configure(String name, Class<T> clazz, Action<T> configure) so that it is lazy
public <T extends FormatExtension> void format(String name, Class<T> clazz, Action<T> configure) {
maybeCreate(name, clazz).modernLazyActions.add((Action<FormatExtension>) configure);
}

@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
boolean isIdeHook = project.hasProperty(IdeHook.PROPERTY);
TaskContainer tasks = project.getTasks();
TaskProvider<?> cleanTask = tasks.named(BasePlugin.CLEAN_TASK_NAME);
Expand All @@ -62,7 +67,16 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
task.mustRunAfter(cleanTask);
});

project.afterEvaluate(unused -> spotlessTask.configure(formatExtension::setupTask));
project.afterEvaluate(unused -> {
spotlessTask.configure(task -> {
// now that the task is being configured, we execute our actions
for (Action<FormatExtension> lazyAction : formatExtension.modernLazyActions) {
lazyAction.execute(formatExtension);
}
// and now we'll setup the task
formatExtension.setupTask(task);
});
});

// create the check and apply control tasks
TaskProvider<SpotlessApply> applyTask = tasks.register(taskName + APPLY, SpotlessApply.class, task -> {
Expand Down

0 comments on commit aa42229

Please sign in to comment.