Remove clashing error prefix; Use better name for RatufaCoat ROMs.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / util / SimpleJavaExecSpecFiller.java
blob15ac03f0bd6ecb0711a3368429712373879f0723
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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.util;
12 import cc.squirreljme.plugin.multivm.VMHelpers;
13 import java.io.File;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.gradle.internal.os.OperatingSystem;
24 /**
25 * This is a simple Java execution specification filler which provides the
26 * minimal functionality.
28 * @since 2020/12/26
30 public class SimpleJavaExecSpecFiller
31 implements JavaExecSpecFiller
33 /** Program arguments. */
34 private final List<String> _args =
35 new ArrayList<>();
37 /** Java virtual machine arguments. */
38 private final List<String> _jvmArgs =
39 new ArrayList<>();
41 /** System properties. */
42 private final Map<String, String> _sysProps =
43 new LinkedHashMap<>();
45 /** Potential classPath objects. */
46 private final List<Object> _classPath =
47 new ArrayList<>();
49 /** The main class to use. */
50 private String _mainClass;
52 /**
53 * {@inheritDoc}
54 * @since 2020/12/26
56 @Override
57 public void classpath(Collection<Object> __classPath)
59 List<Object> classPath = this._classPath;
61 classPath.clear();
62 classPath.addAll(__classPath);
65 /**
66 * {@inheritDoc}
67 * @since 2020/12/26
69 @Override
70 public Iterable<String> getCommandLine()
72 // The final arguments used
73 List<String> result = new ArrayList<>();
75 // Find the Java executable
76 result.add(SimpleJavaExecSpecFiller.__findJavaExe().toString());
78 // Define the classpath
79 StringBuilder classPath = new StringBuilder();
80 String pathSep = File.pathSeparator;
81 for (Path path : VMHelpers.resolvePath(this._classPath))
83 // We need the path separator between libraries
84 if (classPath.length() > 0)
85 classPath.append(pathSep);
87 // Add the path
88 classPath.append(path);
91 // Was a classpath defined?
92 if (classPath.length() > 0)
94 result.add("-classpath");
95 result.add(classPath.toString());
98 // Define any system properties
99 for (Map.Entry<String, String> sysProp : this._sysProps.entrySet())
100 result.add(String.format("-D%s=%s",
101 sysProp.getKey(), sysProp.getValue()));
103 // Add JVM args
104 result.addAll(this._jvmArgs);
106 // Main class to run
107 result.add(this._mainClass);
109 // Any arguments to pass to the VM
110 result.addAll(this._args);
112 return result;
116 * {@inheritDoc}
117 * @since 2020/12/26
119 @Override
120 public void setMain(String __mainClass)
122 this._mainClass = __mainClass;
126 * {@inheritDoc}
127 * @since 2020/12/26
129 @Override
130 public void setArgs(Collection<String> __args)
132 List<String> args = this._args;
134 args.clear();
135 args.addAll(__args);
139 * {@inheritDoc}
140 * @since 2021/06/18
142 @Override
143 public void setJvmArgs(Collection<String> __args)
145 List<String> jvmArgs = this._jvmArgs;
147 jvmArgs.clear();
148 jvmArgs.addAll(__args);
152 * {@inheritDoc}
153 * @since 2020/12/26
155 @Override
156 public void systemProperties(Map<String, String> __sysProps)
158 Map<String, String> sysProps = this._sysProps;
160 sysProps.clear();
161 sysProps.putAll(__sysProps);
165 * Finds the Java executable.
167 * @return The Java executable.
168 * @since 2020/12/27
170 private static Path __findJavaExe()
172 String javaHomeRaw = System.getProperty("java.home");
173 Path javaHome = (javaHomeRaw != null ? Paths.get(javaHomeRaw) : null);
175 // Name differs per operating system
176 Path javaExeName;
177 if (OperatingSystem.current() == OperatingSystem.WINDOWS)
178 javaExeName = Paths.get("java.exe");
179 else
180 javaExeName = Paths.get("java");
182 // Check to see if the Java executable exists here
183 Path binPath = (javaHome != null ? javaHome.resolve("bin")
184 .resolve(javaExeName) : null);
185 if (binPath != null && Files.exists(binPath) &&
186 Files.isExecutable(binPath))
187 return binPath;
189 // Fallback to the system Java, assuming it exists
190 return Paths.get("java");