| // Copyright 2018 The Bazel Authors. All rights reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package com.google.devtools.build.skydoc.fakebuildapi.repository; |
| |
| import com.google.common.collect.ImmutableList; |
| import com.google.common.collect.ImmutableMap; |
| import com.google.devtools.build.lib.events.Location; |
| import com.google.devtools.build.lib.skylarkbuildapi.repository.RepositoryModuleApi; |
| import com.google.devtools.build.lib.syntax.BaseFunction; |
| import com.google.devtools.build.lib.syntax.Dict; |
| import com.google.devtools.build.lib.syntax.EvalException; |
| import com.google.devtools.build.lib.syntax.FunctionSignature; |
| import com.google.devtools.build.lib.syntax.Sequence; |
| import com.google.devtools.build.lib.syntax.Starlark; |
| import com.google.devtools.build.lib.syntax.StarlarkFunction; |
| import com.google.devtools.build.lib.syntax.StarlarkThread; |
| import com.google.devtools.build.skydoc.fakebuildapi.FakeDescriptor; |
| import com.google.devtools.build.skydoc.fakebuildapi.FakeSkylarkRuleFunctionsApi.AttributeNameComparator; |
| import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper; |
| import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo; |
| import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType; |
| import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo; |
| import java.util.List; |
| import java.util.stream.Collectors; |
| |
| /** |
| * Fake implementation of {@link RepositoryModuleApi}. |
| */ |
| public class FakeRepositoryModule implements RepositoryModuleApi { |
| private static final FakeDescriptor IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR = |
| new FakeDescriptor( |
| AttributeType.NAME, "A unique name for this repository.", true, ImmutableList.of(), ""); |
| |
| private final List<RuleInfoWrapper> ruleInfoList; |
| |
| public FakeRepositoryModule(List<RuleInfoWrapper> ruleInfoList) { |
| this.ruleInfoList = ruleInfoList; |
| } |
| |
| @Override |
| public BaseFunction repositoryRule( |
| StarlarkFunction implementation, |
| Object attrs, |
| Boolean local, |
| Sequence<?> environ, // <String> expected |
| Boolean configure, |
| Boolean remotable, |
| String doc, |
| StarlarkThread thread) |
| throws EvalException { |
| List<AttributeInfo> attrInfos; |
| ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder(); |
| if (attrs != null && attrs != Starlark.NONE) { |
| Dict<?, ?> attrsDict = (Dict<?, ?>) attrs; |
| attrsMapBuilder.putAll(attrsDict.getContents(String.class, FakeDescriptor.class, "attrs")); |
| } |
| |
| attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR); |
| attrInfos = |
| attrsMapBuilder.build().entrySet().stream() |
| .filter(entry -> !entry.getKey().startsWith("_")) |
| .map(entry -> entry.getValue().asAttributeInfo(entry.getKey())) |
| .collect(Collectors.toList()); |
| attrInfos.sort(new AttributeNameComparator()); |
| |
| RepositoryRuleDefinitionIdentifier functionIdentifier = |
| new RepositoryRuleDefinitionIdentifier(); |
| |
| // Only the Builder is passed to RuleInfoWrapper as the rule name is not yet available. |
| RuleInfo.Builder ruleInfo = RuleInfo.newBuilder().setDocString(doc).addAllAttribute(attrInfos); |
| |
| Location loc = thread.getCallerLocation(); |
| ruleInfoList.add(new RuleInfoWrapper(functionIdentifier, loc, ruleInfo)); |
| return functionIdentifier; |
| } |
| |
| /** |
| * A fake {@link BaseFunction} implementation which serves as an identifier for a rule definition. |
| * A skylark invocation of 'rule()' should spawn a unique instance of this class and return it. |
| * Thus, skylark code such as 'foo = rule()' will result in 'foo' being assigned to a unique |
| * identifier, which can later be matched to a registered rule() invocation saved by the fake |
| * build API implementation. |
| */ |
| private static class RepositoryRuleDefinitionIdentifier extends BaseFunction { |
| |
| private static int idCounter = 0; |
| private final String name = "RepositoryRuleDefinitionIdentifier" + idCounter++; |
| |
| @Override |
| public String getName() { |
| return name; |
| } |
| |
| @Override |
| public FunctionSignature getSignature() { |
| return FunctionSignature.KWARGS; |
| } |
| } |
| |
| @Override |
| public void failWithIncompatibleUseCcConfigureFromRulesCc(StarlarkThread thread) |
| throws EvalException { |
| // Noop until --incompatible_use_cc_configure_from_rules_cc is implemented. |
| } |
| } |