Skip to content

Commit

Permalink
Copy ConfigureUtil from Gradle: consider moving to Test Suites instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed May 15, 2023
1 parent 4d64075 commit f80525a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nebula.plugin.responsible.gradle

import org.codehaus.groovy.runtime.GeneratedClosure
import org.gradle.internal.metaobject.ConfigureDelegate
import org.gradle.util.Configurable
import org.gradle.util.internal.ClosureBackedAction

import javax.annotation.Nullable

class ConfigureUtil {

/**
* <p>Configures {@code target} with {@code configureClosure}, via the {@link Configurable} interface if necessary.</p>
*
* <p>If {@code target} does not implement {@link Configurable} interface, it is set as the delegate of a clone of
* {@code configureClosure} with a resolve strategy of {@code DELEGATE_FIRST}.</p>
*
* <p>If {@code target} does implement the {@link Configurable} interface, the {@code configureClosure} will be passed to
* {@code delegate}'s {@link Configurable#configure(Closure)} method.</p>
*
* @param configureClosure The configuration closure
* @param target The object to be configured
* @return The delegate param
*/
static <T> T configure(@Nullable Closure configureClosure, T target) {
if (configureClosure == null) {
return target
}

if (target instanceof Configurable) {
((Configurable) target).configure(configureClosure)
} else {
configureTarget(configureClosure, target, new ConfigureDelegate(configureClosure, target))
}

return target
}

private static <T> void configureTarget(Closure configureClosure, T target, ConfigureDelegate closureDelegate) {
if (!(configureClosure instanceof GeneratedClosure)) {
new ClosureBackedAction<T>(configureClosure, Closure.DELEGATE_FIRST, false).execute(target)
return;
}

// Hackery to make closure execution faster, by short-circuiting the expensive property and method lookup on Closure
Closure withNewOwner = configureClosure.rehydrate(target, closureDelegate, configureClosure.getThisObject())
new ClosureBackedAction<T>(withNewOwner, Closure.OWNER_ONLY, false).execute(target)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.gradle.api.internal.CollectionCallbackActionDecorator
import org.gradle.api.internal.FactoryNamedDomainObjectContainer
import org.gradle.api.internal.MutationGuard
import org.gradle.internal.reflect.Instantiator
import org.gradle.util.ConfigureUtil

class NamedContainerProperOrder<T> extends FactoryNamedDomainObjectContainer<T> {

Expand Down Expand Up @@ -36,7 +35,7 @@ class NamedContainerProperOrder<T> extends FactoryNamedDomainObjectContainer<T>
assertCanAdd(name);
T object = doCreate(name);
// Configure the object BEFORE, adding and kicking off addEvents in doAdd
ConfigureUtil.configure(configureClosure, object);
ConfigureUtil.configure(configureClosure, object)
add(object);
return object;
}
Expand Down

0 comments on commit f80525a

Please # to comment.