forked from helidon-io/helidon-build-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f80dd72
commit d8a7a70
Showing
18 changed files
with
377 additions
and
713 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ScriptCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
/* | ||
* Copyright (c) 2022, 2025 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.archetype.engine.v2; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import io.helidon.build.archetype.engine.v2.ast.DeclaredBlock; | ||
import io.helidon.build.archetype.engine.v2.ast.Invocation; | ||
import io.helidon.build.archetype.engine.v2.ast.Invocation.MethodInvocation; | ||
import io.helidon.build.archetype.engine.v2.ast.Invocation.ScriptInvocation; | ||
import io.helidon.build.archetype.engine.v2.ast.Method; | ||
import io.helidon.build.archetype.engine.v2.ast.Node; | ||
import io.helidon.build.archetype.engine.v2.ast.Script; | ||
|
||
import static io.helidon.build.common.FileUtils.pathOf; | ||
|
||
/** | ||
* Script "pseudo" compiler. | ||
*/ | ||
public class ScriptCompiler { | ||
|
||
/** | ||
* Compiler features. | ||
*/ | ||
public enum Feature { | ||
/** | ||
* Obfuscate method names. | ||
*/ | ||
OBFUSCATE(ScriptCompiler::obfuscate), | ||
/** | ||
* Inline invocations. | ||
*/ | ||
INLINE(ScriptCompiler::inline), | ||
/** | ||
* Remove outputs. | ||
*/ | ||
NO_OUTPUT(ScriptCompiler::removeOutputs); | ||
|
||
// TODO validation | ||
|
||
private final Consumer<ScriptCompiler> function; | ||
|
||
Feature(Consumer<ScriptCompiler> function) { | ||
this.function = function; | ||
} | ||
} | ||
|
||
/** | ||
* "Compile" the given script. | ||
* | ||
* @param entrypoint entrypoint | ||
* @param features features | ||
* @return compiled script | ||
*/ | ||
public static Script compile(Path entrypoint, Feature... features) { | ||
ScriptCompiler compiler = new ScriptCompiler(); | ||
compiler.readScript(entrypoint); | ||
for (Feature feature : features) { | ||
feature.function.accept(compiler); | ||
} | ||
return compiler.script; | ||
} | ||
|
||
private final Map<Path, Script> scripts = new HashMap<>(); | ||
private final Map<DeclaredBlock, Set<Invocation>> invocations = new HashMap<>(); | ||
private final Map<Node, DeclaredBlock> blocks = new HashMap<>(); | ||
private final Map<DeclaredBlock, Set<Node>> nodes = new HashMap<>(); | ||
private Script script; | ||
|
||
private ScriptCompiler() { | ||
} | ||
|
||
private void readScript(Path path) { | ||
Deque<Unresolved> stack = new ArrayDeque<>(); | ||
script = readScript(path, inv -> stack.push(new Unresolved(path.getParent(), path.getParent(), inv))); | ||
while (!stack.isEmpty()) { | ||
Unresolved elt = stack.pop(); | ||
Script script = scripts.computeIfAbsent(elt.path, p -> | ||
readScript(elt.path, inv -> stack.push(new Unresolved(elt, inv)))); | ||
invocations.computeIfAbsent(script, s -> new HashSet<>()).add(elt.inv); | ||
} | ||
} | ||
|
||
private Script readScript(Path path, Consumer<ScriptInvocation> consumer) { | ||
try (Scanner scanner = new Scanner(path, consumer)) { | ||
return scanner.readScript(ScriptLoader.EMPTY); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private void inline() { | ||
Deque<DeclaredBlock> stack = new ArrayDeque<>(blocks.values()); | ||
while (!stack.isEmpty()) { | ||
DeclaredBlock block = stack.pop(); | ||
Set<Node> enclosed = nodes.get(block); | ||
if (enclosed == null || enclosed.isEmpty()) { | ||
nodes.remove(block); | ||
Set<Invocation> refs = invocations.remove(block); | ||
if (refs != null) { | ||
for (Invocation inv : refs) { | ||
// TODO remove invocation | ||
DeclaredBlock enclosing = blocks.get(inv); | ||
if (enclosing != null) { | ||
stack.push(enclosing); | ||
} | ||
} | ||
} | ||
} | ||
Set<Invocation> refs = invocations.get(block); | ||
if (refs != null && refs.size() == 1) { | ||
// TODO inline invocations | ||
|
||
} | ||
} | ||
} | ||
|
||
private void obfuscate() { | ||
} | ||
|
||
private void removeOutputs() { | ||
} | ||
|
||
private final class Scanner implements ScriptReader { | ||
|
||
private final Deque<DeclaredBlock> stack = new ArrayDeque<>(); | ||
private final ScriptReader delegate; | ||
private final Consumer<ScriptInvocation> consumer; | ||
|
||
Scanner(Path path, Consumer<ScriptInvocation> consumer) { | ||
this.delegate = ScriptReader.create(path); | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public Script readScript(ScriptLoader loader) { | ||
return delegate.readScript(loader); | ||
} | ||
|
||
@Override | ||
public <T extends Node.Builder<?, ?>> T init(T node) { | ||
if (node instanceof DeclaredBlock) { | ||
stack.push((DeclaredBlock) node); | ||
} | ||
return delegate.init(node); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends Node> T build(Node.Builder<T, ?> node) { | ||
if (node instanceof DeclaredBlock) { | ||
stack.pop(); | ||
} else { | ||
if (node instanceof MethodInvocation) { | ||
MethodInvocation inv = (MethodInvocation) node; | ||
Method method = inv.script().methods().get(inv.method()); | ||
if (method == null) { | ||
throw new IllegalStateException("Method not found: " + inv.method()); | ||
} | ||
invocations.computeIfAbsent(method, s -> new HashSet<>()).add(inv); | ||
} else if (node instanceof ScriptInvocation) { | ||
consumer.accept((ScriptInvocation) node); | ||
} | ||
DeclaredBlock block = stack.peek(); | ||
blocks.put(node, block); | ||
nodes.computeIfAbsent(block, s -> new HashSet<>()).add(node); | ||
} | ||
return (T) node; | ||
} | ||
} | ||
|
||
private static final class Unresolved { | ||
private final ScriptInvocation inv; | ||
private final Path cwd; | ||
private final Path path; | ||
|
||
Unresolved(Unresolved parent, ScriptInvocation inv) { | ||
this(parent.cwd, parent.path, inv); | ||
} | ||
|
||
Unresolved(Path cwd, Path dir, ScriptInvocation inv) { | ||
this.inv = inv; | ||
if (inv.src() != null) { | ||
this.path = cwd.resolve(inv.src()).toAbsolutePath().normalize(); | ||
} else { | ||
this.path = pathOf(URI.create(inv.url()), getClass().getClassLoader()); | ||
} | ||
if (inv.kind() == ScriptInvocation.Kind.EXEC) { | ||
this.cwd = dir; | ||
} else { | ||
this.cwd = cwd; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.