Limit how often GC can be run.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / NanoCoatBuiltInTaskAction.java
blob2f715bfc10b3446aa774ff6176830c45f04562a1
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.multivm.ident.SourceTargetClassifier;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.StandardCopyOption;
19 import java.nio.file.StandardOpenOption;
20 import java.util.regex.Pattern;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipInputStream;
23 import org.apache.tools.ant.util.StreamUtils;
24 import org.gradle.api.Action;
25 import org.gradle.api.Task;
27 /**
28 * Copies over the source file for NanoCoat.
30 * @since 2023/05/31
32 public class NanoCoatBuiltInTaskAction
33 implements Action<Task>
35 /** The classifier used. */
36 protected final SourceTargetClassifier classifier;
38 /**
39 * Initializes the task.
41 * @param __classifier The classifier used.
42 * @throws NullPointerException On null arguments.
43 * @since 2023/05/31
45 public NanoCoatBuiltInTaskAction(SourceTargetClassifier __classifier)
46 throws NullPointerException
48 if (__classifier == null)
49 throw new NullPointerException("NARG");
51 this.classifier = __classifier;
54 /**
55 * {@inheritDoc}
56 * @since 2023/05/31
58 @Override
59 public void execute(Task __task)
61 NanoCoatBuiltInTask task = (NanoCoatBuiltInTask)__task;
63 // Where is the ROM going?
64 Path input = __task.getInputs().getFiles().getSingleFile().toPath();
66 // Shared file output
67 Path romOutput = task.romBasePath().get();
68 Path moduleOutput = task.specificPath().get();
69 Path sharedOutput = task.sharedPath().get();
71 // This could fail to write
72 Path sourceTemp = null;
73 try
75 // We will be writing to a file then moving it over, so we only
76 // need a single temporary file for everything
77 sourceTemp = Files.createTempFile("source", ".x");
79 // Make sure the target directories exist first, since we just
80 // deleted them
81 Files.createDirectories(moduleOutput);
82 Files.createDirectories(sharedOutput);
84 // The ROM is just a ZIP of sources which get copied over
85 try (InputStream in = Files.newInputStream(input,
86 StandardOpenOption.READ);
87 ZipInputStream zip = new ZipInputStream(in))
89 for (;;)
91 // Load in next entry
92 ZipEntry entry = zip.getNextEntry();
93 if (entry == null)
94 break;
96 // Ignore directories
97 if (entry.isDirectory())
98 continue;
100 // Dump all the input into the output
101 try (OutputStream out = Files.newOutputStream(
102 sourceTemp, StandardOpenOption.TRUNCATE_EXISTING,
103 StandardOpenOption.WRITE,
104 StandardOpenOption.CREATE))
106 VMHelpers.copy(zip, out);
109 // Determine the target output file
110 Path target = romOutput.resolve(
111 VMHelpers.stringToPath(entry.getName()));
113 // Make sure the parent directories exist
114 Files.createDirectories(target.getParent());
116 // Move over to target, replace if it exists since
117 // we want to update it
118 Files.move(sourceTemp, target,
119 StandardCopyOption.REPLACE_EXISTING);
123 // Cleanup after this
124 finally
126 if (sourceTemp != null)
129 Files.delete(sourceTemp);
131 catch (IOException ignored)
137 // It did fail to write
138 catch (IOException e)
140 throw new RuntimeException("Could not extract ROM.", e);