Skip to content

Commit

Permalink
Save work
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-grecourt committed Feb 20, 2025
1 parent f80dd72 commit d8a7a70
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 713 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import io.helidon.build.archetype.engine.v2.ast.Step;
import io.helidon.build.archetype.engine.v2.ast.Value;
import io.helidon.build.archetype.engine.v2.ast.Variable;
import io.helidon.build.archetype.engine.v2.util.ClientPredicate;
import io.helidon.build.archetype.engine.v2.util.ScriptCompiler;

/**
* Script serializer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* 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.
Expand Down
20 changes: 1 addition & 19 deletions archetype/engine-v2/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022, 2023 Oracle and/or its affiliates.
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.
Expand Down Expand Up @@ -40,12 +40,6 @@
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>

<Match>
<!-- This random generator (java.util.Random) is predictable -->
<Class name="io.helidon.build.archetype.engine.v2.ScriptLoader"/>
<Bug pattern="PREDICTABLE_RANDOM"/>
</Match>

<Match>
<!-- Found reliance on default encoding -->
<Class name="io.helidon.build.archetype.engine.v2.TerminalInputResolver"/>
Expand All @@ -70,18 +64,6 @@
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>

<Match>
<!-- This random generator (java.util.Random) is predictable -->
<Class name="io.helidon.build.archetype.engine.v2.ScriptLoader"/>
<Bug pattern="PREDICTABLE_RANDOM"/>
</Match>

<Match>
<!-- Unchecked/unconfirmed cast -->
<Class name="io.helidon.build.archetype.engine.v2.util.ArchetypeScanner"/>
<Bug pattern="BC_UNCONFIRMED_CAST"/>
</Match>

<Match>
<!-- This API reads a file whose location might be specified by user input -->
<Class name="io.helidon.build.archetype.engine.v2.ArchetypeEngineV2"/>
Expand Down
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import io.helidon.build.archetype.engine.v2.ast.Script;

Expand All @@ -41,7 +42,7 @@ public interface ScriptLoader {
* @return ScriptLoader
*/
static ScriptLoader create() {
return new ScriptLoaderImpl(ScriptReader.Factory.DEFAULT);
return new ScriptLoaderImpl(ScriptReader::create);
}

/**
Expand All @@ -51,7 +52,7 @@ static ScriptLoader create() {
* @param factory factory
* @return script
*/
static Script load(Path path, ScriptReader.Factory factory) {
static Script load(Path path, Function<Path, ScriptReader> factory) {
return new ScriptLoaderImpl(factory).get(path);
}

Expand All @@ -76,12 +77,12 @@ static Script load(Path path) {
/**
* Script loader implementation.
*/
class ScriptLoaderImpl implements ScriptLoader {
final class ScriptLoaderImpl implements ScriptLoader {

private final Map<Path, Script> scripts = new HashMap<>();
private final ScriptReader.Factory factory;
private final Function<Path, ScriptReader> factory;

private ScriptLoaderImpl(ScriptReader.Factory factory) {
private ScriptLoaderImpl(Function<Path, ScriptReader> factory) {
this.factory = factory;
}

Expand All @@ -91,8 +92,8 @@ public Script get(Path path) {
}

private Script loadScript(Path path) {
try (ScriptReader reader = factory.newReader(this, path)) {
return reader.readScript();
try (ScriptReader reader = factory.apply(path)) {
return reader.readScript(this);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
Expand Down
Loading

0 comments on commit d8a7a70

Please sign in to comment.