Skip to content

Commit b842276

Browse files
f0rmigaalexeagle
andauthored
refactor: make modules_mapping a regular rule (#578)
* refactor: make modules_mapping a regular rule * fix: remove unnecessary comment Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com> Co-authored-by: Alex Eagle <eagle@post.harvard.edu>
1 parent 3525853 commit b842276

File tree

7 files changed

+84
-443
lines changed

7 files changed

+84
-443
lines changed

examples/build_file_generation/BUILD

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
load("@bazel_gazelle//:def.bzl", "gazelle")
2+
load("@pip//:requirements.bzl", "all_whl_requirements")
23
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
34
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
5+
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
46
load("@rules_python//python:defs.bzl", "py_library")
57

8+
# This rule fetches the metadata for python packages we depend on. That data is
9+
# required for the gazelle_python_manifest rule to update our manifest file.
10+
modules_mapping(
11+
name = "modules_map",
12+
wheels = all_whl_requirements,
13+
)
14+
615
# Gazelle python extension needs a manifest file mapping from
716
# an import to the installed package that provides it.
817
# This macro produces two targets:
@@ -12,7 +21,7 @@ load("@rules_python//python:defs.bzl", "py_library")
1221
# the manifest doesn't need to be updated
1322
gazelle_python_manifest(
1423
name = "gazelle_python_manifest",
15-
modules_mapping = "@modules_map//:modules_mapping.json",
24+
modules_mapping = ":modules_map",
1625
pip_deps_repository_name = "pip",
1726
requirements = "//:requirements_lock.txt",
1827
)

examples/build_file_generation/WORKSPACE

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,3 @@ pip_install(
6060
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
6161

6262
_py_gazelle_deps()
63-
64-
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
65-
66-
# This repository rule fetches the metadata for python packages we
67-
# depend on. That data is required for the gazelle_python_manifest
68-
# rule to update our manifest file.
69-
# To see what this rule does, try `bazel run @modules_map//:print`
70-
modules_mapping(
71-
name = "modules_map",
72-
requirements = "//:requirements_lock.txt",
73-
)

gazelle/README.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ that generates BUILD file content for Python code.
99
First, you'll need to add Gazelle to your `WORKSPACE` file.
1010
Follow the instructions at https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
1111

12-
Next, we need to add two more things to the `WORKSPACE`:
13-
14-
1. fetch the third-party Go libraries that the python extension depends on
15-
1. fetch metadata about your Python dependencies, so that gazelle can
16-
determine which package a given import statement comes from.
12+
Next, we need to fetch the third-party Go libraries that the python extension
13+
depends on.
1714

1815
Add this to your `WORKSPACE`:
1916

@@ -23,22 +20,12 @@ Add this to your `WORKSPACE`:
2320
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
2421

2522
_py_gazelle_deps()
26-
27-
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
28-
29-
# This repository rule fetches the metadata for python packages we
30-
# depend on. That data is required for the gazelle_python_manifest
31-
# rule to update our manifest file.
32-
# To see what this rule does, try `bazel run @modules_map//:print`
33-
modules_mapping(
34-
name = "modules_map",
35-
# This should point to wherever we declare our python dependencies
36-
requirements = "//:requirements_lock.txt",
37-
)
3823
```
3924

40-
Next, we'll make a pair of targets for consuming that `modules_mapping` we
41-
fetched, and writing it as a manifest file for Gazelle to read.
25+
Next, we'll fetch metadata about your Python dependencies, so that gazelle can
26+
determine which package a given import statement comes from. This is provided
27+
by the `modules_mapping` rule. We'll make a target for consuming this
28+
`modules_mapping`, and writing it as a manifest file for Gazelle to read.
4229
This is checked into the repo for speed, as it takes some time to calculate
4330
in a large monorepo.
4431

@@ -48,7 +35,16 @@ file. (You can just use `touch` at this point, it just needs to exist.)
4835
Then put this in your `BUILD.bazel` file next to the `requirements.txt`:
4936

5037
```starlark
38+
load("@pip//:requirements.bzl", "all_whl_requirements")
5139
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
40+
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
41+
42+
# This rule fetches the metadata for python packages we depend on. That data is
43+
# required for the gazelle_python_manifest rule to update our manifest file.
44+
modules_mapping(
45+
name = "modules_map",
46+
wheels = all_whl_requirements,
47+
)
5248

5349
# Gazelle python extension needs a manifest file mapping from
5450
# an import to the installed package that provides it.
@@ -59,9 +55,7 @@ load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
5955
# the manifest doesn't need to be updated
6056
gazelle_python_manifest(
6157
name = "gazelle_python_manifest",
62-
# The @modules_map refers to the name we gave in the modules_mapping
63-
# rule in the WORKSPACE
64-
modules_mapping = "@modules_map//:modules_mapping.json",
58+
modules_mapping = ":modules_map",
6559
# This is what we called our `pip_install` rule, where third-party
6660
# python libraries are loaded in BUILD files.
6761
pip_deps_repository_name = "pip",

gazelle/modules_mapping/BUILD.bazel

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
exports_files([
2-
"builder.py",
3-
"generator.py",
4-
])
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
3+
py_binary(
4+
name = "generator",
5+
srcs = ["generator.py"],
6+
visibility = ["//visibility:public"],
7+
)

gazelle/modules_mapping/builder.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)