Skip to content

Commit 385a983

Browse files
committed
refactor: make modules_mapping a regular rule
1 parent 2b1d6be commit 385a983

File tree

7 files changed

+81
-443
lines changed

7 files changed

+81
-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 & 20 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.
@@ -61,7 +57,7 @@ gazelle_python_manifest(
6157
name = "gazelle_python_manifest",
6258
# The @modules_map refers to the name we gave in the modules_mapping
6359
# rule in the WORKSPACE
64-
modules_mapping = "@modules_map//:modules_mapping.json",
60+
modules_mapping = ":modules_map",
6561
# This is what we called our `pip_install` rule, where third-party
6662
# python libraries are loaded in BUILD files.
6763
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy