Skip to content

Commit

Permalink
refactor: make modules_mapping a regular rule (#578)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
f0rmiga and alexeagle authored Jan 5, 2022
1 parent 3525853 commit b842276
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 443 deletions.
11 changes: 10 additions & 1 deletion examples/build_file_generation/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_library")

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
modules_mapping(
name = "modules_map",
wheels = all_whl_requirements,
)

# Gazelle python extension needs a manifest file mapping from
# an import to the installed package that provides it.
# This macro produces two targets:
Expand All @@ -12,7 +21,7 @@ load("@rules_python//python:defs.bzl", "py_library")
# the manifest doesn't need to be updated
gazelle_python_manifest(
name = "gazelle_python_manifest",
modules_mapping = "@modules_map//:modules_mapping.json",
modules_mapping = ":modules_map",
pip_deps_repository_name = "pip",
requirements = "//:requirements_lock.txt",
)
Expand Down
11 changes: 0 additions & 11 deletions examples/build_file_generation/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,3 @@ pip_install(
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")

_py_gazelle_deps()

load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")

# This repository rule fetches the metadata for python packages we
# depend on. That data is required for the gazelle_python_manifest
# rule to update our manifest file.
# To see what this rule does, try `bazel run @modules_map//:print`
modules_mapping(
name = "modules_map",
requirements = "//:requirements_lock.txt",
)
38 changes: 16 additions & 22 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ that generates BUILD file content for Python code.
First, you'll need to add Gazelle to your `WORKSPACE` file.
Follow the instructions at https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel

Next, we need to add two more things to the `WORKSPACE`:

1. fetch the third-party Go libraries that the python extension depends on
1. fetch metadata about your Python dependencies, so that gazelle can
determine which package a given import statement comes from.
Next, we need to fetch the third-party Go libraries that the python extension
depends on.

Add this to your `WORKSPACE`:

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

_py_gazelle_deps()

load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")

# This repository rule fetches the metadata for python packages we
# depend on. That data is required for the gazelle_python_manifest
# rule to update our manifest file.
# To see what this rule does, try `bazel run @modules_map//:print`
modules_mapping(
name = "modules_map",
# This should point to wherever we declare our python dependencies
requirements = "//:requirements_lock.txt",
)
```

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

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

```starlark
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
modules_mapping(
name = "modules_map",
wheels = all_whl_requirements,
)

# Gazelle python extension needs a manifest file mapping from
# an import to the installed package that provides it.
Expand All @@ -59,9 +55,7 @@ load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
# the manifest doesn't need to be updated
gazelle_python_manifest(
name = "gazelle_python_manifest",
# The @modules_map refers to the name we gave in the modules_mapping
# rule in the WORKSPACE
modules_mapping = "@modules_map//:modules_mapping.json",
modules_mapping = ":modules_map",
# This is what we called our `pip_install` rule, where third-party
# python libraries are loaded in BUILD files.
pip_deps_repository_name = "pip",
Expand Down
11 changes: 7 additions & 4 deletions gazelle/modules_mapping/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
exports_files([
"builder.py",
"generator.py",
])
load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "generator",
srcs = ["generator.py"],
visibility = ["//visibility:public"],
)
70 changes: 0 additions & 70 deletions gazelle/modules_mapping/builder.py

This file was deleted.

Loading

0 comments on commit b842276

Please sign in to comment.