1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
24 * Task for the assembly of Jasmin sources.
28 public class JasminAssembleTask
29 extends AbstractResourceTask
31 /** The extension for Jasmin files. */
32 public static final String EXTENSION
=
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.
44 public JasminAssembleTask(String __sourceSet
, ProcessResources __prTask
,
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.");
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
))
71 ClassFile jasClass
= new ClassFile();
74 jasClass
.readJasmin(new BufferedInputStream(in
),
75 output
.input
.getRelative().getFileName().toString(),
80 catch (jas
.jasError e
)
82 throw new RuntimeException(String
.format(
83 "Error assembling: %s (%d errors): %s",
84 output
.input
.getAbsolute(), jasClass
.errorCount(),
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()));
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
))
108 // Fallback to catch any other failures
111 if (e
instanceof RuntimeException
)
112 throw (RuntimeException
)e
;
114 throw new RuntimeException(String
.format(
115 "Could not assemble %s: %s", output
.input
.getAbsolute(),