|
13 | 13 | // limitations under the License. |
14 | 14 | package wyjs.tasks; |
15 | 15 |
|
16 | | -import java.io.IOException; |
17 | 16 | import java.util.Arrays; |
18 | 17 | import java.util.Collections; |
19 | 18 | import java.util.List; |
20 | | -import java.util.function.Function; |
21 | | - |
22 | 19 | import wycc.lang.Build; |
| 20 | +import wycc.lang.Build.Artifact; |
| 21 | +import wycc.lang.Build.SnapShot; |
| 22 | +import wycc.lang.Path; |
23 | 23 | import wycc.util.Logger; |
| 24 | +import wycc.util.Pair; |
24 | 25 | import wyil.lang.WyilFile; |
25 | 26 | import wyjs.core.JavaScriptFile; |
26 | 27 |
|
27 | | -public class JavaScriptCompileTask extends AbstractBuildTask<WyilFile, JavaScriptFile> { |
28 | | - |
| 28 | +public class JavaScriptCompileTask implements Build.Task { |
29 | 29 | /** |
30 | 30 | * Enable debug mode |
31 | 31 | */ |
32 | 32 | protected boolean debug = true; |
33 | | - |
34 | 33 | /** |
35 | | - * For logging information. |
| 34 | + * The set of source files that this task will compiler from. |
36 | 35 | */ |
37 | | - private Logger logger = Logger.NULL; |
38 | | - |
| 36 | + private final WyilFile source; |
| 37 | + /** |
| 38 | + * Identifier for target of this build task. |
| 39 | + */ |
| 40 | + private final Path target; |
39 | 41 | /** |
40 | 42 | * Additional JavaScript files to include in generated file. |
41 | 43 | */ |
42 | | - private List<Path.Entry<JavaScriptFile>> includes = Collections.EMPTY_LIST; |
43 | | - |
44 | | - public JavaScriptCompileTask(Build.Project project, Path.Entry<JavaScriptFile> target, |
45 | | - Path.Entry<WyilFile> sources) { |
46 | | - super(project, target, Arrays.asList(sources)); |
47 | | - } |
| 44 | + private List<JavaScriptFile> includes = Collections.EMPTY_LIST; |
48 | 45 |
|
49 | | - public void setLogger(Logger logger) { |
50 | | - this.logger = logger; |
| 46 | + public JavaScriptCompileTask(Path target, |
| 47 | + WyilFile source) { |
| 48 | + this.target = target; |
| 49 | + this.source = source; |
51 | 50 | } |
52 | 51 |
|
53 | 52 | public void setDebug(boolean debug) { |
54 | 53 | this.debug = debug; |
55 | 54 | } |
56 | 55 |
|
57 | | - public void setIncludes(List<Path.Entry<JavaScriptFile>> includes) { |
| 56 | + public void setIncludes(List<JavaScriptFile> includes) { |
58 | 57 | this.includes = includes; |
59 | 58 | } |
60 | 59 |
|
61 | 60 | @Override |
62 | | - public Build.Project project() { |
63 | | - return project; |
| 61 | + public Path getPath() { |
| 62 | + return target; |
64 | 63 | } |
65 | 64 |
|
66 | 65 | @Override |
67 | | - public Function<Build.Meter,Boolean> initialise() throws IOException { |
68 | | - // Extract target and source files for compilation. This is the component which |
69 | | - // requires I/O. |
70 | | - JavaScriptFile jsf = target.read(); |
71 | | - WyilFile wyf = sources.get(0).read(); |
72 | | - // Extract other (native) includes |
73 | | - JavaScriptFile[] jsincs = readAll(includes); |
74 | | - // Construct the lambda for subsequent execution. This will eventually make its |
75 | | - // way into some kind of execution pool, possibly for concurrent execution with |
76 | | - // other tasks. |
77 | | - return (Build.Meter meter) -> execute(meter, jsf, wyf, jsincs); |
| 66 | + public Type<? extends Artifact> getContentType() { |
| 67 | + return JavaScriptFile.ContentType; |
78 | 68 | } |
79 | 69 |
|
80 | | - public boolean execute(Build.Meter meter, JavaScriptFile target, WyilFile source, JavaScriptFile... includes) { |
81 | | - meter = meter.fork("JavaScriptCompiler"); |
| 70 | + @Override |
| 71 | + public List<? extends Artifact> getSourceArtifacts() { |
| 72 | + return Arrays.asList(source); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Pair<SnapShot, Boolean> apply(SnapShot s) { |
| 77 | + // Compile into a single binary target |
| 78 | + Pair<JavaScriptFile, Boolean> r = compile(source); |
| 79 | + // Write target into snapshot |
| 80 | + s = s.put(r.first()); |
| 81 | + // Done |
| 82 | + return new Pair<>(s, r.second()); |
| 83 | + } |
| 84 | + |
| 85 | + private Pair<JavaScriptFile, Boolean> compile(WyilFile source) { |
| 86 | + boolean strictMode = true; |
| 87 | + JavaScriptFile.Standard std = JavaScriptFile.Standard.ES6; |
| 88 | + JavaScriptFile jsFile = new JavaScriptFile(target, Arrays.asList(source), strictMode, std); |
82 | 89 | // FIXME: this is a fairly temporary solution at the moment which just |
83 | 90 | // turns the WyIL file directly into a string. A more useful solution |
84 | 91 | // will be to generate an intermediate file representing JavaScript in |
85 | 92 | // an AST. This would enable, for example, better support for different |
86 | 93 | // standards. It would also enable minification, and allow support for |
87 | 94 | // different module systems (e.g. CommonJS). |
88 | | - new JavaScriptCompiler(meter,target).visitModule(source); |
| 95 | + new JavaScriptCompiler(jsFile).visitModule(source); |
89 | 96 | // Process includes |
90 | 97 | for (JavaScriptFile i : includes) { |
91 | | - target.getDeclarations().addAll(i.getDeclarations()); |
| 98 | + jsFile.getDeclarations().addAll(i.getDeclarations()); |
92 | 99 | } |
93 | | - // |
94 | | - meter.done(); |
95 | | - // How can this fail? |
96 | | - return true; |
| 100 | + // How could this fail? |
| 101 | + return new Pair<>(jsFile, true); |
97 | 102 | } |
98 | 103 |
|
99 | | - private JavaScriptFile[] readAll(List<Path.Entry<JavaScriptFile>> includes) throws IOException { |
100 | | - JavaScriptFile[] files = new JavaScriptFile[includes.size()]; |
101 | | - for(int i=0;i!=files.length;++i) { |
102 | | - files[i] = includes.get(i).read(); |
103 | | - } |
104 | | - return files; |
105 | | - } |
| 104 | +// @Override |
| 105 | +// public Function<Build.Meter,Boolean> initialise() throws IOException { |
| 106 | +// // Extract target and source files for compilation. This is the component which |
| 107 | +// // requires I/O. |
| 108 | +// JavaScriptFile jsf = target.read(); |
| 109 | +// WyilFile wyf = sources.get(0).read(); |
| 110 | +// // Extract other (native) includes |
| 111 | +// JavaScriptFile[] jsincs = readAll(includes); |
| 112 | +// // Construct the lambda for subsequent execution. This will eventually make its |
| 113 | +// // way into some kind of execution pool, possibly for concurrent execution with |
| 114 | +// // other tasks. |
| 115 | +// return (Build.Meter meter) -> execute(meter, jsf, wyf, jsincs); |
| 116 | +// } |
| 117 | +// |
| 118 | +// public boolean execute(Build.Meter meter, JavaScriptFile target, WyilFile source, JavaScriptFile... includes) { |
| 119 | +// meter = meter.fork("JavaScriptCompiler"); |
| 120 | + |
| 121 | +// // |
| 122 | +// meter.done(); |
| 123 | +// // How can this fail? |
| 124 | +// return true; |
| 125 | +// } |
| 126 | +// |
| 127 | +// private JavaScriptFile[] readAll(List<Path.Entry<JavaScriptFile>> includes) throws IOException { |
| 128 | +// JavaScriptFile[] files = new JavaScriptFile[includes.size()]; |
| 129 | +// for(int i=0;i!=files.length;++i) { |
| 130 | +// files[i] = includes.get(i).read(); |
| 131 | +// } |
| 132 | +// return files; |
| 133 | +// } |
106 | 134 | } |
0 commit comments