Skip to content

Commit 7086655

Browse files
authored
Reduce nesting for Plugin.apply (#1407)
1 parent c86a9b3 commit 7086655

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator
2727
* @see [ApplicationPlugin]
2828
*/
2929
public abstract class ShadowApplicationPlugin : Plugin<Project> {
30-
override fun apply(project: Project) {
31-
project.addRunTask()
32-
project.addCreateScriptsTask()
33-
project.configureDistribution()
34-
project.configureShadowJarMainClass()
35-
project.configureInstallTask()
30+
override fun apply(project: Project): Unit = with(project) {
31+
addRunTask()
32+
addCreateScriptsTask()
33+
configureDistribution()
34+
configureShadowJarMainClass()
35+
configureInstallTask()
3636
}
3737

3838
protected open fun Project.addRunTask() {

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import org.gradle.util.GradleVersion
1010

1111
public abstract class ShadowBasePlugin : Plugin<Project> {
1212

13-
override fun apply(project: Project) {
13+
override fun apply(project: Project): Unit = with(project) {
1414
if (GradleVersion.current() < GradleVersion.version("8.3")) {
1515
throw GradleException("This version of Shadow supports Gradle 8.3+ only. Please upgrade.")
1616
}
1717
@Suppress("DEPRECATION")
18-
project.extensions.create(EXTENSION_NAME, ShadowExtension::class.java, project)
19-
project.configurations.create(CONFIGURATION_NAME)
18+
extensions.create(EXTENSION_NAME, ShadowExtension::class.java, project)
19+
configurations.create(CONFIGURATION_NAME)
2020
}
2121

2222
public companion object {

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public abstract class ShadowJavaPlugin @Inject constructor(
3131
private val softwareComponentFactory: SoftwareComponentFactory,
3232
) : Plugin<Project> {
3333

34-
override fun apply(project: Project) {
35-
project.configureShadowJar()
36-
project.configureConfigurations()
37-
project.configureComponents()
38-
project.configureJavaGradlePlugin()
34+
override fun apply(project: Project): Unit = with(project) {
35+
configureShadowJar()
36+
configureConfigurations()
37+
configureComponents()
38+
configureJavaGradlePlugin()
3939
}
4040

4141
protected open fun Project.configureShadowJar() {

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowKmpPlugin.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
1212

1313
public abstract class ShadowKmpPlugin : Plugin<Project> {
1414

15-
override fun apply(project: Project) {
16-
with(project) {
17-
extensions.getByType(KotlinMultiplatformExtension::class.java).targets.configureEach { target ->
18-
if (target !is KotlinJvmTarget) return@configureEach
15+
override fun apply(project: Project): Unit = with(project) {
16+
extensions.getByType(KotlinMultiplatformExtension::class.java).targets.configureEach { target ->
17+
if (target !is KotlinJvmTarget) return@configureEach
1918

20-
configureShadowJar(target)
21-
}
19+
configureShadowJar(target)
2220
}
2321
}
2422

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@ import org.gradle.api.plugins.JavaPlugin
88

99
public abstract class ShadowPlugin : Plugin<Project> {
1010

11-
override fun apply(project: Project) {
12-
with(project.plugins) {
13-
apply(ShadowBasePlugin::class.java)
14-
@Suppress("WithTypeWithoutConfigureEach")
15-
withType(JavaPlugin::class.java) {
16-
apply(ShadowJavaPlugin::class.java)
17-
}
18-
@Suppress("WithTypeWithoutConfigureEach")
19-
withType(ApplicationPlugin::class.java) {
20-
apply(ShadowApplicationPlugin::class.java)
21-
}
22-
withId("org.jetbrains.kotlin.multiplatform") {
23-
apply(ShadowKmpPlugin::class.java)
24-
}
25-
26-
// Apply the legacy plugin last.
27-
// Because we apply the ShadowJavaPlugin/ShadowApplication plugin in a withType callback for the
28-
// respective JavaPlugin/ApplicationPlugin, it may still apply before the shadowJar task is created etc.
29-
// If the user applies shadow before those plugins. However, this is fine, because this was also
30-
// the behavior with the old plugin when applying in that order.
31-
apply(LegacyShadowPlugin::class.java)
11+
override fun apply(project: Project): Unit = with(project.plugins) {
12+
apply(ShadowBasePlugin::class.java)
13+
@Suppress("WithTypeWithoutConfigureEach")
14+
withType(JavaPlugin::class.java) {
15+
apply(ShadowJavaPlugin::class.java)
16+
}
17+
@Suppress("WithTypeWithoutConfigureEach")
18+
withType(ApplicationPlugin::class.java) {
19+
apply(ShadowApplicationPlugin::class.java)
3220
}
21+
withId("org.jetbrains.kotlin.multiplatform") {
22+
apply(ShadowKmpPlugin::class.java)
23+
}
24+
25+
// Apply the legacy plugin last.
26+
// Because we apply the ShadowJavaPlugin/ShadowApplication plugin in a withType callback for the
27+
// respective JavaPlugin/ApplicationPlugin, it may still apply before the shadowJar task is created etc.
28+
// If the user applies shadow before those plugins. However, this is fine, because this was also
29+
// the behavior with the old plugin when applying in that order.
30+
apply(LegacyShadowPlugin::class.java)
3331
}
3432
}

0 commit comments

Comments
 (0)