Skip to content

Commit

Permalink
ROO-1480: Make FileConverter respect Shell.getHome() convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Alex committed Oct 3, 2010
1 parent 5784a23 commit 0318f13
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.springframework.roo.shell.jline;


import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileWriter;
Expand Down Expand Up @@ -371,14 +372,20 @@ protected void logCommandToOutput(String processedLine) {
}

/**
* Obtains the "roo.home" from the system property, throwing an exception if missing.
* Obtains the "roo.home" from the system property, falling back to the current working directory if missing.
*
* @return the 'roo.home' system property
*/
@Override
protected String getHomeAsString() {
String rooHome = System.getProperty("roo.home");
Assert.hasText(rooHome, "roo.home system property is not set");
if (rooHome == null) {
try {
rooHome = new File(".").getCanonicalPath();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
return rooHome;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.springframework.roo.shell.osgi.converters;

import java.io.File;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.shell.Shell;
import org.springframework.roo.shell.converters.FileConverter;

/**
Expand All @@ -12,4 +16,12 @@
*/
@Component
@Service
public class FileConverterComponent extends FileConverter {}
public class FileConverterComponent extends FileConverter {
@Reference private Shell shell;

@Override
protected File getWorkingDirectory() {
return shell.getHome();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,33 @@
* @since 1.0
*
*/
public class FileConverter implements Converter {
public abstract class FileConverter implements Converter {

private static final String home = System.getProperty("user.home");

/**
* @return the "current working directory" this {@link FileConverter} should use if the user fails to provide
* an explicit directory in their input (required)
*/
protected abstract File getWorkingDirectory();

public Object convertFromText(String value, Class<?> requiredType, String optionContext) {
return new File(removeTildeIfNeeded(value));
return new File(convertUserInputIntoAFullyQualifiedPath(value));
}

public boolean getAllPossibleValues(List<String> completions, Class<?> requiredType, String originalUserInput, String optionContext, MethodTarget target) {
String adjustedUserInput = removeTildeIfNeeded(originalUserInput);
String adjustedUserInput = convertUserInputIntoAFullyQualifiedPath(originalUserInput);

String directoryData = "";
if (adjustedUserInput != null && adjustedUserInput.contains(File.separator)) {
directoryData = adjustedUserInput.substring(0, adjustedUserInput.lastIndexOf(File.separator) + 1);
adjustedUserInput = adjustedUserInput.substring(adjustedUserInput.lastIndexOf(File.separator) + 1);
}
String directoryData = adjustedUserInput.substring(0, adjustedUserInput.lastIndexOf(File.separator) + 1);
adjustedUserInput = adjustedUserInput.substring(adjustedUserInput.lastIndexOf(File.separator) + 1);

populate(completions, adjustedUserInput, originalUserInput, directoryData);

return false;
}

protected void populate(List<String> completions, String adjustedUserInput, String originalUserInput, String directoryData) {
File directory = new File(directoryData.length() > 0 ? directoryData : ".");
File directory = new File(directoryData);

if (!directory.isDirectory()) {
return;
Expand All @@ -54,7 +57,7 @@ protected void populate(List<String> completions, String adjustedUserInput, Stri
completion += directoryData;
completion += file.getName();

completion = addTildeIfNeeded(originalUserInput, completion);
completion = convertCompletionBackIntoUserInputStyle(originalUserInput, completion);

if (file.isDirectory()) {
completions.add(completion + File.separator);
Expand All @@ -69,27 +72,51 @@ public boolean supports(Class<?> requiredType, String optionContext) {
return File.class.isAssignableFrom(requiredType);
}

private String addTildeIfNeeded(String originalUserInput, String completion) {
if (!originalUserInput.startsWith("~")) {
private String convertCompletionBackIntoUserInputStyle(String originalUserInput, String completion) {
if (originalUserInput.startsWith(File.separator)) {
// Input was originally as a fully-qualified path, so we just keep the completion in that form
return completion;
}
Assert.notNull(home, "Home directory could not be determined from system properties");
if (!completion.startsWith(home)) {
// this completion isn't even under the home directory (which is a bit weird given it started with ~, but anyway...)
return completion;
if (originalUserInput.startsWith("~")) {
// Input originally started with a ~, so replace the user's home directory with a ~ again
Assert.notNull(home, "Home directory could not be determined from system properties");
return "~" + completion.substring(home.length());
}
return "~" + completion.substring(home.length());
// The path was working directory specific, so strip the working directory given the user never typed it
return completion.substring(getWorkingDirectoryAsString().length());
}

private String removeTildeIfNeeded(String userInput) {
if (!userInput.startsWith("~")) {
/**
* If the user input starts with a tilde character (~), replace the tilde character with the
* user's home directory. If the user input does not start with a tilde, simply return the original
* user input without any changes.
*
* @param userInput the user input, which may commence with a tilde (required)
* @return a string that is guaranteed to no longer contain a tilde as the first character (never null)
*/
private String convertUserInputIntoAFullyQualifiedPath(String userInput) {
if (userInput.startsWith(File.separator)) {
// Input is already in a fully-qualified path form
return userInput;
}
Assert.notNull(home, "Home directory could not be determined from system properties");
if (userInput.length() > 1) {
return home + userInput.substring(1);
if (userInput.startsWith("~")) {
// Input starts with a ~, so replace with the user's home directory
Assert.notNull(home, "Home directory could not be determined from system properties");
if (userInput.length() > 1) {
return home + userInput.substring(1);
}
}
// The path is working directory specific, so prepend the working directory
String fullPath = getWorkingDirectoryAsString() + userInput;
return fullPath;
}

private String getWorkingDirectoryAsString() {
try {
return getWorkingDirectory().getCanonicalPath() + File.separator;
} catch (Exception e) {
throw new IllegalStateException(e);
}
return home;
}

}

0 comments on commit 0318f13

Please sign in to comment.