Skip to content

Commit 8a40eaf

Browse files
committed
Migrate all system property checks to single location for better documentation.
Add net.minecraftforge.gradle.repo.sources, to disable creating source artifacts Add net.minecraftforge.gradle.repo.recompile, to disable recompiling Minecraft in UserDev Add net.minecraftforge.gradle.repo.recompile.args, in case you want to add extra arguments Add net.minecraftforge.gradle.repo.recompile.fork, to enable forking the recompile process Add net.minecraftforge.gradle.repo.recompile.fork.args, in case you want to add extra arguments to the newly forked process Make it non-fatal when recompiling Minecraft in UserDev fails. Make the UserDev Minecraft recompile less spammy by passing -nowarn
1 parent 3e772e9 commit 8a40eaf

File tree

6 files changed

+172
-22
lines changed

6 files changed

+172
-22
lines changed

src/common/java/net/minecraftforge/gradle/common/util/BaseRepo.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import javax.annotation.Nullable;
2828

2929
public abstract class BaseRepo implements ArtifactProvider<ArtifactIdentifier> {
30-
31-
public static final boolean DEBUG = Boolean.getBoolean("fg.debugRepo");
32-
3330
private final File cache;
3431
protected final Logger log;
3532
protected final String REPO_NAME = getClass().getSimpleName();
@@ -52,7 +49,7 @@ protected String clean(ArtifactIdentifier art) {
5249
}
5350

5451
protected void debug(String message) {
55-
if (DEBUG)
52+
if (EnvironmentChecks.DEBUG_REPOS.isEnabled())
5653
this.log.lifecycle(message);
5754
}
5855
protected void info(String message) {

src/common/java/net/minecraftforge/gradle/common/util/EnvironmentChecks.java

Lines changed: 132 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,143 @@
1919
import javax.net.ssl.SSLException;
2020

2121
/**
22-
* Various environment checks.
23-
*
22+
* Utility for common environment variables to toggle various features in FG.
23+
* <p>
24+
* These can be configured using the -D{Name}={true|false} to set the system property.
25+
* <p>
26+
* Various environment checks, such as Java version, Gradle version, and certificate validation.
2427
* @see #checkEnvironment(Project)
2528
*/
2629
public class EnvironmentChecks {
2730
public static final String ENABLE_CERTIFICATE_CHECK_VARIABLE = "net.minecraftforge.gradle.check.certs";
2831
public static final String ENABLE_GRADLE_CHECK_VARIABLE = "net.minecraftforge.gradle.check.gradle";
2932
public static final String ENABLE_JAVA_CHECK_VARIABLE = "net.minecraftforge.gradle.check.java";
33+
private static final String FILTER_REPOS_VARIABLE = "net.minecraftforge.gradle.filter.repos";
34+
private static final String INVALIDATE_CACHE_VARIABLE = "net.minecraftforge.gradle.invalidate.cache";
35+
private static final String DEBUG_REPOS_VARIABLE = "net.minecraftforge.gradle.repo.debug";
36+
private static final String ENABLE_SOURCES_VARIABLE = "net.minecraftforge.gradle.repo.sources";
37+
private static final String ENABLE_RECOMPILE_VARIABLE = "net.minecraftforge.gradle.repo.recompile";
38+
private static final String RECOMPILE_ARGS_VARIABLE = "net.minecraftforge.gradle.repo.recompile.args";
39+
private static final String ENABLE_RECOMPILE_FORK_VARIABLE = "net.minecraftforge.gradle.repo.recompile.fork";
40+
private static final String RECOMPILE_FORK_ARGS_VARIABLE = "net.minecraftforge.gradle.repo.recompile.fork.args";
41+
42+
private static final EnvironmentFlag ENABLE_CERTIFICATE_CHECK = new EnvironmentFlag(ENABLE_CERTIFICATE_CHECK_VARIABLE, true);
43+
private static final EnvironmentFlag ENABLE_GRADLE_CHECK = new EnvironmentFlag(ENABLE_GRADLE_CHECK_VARIABLE, true);
44+
private static final EnvironmentFlag ENABLE_JAVA_CHECK = new EnvironmentFlag(ENABLE_JAVA_CHECK_VARIABLE, true);
45+
46+
/**
47+
* Attempts to filter all repositories to not include any 'mapped' dependencies, this should
48+
* speed up dependency resolution by not having it check public repositories for things we create.
49+
* <p>
50+
* Specifically it filters anything with a version that matches `.*_mapped_.*`
51+
* <p>
52+
* Environment Flag: {@value #FILTER_REPOS_VARIABLE}
53+
*/
54+
public static final EnvironmentFlag FILTER_REPOS = new EnvironmentFlag(FILTER_REPOS_VARIABLE, true);
55+
56+
/**
57+
* Forces anything that uses {@link net.minecraftforge.gradle.common.util.HashStore HashStore} to miss
58+
* the cache this run, forcing all tasks and dependencies to be re-evaluated.
59+
* <p>
60+
* This is typically only used when developing ForgeGradle itself, as we would want to bust the cache
61+
* when the code changes.
62+
* <p>
63+
* Environment Flag: {@value #INVALIDATE_CACHE_VARIABLE}
64+
*/
65+
public static final EnvironmentFlag INVALIDATE_CACHE = new EnvironmentFlag(INVALIDATE_CACHE_VARIABLE, false);
66+
67+
/**
68+
* Enables debugging for repositories, this will log a lot of information about what the repositories are doing.
69+
* <p>
70+
* Environment Flag: {@value #DEBUG_REPOS_VARIABLE}
71+
*/
72+
public static final EnvironmentFlag DEBUG_REPOS = new EnvironmentFlag(DEBUG_REPOS_VARIABLE, false);
73+
74+
/**
75+
* Enables generating source artifacts in our dynamic repositories. Disabling this is recommended for
76+
* build services where the source code is not needed.<br>
77+
* Default is true.
78+
* <p>
79+
* Environment Flag: {@value #ENABLE_SOURCES_VARIABLE}
80+
*/
81+
public static final EnvironmentFlag ENABLE_SOURCES = new EnvironmentFlag(ENABLE_SOURCES_VARIABLE, true);
82+
83+
/**
84+
* Enables recompiling Minecraft's source into a jar and serving it from the User Repo after the source
85+
* has been requested. You can disable this if you don't care about line numbers matching between source
86+
* and binary. <br>
87+
* Default is true.
88+
* <p>
89+
* Environment Flag: {@value #ENABLE_RECOMPILE_VARIABLE}
90+
*/
91+
public static final EnvironmentFlag ENABLE_RECOMPILE = new EnvironmentFlag(ENABLE_RECOMPILE_VARIABLE, true);
92+
93+
/**
94+
* Additional arguments to pass to the recompile process in the UserDev repo. Added in cause you need to
95+
* add more memory or something. <br>
96+
* Default is null.
97+
* <p>
98+
* Environment Value: {@value #RECOMPILE_ARGS_VARIABLE}
99+
*/
100+
public static final EnvironmentValue RECOMPILE_ARGS = new EnvironmentValue(RECOMPILE_ARGS_VARIABLE, null);
101+
102+
/**
103+
* Enables forking the recompile process into a separate JVM. <br>
104+
* Default is false.
105+
* <p>
106+
* Environment Flag: {@value #ENABLE_RECOMPILE_FORK_VARIABLE}
107+
*/
108+
public static final EnvironmentFlag ENABLE_RECOMPILE_FORK = new EnvironmentFlag(ENABLE_RECOMPILE_FORK_VARIABLE, false);
109+
110+
/**
111+
* Additional arguments to pass to the recompile process in the UserDev repo when being forked. Added in cause you need to
112+
* add more memory or something. <br>
113+
* Default is null.
114+
* <p>
115+
* Environment Value: {@value #RECOMPILE_FORK_ARGS_VARIABLE}
116+
*/
117+
public static final EnvironmentValue RECOMPILE_FORK_ARGS = new EnvironmentValue(RECOMPILE_FORK_ARGS_VARIABLE, null);
30118

31-
private static final boolean ENABLE_CERTIFICATE_CHECK = Boolean.parseBoolean(System.getProperty(ENABLE_CERTIFICATE_CHECK_VARIABLE, "true"));
32-
private static final boolean ENABLE_GRADLE_CHECK = Boolean.parseBoolean(System.getProperty(ENABLE_GRADLE_CHECK_VARIABLE, "true"));
33-
private static final boolean ENABLE_JAVA_CHECK = Boolean.parseBoolean(System.getProperty(ENABLE_JAVA_CHECK_VARIABLE, "true"));
34119
private static final Marker ENV_CHECK = MarkerFactory.getMarker("forgegradle.env_check");
35120

121+
public static final class EnvironmentFlag {
122+
private final String key;
123+
private final String simpleKey;
124+
private final boolean _default;
125+
126+
private EnvironmentFlag(String key, boolean _default) {
127+
this.key = key;
128+
this.simpleKey = key.replace("net.minecraftforge.gradle.", "fg.");
129+
this._default = _default;
130+
}
131+
132+
public boolean isEnabled() {
133+
String val = System.getProperty(key);
134+
if (val == null) val = System.getProperty(simpleKey);
135+
//if (val == null) val = System.getenv(key);
136+
//if (val == null) val = System.getenv(simpleKey);
137+
return val == null ? _default : Boolean.parseBoolean(val);
138+
}
139+
}
140+
141+
public static final class EnvironmentValue {
142+
private final String key;
143+
private final String simpleKey;
144+
private final String _default;
145+
146+
private EnvironmentValue(String key, String _default) {
147+
this.key = key;
148+
this.simpleKey = key.replace("net.minecraftforge.gradle.", "fg.");
149+
this._default = _default;
150+
}
151+
152+
public String getValue() {
153+
String val = System.getProperty(key);
154+
if (val == null) val = System.getProperty(simpleKey);
155+
return val == null ? _default : val;
156+
}
157+
}
158+
36159
public static void checkJavaRange(@Nullable JavaVersionParser.JavaVersion minVersionInclusive, @Nullable JavaVersionParser.JavaVersion maxVersionExclusive) {
37160
checkRange("java", JavaVersionParser.getCurrentJavaVersion(), minVersionInclusive, maxVersionExclusive, "", "");
38161
}
@@ -69,7 +192,7 @@ private static <T> void checkRange(String name, Comparable<T> current, @Nullable
69192
*/
70193
public static void checkEnvironment(Project project) {
71194
Logger logger = project.getLogger();
72-
if (ENABLE_JAVA_CHECK) {
195+
if (ENABLE_JAVA_CHECK.isEnabled()) {
73196
logger.debug(ENV_CHECK, "Checking Java version");
74197
checkJavaRange(
75198
// Minimum must be update 101 as it's the first one to include Let's Encrypt certificates.
@@ -80,7 +203,7 @@ public static void checkEnvironment(Project project) {
80203
logger.debug(ENV_CHECK, "Java version check disabled by system property");
81204
}
82205

83-
if (ENABLE_GRADLE_CHECK) {
206+
if (ENABLE_GRADLE_CHECK.isEnabled()) {
84207
logger.debug(ENV_CHECK, "Checking Gradle version");
85208
checkGradleRange(
86209
GradleVersion.version("8.1"),
@@ -90,7 +213,7 @@ public static void checkEnvironment(Project project) {
90213
logger.debug(ENV_CHECK, "Gradle version check disabled by system property");
91214
}
92215

93-
if (ENABLE_CERTIFICATE_CHECK) {
216+
if (ENABLE_CERTIFICATE_CHECK.isEnabled()) {
94217
logger.debug(ENV_CHECK, "Checking server connections");
95218
testServerConnection(Utils.FORGE_MAVEN);
96219
testServerConnection(Utils.MOJANG_MAVEN);
@@ -116,6 +239,7 @@ private static void testServerConnection(String url) {
116239
/**
117240
* Exception thrown when an environment check fails.
118241
*/
242+
@SuppressWarnings("serial")
119243
static class EnvironmentCheckFailedException extends RuntimeException {
120244
EnvironmentCheckFailedException(String message) {
121245
super(message);

src/common/java/net/minecraftforge/gradle/common/util/HashStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import javax.annotation.Nullable;
2020

2121
public class HashStore {
22-
private final boolean INVALIDATE_CACHE = System.getProperty("FG_INVALIDATE_CACHE", "false").equals("true");
2322
private final int RAND_CACHE = new Random().nextInt();
2423

2524
private final String root;
@@ -127,7 +126,7 @@ public HashStore add(File file) {
127126
}
128127

129128
public boolean isSame() {
130-
if (INVALIDATE_CACHE)
129+
if (EnvironmentChecks.INVALIDATE_CACHE.isEnabled())
131130
add("invalidate", "" + RAND_CACHE);
132131
return oldHashes.equals(newHashes);
133132
}

src/common/java/net/minecraftforge/gradle/common/util/Utils.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@
7777
import javax.annotation.Nullable;
7878

7979
public class Utils {
80-
private static final boolean ENABLE_FILTER_REPOS = Boolean.parseBoolean(System.getProperty("net.minecraftforge.gradle.filter_repos", "true"));
81-
8280
public static final Gson GSON = new GsonBuilder()
8381
.registerTypeAdapter(MCPConfigV1.Step.class, new MCPConfigV1.Step.Deserializer())
8482
.registerTypeAdapter(VersionJson.Argument.class, new VersionJson.Argument.Deserializer())
@@ -411,7 +409,7 @@ public static void createRunConfigTasks(final MinecraftExtension extension, fina
411409
}
412410

413411
public static void addRepoFilters(Project project) {
414-
if (!ENABLE_FILTER_REPOS) return;
412+
if (!EnvironmentChecks.FILTER_REPOS.isEnabled()) return;
415413

416414
if (project.getGradle().getStartParameter().getTaskNames().stream().anyMatch(t -> t.endsWith("DownloadSources"))) {
417415
// Only modify repos already present to fix issues with IntelliJ's download sources

src/userdev/java/net/minecraftforge/gradle/userdev/MinecraftUserRepo.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraftforge.gradle.common.tasks.JarExec;
2424
import net.minecraftforge.gradle.common.util.Artifact;
2525
import net.minecraftforge.gradle.common.util.BaseRepo;
26+
import net.minecraftforge.gradle.common.util.EnvironmentChecks;
2627
import net.minecraftforge.gradle.common.util.HashFunction;
2728
import net.minecraftforge.gradle.common.util.HashStore;
2829
import net.minecraftforge.gradle.common.util.MavenArtifactDownloader;
@@ -40,7 +41,6 @@
4041
import net.minecraftforge.gradle.userdev.tasks.ApplyMCPFunction;
4142
import net.minecraftforge.gradle.userdev.tasks.HackyJavaCompile;
4243
import net.minecraftforge.gradle.userdev.tasks.RenameJar;
43-
import net.minecraftforge.gradle.userdev.tasks.RenameJarInPlace;
4444
import net.minecraftforge.srgutils.IMappingFile;
4545
import net.minecraftforge.srgutils.IMappingFile.IField;
4646
import net.minecraftforge.srgutils.IMappingFile.IMethod;
@@ -1081,6 +1081,7 @@ private File findPatched(boolean generate) throws IOException {
10811081

10821082
boolean failed = false;
10831083
byte[] lastPatched = FileUtils.readFileToByteArray(decomp);
1084+
boolean debug = EnvironmentChecks.DEBUG_REPOS.isEnabled();
10841085
for (Patcher p : parents) {
10851086
ByteArrayOutputStream bout = new ByteArrayOutputStream();
10861087
PatchOperation.Builder opBuilder = PatchOperation.builder()
@@ -1090,8 +1091,8 @@ private File findPatched(boolean generate) throws IOException {
10901091
.patchesPrefix(p.getPatches())
10911092
.outputPath(bout, ArchiveFormat.ZIP)
10921093
.mode(PatchMode.ACCESS)
1093-
.verbose(DEBUG)
1094-
.summary(DEBUG);
1094+
.verbose(debug)
1095+
.summary(debug);
10951096
// Note that pre-1.13 patches use ../{src-base,src-work}/minecraft/ prefixes
10961097
// instead of the default {a,b}/ prefixes. Also, be sure not to override the
10971098
// defaults with null values.
@@ -1144,6 +1145,9 @@ private File findPatched(boolean generate) throws IOException {
11441145

11451146
@Nullable
11461147
private File findSource(@Nullable String mapping, boolean generate) throws IOException {
1148+
if (!EnvironmentChecks.ENABLE_SOURCES.isEnabled())
1149+
return null;
1150+
11471151
File patched = findPatched(generate);
11481152
if (patched == null || !patched.exists()) {
11491153
debug(" Finding Source: Patched not found");
@@ -1213,6 +1217,9 @@ private File findSource(@Nullable String mapping, boolean generate) throws IOExc
12131217

12141218
@Nullable
12151219
private File findRecomp(@Nullable String mapping, boolean generate) throws IOException {
1220+
if (!EnvironmentChecks.ENABLE_RECOMPILE.isEnabled())
1221+
return null;
1222+
12161223
File source = findSource(mapping, generate);
12171224
if (source == null || !source.exists()) {
12181225
debug(" Finding Recomp: Sources not found");
@@ -1238,8 +1245,8 @@ private File findRecomp(@Nullable String mapping, boolean generate) throws IOExc
12381245
debug(" Compiling");
12391246
File compiled = compileJava(source);
12401247
if (compiled == null) {
1241-
debug(" Compiling failed");
1242-
throw new IllegalStateException("Compile failed in findRecomp. See log for more details");
1248+
info("Compile failed in findRecomp. See log for more details");
1249+
return null;
12431250
}
12441251

12451252
debug(" Injecting resources");
@@ -1360,6 +1367,27 @@ private File compileJava(File source, File... extraDeps) {
13601367
compile.getDestinationDirectory().set(output);
13611368
compile.setSource(source.isDirectory() ? project.fileTree(source) : project.zipTree(source));
13621369

1370+
List<String> args = new ArrayList<>(compile.getOptions().getCompilerArgs());
1371+
String customArgs = EnvironmentChecks.RECOMPILE_ARGS.getValue();
1372+
if (customArgs == null) {
1373+
//args.add("-Xlint:none");
1374+
args.add("-nowarn"); // Shut up about simple warnings
1375+
} else {
1376+
args.addAll(Arrays.asList(customArgs.split(" ")));
1377+
}
1378+
compile.getOptions().setCompilerArgs(args);
1379+
1380+
if (EnvironmentChecks.ENABLE_RECOMPILE_FORK.isEnabled()) {
1381+
compile.getOptions().setFork(true);
1382+
1383+
customArgs = EnvironmentChecks.RECOMPILE_FORK_ARGS.getValue();
1384+
if (customArgs != null) {
1385+
args = new ArrayList<>(compile.getOptions().getForkOptions().getJvmArgs());
1386+
args.addAll(Arrays.asList(customArgs.split(" ")));
1387+
compile.getOptions().getForkOptions().setJvmArgs(args);
1388+
}
1389+
}
1390+
13631391
compile.doHackyCompile();
13641392

13651393
return output;

src/userdev/java/net/minecraftforge/gradle/userdev/util/DeobfuscatingRepo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier;
99
import net.minecraftforge.gradle.common.util.Artifact;
1010
import net.minecraftforge.gradle.common.util.BaseRepo;
11+
import net.minecraftforge.gradle.common.util.EnvironmentChecks;
1112
import net.minecraftforge.gradle.common.util.MavenArtifactDownloader;
1213
import net.minecraftforge.gradle.common.util.Utils;
1314

@@ -130,6 +131,9 @@ private File findRaw(Artifact artifact, String mapping) throws IOException {
130131

131132
@Nullable
132133
private File findSource(Artifact artifact, String mapping) throws IOException {
134+
if (!EnvironmentChecks.ENABLE_SOURCES.isEnabled())
135+
return null;
136+
133137
// Check if we have previously failed to retrieve sources for the artifact.
134138
// If so, don't attempt the download again.
135139
File noSourceFlag = cache(getArtifactPath(artifact, mapping) + ".nosources");

0 commit comments

Comments
 (0)