diff options
author | John Field <jfield@google.com> | 2015-12-30 18:08:26 +0000 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2016-01-04 12:58:35 +0000 |
commit | 6563de696ff5b0be9113fba89e55ef1a9f5548f8 (patch) | |
tree | 11d5c9e839fc4a827c599eb4cbc271ea91b80041 /src/main/java/com/google/devtools/build/lib/syntax | |
parent | 7b8dbebbd2b7ce3f437f6b74511e6878065f9dc2 (diff) |
Add a new method to the SkylarkImport class to allow tools to access the import as a PathFragment.
--
MOS_MIGRATED_REVID=111138001
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java | 14 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java | 23 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java index 5aa744a2ba..95ddf96ffb 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java @@ -28,11 +28,21 @@ public interface SkylarkImport extends Serializable { * Returns the string originally used to specify the import (may represent a label or a path). */ String getImportString(); - + + /** + * Returns the import in the form of a path fragment for use by tools. Label-based imports are + * converted to paths as follows: Imports using absolute labels or paths yield an absolute path + * (whose root corresponds to the containing depot). Imports using relative labels yield a + * package-relate path, and imports using relative paths yield a directory (of the importing-file) + * relative path. All paths reference file names ending in '.bzl'. If there is an external + * repository prefix, it is ignored. + */ + PathFragment asPathFragment(); + /** * Given a {@link Label} representing the file that contains this import, returns a {@link Label} * representing the .bzl file to be imported. - * + * * @throws IllegalStateException if this import takes the form of an absolute path. */ Label getLabel(Label containingFileLabel); diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java index 6c82a56b65..2edd38fc3b 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java @@ -39,6 +39,9 @@ public class SkylarkImports { } @Override + public abstract PathFragment asPathFragment(); + + @Override public abstract Label getLabel(Label containingFileLabel); @Override @@ -61,6 +64,11 @@ public class SkylarkImports { } @Override + public PathFragment asPathFragment() { + return importPath; + } + + @Override public Label getLabel(Label containingFileLabel) { throw new IllegalStateException("can't request a label from an absolute path import"); } @@ -85,6 +93,11 @@ public class SkylarkImports { } @Override + public PathFragment asPathFragment() { + return new PathFragment(importFile); + } + + @Override public Label getLabel(Label containingFileLabel) { // The twistiness of the code below is due to the fact that the containing file may be in // a subdirectory of the package that contains it. We need to construct a Label with @@ -111,6 +124,11 @@ public class SkylarkImports { } @Override + public PathFragment asPathFragment() { + return new PathFragment(PathFragment.ROOT_DIR).getRelative(importLabel.toPathFragment()); + } + + @Override public Label getLabel(Label containingFileLabel) { // When the import label contains no explicit repository identifier, we resolve it relative // to the repo of the containing file. @@ -127,6 +145,11 @@ public class SkylarkImports { } @Override + public PathFragment asPathFragment() { + return new PathFragment(importTarget); + } + + @Override public Label getLabel(Label containingFileLabel) { // Unlike a relative path import, the import target is relative to the containing package, // not the containing directory within the package. |