diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/Prompter.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/Prompter.java deleted file mode 100644 index 085952ff180c6..0000000000000 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/Prompter.java +++ /dev/null @@ -1,83 +0,0 @@ -package io.quarkus.gradle; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.function.Consumer; - -import org.aesh.readline.Readline; -import org.aesh.readline.ReadlineBuilder; -import org.aesh.readline.tty.terminal.TerminalConnection; -import org.aesh.terminal.tty.Signal; - -/** - * Prompt implementation. - */ -public class Prompter { - - private static class Prompt { - private final String prompt; - private final String defaultValue; - private final Consumer inputConsumer; - - public Prompt(String prompt, String defaultValue, Consumer inputConsumer) { - this.prompt = prompt; - this.defaultValue = defaultValue; - this.inputConsumer = inputConsumer; - } - } - - private final List prompts = new ArrayList<>(); - - public Prompter() throws IOException { - } - - public Prompter addPrompt(String prompt, Consumer inputConsumer) { - prompts.add(new Prompt(prompt, null, inputConsumer)); - return this; - } - - public Prompter addPrompt(String prompt, String defaultValue, Consumer inputConsumer) { - prompts.add(new Prompt(prompt, defaultValue, inputConsumer)); - return this; - } - - public void collectInput() throws IOException { - if (prompts.isEmpty()) { - return; - } - final TerminalConnection connection = new TerminalConnection(); - connection.setSignalHandler(interruptionSignalHandler()); - try { - read(connection, ReadlineBuilder.builder().enableHistory(false).build(), prompts.iterator()); - connection.openBlocking(); - } finally { - connection.close(); - } - } - - private static void read(TerminalConnection connection, Readline readline, Iterator prompts) { - final Prompt prompt = prompts.next(); - readline.readline(connection, prompt.prompt, input -> { - prompt.inputConsumer.accept( - (input == null || input.isBlank()) && prompt.defaultValue != null ? prompt.defaultValue : input); - if (!prompts.hasNext()) { - connection.close(); - } else { - read(connection, readline, prompts); - } - }); - } - - private Consumer interruptionSignalHandler() { - return new Consumer() { - @Override - public void accept(Signal signal) { - if (signal == Signal.INT) { - throw new RuntimeException("Process interrupted"); - } - } - }; - } -} diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java index 9124c1a7157aa..087cfe11dd965 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java @@ -5,6 +5,7 @@ import java.io.BufferedWriter; import java.io.File; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -17,8 +18,8 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Scanner; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import javax.inject.Inject; @@ -69,7 +70,6 @@ import io.quarkus.deployment.dev.DevModeContext; import io.quarkus.deployment.dev.DevModeMain; import io.quarkus.deployment.dev.QuarkusDevModeLauncher; -import io.quarkus.gradle.Prompter; import io.quarkus.gradle.dependency.ApplicationDeploymentClasspathBuilder; import io.quarkus.gradle.dsl.CompilerOption; import io.quarkus.gradle.dsl.CompilerOptions; @@ -324,14 +324,16 @@ public void startDev() { AnalyticsService analyticsService = new AnalyticsService(FileLocationsImpl.INSTANCE, new GradleMessageWriter(getLogger())); analyticsService.buildAnalyticsUserInput((String prompt) -> { - try { - final AtomicReference userInput = new AtomicReference<>(""); - final Prompter prompter = new Prompter(); - prompter.addPrompt(prompt, input -> userInput.set(input)); - prompter.collectInput(); - return userInput.get(); - } catch (IOException e) { - getLogger().debug("Failed to collect user input for analytics", e); + System.out.print(prompt); + try (Scanner scanner = new Scanner(new FilterInputStream(System.in) { + @Override + public void close() throws IOException { + //don't close System.in! + } + })) { + return scanner.nextLine(); + } catch (Exception e) { + getLogger().warn("Failed to collect user input for analytics", e); return ""; } }); diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index e204db0bf7218..df7db12ab28e5 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -15,6 +15,7 @@ import static org.twdata.maven.mojoexecutor.MojoExecutor.version; import java.io.File; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -31,8 +32,8 @@ import java.util.Map; import java.util.Optional; import java.util.Properties; +import java.util.Scanner; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -102,7 +103,6 @@ import io.quarkus.maven.MavenDevModeLauncher.Builder; import io.quarkus.maven.components.CompilerOptions; import io.quarkus.maven.components.MavenVersionEnforcer; -import io.quarkus.maven.components.Prompter; import io.quarkus.maven.components.QuarkusWorkspaceProvider; import io.quarkus.maven.dependency.ArtifactCoords; import io.quarkus.maven.dependency.ArtifactKey; @@ -411,14 +411,16 @@ public void execute() throws MojoFailureException, MojoExecutionException { saveTerminalState(); analyticsProvider.buildAnalyticsUserInput((String prompt) -> { - try { - final AtomicReference userInput = new AtomicReference<>(""); - final Prompter prompter = new Prompter(); - prompter.addPrompt(prompt, input -> userInput.set(input)); - prompter.collectInput(); - return userInput.get(); - } catch (IOException e) { - getLog().debug("Failed to collect user input for analytics", e); + System.out.print(prompt); + try (Scanner scanner = new Scanner(new FilterInputStream(System.in) { + @Override + public void close() throws IOException { + //don't close System.in! + } + })) { + return scanner.nextLine(); + } catch (Exception e) { + getLog().warn("Failed to collect user input for analytics", e); return ""; } });