1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.multivm
;
12 import cc
.squirreljme
.plugin
.multivm
.ident
.SourceTargetClassifier
;
13 import cc
.squirreljme
.plugin
.util
.GradleLoggerOutputStream
;
14 import cc
.squirreljme
.plugin
.util
.UnassistedLaunchEntry
;
15 import cc
.squirreljme
.plugin
.util
.GradleJavaExecSpecFiller
;
16 import java
.io
.IOException
;
17 import java
.nio
.file
.Files
;
18 import java
.nio
.file
.Path
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collection
;
21 import java
.util
.LinkedHashMap
;
22 import java
.util
.LinkedHashSet
;
23 import java
.util
.stream
.Stream
;
24 import org
.gradle
.api
.Action
;
25 import org
.gradle
.api
.Project
;
26 import org
.gradle
.api
.Task
;
27 import org
.gradle
.api
.logging
.LogLevel
;
28 import org
.gradle
.api
.tasks
.SourceSet
;
29 import org
.gradle
.process
.ExecResult
;
32 * This is the action for running the full entire suite in the emulator.
36 public class VMFullSuiteTaskAction
37 implements Action
<Task
>
39 /** The additional libraries to load. */
40 public static final String LIBRARIES_PROPERTY
=
43 /** The source target classifier used. */
44 protected final SourceTargetClassifier classifier
;
47 * Initializes the task.
49 * @param __classifier The classifier used.
50 * @throws NullPointerException On null arguments.
53 public VMFullSuiteTaskAction(SourceTargetClassifier __classifier
)
54 throws NullPointerException
56 if (__classifier
== null)
57 throw new NullPointerException("NARG");
59 this.classifier
= __classifier
;
67 public void execute(Task __task
)
69 Project root
= __task
.getProject().getRootProject();
71 // We need all the libraries to load and to be available
72 Collection
<Path
> libPath
= VMHelpers
.fullSuiteLibraries(__task
);
74 // Additional items onto the library set?
75 String exLib
= System
.getProperty(
76 VMFullSuiteTaskAction
.LIBRARIES_PROPERTY
);
78 for (Path p
: VMHelpers
.classpathDecode(exLib
))
80 // Add contents of a given directory
81 if (Files
.isDirectory(p
))
82 try (Stream
<Path
> s
= Files
.list(p
))
84 libPath
.addAll(Arrays
.asList(s
.toArray(Path
[]::new)));
96 // Determine the initial classpath of the launcher, which is always
98 SourceTargetClassifier withMain
= this.classifier
99 .withSourceSet(SourceSet
.MAIN_SOURCE_SET_NAME
);
100 Collection
<Path
> classPath
= new LinkedHashSet
<>();
101 classPath
.addAll(Arrays
102 .asList(VMHelpers
.runClassPath((VMExecutableTask
)root
.project(
103 ":modules:launcher").getTasks().getByName(TaskInitialization
104 .task("lib", withMain
)),
107 // Debug these, just to ensure they work
108 __task
.getLogger().debug("LibPath: {}", libPath
);
109 __task
.getLogger().debug("Classpath: {}", classPath
);
111 // Run the virtual machine with everything
112 ExecResult exitResult
= __task
.getProject().javaexec(__spec
->
114 // Use filled JVM arguments
115 this.classifier
.getVmType().spawnJvmArguments(
118 new GradleJavaExecSpecFiller(__spec
),
119 UnassistedLaunchEntry
.MIDLET_MAIN_CLASS
,
121 new LinkedHashMap
<String
, String
>(),
122 libPath
.<Path
>toArray(new Path
[libPath
.size()]),
123 classPath
.<Path
>toArray(new Path
[classPath
.size()]),
124 "cc.squirreljme.runtime.launcher.ui.MidletMain");
126 // Use these streams directly
127 __spec
.setStandardOutput(new GradleLoggerOutputStream(
128 __task
.getLogger(), LogLevel
.LIFECYCLE
, -1, -1));
129 __spec
.setErrorOutput(new GradleLoggerOutputStream(
130 __task
.getLogger(), LogLevel
.ERROR
, -1, -1));
133 // Did the task fail?
134 int exitValue
= exitResult
.getExitValue();
136 throw new RuntimeException("Task exited with: " + exitValue
);