Cherry pick the banglets and such from wip-l1summercoat, this will be the basis for...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / tasks / JasminAssembleTask.java
blob4953608c79f4d2e60ef34ff1720279e75cdb425e
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.tasks;
12 import jasmin.ClassFile;
13 import java.io.BufferedInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.nio.file.Files;
18 import java.nio.file.StandardOpenOption;
19 import javax.inject.Inject;
20 import org.gradle.api.Task;
21 import org.gradle.language.jvm.tasks.ProcessResources;
23 /**
24 * Task for the assembly of Jasmin sources.
26 * @since 2020/04/04
28 public class JasminAssembleTask
29 extends AbstractResourceTask
31 /** The extension for Jasmin files. */
32 public static final String EXTENSION =
33 ".j";
35 /**
36 * Initializes the task.
38 * @param __sourceSet The source set to use.
39 * @param __prTask The process resources task.
40 * @param __cleanTask The task for cleaning.
41 * @since 2020/04/04
43 @Inject
44 public JasminAssembleTask(String __sourceSet, ProcessResources __prTask,
45 Task __cleanTask)
47 super(JasminAssembleTask.EXTENSION, ".class",
48 __sourceSet, __prTask, __cleanTask);
50 // Set details of this task
51 this.setGroup("squirreljme");
52 this.setDescription("Assembles Jasmin Assembly files.");
55 /**
56 * {@inheritDoc}
57 * @since 2020/04/04
59 @Override
60 protected void doTaskAction(Task __task)
62 // Process all outputs
63 for (__Output__ output : this.taskOutputs())
64 try (ByteArrayOutputStream target = new ByteArrayOutputStream())
66 // Assemble input file
67 try (InputStream in = Files.newInputStream(
68 output.input.getAbsolute(), StandardOpenOption.READ))
70 // Assemble source
71 ClassFile jasClass = new ClassFile();
72 try
74 jasClass.readJasmin(new BufferedInputStream(in),
75 output.input.getRelative().getFileName().toString(),
76 true);
79 // This could fail
80 catch (jas.jasError e)
82 throw new RuntimeException(String.format(
83 "Error assembling: %s (%d errors): %s",
84 output.input.getAbsolute(), jasClass.errorCount(),
85 e.getMessage()));
88 // Failed to assemble?
89 if (jasClass.errorCount() > 0)
90 throw new RuntimeException(String.format(
91 "Error assembling: %s (%d errors)",
92 output.input.getAbsolute(), jasClass.errorCount()));
94 // Write class file
95 jasClass.write(target);
98 // Write to output file
99 try (OutputStream out = Files.newOutputStream(output.output,
100 StandardOpenOption.WRITE, StandardOpenOption.CREATE,
101 StandardOpenOption.TRUNCATE_EXISTING))
103 target.writeTo(out);
104 out.flush();
108 // Fallback to catch any other failures
109 catch (Exception e)
111 if (e instanceof RuntimeException)
112 throw (RuntimeException)e;
114 throw new RuntimeException(String.format(
115 "Could not assemble %s: %s", output.input.getAbsolute(),
116 e.getMessage()), e);