| 1 |
package org.dtk; |
| 2 |
|
| 3 |
import org.mozilla.javascript.Context; |
| 4 |
import org.mozilla.javascript.ContextAction; |
| 5 |
import org.mozilla.javascript.Function; |
| 6 |
import org.mozilla.javascript.Script; |
| 7 |
import org.mozilla.javascript.Scriptable; |
| 8 |
import org.mozilla.javascript.ScriptableObject; |
| 9 |
|
| 10 |
public class BuilderContextAction implements ContextAction { |
| 11 |
private String builderPath; |
| 12 |
private String version; |
| 13 |
private String cdn; |
| 14 |
private String dependencies; |
| 15 |
private String optimize; |
| 16 |
|
| 17 |
private Exception exception; |
| 18 |
private Scriptable topScope; |
| 19 |
private Context context; |
| 20 |
private String result; |
| 21 |
|
| 22 |
public BuilderContextAction(String builderPath, String version, String cdn, String dependencies, String optimize) { |
| 23 |
this.builderPath = builderPath; |
| 24 |
this.version = version; |
| 25 |
this.cdn = cdn; |
| 26 |
this.dependencies = dependencies; |
| 27 |
this.optimize = optimize; |
| 28 |
|
| 29 |
this.exception = null; |
| 30 |
this.context = null; |
| 31 |
this.topScope = null; |
| 32 |
} |
| 33 |
|
| 34 |
public Object run(Context newContext) { |
| 35 |
context = newContext; |
| 36 |
context.setOptimizationLevel(-1); |
| 37 |
|
| 38 |
|
| 39 |
topScope = context.initStandardObjects(); |
| 40 |
|
| 41 |
try { |
| 42 |
String fileName = builderPath + "build.js"; |
| 43 |
String fileContent = FileUtil.readFromFile(fileName, null); |
| 44 |
Script script = context.compileString(fileContent, fileName, 1, null); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
ScriptableObject.putProperty(topScope, "global", Context.javaToJS(topScope, topScope)); |
| 50 |
|
| 51 |
|
| 52 |
script.exec(context, topScope); |
| 53 |
|
| 54 |
|
| 55 |
Scriptable build = Context.toObject(topScope.get("build", topScope), topScope); |
| 56 |
Object args[] = { |
| 57 |
builderPath, |
| 58 |
version, |
| 59 |
cdn, |
| 60 |
dependencies, |
| 61 |
optimize |
| 62 |
}; |
| 63 |
Function make = (Function) build.get("make", topScope); |
| 64 |
Object resultObj = make.call(context, topScope, build, args); |
| 65 |
result = (String) Context.jsToJava(resultObj, String.class); |
| 66 |
} catch (Exception e) { |
| 67 |
this.exception = e; |
| 68 |
|
| 69 |
} |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
public String getResult() { |
| 74 |
return result; |
| 75 |
} |
| 76 |
|
| 77 |
public Exception getException() { |
| 78 |
return exception; |
| 79 |
} |
| 80 |
} |