Skip to content

Commit cccc682

Browse files
committed
Add shadow plugin compatibility.
1 parent b0ecdb1 commit cccc682

File tree

6 files changed

+228
-9
lines changed

6 files changed

+228
-9
lines changed

dev/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ dependencies {
4848
include(project(":patching")) {
4949
transitive = false
5050
}
51+
// Shadow compatibility
52+
compileOnly("com.gradleup.shadow:shadow-gradle-plugin:8.3.7") {
53+
transitive = false
54+
}
5155
// For tests only
5256
testImplementation(gradleTestKit())
5357
testImplementation(gradleFixtures()) { transitive = false }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.dev;
25+
26+
import com.fox2code.foxloader.dependencies.DependencyHelper;
27+
import com.fox2code.foxloader.launcher.BuildConfig;
28+
import org.gradle.api.Project;
29+
30+
import java.util.ArrayList;
31+
import java.util.Collection;
32+
33+
public abstract class CompatibilityModule {
34+
private final String id;
35+
36+
public CompatibilityModule(String id) {
37+
this.id = id;
38+
}
39+
40+
public String getId() {
41+
return this.id;
42+
}
43+
44+
public void onApplyOnConfig(Project project, FoxLoaderConfig config) {}
45+
46+
public void onLateApply(Project project, FoxLoaderConfig config) {}
47+
48+
public static ArrayList<String> getDependenciesNamesToExclude(FoxLoaderConfig config) {
49+
ArrayList<String> dependenciesToExclude = new ArrayList<>();
50+
appendDependenciesNamesToExclude(config, dependenciesToExclude);
51+
return dependenciesToExclude;
52+
}
53+
54+
public static void appendDependenciesNamesToExclude(FoxLoaderConfig config, Collection<String> collection) {
55+
collection.add("org.lwjgl.lwjgl:lwjgl:2.9.1");
56+
collection.add("org.lwjgl.lwjgl:lwjgl_util:2.9.1");
57+
collection.add("org.lwjgl.lwjgl:lwjgl-platform:2.9.1");
58+
if (config.useLWJGLX) {
59+
collection.add("com.fox2code:lwjglx:" + config.LWJGLXVersion);
60+
}
61+
String foxLoaderVersion = BuildConfig.FOXLOADER_VERSION;
62+
if (config.foxLoaderLibVersionOverride != null) {
63+
foxLoaderVersion = config.foxLoaderLibVersionOverride;
64+
}
65+
collection.add("com.fox2code.FoxLoader:loader:" + foxLoaderVersion);
66+
for (DependencyHelper.Dependency dependency : DependencyHelper.commonDependencies) {
67+
collection.add(dependency.name);
68+
}
69+
for (DependencyHelper.Dependency dependency : DependencyHelper.commonDependenciesModernJava) {
70+
collection.add(dependency.name);
71+
}
72+
for (String bundle : config.usedDependencyBundlesList) {
73+
for (DependencyHelper.Dependency dependency : DependencyHelper.getDependencyBundle(bundle)) {
74+
collection.add(dependency.name);
75+
}
76+
}
77+
}
78+
}

dev/src/main/groovy/com/fox2code/foxloader/dev/FoxLoaderConfig.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
import com.fox2code.foxloader.dependencies.DependencyHelper;
2727
import com.fox2code.foxloader.launcher.BuildConfig;
2828
import org.gradle.api.Project;
29+
import org.gradle.api.Task;
2930
import org.gradle.api.tasks.TaskProvider;
3031
import org.gradle.jvm.tasks.Jar;
3132

3233
import java.text.Normalizer;
33-
import java.util.ArrayList;
34-
import java.util.NoSuchElementException;
35-
import java.util.StringJoiner;
34+
import java.util.*;
3635

3736
/**
3837
* Config for the FoxLoader gradle plugin
@@ -58,6 +57,7 @@ public FoxLoaderConfig() {}
5857
public String modWebsite;
5958
public String modClassTransformer;
6059
public String modLoadingPlugin;
60+
public boolean unofficial = false;
6161

6262
public void modDesc() {
6363
this.checkConfigMutable();
@@ -95,17 +95,29 @@ public String getUsedDependencyBundles() {
9595
return stringJoiner.toString();
9696
}
9797

98+
// Compatibility support
99+
final LinkedHashMap<String, CompatibilityModule> compatibilityModules = new LinkedHashMap<>();
100+
public boolean disableKotlinCompatibility = false;
101+
public boolean disableShadowCompatibility = false; // -> https://github.com/GradleUp/shadow
102+
103+
public void applyCompatibilityModule(CompatibilityModule compatibilityModule) {
104+
this.checkConfigMutable();
105+
if (!this.compatibilityModules.containsKey(compatibilityModule.getId())) {
106+
this.compatibilityModules.put(compatibilityModule.getId(), compatibilityModule);
107+
}
108+
}
109+
98110
// Special jar task replacement for complex build scripts
99111
Jar jarTaskToUse;
100-
TaskProvider<Jar> jarTaskToUseProvider;
112+
TaskProvider<? extends Task> jarTaskToUseProvider;
101113

102114
public final void useJarTask(Jar jarTaskToUse) {
103115
this.checkConfigMutable();
104116
this.jarTaskToUse = jarTaskToUse;
105117
this.jarTaskToUseProvider = null;
106118
}
107119

108-
public final void useJarTask(TaskProvider<Jar> jarTaskToUseProvider) {
120+
public final void useJarTask(TaskProvider<? extends Task> jarTaskToUseProvider) {
109121
this.checkConfigMutable();
110122
this.jarTaskToUse = null;
111123
this.jarTaskToUseProvider = jarTaskToUseProvider;
@@ -116,7 +128,7 @@ final Jar getJarTaskToUse(Project project) {
116128
return this.jarTaskToUse;
117129
}
118130
if (this.jarTaskToUseProvider != null) {
119-
return this.jarTaskToUseProvider.get();
131+
return (Jar) this.jarTaskToUseProvider.get();
120132
}
121133
return (Jar) project.getTasks().named("jar").get();
122134
}
@@ -126,7 +138,6 @@ final Jar getJarTaskToUse(Project project) {
126138
public String foxLoaderLibVersionOverride;
127139
public boolean localTesting = false;
128140
public boolean forceReload = false;
129-
public boolean unofficial = false;
130141
public boolean useLWJGLX = false;
131142
public String LWJGLXVersion = BuildConfig.LWJGLX_VERSION;
132143
public String LWJGLXLWJGLVersion = "3.3.6";

dev/src/main/groovy/com/fox2code/foxloader/dev/GradlePlugin.groovy

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package com.fox2code.foxloader.dev
2525

2626
import com.fox2code.foxloader.dependencies.DependencyHelper
27+
import com.fox2code.foxloader.dev.compatibility.KotlinCompatibility
28+
import com.fox2code.foxloader.dev.compatibility.ShadowCompatibility
2729
import com.fox2code.foxloader.launcher.BuildConfig
2830
import com.fox2code.foxloader.patching.PatchBridge
2931
import com.fox2code.foxloader.utils.Platform
@@ -202,8 +204,16 @@ class GradlePlugin implements Plugin<Project> {
202204
options.encoding = 'UTF-8'
203205
}
204206
FoxLoaderConfig config = ((FoxLoaderConfig) project.extensions.getByName("foxloader"))
205-
if (project.pluginManager.hasPlugin("org.jetbrains.kotlin.jvm")) {
206-
config.useDependencyBundle("kotlin")
207+
if ((!config.disableKotlinCompatibility) &&
208+
project.pluginManager.hasPlugin("org.jetbrains.kotlin.jvm")) {
209+
config.applyCompatibilityModule(KotlinCompatibility.INSTANCE)
210+
}
211+
if ((!config.disableShadowCompatibility) &&
212+
project.pluginManager.hasPlugin("com.gradleup.shadow")) {
213+
config.applyCompatibilityModule(ShadowCompatibility.INSTANCE)
214+
}
215+
for (CompatibilityModule compatibilityModule : config.compatibilityModules.values()) {
216+
compatibilityModule.onApplyOnConfig(project, config)
207217
}
208218
config.configImmutable = true
209219
for (DependencyHelper.Dependency dependency : DependencyHelper.commonDependencies) {
@@ -432,6 +442,9 @@ class GradlePlugin implements Plugin<Project> {
432442
}
433443
}
434444
}
445+
for (CompatibilityModule compatibilityModule : config.compatibilityModules.values()) {
446+
compatibilityModule.onLateApply(project, config)
447+
}
435448
}
436449
}
437450

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.dev.compatibility
25+
26+
import com.fox2code.foxloader.dev.CompatibilityModule
27+
import com.fox2code.foxloader.dev.FoxLoaderConfig
28+
import org.gradle.api.Project
29+
30+
final class KotlinCompatibility extends CompatibilityModule {
31+
public static CompatibilityModule INSTANCE = new KotlinCompatibility()
32+
33+
private KotlinCompatibility() {
34+
super("kotlin")
35+
}
36+
37+
@Override
38+
void onApplyOnConfig(Project project, FoxLoaderConfig config) {
39+
config.useDependencyBundle("kotlin")
40+
}
41+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.dev.compatibility
25+
26+
import com.fox2code.foxloader.dev.CompatibilityModule
27+
import com.fox2code.foxloader.dev.FoxLoaderConfig
28+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
29+
import org.gradle.api.Project
30+
import org.gradle.api.artifacts.ResolvedDependency
31+
import org.gradle.api.specs.Spec
32+
33+
final class ShadowCompatibility extends CompatibilityModule {
34+
public static CompatibilityModule INSTANCE = new ShadowCompatibility()
35+
36+
private ShadowCompatibility() {
37+
super("shadow")
38+
}
39+
40+
@Override
41+
void onApplyOnConfig(Project project, FoxLoaderConfig config) {
42+
config.useJarTask(project.tasks.named('shadowJar', ShadowJar))
43+
project.tasks.jar.enabled = false
44+
project.tasks.assemble.dependsOn("shadowJar")
45+
}
46+
47+
@Override
48+
void onLateApply(Project project, FoxLoaderConfig config) {
49+
project.tasks.named('shadowJar', ShadowJar) {
50+
archiveClassifier.set("")
51+
dependencies {
52+
for (String dependencyToExclude : getDependenciesNamesToExclude(config)) {
53+
exclude(dependency(dependencyToExclude))
54+
}
55+
// LWJGLX support.
56+
exclude(new Spec<? super ResolvedDependency>() {
57+
@Override
58+
boolean isSatisfiedBy(Object o) {
59+
return o instanceof ResolvedDependency &&
60+
isSatisfiedBy(o as ResolvedDependency)
61+
}
62+
63+
boolean isSatisfiedBy(ResolvedDependency resolvedDependency) {
64+
String dependencyName = resolvedDependency.getName()
65+
return dependencyName.startsWith("org.lwjgl.lwjgl:") ||
66+
dependencyName.startsWith("org.lwjgl:")
67+
}
68+
})
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)