Skip to content

Commit

Permalink
Add module_ctx.extension_metadata (bazelbuild#18174)
Browse files Browse the repository at this point in the history
Fixes bazelbuild#17908

Closes bazelbuild#17970.

PiperOrigin-RevId: 525134299
Change-Id: I9088e3f4561c0c27135cfdd1e5be8390ea8da7eb

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
  • Loading branch information
Wyverald and fmeum authored Apr 21, 2023
1 parent 755cf95 commit 6c61110
Show file tree
Hide file tree
Showing 14 changed files with 1,085 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ java_library(
"Discovery.java",
"GsonTypeAdapterUtil.java",
"ModuleExtensionContext.java",
"ModuleExtensionMetadata.java",
"ModuleFileFunction.java",
"ModuleFileGlobals.java",
"Selection.java",
Expand All @@ -168,7 +169,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function",
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
Expand All @@ -29,8 +30,10 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
Expand Down Expand Up @@ -92,6 +95,14 @@ private DelegateTypeAdapterFactory(
raw -> new ArrayList<>((List<?>) raw),
delegate -> ImmutableList.copyOf((List<?>) delegate));

public static final TypeAdapterFactory IMMUTABLE_SET =
new DelegateTypeAdapterFactory<>(
ImmutableSet.class,
Set.class,
LinkedHashSet.class,
raw -> new LinkedHashSet<>((Set<?>) raw),
delegate -> ImmutableSet.copyOf((Set<?>) delegate));

@SuppressWarnings("unchecked")
@Override
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_BIMAP;
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_LIST;
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_MAP;
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_SET;

import com.google.common.base.Splitter;
import com.google.devtools.build.lib.bazel.bzlmod.Version.ParseException;
Expand Down Expand Up @@ -95,6 +96,7 @@ public ModuleKey read(JsonReader jsonReader) throws IOException {
.registerTypeAdapterFactory(IMMUTABLE_MAP)
.registerTypeAdapterFactory(IMMUTABLE_LIST)
.registerTypeAdapterFactory(IMMUTABLE_BIMAP)
.registerTypeAdapterFactory(IMMUTABLE_SET)
.registerTypeAdapter(Version.class, VERSION_TYPE_ADAPTER)
.registerTypeAdapter(ModuleKey.class, MODULE_KEY_TYPE_ADAPTER)
.registerTypeAdapter(AttributeValues.class, new AttributeValuesAdapter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ public Builder addExtensionUsage(ModuleExtensionUsage value) {
return this;
}

abstract ModuleKey getKey();

abstract String getName();

abstract Optional<String> getRepoName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkSemantics;

Expand Down Expand Up @@ -117,4 +119,68 @@ public StarlarkList<StarlarkBazelModule> getModules() {
public boolean isDevDependency(TypeCheckedTag tag) {
return tag.isDevDependency();
}

@StarlarkMethod(
name = "extension_metadata",
doc =
"Constructs an opaque object that can be returned from the module extension's"
+ " implementation function to provide metadata about the repositories generated by"
+ " the extension to Bazel.",
parameters = {
@Param(
name = "root_module_direct_deps",
doc =
"The names of the repositories that the extension considers to be direct"
+ " dependencies of the root module. If the root module imports additional"
+ " repositories or does not import all of these repositories via <a"
+ " href=\"../globals/module.html#use_repo\"><code>use_repo</code></a>, Bazel"
+ " will print a warning and a fixup command when the extension is"
+ " evaluated.<p>If one of <code>root_module_direct_deps</code> and"
+ " <code>root_module_direct_dev_deps</code> is specified, the other has to be"
+ " as well. The lists specified by these two parameters must be"
+ " disjoint.<p>Exactly one of <code>root_module_direct_deps</code> and"
+ " <code>root_module_direct_dev_deps</code> can be set to the special value"
+ " <code>\"all\"</code>, which is treated as if a list with the names of"
+ " allrepositories generated by the extension was specified as the value.",
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = String.class),
@ParamType(type = String.class),
@ParamType(type = NoneType.class)
}),
@Param(
name = "root_module_direct_dev_deps",
doc =
"The names of the repositories that the extension considers to be direct dev"
+ " dependencies of the root module. If the root module imports additional"
+ " repositories or does not import all of these repositories via <a"
+ " href=\"../globals/module.html#use_repo\"><code>use_repo</code></a> on an"
+ " extension proxy created with <code><a"
+ " href=\"../globals/module.html#use_extension>use_extension</a>(...,"
+ " dev_dependency = True)</code>, Bazel will print a warning and a fixup"
+ " command when the extension is evaluated.<p>If one of"
+ " <code>root_module_direct_deps</code> and"
+ " <code>root_module_direct_dev_deps</code> is specified, the other has to be"
+ " as well. The lists specified by these two parameters must be"
+ " disjoint.<p>Exactly one of <code>root_module_direct_deps</code> and"
+ " <code>root_module_direct_dev_deps</code> can be set to the special value"
+ " <code>\"all\"</code>, which is treated as if a list with the names of"
+ " allrepositories generated by the extension was specified as the value.",
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = String.class),
@ParamType(type = String.class),
@ParamType(type = NoneType.class)
}),
})
public ModuleExtensionMetadata extensionMetadata(
Object rootModuleDirectDepsUnchecked, Object rootModuleDirectDevDepsUnchecked)
throws EvalException {
return ModuleExtensionMetadata.create(
rootModuleDirectDepsUnchecked, rootModuleDirectDevDepsUnchecked);
}
}
Loading

0 comments on commit 6c61110

Please sign in to comment.