aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.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/BlazeRuntime.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/BlazeRuntime.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java66
1 files changed, 48 insertions, 18 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
index f4886aace7..7d2cfae097 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
@@ -26,6 +26,7 @@ import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import com.google.common.collect.Sets;
@@ -127,6 +128,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -188,8 +190,7 @@ public final class BlazeRuntime {
// We pass this through here to make it available to the MasterLogWriter.
private final OptionsProvider startupOptionsProvider;
- private String outputFileSystem;
- private Map<String, BlazeCommand> commandMap;
+ private final Map<String, BlazeCommand> commandMap = new LinkedHashMap<>();
private final SubscriberExceptionHandler eventBusExceptionHandler;
@@ -207,7 +208,8 @@ public final class BlazeRuntime {
OptionsProvider startupOptionsProvider, Iterable<BlazeModule> blazeModules,
TimestampGranularityMonitor timestampGranularityMonitor,
SubscriberExceptionHandler eventBusExceptionHandler,
- BinTools binTools, ProjectFile.Provider projectFileProvider) {
+ BinTools binTools, ProjectFile.Provider projectFileProvider,
+ Iterable<BlazeCommand> commands) {
this.workspaceStatusActionFactory = workspaceStatusActionFactory;
this.directories = directories;
this.workingDirectory = directories.getWorkspace();
@@ -232,6 +234,7 @@ public final class BlazeRuntime {
this.startupOptionsProvider = startupOptionsProvider;
this.eventBusExceptionHandler = eventBusExceptionHandler;
+ overrideCommands(commands);
if (inWorkspace()) {
writeOutputBaseReadmeFile();
@@ -255,6 +258,31 @@ public final class BlazeRuntime {
}
/**
+ * 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 addCommand(BlazeCommand command) {
+ String name = command.getClass().getAnnotation(Command.class).name();
+ if (commandMap.containsKey(name)) {
+ throw new IllegalStateException("Command name or alias " + name + " is already used.");
+ }
+ commandMap.put(name, command);
+ }
+
+ final void overrideCommands(Iterable<BlazeCommand> commands) {
+ commandMap.clear();
+ for (BlazeCommand command : commands) {
+ addCommand(command);
+ }
+ for (BlazeModule module : blazeModules) {
+ for (BlazeCommand command : module.getCommands()) {
+ addCommand(command);
+ }
+ }
+ }
+
+ /**
* Figures out what file system we are writing output to. Here we use
* outputBase instead of outputPath because we need a file system to create the latter.
*/
@@ -267,10 +295,6 @@ public final class BlazeRuntime {
}
}
- public String getOutputFileSystem() {
- return outputFileSystem;
- }
-
public CommandEnvironment initCommand() {
EventBus eventBus = new EventBus(eventBusExceptionHandler);
skyframeExecutor.setEventBus(eventBus);
@@ -663,7 +687,7 @@ public final class BlazeRuntime {
? null
: outputService.getBatchStatter());
- outputFileSystem = determineOutputFileSystem();
+ env.setOutputFileSystem(determineOutputFileSystem());
// Ensure that the working directory will be under the workspace directory.
Path workspace = getWorkspace();
@@ -843,10 +867,6 @@ public final class BlazeRuntime {
}
}
- void setCommandMap(Map<String, BlazeCommand> commandMap) {
- this.commandMap = ImmutableMap.copyOf(commandMap);
- }
-
public Map<String, BlazeCommand> getCommandMap() {
return commandMap;
}
@@ -1117,8 +1137,7 @@ public final class BlazeRuntime {
return e.getExitCode().getNumericExitCode();
}
- BlazeCommandDispatcher dispatcher =
- new BlazeCommandDispatcher(runtime, getBuiltinCommandList());
+ BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime);
try {
LOG.info(getRequestLogString(commandLineOptions.getOtherArgs()));
@@ -1166,8 +1185,7 @@ public final class BlazeRuntime {
BlazeServerStartupOptions startupOptions = options.getOptions(BlazeServerStartupOptions.class);
final BlazeRuntime runtime = newRuntime(modules, options);
- final BlazeCommandDispatcher dispatcher =
- new BlazeCommandDispatcher(runtime, getBuiltinCommandList());
+ final BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime);
final ServerCommand blazeCommand;
@@ -1344,6 +1362,7 @@ public final class BlazeRuntime {
for (BlazeModule blazeModule : blazeModules) {
runtimeBuilder.addBlazeModule(blazeModule);
}
+ runtimeBuilder.addCommands(getBuiltinCommandList());
BlazeRuntime runtime = runtimeBuilder.build();
AutoProfiler.setClock(runtime.getClock());
@@ -1440,11 +1459,12 @@ public final class BlazeRuntime {
private ConfigurationFactory configurationFactory;
private Clock clock;
private OptionsProvider startupOptionsProvider;
- private final List<BlazeModule> blazeModules = Lists.newArrayList();
+ private final List<BlazeModule> blazeModules = new ArrayList<>();
private SubscriberExceptionHandler eventBusExceptionHandler =
new RemoteExceptionHandler();
private BinTools binTools;
private UUID instanceId;
+ private final List<BlazeCommand> commands = new ArrayList<>();
public BlazeRuntime build() throws AbruptExitException {
Preconditions.checkNotNull(directories);
@@ -1607,7 +1627,7 @@ public final class BlazeRuntime {
return new BlazeRuntime(directories, reporter, workspaceStatusActionFactory, skyframeExecutor,
pkgFactory, ruleClassProvider, configurationFactory,
clock, startupOptionsProvider, ImmutableList.copyOf(blazeModules),
- timestampMonitor, eventBusExceptionHandler, binTools, projectFileProvider);
+ timestampMonitor, eventBusExceptionHandler, binTools, projectFileProvider, commands);
}
public Builder setBinTools(BinTools binTools) {
@@ -1666,5 +1686,15 @@ public final class BlazeRuntime {
this.eventBusExceptionHandler = eventBusExceptionHandler;
return this;
}
+
+ public Builder addCommands(BlazeCommand... commands) {
+ this.commands.addAll(Arrays.asList(commands));
+ return this;
+ }
+
+ public Builder addCommands(Iterable<BlazeCommand> commands) {
+ Iterables.addAll(this.commands, commands);
+ return this;
+ }
}
}