aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2015-09-18 08:12:30 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2015-09-21 08:55:24 +0000
commit47cb916ec6f41b8ecbd377ed875f842c3d349b12 (patch)
treeed304d89738774b22792196626473bccd15da277 /src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
parent3815b4cb1e8af84ddfef411a621f47ba6dcaa161 (diff)
Move getOutputFilesystem to CommandEnvironment; refactor BlazeRuntime commands.
This change makes it so commands are no longer both stored in the BlazeRuntime and in the BlazeCommandDispatcher. Instead, they are only stored in BlazeRuntime and usually passed there during construction. We have some tests where this is tricky, so I'm keeping the old code path for now. -- MOS_MIGRATED_REVID=103364581
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java54
1 files changed, 10 insertions, 44 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
index 5629c405cc..9f701b1999 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
@@ -44,11 +44,9 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedHashMap;
+import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
@@ -92,50 +90,25 @@ public class BlazeCommandDispatcher {
}
private final BlazeRuntime runtime;
- private final Map<String, BlazeCommand> commandsByName = new LinkedHashMap<>();
private OutputStream logOutputStream = null;
/**
- * Create a Blaze dispatcher that uses the specified {@code BlazeRuntime}
- * instance, and no default options, and delegates to {@code commands} as
- * appropriate.
+ * Create a Blaze dispatcher that uses the specified {@code BlazeRuntime} instance, but overrides
+ * the command map with the given commands (plus any commands from modules).
*/
@VisibleForTesting
public BlazeCommandDispatcher(BlazeRuntime runtime, BlazeCommand... commands) {
- this(runtime, ImmutableList.copyOf(commands));
+ this(runtime);
+ runtime.overrideCommands(Arrays.asList(commands));
}
/**
- * Create a Blaze dispatcher that uses the specified {@code BlazeRuntime}
- * instance, and delegates to {@code commands} as appropriate.
+ * Create a Blaze dispatcher that uses the specified {@code BlazeRuntime} instance.
*/
- public BlazeCommandDispatcher(BlazeRuntime runtime, Iterable<BlazeCommand> commands) {
+ @VisibleForTesting
+ public BlazeCommandDispatcher(BlazeRuntime runtime) {
this.runtime = runtime;
- for (BlazeCommand command : commands) {
- addCommandByName(command);
- }
-
- for (BlazeModule module : runtime.getBlazeModules()) {
- for (BlazeCommand command : module.getCommands()) {
- addCommandByName(command);
- }
- }
-
- runtime.setCommandMap(commandsByName);
- }
-
- /**
- * Adds the given command under the given name to the map of commands.
- *
- * @throws AssertionError if the name is already used by another command.
- */
- private void addCommandByName(BlazeCommand command) {
- String name = command.getClass().getAnnotation(Command.class).name();
- if (commandsByName.containsKey(name)) {
- throw new IllegalStateException("Command name or alias " + name + " is already used.");
- }
- commandsByName.put(name, command);
}
/**
@@ -199,7 +172,7 @@ public class BlazeCommandDispatcher {
CommonCommandOptions rcFileOptions = optionsParser.getOptions(CommonCommandOptions.class);
List<Pair<String, ListMultimap<String, String>>> optionsMap =
getOptionsMap(outErr, rcFileOptions.rcSource, rcFileOptions.optionsOverrides,
- commandsByName.keySet());
+ runtime.getCommandMap().keySet());
parseOptionsForCommand(rcfileNotes, commandAnnotation, optionsParser, optionsMap, null);
@@ -250,7 +223,7 @@ public class BlazeCommandDispatcher {
commandName = "help";
}
- BlazeCommand command = commandsByName.get(commandName);
+ BlazeCommand command = runtime.getCommandMap().get(commandName);
if (command == null) {
outErr.printErrLn(String.format(
"Command '%s' not found. Try '%s help'.", commandName, Constants.PRODUCT_NAME));
@@ -657,13 +630,6 @@ public class BlazeCommandDispatcher {
}
/**
- * The map from command names to commands that this dispatcher dispatches to.
- */
- Map<String, BlazeCommand> getCommandsByName() {
- return Collections.unmodifiableMap(commandsByName);
- }
-
- /**
* Shuts down all the registered commands to give them a chance to cleanup or
* close resources. Should be called by the owner of this command dispatcher
* in all termination cases.