Indentations break the feed.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMRomTaskAction.java
blob93a7cb45958d52dfe54bcd8dabd572f2e6157b0c
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 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.SquirrelJMEPluginConfiguration;
13 import cc.squirreljme.plugin.multivm.ident.SourceTargetClassifier;
14 import cc.squirreljme.plugin.util.UnassistedLaunchEntry;
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.StandardCopyOption;
21 import java.nio.file.StandardOpenOption;
22 import java.util.ArrayList;
23 import java.util.LinkedHashSet;
24 import java.util.Set;
25 import org.gradle.api.Action;
26 import org.gradle.api.Task;
27 import org.gradle.api.tasks.SourceSet;
29 /**
30 * This performs the actual work that is needed to build the ROM.
32 * @since 2020/08/23
34 public class VMRomTaskAction
35 implements Action<Task>
37 /** The classifier used. */
38 protected final SourceTargetClassifier classifier;
40 /**
41 * Initializes the task.
43 * @param __classifier The classifier used.
44 * @throws NullPointerException On null arguments.
45 * @since 2020/08/23
47 public VMRomTaskAction(SourceTargetClassifier __classifier)
48 throws NullPointerException
50 if (__classifier == null)
51 throw new NullPointerException("NARG");
53 this.classifier = __classifier;
56 /**
57 * {@inheritDoc}
58 * @since 2020/08/23
60 @Override
61 public void execute(Task __task)
62 throws NullPointerException
64 String sourceSet = this.classifier.getSourceSet();
65 VMSpecifier vmType = this.classifier.getVmType();
67 // Is this a single source set ROM?
68 boolean isSingleSourceSet = vmType.isSingleSourceSetRom(
69 this.classifier.getBangletVariant());
70 boolean bootLoaderEnabled = !isSingleSourceSet ||
71 (isSingleSourceSet &&
72 SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet));
74 Path tempFile = null;
75 try
77 // We need somewhere safe to store the file
78 tempFile = Files.createTempFile(
79 vmType.vmName(VMNameFormat.LOWERCASE), sourceSet);
81 // ROM building parameters
82 RomBuildParameters build = new RomBuildParameters();
84 // The boot libraries which will be placed first always
85 Set<Path> bootPaths = new LinkedHashSet<>();
87 // Get all of the libraries to translate
88 Set<Path> normalPaths = new LinkedHashSet<>();
89 for (VMLibraryTask task : VMRomDependencies.libraries(__task,
90 this.classifier))
92 // If we are single source set and this library is another
93 // source set, then do not include it here
94 String taskSourceSet = task.getSourceSet();
95 if (isSingleSourceSet &&
96 !sourceSet.equals(taskSourceSet))
97 continue;
99 // Determine the path set
100 Set<Path> pathSet = new LinkedHashSet<>();
101 for (File f : task.getOutputs().getFiles().getFiles())
102 pathSet.add(f.toPath());
104 // Is this the boot loader library?
105 SquirrelJMEPluginConfiguration config =
106 SquirrelJMEPluginConfiguration.configurationOrNull(
107 task.getProject());
108 boolean isMain = SourceSet.MAIN_SOURCE_SET_NAME
109 .equals(taskSourceSet);
110 boolean isBootLoader = (isMain && config != null &&
111 config.isBootLoader);
112 boolean isMainLauncher = (isMain && config != null &&
113 config.isMainLauncher);
115 // If this is the boot loader, add our paths
116 if (bootLoaderEnabled)
118 if (isBootLoader)
120 build.bootLoaderMainClass = config.bootLoaderMainClass;
121 build.bootLoaderClassPath = pathSet.toArray(
122 new Path[pathSet.size()]);
125 // If this is the launcher, set the information needed to
126 // make sure it can actually be launched properly
127 if (isMainLauncher)
129 UnassistedLaunchEntry entry = config.primaryEntry();
131 build.launcherMainClass = entry.mainClass;
132 build.launcherArgs = entry.args();
133 build.launcherClassPath = VMHelpers.runClassPath(task,
134 this.classifier);
138 // Add to the correct set of paths
139 if (bootLoaderEnabled && isBootLoader)
140 bootPaths.addAll(pathSet);
141 else
142 normalPaths.addAll(pathSet);
145 // Make sure the boot libraries are always first
146 Set<Path> libPaths = new LinkedHashSet<>();
147 if (bootLoaderEnabled)
148 libPaths.addAll(bootPaths);
149 libPaths.addAll(normalPaths);
151 // Setup output file for writing
152 try (OutputStream out = Files.newOutputStream(tempFile,
153 StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING,
154 StandardOpenOption.CREATE))
156 // Run the ROM processing
157 vmType.processRom((VMBaseTask)__task,
158 this.classifier.getBangletVariant(), out, build,
159 new ArrayList<>(libPaths));
162 // Recompress it
163 VMHelpers.recompressZip(tempFile);
165 // Move the file over
166 Files.move(tempFile,
167 __task.getOutputs().getFiles().getSingleFile().toPath(),
168 StandardCopyOption.REPLACE_EXISTING);
170 catch (IOException e)
172 throw new RuntimeException("I/O Error linking ROM for " +
173 __task.getProject().getName(), e);
176 // Always try to cleanup the temporary file
177 finally
179 if (tempFile != null)
182 Files.deleteIfExists(tempFile);
184 catch (IOException ignored)