JAL-3210 Now add/override gradle.properties with local.properties
[jalview.git] / utils / jalviewjs / Java2ScriptCompilationParticipant.java
1 package net.sf.j2s.core;
2
3 import java.util.Arrays;
4 import java.util.Properties;
5
6 import org.eclipse.jdt.core.IJavaProject;
7 import org.eclipse.jdt.core.compiler.BuildContext;
8 import org.eclipse.jdt.core.compiler.ReconcileContext;
9
10 /**
11  * New Java2Script compiler uses org.eclipse.jdt.core.compiler.CompilationParticipant instead of builder
12  * 
13  * source: https://github.com/eclipse/org.aspectj.shadows/blob/master/org.eclipse.jdt.core/model/org/eclipse/jdt/core/compiler/CompilationParticipant.java
14  * 
15  * @author hansonr
16  *
17  */
18 public class Java2ScriptCompilationParticipant extends org.eclipse.jdt.core.compiler.CompilationParticipant {
19         private BuildContext[] javaFiles;
20         private boolean isCleanBuild;
21
22         public Java2ScriptCompilationParticipant() {
23                 System.out.println("CompilationParticipant started");
24         }
25
26         /**
27          * Returns whether this participant is active for a given project.
28          * <p>
29          * Default is to return <code>false</code>.
30          * </p>
31          * <p>
32          * For efficiency, participants that are not interested in the given project
33          * should return <code>false</code> for that project.
34          * </p>
35          * 
36          * @param project
37          *            the project to participate in
38          * @return whether this participant is active for a given project
39          */
40         public boolean isActive(IJavaProject project) {
41                 boolean isj2s = Java2ScriptCompiler.isActive(project);
42                 System.out.println("isActive " + isj2s + " " + project.getProject().getLocation());
43                 return isj2s;
44         }
45
46         /**
47          * Notifies this participant that a build is about to start and provides it
48          * the opportunity to create missing source folders for generated source
49          * files. Additional source folders should be marked as optional so the
50          * project can be built when the folders do not exist. Only sent to
51          * participants interested in the project.
52          * <p>
53          * Default is to return <code>READY_FOR_BUILD</code>.
54          * </p>
55          * 
56          * @see #buildFinished(IJavaProject project)
57          * @param project
58          *            the project about to build
59          * @return READY_FOR_BUILD or NEEDS_FULL_BUILD
60          */
61         public int aboutToBuild(IJavaProject project) {
62                 System.out.println("aboutToBuild " + project.getProject().getLocation());
63                 return READY_FOR_BUILD;
64         }
65
66         /**
67          * Notifies this participant that a clean is about to start and provides it
68          * the opportunity to delete generated source files. Only sent to
69          * participants interested in the project.
70          * 
71          * @param project
72          *            the project about to be cleaned
73          */
74         public void cleanStarting(IJavaProject project) {
75                 System.out.println("cleanStarting " + project.getProject().getLocation());
76                 isCleanBuild = true;
77         }
78
79         /**
80          * Notifies this participant that a compile operation is about to start and
81          * provides it the opportunity to generate source files based on the source
82          * files about to be compiled. When isBatchBuild is true, then files
83          * contains all source files in the project. Only sent to participants
84          * interested in the current build project.
85          *
86          * @param files
87          *            is an array of BuildContext
88          * @param isBatch
89          *            identifies when the build is a batch build
90          */
91         public void buildStarting(BuildContext[] files, boolean isBatch) {
92                 if (javaFiles != null) {
93                         BuildContext[] concat = Arrays.copyOf(javaFiles, javaFiles.length + files.length);
94                         System.arraycopy(files, 0, concat , javaFiles.length, files.length);
95                         javaFiles = concat;
96                 } else {
97                         javaFiles = files;
98                 }
99                 System.out.println("buildStarting " + files.length + " files, isBatch=" + isBatch);
100         }
101
102         /**
103          * Notifies this participant that a build has finished for the project. This
104          * will be sent, even if buildStarting() was not sent when no source files
105          * needed to be compiled or the build failed. Only sent to participants
106          * interested in the project.
107          * 
108          * @param project
109          *            the project about to build
110          * @since 3.4
111          */
112         public void buildFinished(IJavaProject project) {
113                 if (javaFiles != null) {
114                         Java2ScriptCompiler j2sCompiler = new Java2ScriptCompiler();
115                         j2sCompiler.startBuild(isCleanBuild);
116                         if (!j2sCompiler.initializeProject(project, true)) {
117                                 System.out.println(".j2s disabled");
118                                 return;
119                         }
120                         System.out.println("building JavaScript " + project.getProject().getLocation());
121                         for (int i = 0; i < javaFiles.length; i++) {
122                                 System.out.println("[" + (i+1) + "/" + javaFiles.length + "] transpiling " + javaFiles[i]);
123 // trying to keep the progess monitor running - didn't work
124 //                              try {
125 //                                      Thread.currentThread().sleep(1);
126 //                              } catch (InterruptedException e) {
127 //                                      // ignore
128 //                              }
129                                 if (!j2sCompiler.compileToJavaScript(javaFiles[i].getFile())) {
130                                         System.out.println("Error processing " + javaFiles[i].getFile());
131                                         break;
132                                 }
133                         }
134                         javaFiles = null;
135                         System.out.println("build finished " + project.getProject().getLocation());
136                 }
137                 isCleanBuild = false;
138         }
139
140         /**
141          * Returns whether this participant is interested in only Annotations.
142          * <p>
143          * Default is to return <code>false</code>.
144          * </p>
145          * 
146          * @return whether this participant is interested in only Annotations.
147          */
148         public boolean isAnnotationProcessor() {
149                 return false;
150         }
151
152         /**
153          * Notifies this participant that a compile operation has found source files
154          * using Annotations. Only sent to participants interested in the current
155          * build project that answer true to isAnnotationProcessor(). Each
156          * BuildContext was informed whether its source file currently
157          * hasAnnotations().
158          *
159          * @param files
160          *            is an array of BuildContext
161          */
162         public void processAnnotations(BuildContext[] files) {
163                 // nothing to do
164         }
165
166         /**
167          * Notifies this participant that a reconcile operation is happening. The
168          * participant can act on this reconcile operation by using the given
169          * context. Other participant can then see the result of this participation
170          * on this context.
171          * <p>
172          * Note that a participant should not modify the buffer of the working copy
173          * that is being reconciled.
174          * </p>
175          * <p>
176          * Default is to do nothing.
177          * </p>
178          * 
179          * @param context
180          *            the reconcile context to act on
181          */
182         public void reconcile(ReconcileContext context) {
183                 // fired whenever a source file is changed -- before it is saved
184         }
185 }