26
26
import org .gradle .api .tasks .compile .ForkOptions ;
27
27
import org .gradle .api .tasks .compile .JavaCompile ;
28
28
import org .gradle .internal .jvm .Jvm ;
29
+ import org .gradle .jvm .toolchain .JavaLanguageVersion ;
30
+ import org .gradle .jvm .toolchain .JavaLauncher ;
31
+ import org .gradle .jvm .toolchain .JavaToolchainService ;
29
32
import xyz .wagyourtail .unimined .api .UniminedExtension ;
30
33
import xyz .wagyourtail .unimined .api .minecraft .MinecraftConfig ;
31
34
47
50
public class ProguardTask extends BaritoneGradleTask {
48
51
49
52
@ Input
50
- private String url ;
53
+ private String proguardVersion ;
51
54
52
- public String getUrl () {
53
- return url ;
54
- }
55
-
56
- @ Input
57
- private String extract ;
58
-
59
- public String getExtract () {
60
- return extract ;
55
+ public String getProguardVersion () {
56
+ return proguardVersion ;
61
57
}
62
58
63
59
private List <String > requiredLibraries ;
@@ -99,98 +95,33 @@ private void processArtifact() throws Exception {
99
95
}
100
96
101
97
private void downloadProguard () throws Exception {
102
- Path proguardZip = getTemporaryFile (PROGUARD_ZIP );
98
+ Path proguardZip = getTemporaryFile (String . format ( PROGUARD_ZIP , proguardVersion ) );
103
99
if (!Files .exists (proguardZip )) {
104
- write (new URL (this . url ).openStream (), proguardZip );
100
+ write (new URL (String . format ( "https://github.com/Guardsquare/proguard/releases/download/v%s/proguard-%s.zip" , proguardVersion , proguardVersion ) ).openStream (), proguardZip );
105
101
}
106
102
}
107
103
108
104
private void extractProguard () throws Exception {
109
- Path proguardJar = getTemporaryFile (PROGUARD_JAR );
105
+ Path proguardJar = getTemporaryFile (String . format ( PROGUARD_JAR , proguardVersion ) );
110
106
if (!Files .exists (proguardJar )) {
111
- ZipFile zipFile = new ZipFile (getTemporaryFile (PROGUARD_ZIP ).toFile ());
112
- ZipEntry zipJarEntry = zipFile .getEntry (this . extract );
107
+ ZipFile zipFile = new ZipFile (getTemporaryFile (String . format ( PROGUARD_ZIP , proguardVersion ) ).toFile ());
108
+ ZipEntry zipJarEntry = zipFile .getEntry (String . format ( "proguard-%s/lib/proguard.jar" , proguardVersion ) );
113
109
write (zipFile .getInputStream (zipJarEntry ), proguardJar );
114
110
zipFile .close ();
115
111
}
116
112
}
117
113
118
- private String getJavaBinPathForProguard () throws Exception {
119
- String path ;
120
- try {
121
- path = findJavaPathByGradleConfig ();
122
- if (path != null ) return path ;
123
- } catch (Exception ex ) {
124
- System .err .println ("Unable to find java by javaCompile options" );
125
- ex .printStackTrace ();
126
- }
127
-
128
- path = findJavaByGradleCurrentRuntime ();
129
- if (path != null ) return path ;
130
-
131
- try {
132
- path = findJavaByJavaHome ();
133
- if (path != null ) return path ;
134
- } catch (Exception ex ) {
135
- System .err .println ("Unable to find java by JAVA_HOME" );
136
- ex .printStackTrace ();
137
- }
138
-
139
- throw new Exception ("Unable to find java to determine ProGuard libraryjars. Please specify forkOptions.executable in javaCompile," +
140
- " JAVA_HOME environment variable, or make sure to run Gradle with the correct JDK (a v1.8 only)" );
141
- }
142
-
143
- private String findJavaByGradleCurrentRuntime () {
144
- String path = Jvm .current ().getJavaExecutable ().getAbsolutePath ();
145
- System .out .println ("Using Gradle's runtime Java for ProGuard" );
146
- return path ;
147
- }
148
-
149
- private String findJavaByJavaHome () {
150
- final String javaHomeEnv = System .getenv ("JAVA_HOME" );
151
- if (javaHomeEnv != null ) {
152
- String path = Jvm .forHome (new File (javaHomeEnv )).getJavaExecutable ().getAbsolutePath ();
153
- System .out .println ("Detected Java path by JAVA_HOME" );
154
- return path ;
155
- }
156
- return null ;
157
- }
114
+ private JavaLauncher getJavaLauncherForProguard () {
115
+ var toolchains = getProject ().getExtensions ().getByType (JavaToolchainService .class );
116
+ var toolchain = toolchains .launcherFor ((spec ) -> {
117
+ spec .getLanguageVersion ().set (JavaLanguageVersion .of (getProject ().findProperty ("java_version" ).toString ()));
118
+ }).getOrNull ();
158
119
159
- private String findJavaPathByGradleConfig () {
160
- final TaskCollection <JavaCompile > javaCompiles = super .getProject ().getTasks ().withType (JavaCompile .class );
161
-
162
- final JavaCompile compileTask = javaCompiles .iterator ().next ();
163
- final ForkOptions forkOptions = compileTask .getOptions ().getForkOptions ();
164
-
165
- if (forkOptions != null ) {
166
- String javacPath = forkOptions .getExecutable ();
167
- if (javacPath != null ) {
168
- File javacFile = new File (javacPath );
169
- if (javacFile .exists ()) {
170
- File [] maybeJava = javacFile .getParentFile ().listFiles ((dir , name ) -> name .equals ("java" ));
171
- if (maybeJava != null && maybeJava .length > 0 ) {
172
- String path = maybeJava [0 ].getAbsolutePath ();
173
- System .out .println ("Detected Java path by forkOptions" );
174
- return path ;
175
- }
176
- }
177
- }
120
+ if (toolchain == null ) {
121
+ throw new IllegalStateException ("Java toolchain not found" );
178
122
}
179
- return null ;
180
- }
181
123
182
- private boolean validateJavaVersion (String java ) {
183
- //TODO: fix for j16
184
- // final JavaVersion javaVersion = new DefaultJvmVersionDetector(new DefaultExecActionFactory(new IdentityFileResolver())).getJavaVersion(java);
185
- //
186
- // if (!javaVersion.getMajorVersion().equals("8")) {
187
- // System.out.println("Failed to validate Java version " + javaVersion.toString() + " [" + java + "] for ProGuard libraryjars");
188
- // // throw new RuntimeException("Java version incorrect: " + javaVersion.getMajorVersion() + " for " + java);
189
- // return false;
190
- // }
191
- //
192
- // System.out.println("Validated Java version " + javaVersion.toString() + " [" + java + "] for ProGuard libraryjars");
193
- return true ;
124
+ return toolchain ;
194
125
}
195
126
196
127
private void generateConfigs () throws Exception {
@@ -284,13 +215,8 @@ private void cleanup() {
284
215
} catch (IOException ignored ) {}
285
216
}
286
217
287
- public void setUrl (String url ) {
288
- this .url = url ;
289
- }
290
-
291
-
292
- public void setExtract (String extract ) {
293
- this .extract = extract ;
218
+ public void setProguardVersion (String url ) {
219
+ this .proguardVersion = url ;
294
220
}
295
221
296
222
private void runProguard (Path config ) throws Exception {
@@ -299,39 +225,15 @@ private void runProguard(Path config) throws Exception {
299
225
Files .delete (this .proguardOut );
300
226
}
301
227
302
- // Make paths relative to work directory; fixes spaces in path to config, @"" doesn't work
303
228
Path workingDirectory = getTemporaryFile ("" );
304
- Path proguardJar = workingDirectory .relativize (getTemporaryFile (PROGUARD_JAR ));
305
- config = workingDirectory .relativize (config );
306
-
307
- // Honestly, if you still have spaces in your path at this point, you're SOL.
308
229
309
- Process p = new ProcessBuilder ("java" , "-jar" , proguardJar .toString (), "@" + config .toString ())
310
- .directory (workingDirectory .toFile ()) // Set the working directory to the temporary folder]
311
- .start ();
230
+ getProject ().javaexec (spec -> {
231
+ spec .workingDir (workingDirectory .toFile ());
232
+ spec .args ("@" + workingDirectory .relativize (config ));
233
+ spec .classpath (getTemporaryFile (String .format (PROGUARD_JAR , proguardVersion )));
312
234
313
- // We can't do output inherit process I/O with gradle for some reason and have it work, so we have to do this
314
- this .printOutputLog (p .getInputStream (), System .out );
315
- this .printOutputLog (p .getErrorStream (), System .err );
316
-
317
- // Halt the current thread until the process is complete, if the exit code isn't 0, throw an exception
318
- int exitCode = p .waitFor ();
319
- if (exitCode != 0 ) {
320
- Thread .sleep (1000 );
321
- throw new IllegalStateException ("Proguard exited with code " + exitCode );
322
- }
235
+ spec .executable (getJavaLauncherForProguard ().getExecutablePath ().getAsFile ());
236
+ }).assertNormalExitValue ().rethrowFailure ();
323
237
}
324
238
325
- private void printOutputLog (InputStream stream , PrintStream outerr ) {
326
- new Thread (() -> {
327
- try (BufferedReader reader = new BufferedReader (new InputStreamReader (stream ))) {
328
- String line ;
329
- while ((line = reader .readLine ()) != null ) {
330
- outerr .println (line );
331
- }
332
- } catch (Exception e ) {
333
- e .printStackTrace ();
334
- }
335
- }).start ();
336
- }
337
239
}
0 commit comments