Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move off plexus #49

Merged
merged 1 commit into from
Dec 31, 2023
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
44 changes: 37 additions & 7 deletions plexus-interactivity-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,48 @@
<artifactId>plexus-utils</artifactId>
<version>4.0.0</version>
</dependency>

<!-- JLine is optional -->
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.23.0</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<version>0.9.0.M2</version>
<scope>test</scope>
</dependency>
<!--
| @PostConstruct and @PreDestroy help with Plexus->JSR330 migration
-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>6.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
* @version $Id$
*/
public abstract class AbstractInputHandler implements InputHandler {
@Override
public List<String> readMultipleLines() throws IOException {
List<String> lines = new ArrayList<>();
String line = readLine();
while (line != null && line.length() > 0) {
while (line != null && !line.isEmpty()) {
lines.add(line);
line = readLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* SOFTWARE.
*/

import javax.annotation.PostConstruct;
import javax.inject.Named;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -36,19 +36,17 @@
* @author Brett Porter
* @version $Id$
*/
@Named
public class DefaultInputHandler extends AbstractInputHandler {
private BufferedReader consoleReader;
private final BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

@Override
public String readLine() throws IOException {
return consoleReader.readLine();
}

@Override
public String readPassword() throws IOException {
return consoleReader.readLine();
}

@PostConstruct
public void initialize() {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
* SOFTWARE.
*/

import javax.annotation.PostConstruct;
import javax.inject.Named;

import java.io.IOException;
import java.io.PrintWriter;

/**
Expand All @@ -35,20 +34,18 @@
* @author Brett Porter
* @version $Id$
*/
@Named
public class DefaultOutputHandler implements OutputHandler {
private PrintWriter consoleWriter;
private final PrintWriter consoleWriter = new PrintWriter(System.out);

@PostConstruct
public void initialize() {
consoleWriter = new PrintWriter(System.out);
}

public void write(String line) throws IOException {
@Override
public void write(String line) {
consoleWriter.print(line);
consoleWriter.flush();
}

public void writeLine(String line) throws IOException {
@Override
public void writeLine(String line) {
consoleWriter.println();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* SOFTWARE.
*/

import javax.inject.Inject;
import javax.inject.Named;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
Expand All @@ -36,26 +39,19 @@
* @author Brett Porter
* @version $Id$
*/
@Named
public class DefaultPrompter implements Prompter {
/**
* @requirement
*/
private OutputHandler outputHandler;

/**
* @requirement
*/
private InputHandler inputHandler;

public DefaultPrompter() {
super();
}
private final OutputHandler outputHandler;

private final InputHandler inputHandler;

@Inject
public DefaultPrompter(OutputHandler outputHandler, InputHandler inputHandler) {
this.outputHandler = outputHandler;
this.inputHandler = inputHandler;
}

@Override
public String prompt(String message) throws PrompterException {
try {
writePrompt(message);
Expand All @@ -70,6 +66,7 @@ public String prompt(String message) throws PrompterException {
}
}

@Override
public String prompt(String message, String defaultReply) throws PrompterException {
try {
writePrompt(formatMessage(message, null, defaultReply));
Expand All @@ -90,6 +87,7 @@ public String prompt(String message, String defaultReply) throws PrompterExcepti
}
}

@Override
public String prompt(String message, List<String> possibleValues, String defaultReply) throws PrompterException {
String formattedMessage = formatMessage(message, possibleValues, defaultReply);

Expand Down Expand Up @@ -127,10 +125,12 @@ public String prompt(String message, List<String> possibleValues, String default
return line;
}

@Override
public String prompt(String message, List<String> possibleValues) throws PrompterException {
return prompt(message, possibleValues, null);
}

@Override
public String promptForPassword(String message) throws PrompterException {
try {
writePrompt(message);
Expand Down Expand Up @@ -177,6 +177,7 @@ private void writePrompt(String message) throws IOException {
outputHandler.write(message + ": ");
}

@Override
public void showMessage(String message) throws PrompterException {
try {
writePrompt(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
* @version $Id$
*/
public interface InputHandler {
String ROLE = InputHandler.class.getName();

/**
* Read a single line of input, swalling the newline at the end.
* If the input can be echoed, it will be.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
* @version $Id$
*/
public interface OutputHandler {
String ROLE = OutputHandler.class.getName();

/**
* Write a single line of input, excluding the newline at the end.
* @param line the line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
* @version $Id$
*/
public interface Prompter {
String ROLE = Prompter.class.getName();

String prompt(String message) throws PrompterException;

String prompt(String message, String defaultReply) throws PrompterException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
* SOFTWARE.
*/

import javax.annotation.PostConstruct;
import javax.inject.Named;

import java.io.IOException;

import jline.ConsoleReader;
import org.codehaus.plexus.components.interactivity.AbstractInputHandler;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;

/**
* Default input handler, that uses the console.
*
* @author Brett Porter
* @version $Id$
*/
@Named("jline")
public class JLineInputHandler extends AbstractInputHandler {
private ConsoleReader consoleReader;
private LineReader consoleReader = LineReaderBuilder.builder().build();

public String readLine() throws IOException {
return consoleReader.readLine();
Expand All @@ -47,13 +49,4 @@ public String readLine() throws IOException {
public String readPassword() throws IOException {
return consoleReader.readLine(new Character('*'));
}

@PostConstruct
public void initialize() {
try {
consoleReader = new ConsoleReader();
} catch (IOException e) {
throw new IllegalStateException("Cannot create console reader: ", e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.codehaus.plexus.components.interactivity;

/*
* The MIT License
*
* Copyright (c) 2005, The Codehaus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import javax.inject.Inject;

import org.eclipse.sisu.launch.InjectedTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DefaultPrompterComponentTest extends InjectedTest {

@Inject
private Prompter prompter;

@Test
void smoke() throws PrompterException {
assertNotNull(prompter);
}
}
Loading