-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy ConfigureUtil from Gradle: consider moving to Test Suites instead
- Loading branch information
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/groovy/nebula/plugin/responsible/gradle/ConfigureUtil.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters