Add input and output jars as inputs for VMCompactLibraryTask.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / ErrorListManager.java
blob673f244d35de8c4e8fe12f9cc5fe18945cb2234d
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;
12 import cc.squirreljme.plugin.util.CommentReader;
13 import cc.squirreljme.plugin.util.ErrorListTokenizer;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.PrintStream;
18 import java.nio.file.Files;
19 import java.nio.file.StandardOpenOption;
20 import java.util.Map;
21 import java.util.TreeMap;
22 import org.gradle.api.Project;
23 import org.gradle.api.file.FileTree;
24 import org.gradle.api.plugins.JavaPluginConvention;
25 import org.gradle.api.tasks.SourceSet;
27 /**
28 * This is a mapping of individual error codes for a project.
30 * @since 2020/02/22
32 public final class ErrorListManager
34 /** The project to manage the list for. */
35 protected final Project project;
37 /** The listed errors. */
38 private final Map<Integer, SourceError> _errors =
39 new TreeMap<>();
41 /** The error code for this project. */
42 private String _projectErrorCode;
44 /**
45 * Initializes the error code manager.
47 * @param __project The project to manage.
48 * @throws NullPointerException On null arguments.
49 * @since 2020/02/22
51 public ErrorListManager(Project __project)
52 throws NullPointerException
54 if (__project == null)
55 throw new NullPointerException("No project.");
57 this.project = __project;
60 /**
61 * Returns the next available error.
63 * @return The next available error.
64 * @since 2020/02/22
66 public final String next()
68 this.__init();
70 return this._projectErrorCode +
71 SourceError.indexToString(this.nextIndex());
74 /**
75 * Returns the next available error.
77 * @return The next available error.
78 * @since 2020/02/22
80 public final int nextIndex()
82 this.__init();
84 Map<Integer, SourceError> errors = this._errors;
85 synchronized (this)
87 for (int i = 1; i <= SourceError._MAX_ERROR_CODE; i++)
88 if (!errors.containsKey(i))
89 return i;
91 throw new IllegalArgumentException("Ran out of error indexes.");
95 /**
96 * Prints the errors to the given stream.
98 * @param __out The stream to write to.
99 * @throws NullPointerException On null arguments.
100 * @since 2020/02/22
102 public final void print(PrintStream __out)
103 throws NullPointerException
105 if (__out == null)
106 throw new NullPointerException("No stream specified.");
108 this.__init();
110 synchronized (this)
112 for (SourceError e : this._errors.values())
113 __out.println(e);
118 * Initializes the code map.
120 * @since 2020/02/22
122 private void __init()
124 Project project = this.project;
125 Map<Integer, SourceError> errors = this._errors;
127 synchronized (this)
129 // Already loaded?
130 if (this._projectErrorCode != null)
131 return;
133 // This must exist
134 SquirrelJMEPluginConfiguration config = project.getExtensions()
135 .<SquirrelJMEPluginConfiguration>getByType(
136 SquirrelJMEPluginConfiguration.class);
138 // Fail if this is missing
139 String projectErrorCode = config.javaDocErrorCode;
140 if (projectErrorCode == null)
141 throw new IllegalStateException(String.format(
142 "Project %s has no error code.", project.getName()));
144 // Used as a flag and to indicate the group for this
145 this._projectErrorCode = projectErrorCode;
147 // Temporary buffer for data reading
148 StringBuilder buffer = new StringBuilder();
150 // Get source code
151 FileTree sources = project.getConvention()
152 .getPlugin(JavaPluginConvention.class).getSourceSets()
153 .getByName(SourceSet.MAIN_SOURCE_SET_NAME).getAllJava()
154 .getAsFileTree();
156 // Parse source code
157 for (File source : sources)
158 try (InputStream in = Files.newInputStream(source.toPath(),
159 StandardOpenOption.READ);
160 ErrorListTokenizer tokenizer = new ErrorListTokenizer(
161 source.toPath().getFileName(),
162 new CommentReader(in)))
164 // Keep reading in errors
165 for (;;)
167 // Read the next error
168 SourceError error = tokenizer.next();
169 if (error == null)
170 break;
172 // Store error in the map
173 SourceError old = errors.put(error.index, error);
175 // Check duplicate
176 if (old != null)
177 System.err.printf("Duplicate error %s and %s.%n",
178 error, old);
180 // Wrong project
181 if (!error.projectCode.equals(projectErrorCode))
182 System.err.printf("Error has wrong project: %s," +
183 "should be: %s%n", error, projectErrorCode);
186 catch (IOException e)
188 throw new RuntimeException("Could not decode file.", e);