Skip to content

Commit

Permalink
Add CommandLinePathFactory to CommandEnvironment
Browse files Browse the repository at this point in the history
Progress on #15856

Closes #15905.

PiperOrigin-RevId: 462369707
Change-Id: If8dd493227b67dd4ffe30c9e0af4d8f71de4fb9f
  • Loading branch information
Yannic authored and Copybara-Service committed Jul 21, 2022
1 parent 40b95c3 commit e603222
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class CommandEnvironment {
private final ImmutableList.Builder<Any> responseExtensions = ImmutableList.builder();
private final Consumer<String> shutdownReasonConsumer;
private final BuildResultListener buildResultListener;
private final CommandLinePathFactory commandLinePathFactory;

private OutputService outputService;
private String workspaceName;
Expand Down Expand Up @@ -273,6 +274,9 @@ public void exit(AbruptExitException exception) {
}
this.buildResultListener = new BuildResultListener();
this.eventBus.register(this.buildResultListener);

this.commandLinePathFactory =
CommandLinePathFactory.create(runtime.getFileSystem(), directories);
}

private Path computeWorkingDirectory(CommonCommandOptions commandOptions)
Expand Down Expand Up @@ -840,4 +844,8 @@ public void addResponseExtensions(Iterable<Any> extensions) {
public BuildResultListener getBuildResultListener() {
return buildResultListener;
}

public CommandLinePathFactory getCommandLinePathFactory() {
return commandLinePathFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
// limitations under the License.
package com.google.devtools.build.lib.runtime;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -44,11 +46,27 @@ public final class CommandLinePathFactory {
private final FileSystem fileSystem;
private final ImmutableMap<String, Path> roots;

@VisibleForTesting
public CommandLinePathFactory(FileSystem fileSystem, ImmutableMap<String, Path> roots) {
this.fileSystem = Preconditions.checkNotNull(fileSystem);
this.roots = Preconditions.checkNotNull(roots);
}

static CommandLinePathFactory create(FileSystem fileSystem, BlazeDirectories directories) {
Preconditions.checkNotNull(fileSystem);
Preconditions.checkNotNull(directories);

ImmutableMap.Builder<String, Path> wellKnownRoots = ImmutableMap.builder();

// This is necessary because some tests don't have a workspace set.
Path workspace = directories.getWorkspace();
if (workspace != null) {
wellKnownRoots.put("workspace", workspace);
}

return new CommandLinePathFactory(fileSystem, wellKnownRoots.buildOrThrow());
}

/** Creates a {@link Path}. */
public Path create(Map<String, String> env, String value) throws IOException {
Preconditions.checkNotNull(env);
Expand Down

0 comments on commit e603222

Please sign in to comment.