Skip to content

Commit

Permalink
aesh now checks for newly added commands and syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
stalep committed Feb 7, 2013
1 parent 5e00f8e commit 962f1b1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
44 changes: 42 additions & 2 deletions aesh/src/main/java/org/jboss/forge/aesh/ForgeShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import javax.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
Expand Down Expand Up @@ -77,13 +79,51 @@ public void initShell() throws Exception
}
}

//not very optimal atm.
private void verifyLoadedCommands() throws Exception {
//only bother to check whats been added/removed if it doesnt
// have the same size
if(registry.getExportedInstances(UICommand.class).size() !=
commands.size()) {
for(ExportedInstance<UICommand> instance : registry.getExportedInstances(UICommand.class)) {
if(!doCommandExist(instance.get().getMetadata().getName())) {
addCommand(new ShellCommand(instance.get(), this));
}
}
Iterator<ShellCommand> iterable = commands.iterator();
while(iterable.hasNext()) {
if(!isCommandFound(iterable.next().getCommand().getMetadata().getName(),
registry.getExportedInstances(UICommand.class))) {
iterable.remove();
}
}
}
}

private boolean doCommandExist(String name) {
for(ShellCommand command : commands) {
if(command.getCommand().getMetadata().getName().equals(name))
return true;
}
return false;
}

private boolean isCommandFound(String name, Set<ExportedInstance<UICommand>> uiCommands) {
for(ExportedInstance<UICommand> command : uiCommands) {
if(command.get().getMetadata().getName().equals(name))
return true;
}
return false;
}

public void startShell() throws Exception
{
output = null;
while ((output = console.read(prompt, null)) != null)
{
CommandLine cl = null;
if(output.getBuffer() != null && !output.getBuffer().trim().isEmpty()) {
verifyLoadedCommands();
for (ShellCommand command : commands)
{
try
Expand Down Expand Up @@ -165,8 +205,8 @@ private Prompt createPrompt() {
CharacterType.BOLD));
chars.add(new TerminalCharacter(']', Color.DEFAULT_BG, Color.BLUE_TEXT,
CharacterType.PLAIN));
chars.add(new TerminalCharacter('$', Color.DEFAULT_BG, Color.WHITE_TEXT));
chars.add(new TerminalCharacter(' ', Color.DEFAULT_BG, Color.WHITE_TEXT));
chars.add(new TerminalCharacter('$', Color.DEFAULT_BG, Color.DEFAULT_TEXT));
chars.add(new TerminalCharacter(' ', Color.DEFAULT_BG, Color.DEFAULT_TEXT));

return new Prompt(chars);
}
Expand Down
4 changes: 4 additions & 0 deletions aesh/src/main/java/org/jboss/forge/aesh/ShellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public ForgeShell getAeshell() {
return aeshell;
}

public UICommand getCommand() {
return command;
}

public void generateParser(UICommand command) {
parser = CommandLineUtil.generateParser(command, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package org.jboss.forge.aesh.util;

import junit.framework.TestCase;

import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.CommandLineParser;
import org.jboss.aesh.cl.OptionBuilder;
Expand All @@ -22,19 +20,22 @@
import org.jboss.forge.ui.base.UICommandMetadataBase;
import org.jboss.forge.ui.impl.UIInputImpl;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.*;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
*/
@Ignore
public class CommandLineUtilTest extends TestCase
public class CommandLineUtilTest
{

public CommandLineUtilTest(String name)
{
super(name);
}

@Test
public void testGenerateParser() throws Exception
{
ShellContext context = new ShellContext(null);
Expand All @@ -55,6 +56,7 @@ public void testGenerateParser() throws Exception

}

@Test
public void testPopulateUIInputs()
{
UIInput<String> input1 = new UIInputImpl<String>("str", String.class);
Expand Down Expand Up @@ -92,6 +94,7 @@ public void testPopulateUIInputs()
assertNull(input2.getValue());
}

@Test
public void testGenerateAndPopulate() throws Exception
{
ShellContext context = new ShellContext(null);
Expand Down

0 comments on commit 962f1b1

Please sign in to comment.