Skip to content

Commit

Permalink
Define remote repositories with maybe (bazelbuild#1071)
Browse files Browse the repository at this point in the history
This allows individual repositories to be overridden by binding
replacement repositories as long as the same name is used.

The names we give to repositories are not currently part of the API
contract of crate_universe, but this can still be useful.
  • Loading branch information
illicitonion authored and ddeville committed Dec 20, 2021
1 parent 0392487 commit 1e246ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
15 changes: 11 additions & 4 deletions crate_universe/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,11 @@ mod tests {

let expected_repository_rule = indoc! { r#"
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
def pinned_rust_install():
http_archive(
maybe(
http_archive,
name = "rule_prefix__lazy_static__1_4_0",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
sha256 = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646",
Expand Down Expand Up @@ -608,9 +610,11 @@ mod tests {

let expected_repository_rule = indoc! { r#"
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
def pinned_rust_install():
new_git_repository(
maybe(
new_git_repository,
name = "rule_prefix__lazy_static__1_4_0",
strip_prefix = "",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
Expand Down Expand Up @@ -650,9 +654,11 @@ mod tests {
let expected_repository_rule = indoc! { r#"
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
def pinned_rust_install():
new_git_repository(
maybe(
new_git_repository,
name = "rule_prefix__lazy_static__1_4_0",
strip_prefix = "",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
Expand All @@ -661,7 +667,8 @@ mod tests {
commit = "421669662b35fcb455f2902daed2e20bbbba79b6",
)
http_archive(
maybe(
http_archive,
name = "rule_prefix__maplit__1_0_2",
build_file = Label("//:BUILD.maplit-1.0.2.bazel"),
sha256 = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d",
Expand Down
4 changes: 3 additions & 1 deletion crate_universe/src/templates/defs.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{% break %}
{%- endif %}
{%- endfor %}
{%- if crates %}
{%- if crates -%}
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

{% endif -%}
def pinned_rust_install():
{%- if not crates %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
new_git_repository(
maybe(
new_git_repository,
name = "{{ repository_name }}",
strip_prefix = "{{ repo.path_to_crate_root }}",
build_file = Label("//:BUILD.{{crate.pkg_name}}-{{crate.pkg_version}}.bazel"),
Expand Down
3 changes: 2 additions & 1 deletion crate_universe/src/templates/partials/http_archive.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http_archive(
maybe(
http_archive,
name = "{{repository_name}}",
build_file = Label("//:BUILD.{{crate.pkg_name}}-{{crate.pkg_version}}.bazel"),
{%- if crate.sha256 %}
Expand Down

0 comments on commit 1e246ff

Please sign in to comment.