/
BabelTranspiler.java
84 lines (75 loc) · 3.2 KB
/
BabelTranspiler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.jarslab.maven.babel.plugin.transpiler;
import org.apache.maven.plugin.logging.Log;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import static java.lang.String.format;
import static java.lang.System.lineSeparator;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
public class BabelTranspiler implements AutoCloseable
{
private static final String INPUT_VARIABLE = "input";
private static final String FILENAME_VARIABLE = "filename";
private static final String BABEL_EXECUTE = "Babel.transform(%s, {filename: %s, presets: [%s], plugins: [%s]}).code";
private TranspilationContext transpilationContext;
private Context executionContext;
private void initialize(final TranspilationContext context)
{
requireNonNull(context);
if (this.transpilationContext == null || !this.transpilationContext.equals(context)) {
this.transpilationContext = context;
createEngine();
}
}
private void createEngine()
{
transpilationContext.getLog().debug("Initializing script engine");
try {
executionContext = Context.newBuilder()
.allowExperimentalOptions(true)
.allowPolyglotAccess(PolyglotAccess.ALL)
.build();
executionContext.eval(Source.newBuilder("js", transpilationContext.getBabelSource()).build());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public synchronized Transpilation execute(final Transpilation transpilation)
{
initialize(transpilation.getContext());
final Log log = transpilationContext.getLog();
if (transpilationContext.isVerbose()) {
log.info(format("Transpiling %s -> %s", transpilation.getSource(), transpilation.getTarget()));
}
try {
final String source = Files.lines(transpilation.getSource(), transpilationContext.getCharset())
.collect(joining(lineSeparator()));
final Value bindings = executionContext.getBindings("js");
bindings.putMember(INPUT_VARIABLE, source);
bindings.putMember(FILENAME_VARIABLE, transpilation.getSource().toString());
final String result = executionContext.eval("js", format(
BABEL_EXECUTE,
INPUT_VARIABLE,
FILENAME_VARIABLE,
transpilationContext.getPresets(),
transpilationContext.getPlugins())).asString();
if (log.isDebugEnabled()) {
log.debug(format("%s result:\n%s", transpilation.getTarget(), result));
}
return ImmutableTranspilation.copyOf(transpilation).withResult(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void close()
{
ofNullable(executionContext).ifPresent(Context::close);
}
}