Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class PrompterException extends MavenException {
@Serial
private static final long serialVersionUID = -3505070928479515081L;

public PrompterException(String message) {
super(message);
}

/**
* @param message the message to give
* @param e the {@link Exception}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.jline.DefaultPrompter;
import org.codehaus.plexus.components.interactivity.InputHandler;
import org.codehaus.plexus.components.interactivity.OutputHandler;
import org.codehaus.plexus.components.interactivity.Prompter;
Expand All @@ -36,28 +35,36 @@

/**
* This class is injected into any legacy component that would want to use legacy "Plexus Interactivity API".
* It simply delegates to Maven4 API {@link DefaultPrompter}.
* It simply delegates to Maven4 API {@link org.apache.maven.api.services.Prompter}.
*/
@Experimental
@Named
@Singleton
@Priority(10)
public class LegacyPlexusInteractivity implements Prompter, InputHandler, OutputHandler {
private final DefaultPrompter defaultPrompter;
private final org.apache.maven.api.services.Prompter prompter;

@Inject
public LegacyPlexusInteractivity(DefaultPrompter defaultPrompter) {
this.defaultPrompter = defaultPrompter;
public LegacyPlexusInteractivity(org.apache.maven.api.services.Prompter prompter) {
this.prompter = prompter;
}

@Override
public String readLine() throws IOException {
return defaultPrompter.doPrompt(null, false);
try {
return prompter.prompt(null);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new IOException("Unable to prompt", e);
}
}

@Override
public String readPassword() throws IOException {
return defaultPrompter.doPrompt(null, true);
try {
return prompter.promptForPassword(null);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new IOException("Unable to prompt", e);
}
}

@Override
Expand All @@ -71,18 +78,26 @@ public List<String> readMultipleLines() throws IOException {

@Override
public void write(String line) throws IOException {
defaultPrompter.doDisplay(line);
try {
prompter.showMessage(line);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new IOException("Unable to prompt", e);
}
}

@Override
public void writeLine(String line) throws IOException {
defaultPrompter.doDisplay(line + System.lineSeparator());
try {
prompter.showMessage(line + System.lineSeparator());
} catch (org.apache.maven.api.services.PrompterException e) {
throw new IOException("Unable to prompt", e);
}
}

@Override
public String prompt(String message) throws PrompterException {
try {
return defaultPrompter.prompt(message, null, null);
return prompter.prompt(message, null, null);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to prompt", e);
}
Expand All @@ -91,7 +106,7 @@ public String prompt(String message) throws PrompterException {
@Override
public String prompt(String message, String defaultReply) throws PrompterException {
try {
return defaultPrompter.prompt(message, null, defaultReply);
return prompter.prompt(message, null, defaultReply);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to prompt", e);
}
Expand All @@ -100,7 +115,7 @@ public String prompt(String message, String defaultReply) throws PrompterExcepti
@Override
public String prompt(String message, List possibleValues) throws PrompterException {
try {
return defaultPrompter.prompt(message, possibleValues, null);
return prompter.prompt(message, possibleValues, null);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to prompt", e);
}
Expand All @@ -109,7 +124,7 @@ public String prompt(String message, List possibleValues) throws PrompterExcepti
@Override
public String prompt(String message, List possibleValues, String defaultReply) throws PrompterException {
try {
return defaultPrompter.prompt(message, possibleValues, defaultReply);
return prompter.prompt(message, possibleValues, defaultReply);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to prompt", e);
}
Expand All @@ -118,7 +133,7 @@ public String prompt(String message, List possibleValues, String defaultReply) t
@Override
public String promptForPassword(String message) throws PrompterException {
try {
return defaultPrompter.promptForPassword(message);
return prompter.promptForPassword(message);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to promptForPassword", e);
}
Expand All @@ -127,7 +142,7 @@ public String promptForPassword(String message) throws PrompterException {
@Override
public void showMessage(String message) throws PrompterException {
try {
defaultPrompter.showMessage(message);
prompter.showMessage(message);
} catch (org.apache.maven.api.services.PrompterException e) {
throw new PrompterException("Unable to showMessage", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.maven.jline;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

Expand All @@ -35,35 +34,20 @@ public class DefaultPrompter implements Prompter {

@Override
public String prompt(String message, List<String> possibleValues, String defaultReply) throws PrompterException {
try {
return doPrompt(message, possibleValues, defaultReply, false);
} catch (IOException e) {
throw new PrompterException("Failed to prompt", e);
}
return doPrompt(message, possibleValues, defaultReply, false);
}

@Override
public String promptForPassword(String message) throws PrompterException {
try {
return doPrompt(message, null, null, true);
} catch (IOException e) {
throw new PrompterException("Failed to prompt for password", e);
}
return doPrompt(message, null, null, true);
}

@Override
public void showMessage(String message) throws PrompterException {
try {
doDisplay(message);
} catch (IOException e) {
throw new PrompterException("Failed to present message", e);
}
doDisplay(message);
}

/**
* Used by {@code LegacyPlexusInteractivity}
*/
public String doPrompt(String message, boolean password) throws IOException {
private String doPrompt(String message, boolean password) {
try {
if (message != null) {
if (!message.endsWith("\n")) {
Expand All @@ -81,30 +65,26 @@ public String doPrompt(String message, boolean password) throws IOException {
}
return MessageUtils.reader.readLine(message, password ? '*' : null);
} catch (Exception e) {
throw new IOException("Unable to prompt user", e);
throw new PrompterException("Unable to prompt user", e);
}
}

/**
* Used by {@code LegacyPlexusInteractivity}
*/
public void doDisplay(String message) throws IOException {
private void doDisplay(String message) {
try {
MessageUtils.terminal.writer().print(message);
MessageUtils.terminal.flush();
} catch (Exception e) {
throw new IOException("Unable to display message", e);
throw new PrompterException("Unable to display message", e);
}
}

private String doPrompt(String message, List<?> possibleValues, String defaultReply, boolean password)
throws IOException {
private String doPrompt(String message, List<?> possibleValues, String defaultReply, boolean password) {
String formattedMessage = formatMessage(message, possibleValues, defaultReply);
String line;
do {
line = doPrompt(formattedMessage, password);
if (line == null && defaultReply == null) {
throw new IOException("EOF");
throw new PrompterException("EOF");
}
if (line == null || line.isEmpty()) {
line = defaultReply;
Expand All @@ -117,6 +97,9 @@ private String doPrompt(String message, List<?> possibleValues, String defaultRe
}

private String formatMessage(String message, List<?> possibleValues, String defaultReply) {
if (message == null) {
return null;
}
StringBuilder formatted = new StringBuilder(message.length() * 2);
formatted.append(message);
if (possibleValues != null && !possibleValues.isEmpty()) {
Expand Down