11package net .sf .j2s .core ;
22
3+ import java .util .ArrayList ;
4+
5+ import org .eclipse .core .resources .IFile ;
36import org .eclipse .jdt .core .IJavaProject ;
47import org .eclipse .jdt .core .compiler .BuildContext ;
58import org .eclipse .jdt .core .compiler .ReconcileContext ;
1316 *
1417 */
1518public class Java2ScriptCompilationParticipant extends org .eclipse .jdt .core .compiler .CompilationParticipant {
16- private BuildContext [] javaFiles ;
19+ private ArrayList < BuildContext []> contexts ;
1720 private boolean isCleanBuild ;
21+ private static String isActiveNotified = "" ;
1822
1923 public Java2ScriptCompilationParticipant () {
2024 System .out .println ("CompilationParticipant started" );
@@ -34,9 +38,14 @@ public Java2ScriptCompilationParticipant() {
3438 * the project to participate in
3539 * @return whether this participant is active for a given project
3640 */
41+ @ Override
3742 public boolean isActive (IJavaProject project ) {
3843 boolean isj2s = Java2ScriptCompiler .isActive (project );
39- System .out .println ("isActive " + isj2s + " " + project .getProject ().getLocation ());
44+ String loc = " " + project .getProject ().getLocation ().toString () + " " ;
45+ if (isActiveNotified .indexOf (loc ) < 0 ) {
46+ System .out .println ("j2s isActive " + isj2s + loc );
47+ isActiveNotified += loc ;
48+ }
4049 return isj2s ;
4150 }
4251
@@ -55,8 +64,11 @@ public boolean isActive(IJavaProject project) {
5564 * the project about to build
5665 * @return READY_FOR_BUILD or NEEDS_FULL_BUILD
5766 */
67+ @ Override
5868 public int aboutToBuild (IJavaProject project ) {
59- System .out .println ("aboutToBuild " + project .getProject ().getLocation ());
69+ System .out .println ("j2s aboutToBuild " + project .getProject ().getName () + " " + project .getProject ().getLocation ());
70+ if (contexts == null )
71+ contexts = new ArrayList <>();
6072 return READY_FOR_BUILD ;
6173 }
6274
@@ -68,8 +80,9 @@ public int aboutToBuild(IJavaProject project) {
6880 * @param project
6981 * the project about to be cleaned
7082 */
83+ @ Override
7184 public void cleanStarting (IJavaProject project ) {
72- System .out .println ("cleanStarting " + project .getProject ().getLocation ());
85+ System .out .println ("j2s cleanStarting " + project .getProject ().getLocation ());
7386 isCleanBuild = true ;
7487 }
7588
@@ -85,9 +98,12 @@ public void cleanStarting(IJavaProject project) {
8598 * @param isBatch
8699 * identifies when the build is a batch build
87100 */
101+ @ Override
88102 public void buildStarting (BuildContext [] files , boolean isBatch ) {
89- javaFiles = files ;
90- System .out .println ("buildStarting " + files .length + " files, isBatch=" + isBatch );
103+ if (files .length == 0 )
104+ return ;
105+ contexts .add (files );
106+ System .out .println ("j2s buildStarting " + files .length + " files, contexts.size() = " + contexts .size () + ", isBatch=" + isBatch );
91107 }
92108
93109 /**
@@ -96,38 +112,67 @@ public void buildStarting(BuildContext[] files, boolean isBatch) {
96112 * needed to be compiled or the build failed. Only sent to participants
97113 * interested in the project.
98114 *
99- * @param project
100- * the project about to build
115+ * @param project the project about to build
101116 * @since 3.4
102117 */
118+ @ Override
103119 public void buildFinished (IJavaProject project ) {
104- if (javaFiles != null ) {
120+ if (contexts != null ) {
105121 Java2ScriptCompiler j2sCompiler = new Java2ScriptCompiler ();
122+ boolean breakOnError = j2sCompiler .doBreakOnError ();
106123 j2sCompiler .startBuild (isCleanBuild );
107124 if (!j2sCompiler .initializeProject (project , true )) {
108125 System .out .println (".j2s disabled" );
109126 return ;
110127 }
111- System .out .println ("building JavaScript " + project .getProject ().getLocation ());
112- for (int i = 0 ; i < javaFiles .length ; i ++) {
113- System .out .println ("transpiling " + javaFiles [i ]);
128+ System .out .println ("j2s building JavaScript " + project .getProject ().getName () + " "
129+ + project .getProject ().getLocation ());
130+ int ntotal = 0 , nerror = 0 ;
131+ for (int j = 0 ; j < contexts .size (); j ++) {
132+ BuildContext [] files = contexts .get (j );
133+ System .out .println ("j2s building JavaScript for " + files .length + " file" +plural (files .length ));
134+
135+ for (int i = 0 , n = files .length ; i < n ; i ++) {
114136// trying to keep the progess monitor running - didn't work
115137// try {
116138// Thread.currentThread().sleep(1);
117139// } catch (InterruptedException e) {
118140// // ignore
119141// }
120- if (!j2sCompiler .compileToJavaScript (javaFiles [i ].getFile ())) {
121- System .out .println ("Error processing " + javaFiles [i ].getFile ());
122- break ;
142+ // System.out.println("j2s file"
143+ // + " name=" + files[i].getFile().getName()
144+ // + " fullpath=" + files[i].getFile().getFullPath()
145+ // + " location=" + files[i].getFile().getLocation());
146+ //
147+ IFile f = files [i ].getFile ();
148+ String filePath = f .getLocation ().toString ();
149+ if (j2sCompiler .excludeFile (f )) {
150+ System .out .println ("j2s excluded " + filePath );
151+ } else {
152+ System .out .println ("j2s transpiling (" + (i + 1 ) + "/" + n + ") " + filePath );
153+ if (j2sCompiler .compileToJavaScript (f )) {
154+ ntotal ++;
155+ } else {
156+ nerror ++;
157+ System .out .println ("j2s Error processing " + filePath );
158+ if (breakOnError )
159+ break ;
160+ }
161+ }
123162 }
124163 }
125- javaFiles = null ;
126- System .out .println ("build finished " + project .getProject ().getLocation ());
127- }
164+ contexts = null ;
165+ System .out .println (
166+ "j2s buildFinished " + ntotal + " file" +plural (ntotal ) + " transpiled for " + project .getProject ().getLocation ());
167+ System .out .println ("j2s buildFinished nerror = " + nerror );
168+ }
128169 isCleanBuild = false ;
129170 }
130171
172+ private static String plural (int n ) {
173+ return (n == 1 ? "" : "s" );
174+ }
175+
131176 /**
132177 * Returns whether this participant is interested in only Annotations.
133178 * <p>
@@ -136,6 +181,7 @@ public void buildFinished(IJavaProject project) {
136181 *
137182 * @return whether this participant is interested in only Annotations.
138183 */
184+ @ Override
139185 public boolean isAnnotationProcessor () {
140186 return false ;
141187 }
@@ -150,6 +196,7 @@ public boolean isAnnotationProcessor() {
150196 * @param files
151197 * is an array of BuildContext
152198 */
199+ @ Override
153200 public void processAnnotations (BuildContext [] files ) {
154201 // nothing to do
155202 }
@@ -170,6 +217,7 @@ public void processAnnotations(BuildContext[] files) {
170217 * @param context
171218 * the reconcile context to act on
172219 */
220+ @ Override
173221 public void reconcile (ReconcileContext context ) {
174222 // fired whenever a source file is changed -- before it is saved
175223 }
0 commit comments