Skip to content

Commit

Permalink
Add a method for getting the root of a rule workspace to the Label me…
Browse files Browse the repository at this point in the history
…thod

This method is exposed to Skylark and will enable correct handling of protobuf skylark files.
See #784

--
MOS_MIGRATED_REVID=112235357
  • Loading branch information
damienmg committed Jan 15, 2016
1 parent 5bf56a2 commit 9dc1f5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/google/devtools/build/lib/cmdline/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ public PackageIdentifier getPackageIdentifier() {
public String getPackageName() {
return packageIdentifier.getPackageFragment().getPathString();
}

/**
* Returns the execution root for the workspace, relative to the execroot (e.g., for label
* {@code @repo//pkg:b}, it will returns {@code external/repo/pkg} and for label {@code //pkg:a},
* it will returns an empty string.
*/
@SkylarkCallable(name = "workspace_root", structField = true,
doc = "Returns the execution root for the workspace of this label, relative to the execroot. "
+ "For instance:<br>"
+ "<pre class=language-python>Label(\"@repo//pkg/foo:abc\").workspace_root =="
+ " \"external/repo\"</pre>")
public String getWorkspaceRoot() {
return packageIdentifier.getRepository().getPathFragment().toString();
}

/**
* Returns the path fragment of the package in which this rule was declared (e.g. {@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,12 @@ public void testInvalidRepo() throws Exception {
"invalid repository name 'foo': workspace names must start with '@'");
}
}

@Test
public void testGetWorkspaceRoot() throws Exception {
Label label = Label.parseAbsolute("//bar/baz");
assertThat(label.getWorkspaceRoot()).isEmpty();
label = Label.parseAbsolute("@repo//bar/baz");
assertThat(label.getWorkspaceRoot()).isEqualTo("external/repo");
}
}

0 comments on commit 9dc1f5a

Please sign in to comment.