From e820752b116233b7848b7a61fff6f8b3785fa522 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 12 Sep 2023 21:28:01 +0200 Subject: [PATCH] Drop OmniParser; moved to rewrite-polyglot --- .../java/org/openrewrite/NoopProgressBar.java | 50 --- src/main/java/org/openrewrite/OmniParser.java | 319 ------------------ .../java/org/openrewrite/ProgressBar.java | 39 --- .../RemoteProgressBarReceiver.java | 137 -------- .../openrewrite/RemoteProgressBarSender.java | 115 ------- .../openrewrite/RemoteProgressBarTest.java | 82 ----- 6 files changed, 742 deletions(-) delete mode 100644 src/main/java/org/openrewrite/NoopProgressBar.java delete mode 100644 src/main/java/org/openrewrite/OmniParser.java delete mode 100644 src/main/java/org/openrewrite/ProgressBar.java delete mode 100644 src/main/java/org/openrewrite/RemoteProgressBarReceiver.java delete mode 100644 src/main/java/org/openrewrite/RemoteProgressBarSender.java delete mode 100644 src/test/java/org/openrewrite/RemoteProgressBarTest.java diff --git a/src/main/java/org/openrewrite/NoopProgressBar.java b/src/main/java/org/openrewrite/NoopProgressBar.java deleted file mode 100644 index ba861b2..0000000 --- a/src/main/java/org/openrewrite/NoopProgressBar.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import org.openrewrite.internal.lang.Nullable; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -public class NoopProgressBar implements ProgressBar { - @Override - public void intermediateResult(@Nullable String message) { - } - - @Override - public void finish(String message) { - } - - @Override - public void close() { - } - - @Override - public void step() { - } - - @Override - public ProgressBar setExtraMessage(String extraMessage) { - return this; - } - - @Override - public ProgressBar setMax(int max) { - return this; - } -} diff --git a/src/main/java/org/openrewrite/OmniParser.java b/src/main/java/org/openrewrite/OmniParser.java deleted file mode 100644 index 5505314..0000000 --- a/src/main/java/org/openrewrite/OmniParser.java +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import lombok.AccessLevel; -import lombok.RequiredArgsConstructor; -import org.openrewrite.hcl.HclParser; -import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.json.JsonParser; -import org.openrewrite.properties.PropertiesParser; -import org.openrewrite.protobuf.ProtoParser; -import org.openrewrite.quark.QuarkParser; -import org.openrewrite.shaded.jgit.ignore.IgnoreNode; -import org.openrewrite.text.PlainTextParser; -import org.openrewrite.xml.XmlParser; -import org.openrewrite.yaml.YamlParser; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.*; -import java.util.function.Consumer; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -import static java.util.Collections.emptyList; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public class OmniParser implements Parser { - private static final Collection DEFAULT_IGNORED_DIRECTORIES = Arrays.asList( - "build", - "target", - "out", - ".gradle", - ".idea", - ".project", - "node_modules", - ".git", - ".metadata", - ".DS_Store", - ".moderne" - ); - - private final Collection exclusions; - private final Collection exclusionMatchers; - private final int sizeThresholdMb; - private final Collection excludedDirectories; - private final Collection plainTextMasks; - private final boolean parallel; - private final List parsers; - private final Consumer onParse; - - public Stream parseAll(Path rootDir) { - return parse(acceptedPaths(rootDir), rootDir, new InMemoryExecutionContext()); - } - - @Override - public Stream parse(Iterable sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) { - int count = 0; - for (Path ignored : sourceFiles) { - count++; - } - onParse.accept(count); - return Parser.super.parse(sourceFiles, relativeTo, ctx); - } - - public List acceptedPaths(Path rootDir) { - List parseable = new ArrayList<>(); - Map gitignoreStack = new LinkedHashMap<>(); - - try { - Files.walkFileTree(rootDir, new SimpleFileVisitor() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { - loadGitignore(dir).ifPresent(ignoreNode -> gitignoreStack.put(dir, ignoreNode)); - return isExcluded(dir, rootDir) || - isIgnoredDirectory(dir, rootDir) || - excludedDirectories.contains(dir) || - isGitignored(gitignoreStack.values(), dir, rootDir) ? - FileVisitResult.SKIP_SUBTREE : - FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { - if (!attrs.isOther() && !attrs.isSymbolicLink() && - !isExcluded(file, rootDir) && - !isGitignored(gitignoreStack.values(), file, rootDir)) { - if (!isOverSizeThreshold(attrs.size()) && !isParsedAsPlainText(file, rootDir)) { - for (Parser parser : parsers) { - if (parser.accept(file)) { - parseable.add(file); - break; - } - } - } - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) { - gitignoreStack.remove(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - // cannot happen, since none of the visit methods throw an IOException - throw new UncheckedIOException(e); - } - return parseable; - } - - @Override - public Stream parseInputs(Iterable sources, @Nullable Path relativeTo, - ExecutionContext ctx) { - return StreamSupport.stream(sources.spliterator(), parallel).flatMap(input -> { - Path path = input.getPath(); - for (Parser parser : parsers) { - if (parser.accept(path)) { - return parser.parseInputs(Collections.singletonList(input), relativeTo, ctx); - } - } - return Stream.empty(); - }); - } - - @Override - public boolean accept(Path path) { - for (Parser parser : parsers) { - if (parser.accept(path)) { - return true; - } - } - return false; - } - - @Override - public Path sourcePathFromSourceText(Path prefix, String sourceCode) { - return Paths.get("resource.me"); - } - - private boolean isOverSizeThreshold(long fileSize) { - return sizeThresholdMb > 0 && fileSize > sizeThresholdMb * 1024L * 1024L; - } - - private boolean isExcluded(Path path, Path rootDir) { - if (exclusions.contains(path)) { - return true; - } - for (PathMatcher excluded : exclusionMatchers) { - if (excluded.matches(rootDir.relativize(path))) { - return true; - } - } - return false; - } - - private boolean isParsedAsPlainText(Path path, Path rootDir) { - if (!plainTextMasks.isEmpty()) { - Path computed = rootDir.relativize(path); - if (!computed.startsWith("/")) { - computed = Paths.get("/").resolve(computed); - } - for (PathMatcher matcher : plainTextMasks) { - if (matcher.matches(computed)) { - return true; - } - } - } - return false; - } - - private boolean isIgnoredDirectory(Path path, Path rootDir) { - for (Path pathSegment : rootDir.relativize(path)) { - if (DEFAULT_IGNORED_DIRECTORIES.contains(pathSegment.toString())) { - return true; - } - } - return false; - } - - private Optional loadGitignore(Path dir) { - Path gitignore = dir.resolve(".gitignore"); - if (!Files.exists(gitignore)) { - return Optional.empty(); - } - IgnoreNode ignoreNode = new IgnoreNode(); - try (InputStream is = Files.newInputStream(gitignore)) { - ignoreNode.parse(is); - } catch (IOException e) { - throw new UncheckedIOException("Error reading '" + gitignore + "'", e); - } - return Optional.of(ignoreNode); - } - - private boolean isGitignored(Collection gitignoreStack, Path path, Path rootDir) { - // We are retrieving the elements in insertion order thanks to Deque - for (IgnoreNode ignoreNode : gitignoreStack) { - Boolean result = ignoreNode.checkIgnored(rootDir.relativize(path).toFile().getPath(), path.toFile().isDirectory()); - if (result != null) { - return result; - } - } - return false; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder extends Parser.Builder { - private Collection exclusions = emptyList(); - private Collection exclusionMatchers = emptyList(); - private int sizeThresholdMb = 10; - private Collection excludedDirectories = emptyList(); - private Collection plainTextMasks = emptyList(); - private boolean parallel; - private Consumer onParse = inputCount -> { - }; - - private List parsers = new ArrayList<>(Arrays.asList( - new JsonParser(), - new XmlParser(), - new YamlParser(), - new PropertiesParser(), - new ProtoParser(), - HclParser.builder().build(), - new PlainTextParser(), - new QuarkParser() - )); - - public Builder() { - super(SourceFile.class); - } - - public Builder exclusions(Collection exclusions) { - this.exclusions = exclusions; - return this; - } - - public Builder exclusionMatchers(Collection exclusions) { - this.exclusionMatchers = exclusions; - return this; - } - - public Builder sizeThresholdMb(int sizeThresholdMb) { - this.sizeThresholdMb = sizeThresholdMb; - return this; - } - - public Builder excludedDirectories(Collection excludedDirectories) { - this.excludedDirectories = excludedDirectories; - return this; - } - - public Builder plainTextMasks(Collection plainTextMasks) { - this.plainTextMasks = plainTextMasks; - return this; - } - - public Builder onParse(Consumer onParse) { - this.onParse = onParse; - return this; - } - - public Builder parsers(Parser... parsers) { - this.parsers = Arrays.asList(parsers); - return this; - } - - public Builder addParsers(Parser... parsers) { - this.parsers.addAll(Arrays.asList(parsers)); - return this; - } - - /** - * Resource parsers are safe to execute in parallel. This is not true of all parsers, for example - * the MavenParser. - * - * @param parallel whether the parser stream should be parallelized. - * @return this builder. - */ - public Builder parallel(boolean parallel) { - this.parallel = parallel; - return this; - } - - @Override - public OmniParser build() { - return new OmniParser(exclusions, exclusionMatchers, sizeThresholdMb, - excludedDirectories, plainTextMasks, parallel, parsers, onParse); - } - - @Override - public String getDslName() { - return "omni"; - } - } -} diff --git a/src/main/java/org/openrewrite/ProgressBar.java b/src/main/java/org/openrewrite/ProgressBar.java deleted file mode 100644 index 0ebcd7d..0000000 --- a/src/main/java/org/openrewrite/ProgressBar.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import org.openrewrite.internal.lang.Nullable; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -public interface ProgressBar extends AutoCloseable { - - void intermediateResult(@Nullable String message); - - void finish(String message); - - @Override - void close(); - - void step(); - - @SuppressWarnings("UnusedReturnValue") - ProgressBar setExtraMessage(String extraMessage); - - ProgressBar setMax(int max); -} diff --git a/src/main/java/org/openrewrite/RemoteProgressBarReceiver.java b/src/main/java/org/openrewrite/RemoteProgressBarReceiver.java deleted file mode 100644 index 3fd14d0..0000000 --- a/src/main/java/org/openrewrite/RemoteProgressBarReceiver.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import org.openrewrite.RemoteProgressBarSender.Request.Type; -import org.openrewrite.internal.lang.Nullable; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static java.util.Objects.requireNonNull; -import static org.openrewrite.RemoteProgressBarSender.MAX_MESSAGE_SIZE; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -public class RemoteProgressBarReceiver implements ProgressBar { - private static final ExecutorService PROGRESS_RECEIVER_POOL = Executors.newCachedThreadPool(); - - private final ProgressBar delegate; - private final DatagramSocket socket; - - public RemoteProgressBarReceiver(ProgressBar delegate) { - this.delegate = delegate; - try { - this.socket = new DatagramSocket(); - PROGRESS_RECEIVER_POOL.submit(this::receive); - } catch (SocketException e) { - throw new UncheckedIOException(e); - } - } - - public int getPort() { - return socket.getLocalPort(); - } - - public void receive() { - try { - for (; ; ) { - byte[] buf = new byte[MAX_MESSAGE_SIZE]; // no message should be longer than a terminal line length - DatagramPacket packet = new DatagramPacket(buf, buf.length); - try { - socket.receive(packet); - } catch (SocketTimeoutException ignored) { - break; - } - - Type type = null; - for (Type t : Type.values()) { - if (t.ordinal() == buf[0] - '0') { - type = t; - break; - } - } - - if (type == null) { - return; - } - - String message = null; - if (packet.getLength() > 1) { - message = new String(Arrays.copyOfRange(buf, 1, packet.getLength()), - StandardCharsets.UTF_8); - } - - switch (type) { - case IntermediateResult: - delegate.intermediateResult(message); - break; - case Step: - delegate.step(); - break; - case SetExtraMessage: - delegate.setExtraMessage(requireNonNull(message)); - break; - case SetMax: - delegate.setMax(Integer.parseInt(requireNonNull(message))); - break; - } - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void intermediateResult(@Nullable String message) { - delegate.intermediateResult(message); - } - - @Override - public void finish(String message) { - delegate.finish(message); - } - - @Override - public void step() { - delegate.step(); - } - - @Override - public ProgressBar setExtraMessage(String extraMessage) { - return delegate.setExtraMessage(extraMessage); - } - - @Override - public ProgressBar setMax(int max) { - return delegate.setMax(max); - } - - @Override - public void close() { - socket.close(); - } -} diff --git a/src/main/java/org/openrewrite/RemoteProgressBarSender.java b/src/main/java/org/openrewrite/RemoteProgressBarSender.java deleted file mode 100644 index ff12e56..0000000 --- a/src/main/java/org/openrewrite/RemoteProgressBarSender.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import lombok.Value; -import org.openrewrite.internal.StringUtils; -import org.openrewrite.internal.lang.Nullable; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.*; -import java.nio.file.Files; -import java.nio.file.Paths; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -public class RemoteProgressBarSender implements ProgressBar { - final static int MAX_MESSAGE_SIZE = 256; - - private final DatagramSocket socket; - private final InetAddress address; - private final int port; - - public RemoteProgressBarSender(int port) { - this(null, port); - } - - public RemoteProgressBarSender(@Nullable InetAddress address, int port) { - try { - String localhost = Files.exists(Paths.get("/.dockerenv")) ? "host.docker.internal" : "localhost"; - this.address = address == null ? InetAddress.getByName(localhost) : address; - this.port = port; - this.socket = new DatagramSocket(); - } catch (UnknownHostException | SocketException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void intermediateResult(@Nullable String message) { - send(Request.Type.IntermediateResult, message); - } - - @Override - public void finish(String message) { - throw new UnsupportedOperationException("The finish message must be determined by the receiver"); - } - - @Override - public void close() { - socket.close(); - } - - @Override - public void step() { - send(Request.Type.Step, null); - } - - @Override - public ProgressBar setExtraMessage(String extraMessage) { - send(Request.Type.SetExtraMessage, extraMessage); - return this; - } - - @Override - public ProgressBar setMax(int max) { - send(Request.Type.SetMax, Integer.toString(max)); - return this; - } - - private void send(Request.Type type, @Nullable String message) { - try { - if (message != null && message.length() + 1 > MAX_MESSAGE_SIZE) { - throw new IllegalArgumentException("Message size exceeded maximum length: " + message); - } - byte[] buf = (type.ordinal() + (message == null ? "" : message)).getBytes(); - DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); - socket.send(packet); - } catch (SocketException ignored) { - // the remote receiver may not be listening any longer, so ignore - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Value - static class Request { - enum Type { - IntermediateResult, - Step, - SetExtraMessage, - SetMax - } - - Type type; - - @Nullable - String body; - } -} diff --git a/src/test/java/org/openrewrite/RemoteProgressBarTest.java b/src/test/java/org/openrewrite/RemoteProgressBarTest.java deleted file mode 100644 index 24dee7a..0000000 --- a/src/test/java/org/openrewrite/RemoteProgressBarTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite; - -import org.junit.jupiter.api.Test; -import org.openrewrite.internal.lang.Nullable; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @deprecated This class has been moved to rewrite-polyglot. - */ -@Deprecated -public class RemoteProgressBarTest { - - @Test - void remote() throws InterruptedException { - CountDownLatch latch = new CountDownLatch(4); - try ( - ProgressBar progressBar = new ProgressBar() { - @Override - public void intermediateResult(@Nullable String message) { - assertThat(message).isEqualTo("intermediate"); - latch.countDown(); - } - - @Override - public void finish(String message) { - } - - @Override - public void close() { - } - - @Override - public void step() { - latch.countDown(); - } - - @Override - public ProgressBar setExtraMessage(String extraMessage) { - assertThat(extraMessage).isEqualTo("extra"); - latch.countDown(); - return this; - } - - @Override - public ProgressBar setMax(int max) { - assertThat(max).isEqualTo(100); - latch.countDown(); - return this; - } - }; - - RemoteProgressBarReceiver receiver = new RemoteProgressBarReceiver(progressBar)) { - try (ProgressBar sender = new RemoteProgressBarSender(receiver.getPort())) { - sender.setMax(100); - sender.step(); - sender.setExtraMessage("extra"); - sender.intermediateResult("intermediate"); - } - - assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); - } - } -}