Indentations break the feed.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMRunWhateverTaskAction.java
blobdf60e55973eb3957229dc9763bde65a70b334d94
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.multivm;
12 import cc.squirreljme.plugin.swm.JavaMEMidlet;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.Paths;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.jar.Attributes;
24 import java.util.jar.Manifest;
25 import java.util.regex.Pattern;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.gradle.api.Action;
30 import org.gradle.api.Task;
32 /**
33 * Action for the running of whatever task.
35 * @since 2024/07/28
37 public class VMRunWhateverTaskAction
38 implements Action<Task>
40 /** Scratchpad extensions. */
41 private static final String[] _SCRATCHPAD_EXTS =
42 new String[]{".sto", ".sp", ".sp0", ".sp1", ".sp2", ".sp3",
43 ".sp4", ".sp5", ".sp6", ".sp7", ".sp8", ".sp9"};
45 /**
46 * {@inheritDoc}
47 * @since 2024/07/28
49 @Override
50 public void execute(Task __task)
52 VMRunWhateverTask runTask = (VMRunWhateverTask)__task;
54 // Get the Jar to Run
55 String jar = System.getProperty("jar");
56 if (jar == null || jar.isEmpty())
57 jar = System.getenv("SQUIRRELJME_JAR");
59 // This must be here
60 if (jar == null || jar.isEmpty())
61 throw new RuntimeException("`jar` system property is not set.");
63 // Targets
64 String availableMain = null;
65 Map<Integer, JavaMEMidlet> availableMIDlets = new LinkedHashMap<>();
67 // Load in Jar
68 try (ZipFile zip = new ZipFile(Paths.get(jar).toFile()))
70 // We need to get the manifest, assuming there is one
71 ZipEntry entry = zip.getEntry("META-INF/MANIFEST.MF");
72 if (entry != null)
74 // Parse manifest
75 Attributes attr;
76 try (InputStream in = zip.getInputStream(entry))
78 attr = new Manifest(in).getMainAttributes();
81 // Is there a main class?
82 availableMain = attr.getValue("Main-Class");
84 // Find MIDlet properties (MIDlet-1: Name, icon.png, main)
85 for (int i = 1; i <= 10; i++)
87 // Is there something here?
88 String maybe = attr.getValue("MIDlet-" + i);
89 if (maybe == null || maybe.isEmpty())
90 continue;
92 // Need all three to parse
93 String[] splice = maybe.trim().split(Pattern.quote(","));
94 if (splice.length != 3)
95 continue;
97 // Load in MIDlet
98 availableMIDlets.put(Integer.valueOf(i),
99 new JavaMEMidlet(splice[0].trim(), splice[1].trim(),
100 splice[2].trim()));
104 catch (IOException __e)
106 throw new RuntimeException("Could not process Jar.", __e);
109 // Load in full class path
110 List<Path> classPath = new ArrayList<>();
111 classPath.addAll(Arrays.asList(VMHelpers.runClassPath(
112 runTask.getProject().getRootProject()
113 .findProject(":modules:profile-meep"), runTask.classifier,
114 true)));
116 // See if there is a DoJa/Star JAM
117 String baseName = (jar.endsWith(".jar") ?
118 jar.substring(0, jar.length() - 4) : jar);
119 String maybeJamRaw = baseName + ".jam";
120 Path maybeJam = Paths.get(maybeJamRaw);
121 if (Files.exists(maybeJam))
123 // Load in the DoJa classpath
124 classPath.addAll(Arrays.asList(VMHelpers.runClassPath(
125 runTask.getProject().getRootProject()
126 .findProject(":modules:vendor-api-ntt-docomo-doja"),
127 runTask.classifier, true)));
129 // If there are any scratchpads, we need those in the classpath
130 for (String ext : VMRunWhateverTaskAction._SCRATCHPAD_EXTS)
132 String maybeSpRaw = baseName + ext;
133 Path maybeSp = Paths.get(maybeSpRaw);
134 if (Files.exists(maybeSp))
135 classPath.add(maybeSp);
138 // Use MIDlet wrapped as VMRunTaskDetached does not support
139 // DoJa/Star applications
140 classPath.add(maybeJam);
141 availableMIDlets.put(availableMIDlets.size() + 1,
142 new JavaMEMidlet("SquirrelJME DoJa Adapter", "",
143 "cc.squirreljme.runtime.nttdocomo.DoJaMIDletAdapter"));
146 // The base Jar
147 classPath.add(Paths.get(jar));
149 // If not wanting a main class, do not use it
150 String targetMain = null;
151 if (!runTask.mainClass)
152 targetMain = availableMain;
153 JavaMEMidlet targetMIDlet = availableMIDlets.get(
154 Integer.valueOf(runTask.midlet));
156 // Nothing?
157 if (targetMain == null && targetMIDlet == null)
158 throw new RuntimeException(String.format(
159 "Jar does not have %s (main=%s, midlets=%s)",
160 (runTask.mainClass ? "Main" :
161 "MIDlet " + runTask.midlet),
162 targetMain, availableMIDlets));
164 // Setup detached runner then execute it
165 new VMRunTaskDetached(runTask.classifier,
166 __task.getLogger(),
167 classPath.toArray(new Path[classPath.size()]),
168 targetMIDlet,
169 VMHelpers.mainClass(targetMIDlet, targetMain),
170 runTask.getProject().getBuildDir().toPath(),
171 runTask.getProject(),
172 runTask.debugServer).run();