From 6563de696ff5b0be9113fba89e55ef1a9f5548f8 Mon Sep 17 00:00:00 2001 From: John Field Date: Wed, 30 Dec 2015 18:08:26 +0000 Subject: Add a new method to the SkylarkImport class to allow tools to access the import as a PathFragment. -- MOS_MIGRATED_REVID=111138001 --- .../devtools/build/lib/syntax/SkylarkImport.java | 14 +++++++++++-- .../devtools/build/lib/syntax/SkylarkImports.java | 23 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/syntax') 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 @@ -38,6 +38,9 @@ public class SkylarkImports { return importString; } + @Override + public abstract PathFragment asPathFragment(); + @Override public abstract Label getLabel(Label containingFileLabel); @@ -60,6 +63,11 @@ public class SkylarkImports { this.importPath = importPath; } + @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"); @@ -84,6 +92,11 @@ public class SkylarkImports { this.importFile = importFile; } + @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 @@ -110,6 +123,11 @@ public class SkylarkImports { this.importLabel = importLabel; } + @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 @@ -126,6 +144,11 @@ public class SkylarkImports { this.importTarget = importTarget; } + @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, -- cgit v1.2.3