Skip to content

Commit

Permalink
Bzlmod: Add RepoSpec.java
Browse files Browse the repository at this point in the history
RepoSpec holds information of a repository rule definition, including where the rule class is defined and the attributes of a repository rule. It's the output of Bzlmod resolution and will be used to create a repository rule instance.

Related: #13316

RELNOTES: None
PiperOrigin-RevId: 380784644
  • Loading branch information
meteorcloudy authored and copybara-github committed Jun 22, 2021
1 parent 97fb2cf commit 706e54c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ java_library(
"//third_party:jsr305",
],
)

java_library(
name = "repo_spec_values",
srcs = ["RepoSpec.java"],
deps = [
"//third_party:auto_value",
"//third_party:guava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2021 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.lib.bazel.bzlmod;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import java.util.Optional;

/**
* A class holding information about the attributes of a repository rule and where the rule class is
* defined.
*/
@AutoValue
public abstract class RepoSpec {

/**
* The label string for the bzl file this repository rule is defined in, empty for native rule.
*/
public abstract Optional<String> bzlFile();

public abstract String ruleClassName();

public abstract ImmutableMap<String, Object> attributes();

public static Builder builder() {
return new AutoValue_RepoSpec.Builder();
}

/** The builder for {@link RepoSpec} */
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setBzlFile(Optional<String> bzlFile);

public abstract Builder setRuleClassName(String name);

public abstract Builder setAttributes(ImmutableMap<String, Object> attributes);

public abstract RepoSpec build();
}

public boolean isNativeRepoRule() {
return !bzlFile().isPresent();
}

/**
* Return a string representing the rule class eg. Native repo rule: local_repository, Starlark
* repo rule: //:repo.bzl%my_repo
*/
public String getRuleClass() {
return bzlFile().map(f -> f + "%").orElse("") + ruleClassName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:server_directories",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:common",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:registry",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:repo_spec_values",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/pkgcache",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2021 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.lib.bazel.bzlmod;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;

import com.google.common.collect.ImmutableMap;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for RepoSpec.java */
@RunWith(JUnit4.class)
public class RepoSpecTest {

@Test
public void nativeRepoSpecTest() {
RepoSpec repoSpec =
RepoSpec.builder()
.setRuleClassName("local_repository")
.setAttributes(ImmutableMap.of("path", "/foo/bar"))
.build();
assertThat(repoSpec.isNativeRepoRule()).isTrue();
assertThat(repoSpec.ruleClassName()).isEqualTo("local_repository");
assertThat(repoSpec.getRuleClass()).isEqualTo("local_repository");
assertThat(repoSpec.attributes()).containsExactly("path", "/foo/bar");
}

@Test
public void starlarkRepoSpecTest() {
RepoSpec repoSpec =
RepoSpec.builder()
.setBzlFile(Optional.of("//pkg:repo.bzl"))
.setRuleClassName("my_repo")
.setAttributes(ImmutableMap.of("attr1", "foo", "attr2", "bar"))
.build();
assertThat(repoSpec.isNativeRepoRule()).isFalse();
assertThat(repoSpec.bzlFile()).hasValue("//pkg:repo.bzl");
assertThat(repoSpec.ruleClassName()).isEqualTo("my_repo");
assertThat(repoSpec.getRuleClass()).isEqualTo("//pkg:repo.bzl%my_repo");
assertThat(repoSpec.attributes()).containsExactly("attr1", "foo", "attr2", "bar");
}
}

0 comments on commit 706e54c

Please sign in to comment.