Skip to content

Commit

Permalink
Add dirname variants of predefined source/output path variables.
Browse files Browse the repository at this point in the history
These are similar to existing `$(execpath)` and friends, but return the
path of the artifact's directory rather than of the artifact itself.
  • Loading branch information
jmillikin committed Sep 5, 2024
1 parent 7383d33 commit 88c441f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public final class LocationExpander {
private static final boolean EXACTLY_ONE = false;
private static final boolean ALLOW_MULTIPLE = true;

private static final boolean FULL_PATH = false;
private static final boolean DIRNAME_ONLY = true;

private final RuleErrorConsumer ruleErrorConsumer;
private final ImmutableMap<String, LocationFunction> functions;
private final RepositoryMapping repositoryMapping;
Expand Down Expand Up @@ -253,18 +256,21 @@ enum PathType {
private final PathType pathType;
private final boolean legacyExternalRunfiles;
private final boolean multiple;
private final boolean dirnameOnly;

LocationFunction(
Label root,
Supplier<Map<Label, Collection<Artifact>>> locationMapSupplier,
PathType pathType,
boolean legacyExternalRunfiles,
boolean multiple) {
boolean multiple,
boolean dirnameOnly) {
this.root = root;
this.locationMapSupplier = locationMapSupplier;
this.pathType = Preconditions.checkNotNull(pathType);
this.legacyExternalRunfiles = legacyExternalRunfiles;
this.multiple = multiple;
this.dirnameOnly = dirnameOnly;
}

/**
Expand Down Expand Up @@ -348,7 +354,7 @@ private Set<String> getPaths(
}

private PathFragment getPath(Artifact artifact, String workspaceRunfilesDirectory) {
return switch (pathType) {
PathFragment path = switch (pathType) {
case LOCATION ->
legacyExternalRunfiles
? artifact.getPathForLocationExpansion()
Expand All @@ -363,6 +369,10 @@ private PathFragment getPath(Artifact artifact, String workspaceRunfilesDirector
}
}
};
if (dirnameOnly) {
return path.getParentDirectory();
}
return path;
}

private String joinPaths(Collection<String> paths) {
Expand All @@ -387,39 +397,49 @@ static ImmutableMap<String, LocationFunction> allLocationFunctions(
locationMap,
execPaths ? PathType.EXEC : PathType.LOCATION,
legacyExternalRunfiles,
EXACTLY_ONE))
EXACTLY_ONE,
FULL_PATH))
.put(
"locations",
new LocationFunction(
root,
locationMap,
execPaths ? PathType.EXEC : PathType.LOCATION,
legacyExternalRunfiles,
ALLOW_MULTIPLE))
ALLOW_MULTIPLE,
FULL_PATH))
.put(
"rootpath",
new LocationFunction(
root, locationMap, PathType.LOCATION, legacyExternalRunfiles, EXACTLY_ONE))
root, locationMap, PathType.LOCATION, legacyExternalRunfiles, EXACTLY_ONE, FULL_PATH))
.put(
"rootpaths",
new LocationFunction(
root, locationMap, PathType.LOCATION, legacyExternalRunfiles, ALLOW_MULTIPLE))
root, locationMap, PathType.LOCATION, legacyExternalRunfiles, ALLOW_MULTIPLE, FULL_PATH))
.put(
"rootpath_dirname",
new LocationFunction(
root, locationMap, PathType.LOCATION, legacyExternalRunfiles, EXACTLY_ONE, DIRNAME_ONLY))
.put(
"execpath",
new LocationFunction(
root, locationMap, PathType.EXEC, legacyExternalRunfiles, EXACTLY_ONE))
root, locationMap, PathType.EXEC, legacyExternalRunfiles, EXACTLY_ONE, FULL_PATH))
.put(
"execpaths",
new LocationFunction(
root, locationMap, PathType.EXEC, legacyExternalRunfiles, ALLOW_MULTIPLE))
root, locationMap, PathType.EXEC, legacyExternalRunfiles, ALLOW_MULTIPLE, FULL_PATH))
.put(
"execpath_dirname",
new LocationFunction(
root, locationMap, PathType.EXEC, legacyExternalRunfiles, EXACTLY_ONE, DIRNAME_ONLY))
.put(
"rlocationpath",
new LocationFunction(
root, locationMap, PathType.RLOCATION, legacyExternalRunfiles, EXACTLY_ONE))
root, locationMap, PathType.RLOCATION, legacyExternalRunfiles, EXACTLY_ONE, FULL_PATH))
.put(
"rlocationpaths",
new LocationFunction(
root, locationMap, PathType.RLOCATION, legacyExternalRunfiles, ALLOW_MULTIPLE))
root, locationMap, PathType.RLOCATION, legacyExternalRunfiles, ALLOW_MULTIPLE, FULL_PATH))
.buildOrThrow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ public void otherPathExpansion() throws Exception {
.matches("foo .*-out/.*/expansion/foo\\.txt bar");
assertThat(expander.expand("foo $(execpaths :foo) bar"))
.matches("foo .*-out/.*/expansion/foo\\.txt bar");
assertThat(expander.expand("foo $(execpath_dirname :foo) bar"))
.matches("foo .*-out/.*/expansion bar");
assertThat(expander.expand("foo $(rootpath :foo) bar"))
.matches("foo expansion/foo.txt bar");
assertThat(expander.expand("foo $(rootpaths :foo) bar"))
.matches("foo expansion/foo.txt bar");
assertThat(expander.expand("foo $(rootpath_dirname :foo) bar"))
.matches("foo ./expansion bar");
assertThat(expander.expand("foo $(rlocationpath :foo) bar"))
.isEqualTo("foo " + ruleClassProvider.getRunfilesPrefix() + "/expansion/foo.txt bar");
assertThat(expander.expand("foo $(rlocationpaths :foo) bar"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ public void execPath() throws Exception {
.isEqualTo("./bar out/foobar");
}

@Test
public void execPathDirname() throws Exception {
LocationFunction func =
new LocationFunctionBuilder("//foo", true)
.setPathType(LocationFunction.PathType.EXEC)
.setDirnameOnly(true)
.add("//foo", "/exec/bar", "/exec/out/subdir/foobar")
.build();
assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK, null))
.isEqualTo(". out/subdir");
}

@Test
public void rlocationPath() throws Exception {
LocationFunction func =
Expand Down Expand Up @@ -195,16 +207,18 @@ final class LocationFunctionBuilder {
private final boolean multiple;
private LocationFunction.PathType pathType = LocationFunction.PathType.LOCATION;
private boolean legacyExternalRunfiles;
private boolean dirnameOnly;
private final Map<Label, Collection<Artifact>> labelMap = new HashMap<>();

LocationFunctionBuilder(String rootLabel, boolean multiple) {
this.root = Label.parseCanonicalUnchecked(rootLabel);
this.multiple = multiple;
this.dirnameOnly = false;
}

public LocationFunction build() {
return new LocationFunction(
root, Suppliers.ofInstance(labelMap), pathType, legacyExternalRunfiles, multiple);
root, Suppliers.ofInstance(labelMap), pathType, legacyExternalRunfiles, multiple, dirnameOnly);
}

@CanIgnoreReturnValue
Expand All @@ -219,6 +233,12 @@ public LocationFunctionBuilder setLegacyExternalRunfiles(boolean legacyExternalR
return this;
}

@CanIgnoreReturnValue
public LocationFunctionBuilder setDirnameOnly(boolean dirnameOnly) {
this.dirnameOnly = dirnameOnly;
return this;
}

@CanIgnoreReturnValue
public LocationFunctionBuilder add(String label, String... paths) {
labelMap.put(
Expand Down

0 comments on commit 88c441f

Please sign in to comment.