From bc32493847eb5670ed9fdf472abc3a70b1901999 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 24 May 2018 09:12:10 -0700 Subject: Expose [AndroidAssetsInfo].local_assets, [AndroidAssetsInfo].local_asset_dir and [AndroidResourcesInfo].r_txt to Skylark. RELNOTES: none PiperOrigin-RevId: 197902129 --- .../build/lib/rules/android/AndroidAssets.java | 21 +++++++++--- .../build/lib/rules/android/AndroidAssetsInfo.java | 37 ++++++++++++++++++++++ .../lib/rules/android/AndroidResourcesInfo.java | 5 +++ 3 files changed, 58 insertions(+), 5 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssets.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssets.java index cd411b9d8e..423c380bf2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssets.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssets.java @@ -102,7 +102,7 @@ public class AndroidAssets { } } - return new AndroidAssets(assets.build(), assetRoots.build()); + return new AndroidAssets(assets.build(), assetRoots.build(), assetsDir.getPathString()); } @Nullable @@ -135,24 +135,31 @@ public class AndroidAssets { static AndroidAssets forAarImport(SpecialArtifact assetsDir) { Preconditions.checkArgument(assetsDir.isTreeArtifact()); return new AndroidAssets( - ImmutableList.of(assetsDir), ImmutableList.of(assetsDir.getExecPath().getChild("assets"))); + ImmutableList.of(assetsDir), + ImmutableList.of(assetsDir.getExecPath().getChild("assets")), + assetsDir.getExecPathString()); } public static AndroidAssets empty() { - return new AndroidAssets(ImmutableList.of(), ImmutableList.of()); + return new AndroidAssets(ImmutableList.of(), ImmutableList.of(), /* assetDir = */ null); } private final ImmutableList assets; private final ImmutableList assetRoots; + private final @Nullable String assetDir; AndroidAssets(AndroidAssets other) { - this(other.assets, other.assetRoots); + this(other.assets, other.assetRoots, other.assetDir); } @VisibleForTesting - AndroidAssets(ImmutableList assets, ImmutableList assetRoots) { + AndroidAssets( + ImmutableList assets, + ImmutableList assetRoots, + @Nullable String assetDir) { this.assets = assets; this.assetRoots = assetRoots; + this.assetDir = assetDir; } public ImmutableList getAssets() { @@ -163,6 +170,10 @@ public class AndroidAssets { return assetRoots; } + public @Nullable String getAssetDirAsString() { + return assetDir; + } + public ParsedAndroidAssets parse(AndroidDataContext dataContext) throws InterruptedException { return ParsedAndroidAssets.parseFrom(dataContext, this); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssetsInfo.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssetsInfo.java index 80e4f6661a..ff51d94ae2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssetsInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAssetsInfo.java @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.rules.android; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; @@ -23,6 +25,7 @@ import com.google.devtools.build.lib.packages.NativeProvider; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import java.util.Optional; import javax.annotation.Nullable; /** Provides information about transitive Android assets. */ @@ -43,6 +46,12 @@ public class AndroidAssetsInfo extends NativeInfo { private final NestedSet transitiveParsedAssets; private final NestedSet transitiveAssets; private final NestedSet transitiveSymbols; + /** + * Whether the local assets have been specified. This field is needed to distinguish between the + * situation when the local assets haven't been specified and the {@link #directParsedAssets} + * contains assets form the target's dependencies. + */ + private final boolean hasLocalAssets; static AndroidAssetsInfo empty(Label label) { return new AndroidAssetsInfo( @@ -79,6 +88,7 @@ public class AndroidAssetsInfo extends NativeInfo { NestedSet transitiveSymbols) { super(PROVIDER); this.label = label; + this.hasLocalAssets = validationResult != null; this.validationResult = validationResult; this.directParsedAssets = directParsedAssets; this.transitiveParsedAssets = transitiveParsedAssets; @@ -110,6 +120,27 @@ public class AndroidAssetsInfo extends NativeInfo { return directParsedAssets; } + /** Returns the local assets for the target. */ + @SkylarkCallable( + name = "local_assets", + doc = "Returns the local assets for the target.", + allowReturnNones = true, + structField = true) + public ImmutableList getLocalAssets() { + return getLocalParsedAndroidAssets().map(AndroidAssets::getAssets).orElse(null); + } + + /** Returns the local asset dir for the target. */ + @SkylarkCallable( + name = "local_asset_dir", + doc = "Returns the local asset directory for the target.", + allowReturnNones = true, + structField = true) + public String getLocalAssetDir() { + return getLocalParsedAndroidAssets().map(AndroidAssets::getAssetDirAsString).orElse(null); + } + + public NestedSet getTransitiveParsedAssets() { return transitiveParsedAssets; } @@ -121,4 +152,10 @@ public class AndroidAssetsInfo extends NativeInfo { public NestedSet getSymbols() { return transitiveSymbols; } + + private Optional getLocalParsedAndroidAssets() { + return hasLocalAssets && getDirectParsedAssets().isSingleton() + ? Optional.of(Iterables.getOnlyElement(getDirectParsedAssets())) + : Optional.empty(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java index f6626d8a04..4ab92d5e42 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java @@ -106,6 +106,11 @@ public class AndroidResourcesInfo extends NativeInfo { return manifest; } + /** Returns the r.txt file for the target. */ + @SkylarkCallable( + name = "r_txt", + doc = "Returns the R.txt file for the target.", + structField = true) public Artifact getRTxt() { return rTxt; } -- cgit v1.2.3