Skip to content

Commit 1fd5dda

Browse files
authored
Merge pull request #133 from BobHanson/master
java2script 3.2.4.08 adds support for multiple buildStart
2 parents f3b8847 + 58780e1 commit 1fd5dda

File tree

9 files changed

+137
-63
lines changed

9 files changed

+137
-63
lines changed
1.42 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191021071533
1+
20191024113542
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191021071533
1+
20191024113542

sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class CorePlugin extends Plugin {
1919
* register the bundle version properly. So we use VERSION here instead.
2020
*
2121
*/
22-
public static String VERSION = "3.2.4.07";
22+
public static String VERSION = "3.2.4.08";
23+
// BH 2019.10.24 support for multiple buildStart
2324
// TODO/NOTE final static int FOO = (/**@j2sNative 5 || */3) stated but not recognized when used as its new value
2425
// BH 2/3/2019 -- 3.2.4.07 fixes "final static Float = (float)" missing definition
2526
// BH 1/2/2019 -- 3.2.4.06 fixes try(resources) with more than one resource missing semicolon

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompilationParticipant.java

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.sf.j2s.core;
22

3+
import java.util.ArrayList;
4+
5+
import org.eclipse.core.resources.IFile;
36
import org.eclipse.jdt.core.IJavaProject;
47
import org.eclipse.jdt.core.compiler.BuildContext;
58
import org.eclipse.jdt.core.compiler.ReconcileContext;
@@ -13,8 +16,9 @@
1316
*
1417
*/
1518
public 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
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,26 @@ class Java2ScriptCompiler {
6666
*/
6767
private static final String J2S_LOG_METHODS_CALLED = "j2s.log.methods.called";
6868

69-
private static final String J2S_LOG_ALL_CALLS = "j2s.log.all.calls";
69+
/**
70+
* stop processing files if any file has an exception; default is TRUE
71+
*
72+
*/
73+
private static final String J2S_BREAK_ON_ERROR = "j2s.break.on.error";
74+
private static final String J2S_BREAK_ON_ERROR_FALSE = "false";
7075

76+
private static final String J2S_LOG_ALL_CALLS = "j2s.log.all.calls";
7177
private static final String J2S_LOG_ALL_CALLS_TRUE = "true";
7278

7379
private static final String J2S_EXCLUDED_PATHS = "j2s.excluded.paths";
7480

7581
private static final String J2S_TESTING = "j2s.testing";
76-
7782
private static final String J2S_TESTING_TRUE = "true";
7883

7984
private static final String J2S_COMPILER_NONQUALIFIED_PACKAGES = "j2s.compiler.nonqualified.packages";
8085

8186
private static final String J2S_COMPILER_NONQUALIFIED_CLASSES = "j2s.compiler.nonqualified.classes";
8287

8388
private static final String J2S_COMPILER_MODE = "j2s.compiler.mode";
84-
8589
private static final String J2S_COMPILER_MODE_DEBUG = "debug";
8690

8791
private static final String J2S_CLASS_REPLACEMENTS = "j2s.class.replacements";
@@ -93,6 +97,8 @@ class Java2ScriptCompiler {
9397
private String htmlTemplate = null;
9498

9599
private String projectFolder;
100+
101+
private String projectPath;
96102

97103
private String j2sPath;
98104

@@ -120,6 +126,12 @@ class Java2ScriptCompiler {
120126

121127
private boolean isDebugging;
122128

129+
private boolean breakOnError;
130+
131+
public boolean doBreakOnError() {
132+
return breakOnError;
133+
}
134+
123135
static boolean isActive(IJavaProject project) {
124136
try {
125137
return new File(project.getProject().getLocation().toOSString(), J2S_OPTIONS_FILE_NAME).exists();
@@ -166,6 +178,7 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
166178
// the file .j2s does not exist in the project directory -- skip this project
167179
return false;
168180
}
181+
projectPath = "/" + project.getProject().getName() + "/";
169182
projectFolder = project.getProject().getLocation().toOSString();
170183
props = new Properties();
171184
try {
@@ -218,6 +231,8 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
218231
logAllCalls = J2S_LOG_ALL_CALLS_TRUE.equalsIgnoreCase(getProperty(J2S_LOG_ALL_CALLS));
219232
}
220233

234+
breakOnError = !J2S_BREAK_ON_ERROR_FALSE.equalsIgnoreCase(getProperty(J2S_BREAK_ON_ERROR));
235+
221236
excludedPaths = getProperty(J2S_EXCLUDED_PATHS);
222237

223238
lstExcludedPaths = null;
@@ -227,7 +242,7 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
227242
String[] paths = excludedPaths.split(";");
228243
for (int i = 0; i < paths.length; i++)
229244
if (paths[i].trim().length() > 0)
230-
lstExcludedPaths.add(paths[i].trim() + "/");
245+
lstExcludedPaths.add(projectPath + paths[i].trim() + "/");
231246
if (lstExcludedPaths.size() == 0)
232247
lstExcludedPaths = null;
233248
}
@@ -273,6 +288,16 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
273288
return true;
274289
}
275290

291+
boolean excludeFile(IFile javaSource) {
292+
String filePath = javaSource.getFullPath().toString();
293+
if (lstExcludedPaths != null) {
294+
for (int i = lstExcludedPaths.size(); --i >= 0;)
295+
if (filePath.startsWith(lstExcludedPaths.get(i))) {
296+
return true;
297+
}
298+
}
299+
return false;
300+
}
276301
/**
277302
* from Java2ScriptCompilationParticipant.java
278303
*
@@ -282,13 +307,6 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
282307
* @param javaSource
283308
*/
284309
boolean compileToJavaScript(IFile javaSource) {
285-
286-
String fileName = new String(javaSource.getName());
287-
if (lstExcludedPaths != null) {
288-
for (int i = lstExcludedPaths.size(); --i >= 0;)
289-
if (fileName.startsWith(lstExcludedPaths.get(i)))
290-
return true;
291-
}
292310
org.eclipse.jdt.core.ICompilationUnit createdUnit = JavaCore.createCompilationUnitFrom(javaSource);
293311
astParser.setSource(createdUnit);
294312
// note: next call must come before each createAST call
@@ -318,14 +336,14 @@ boolean compileToJavaScript(IFile javaSource) {
318336
e.printStackTrace();
319337
e.printStackTrace(System.out);
320338
// find the file and delete it.
321-
String filePath = j2sPath;
339+
String outPath = j2sPath;
322340
String rootName = root.getJavaElement().getElementName();
323341
rootName = rootName.substring(0, rootName.lastIndexOf('.'));
324342
String packageName = visitor.getMyPackageName();
325343
if (packageName != null) {
326-
File folder = new File(filePath, packageName.replace('.', File.separatorChar));
327-
filePath = folder.getAbsolutePath();
328-
File jsFile = new File(filePath, rootName + ".js"); //$NON-NLS-1$
344+
File folder = new File(outPath, packageName.replace('.', File.separatorChar));
345+
outPath = folder.getAbsolutePath();
346+
File jsFile = new File(outPath, rootName + ".js"); //$NON-NLS-1$
329347
if (jsFile.exists()) {
330348
System.out.println("Java2ScriptCompiler deleting " + jsFile);
331349
jsFile.delete();
@@ -388,7 +406,7 @@ private void logMethods(String logCalled, String logDeclared, boolean doAppend)
388406
private String getProperty(String key) {
389407
String val = props.getProperty(key);
390408
if (showJ2SSettings)
391-
System.err.println(key + " = " + val);
409+
System.out.println(key + " = " + val);
392410
return val;
393411
}
394412

0 commit comments

Comments
 (0)