Skip to content

Commit

Permalink
Merge pull request #37 from roleroz/master
Browse files Browse the repository at this point in the history
Clean up WORKSPACE files for users

Currently, if you have a repository using this one, the WORKSPACE for that repository is full of sections that are dependent on the internal implementation of platformio_rules. This PR creates Starlark libraries to define those WORKSPACE files easily and cleanly

bazel build ... && bazel test ... passes
  • Loading branch information
mum4k authored Mar 27, 2023
2 parents 42274da + 2be1b2f commit 6fe89f5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 27 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ See [Releases](https://github.com/mum4k/platformio_rules/releases) for the most

```
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "platformio_rules",
remote = "http://github.com/mum4k/platformio_rules.git",
tag = "v0.0.9",
tag = "v0.0.13",
)
load("@platformio_rules//bazel:deps.bzl", "platformio_rules_dependencies")
platformio_rules_dependencies()
load("@platformio_rules//bazel:transitive.bzl", "platformio_rules_transitive_dependencies")
platformio_rules_transitive_dependencies()
load("@platformio_rules//bazel:pip_parse.bzl", "platformio_rules_pip_parse_dependencies")
platformio_rules_pip_parse_dependencies()
load("@platformio_rules//bazel:pip_install.bzl", "platformio_rules_pip_install_dependencies")
platformio_rules_pip_install_dependencies()
```

### Load the rule definitions in the BUILD file
Expand Down
33 changes: 8 additions & 25 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,20 @@ http_archive(
sha256 = "4756ab3ec46d94d99e5ed685d2d24aece484015e45af303eb3a11cab3cdc2e71",
)

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("//bazel:deps.bzl", "platformio_rules_dependencies")
platformio_rules_dependencies()

git_repository(
name = "io_bazel_stardoc",
remote = "https://github.com/bazelbuild/stardoc.git",
tag = "0.5.3",
)

load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")
stardoc_repositories()
load("//bazel:transitive.bzl", "platformio_rules_transitive_dependencies")
platformio_rules_transitive_dependencies()

# Python library manager (pip)
http_archive(
name = "rules_python",
sha256 = "81cbfc16dd1c022c4761267fa8b2feb881aaea9c3e1143f2e64630a1ad18c347",
strip_prefix = "rules_python-0.16.1",
url = "https://github.com/bazelbuild/rules_python/archive/0.16.1.zip",
)
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
python_register_toolchains(
name = "python3_10_8",
# Available versions are listed in @rules_python//python:versions.bzl.
# We recommend using the same version your team is already standardized on.
python_version = "3.10.8",
)
load("@python3_10_8//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "py_deps",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)
load("@py_deps//:requirements.bzl", "install_deps")
install_deps()

load("//bazel:pip_install.bzl", "platformio_rules_pip_install_dependencies")
platformio_rules_pip_install_dependencies()
Empty file added bazel/BUILD
Empty file.
22 changes: 22 additions & 0 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Load the main dependencies from platformio_rules
#
# This cannot load all the dependencies, as some load()s are needed from these,
# which cannot be done in a function, but this is the first step of the process
def platformio_rules_dependencies():
# Import Stardoc, to write the documentation to our Starlark rules
git_repository(
name = "io_bazel_stardoc",
remote = "https://github.com/bazelbuild/stardoc.git",
tag = "0.5.3",
)
# Import python, this is the first step to get pip_parse() dependencies to
# be corrently imported
http_archive(
name = "rules_python",
sha256 = "81cbfc16dd1c022c4761267fa8b2feb881aaea9c3e1143f2e64630a1ad18c347",
strip_prefix = "rules_python-0.16.1",
url = "https://github.com/bazelbuild/rules_python/archive/0.16.1.zip",
)
6 changes: 6 additions & 0 deletions bazel/pip_install.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@py_deps//:requirements.bzl", "install_deps")

# Finally, now that pip_parse() has been executed, install the pip dependencies
def platformio_rules_pip_install_dependencies():
# Install all pip dependencies
install_deps()
12 changes: 12 additions & 0 deletions bazel/pip_parse.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@python3_10_8//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

# Now that Python has been registered, load the pip packages that are required
# for template rendering
def platformio_rules_pip_parse_dependencies():
# Load the pip packages needed
pip_parse(
name = "py_deps",
python_interpreter_target = interpreter,
requirements_lock = "@platformio_rules//:requirements_lock.txt",
)
19 changes: 19 additions & 0 deletions bazel/transitive.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")
load("@rules_python//python:repositories.bzl", "python_register_toolchains")

# Load the first level of dependencies that are based on the ones imported in
# platformio_rules_dependencies(), as these imports depend on load() commands
# that come from those imports
def platformio_rules_transitive_dependencies():
# Import all the dependencies for Stardoc, so we can write documentation for
# our Starlark rules
stardoc_repositories()

# Select the Python toolchain that will be used for template rendering. This
# is required for pip_parse() to be executed
python_register_toolchains(
name = "python3_10_8",
# Available versions are listed in @rules_python//python:versions.bzl.
# We recommend using the same version your team is already standardized on.
python_version = "3.10.8",
)

0 comments on commit 6fe89f5

Please sign in to comment.