1919import 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 */
2629public 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 );
0 commit comments