Skip to content

Commit 2d2808f

Browse files
authored
Merge pull request #30 from roleroz/master
Replace templating from bazel to Jinga2
2 parents ffb2c29 + 68b65b5 commit 2d2808f

File tree

9 files changed

+196
-16
lines changed

9 files changed

+196
-16
lines changed

BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
2+
3+
exports_files([
4+
"requirements.in",
5+
"requirements_lock.txt",
6+
"requirements_windows.txt",
7+
])
8+
9+
compile_pip_requirements(
10+
name = "requirements",
11+
extra_args = ["--allow-unsafe"],
12+
requirements_in = "requirements.in",
13+
requirements_txt = "requirements_lock.txt",
14+
requirements_windows = "requirements_windows.txt",
15+
)

WORKSPACE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,27 @@ git_repository(
3131

3232
load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")
3333
stardoc_repositories()
34+
35+
# Python library manager (pip)
36+
http_archive(
37+
name = "rules_python",
38+
sha256 = "81cbfc16dd1c022c4761267fa8b2feb881aaea9c3e1143f2e64630a1ad18c347",
39+
strip_prefix = "rules_python-0.16.1",
40+
url = "https://github.com/bazelbuild/rules_python/archive/0.16.1.zip",
41+
)
42+
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
43+
python_register_toolchains(
44+
name = "python3_10_8",
45+
# Available versions are listed in @rules_python//python:versions.bzl.
46+
# We recommend using the same version your team is already standardized on.
47+
python_version = "3.10.8",
48+
)
49+
load("@python3_10_8//:defs.bzl", "interpreter")
50+
load("@rules_python//python:pip.bzl", "pip_parse")
51+
pip_parse(
52+
name = "py_deps",
53+
python_interpreter_target = interpreter,
54+
requirements_lock = "//:requirements_lock.txt",
55+
)
56+
load("@py_deps//:requirements.bzl", "install_deps")
57+
install_deps()

platformio/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
load("@py_deps//:requirements.bzl", "requirement")
2+
13
package(
24
default_visibility = ["//visibility:public"],
35
)
46

7+
py_binary(
8+
name = "template_renderer",
9+
srcs = ["template_renderer.py"],
10+
deps = [
11+
requirement("jinja2"),
12+
]
13+
)
14+
515
filegroup(
616
name = "platformio_ini_tmpl",
717
srcs = ["platformio.ini.tmpl"],

platformio/platformio.bzl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,22 @@ def _emit_ini_file_action(ctx):
150150
if flag == "":
151151
continue
152152
build_flags.append(flag)
153-
ctx.actions.expand_template(
154-
template=ctx.file._platformio_ini_tmpl,
155-
output=ctx.outputs.platformio_ini,
156-
substitutions={
157-
"%board%": ctx.attr.board,
158-
"%platform%": ctx.attr.platform,
159-
"%framework%": ctx.attr.framework,
160-
"%environment_kwargs%": "\n".join(environment_kwargs),
161-
"%build_flags%": " ".join(build_flags),
162-
},
153+
substitutions = struct(
154+
board=ctx.attr.board,
155+
platform=ctx.attr.platform,
156+
framework=ctx.attr.framework,
157+
environment_kwargs=environment_kwargs,
158+
build_flags=build_flags,
159+
).to_json()
160+
ctx.actions.run(
161+
outputs=[ctx.outputs.platformio_ini],
162+
inputs=[ctx.file._platformio_ini_tmpl],
163+
executable=ctx.executable._template_renderer,
164+
arguments=[
165+
ctx.file._platformio_ini_tmpl.path,
166+
ctx.outputs.platformio_ini.path,
167+
substitutions
168+
],
163169
)
164170

165171

@@ -356,6 +362,11 @@ platformio_project = rule(
356362
default=Label("//platformio:platformio_ini_tmpl"),
357363
allow_single_file=True,
358364
),
365+
"_template_renderer": attr.label(
366+
default=Label("//platformio:template_renderer"),
367+
executable=True,
368+
cfg="exec",
369+
),
359370
"src": attr.label(
360371
allow_single_file=[".cc"],
361372
mandatory=True,

platformio/platformio.ini.tmpl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
{# The following comment applies to the generated file, not to the template.
2+
3+
If you are working on the template, knock yourself out ;) #}
14
; This file was automatically generated by bazel.
25
; Do not edit.
36

47
[platformio]
5-
default_envs = %board%
8+
default_envs = {{ board }}
69

7-
[env:%board%]
8-
build_flags = -Wall -DPLATFORMIO_BUILD %build_flags%
10+
[env:{{ board }}]
11+
build_flags =
12+
-Wall
13+
-DPLATFORMIO_BUILD
14+
{%- for flag in build_flags %}
15+
{{ flag }}
16+
{%- endfor %}
917
lib_ldf_mode = deep+
10-
platform = %platform%
11-
board = %board%
12-
framework = %framework%%environment_kwargs%
18+
platform = {{ platform }}
19+
board = {{ board }}
20+
framework =
21+
{{ framework }}
22+
{%- for kwarg in environment_kwargs %}
23+
{{environment_kwargs}}
24+
{%- endfor %}

platformio/template_renderer.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Utility that reads a template file in Jinja format, a substitutions dictionary
2+
# in json format, and writes the rendered text to a file.
3+
4+
# To use, run
5+
# template_renderer <template file> <output file> <substitutions>
6+
7+
8+
import jinja2
9+
import json
10+
import sys
11+
12+
13+
# Reads a template, renders it, and writes the output to a file.
14+
#
15+
# The template is provided in a text file in Jinja format
16+
# (https://jinja.palletsprojects.com/), and the substitutions are provided in
17+
# json format.
18+
#
19+
# Crashes if either any of the required arguments is not provided in the command
20+
# line, or if anything else is provided in the command line.
21+
#
22+
# Args (these are taken from the command line, i.e. sys.argv):
23+
# - template_filename (sys.argv[1]): The name of the file that contains the
24+
# template to be rendered
25+
# - output_filename (sys.argv[2]): The name of the file to which the rendered
26+
# template will be written
27+
# - substitutions (sys.argv[3]): A json representation of a dictionary with the
28+
# variables and names to be substituted while rendering the provided template
29+
def main() -> int:
30+
# Read parameters
31+
num_parameters = len(sys.argv)
32+
assert num_parameters == 4
33+
template_filename = sys.argv[1]
34+
output_filename = sys.argv[2]
35+
substitutions = json.loads(sys.argv[3])
36+
37+
# Read template file
38+
template_file = open(template_filename, "r")
39+
template_str = template_file.read()
40+
template_file.close()
41+
42+
# Render template
43+
template = jinja2.Environment().from_string(template_str)
44+
rendered_str = template.render(substitutions)
45+
46+
# Write output to desired file
47+
output_file = open(output_filename, "w")
48+
output_file.write(rendered_str)
49+
output_file.close()
50+
51+
return 0
52+
53+
54+
if __name__ == '__main__':
55+
sys.exit(main())

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jinja2

requirements_lock.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# This file is autogenerated by pip-compile with python 3.10
3+
# To update, run:
4+
#
5+
# bazel run //:requirements.update
6+
#
7+
jinja2==3.1.2 \
8+
--hash=sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852 \
9+
--hash=sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61
10+
# via -r ./requirements.in
11+
markupsafe==2.1.1 \
12+
--hash=sha256:0212a68688482dc52b2d45013df70d169f542b7394fc744c02a57374a4207003 \
13+
--hash=sha256:089cf3dbf0cd6c100f02945abeb18484bd1ee57a079aefd52cffd17fba910b88 \
14+
--hash=sha256:10c1bfff05d95783da83491be968e8fe789263689c02724e0c691933c52994f5 \
15+
--hash=sha256:33b74d289bd2f5e527beadcaa3f401e0df0a89927c1559c8566c066fa4248ab7 \
16+
--hash=sha256:3799351e2336dc91ea70b034983ee71cf2f9533cdff7c14c90ea126bfd95d65a \
17+
--hash=sha256:3ce11ee3f23f79dbd06fb3d63e2f6af7b12db1d46932fe7bd8afa259a5996603 \
18+
--hash=sha256:421be9fbf0ffe9ffd7a378aafebbf6f4602d564d34be190fc19a193232fd12b1 \
19+
--hash=sha256:43093fb83d8343aac0b1baa75516da6092f58f41200907ef92448ecab8825135 \
20+
--hash=sha256:46d00d6cfecdde84d40e572d63735ef81423ad31184100411e6e3388d405e247 \
21+
--hash=sha256:4a33dea2b688b3190ee12bd7cfa29d39c9ed176bda40bfa11099a3ce5d3a7ac6 \
22+
--hash=sha256:4b9fe39a2ccc108a4accc2676e77da025ce383c108593d65cc909add5c3bd601 \
23+
--hash=sha256:56442863ed2b06d19c37f94d999035e15ee982988920e12a5b4ba29b62ad1f77 \
24+
--hash=sha256:671cd1187ed5e62818414afe79ed29da836dde67166a9fac6d435873c44fdd02 \
25+
--hash=sha256:694deca8d702d5db21ec83983ce0bb4b26a578e71fbdbd4fdcd387daa90e4d5e \
26+
--hash=sha256:6a074d34ee7a5ce3effbc526b7083ec9731bb3cbf921bbe1d3005d4d2bdb3a63 \
27+
--hash=sha256:6d0072fea50feec76a4c418096652f2c3238eaa014b2f94aeb1d56a66b41403f \
28+
--hash=sha256:6fbf47b5d3728c6aea2abb0589b5d30459e369baa772e0f37a0320185e87c980 \
29+
--hash=sha256:7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b \
30+
--hash=sha256:86b1f75c4e7c2ac2ccdaec2b9022845dbb81880ca318bb7a0a01fbf7813e3812 \
31+
--hash=sha256:8dc1c72a69aa7e082593c4a203dcf94ddb74bb5c8a731e4e1eb68d031e8498ff \
32+
--hash=sha256:8e3dcf21f367459434c18e71b2a9532d96547aef8a871872a5bd69a715c15f96 \
33+
--hash=sha256:8e576a51ad59e4bfaac456023a78f6b5e6e7651dcd383bcc3e18d06f9b55d6d1 \
34+
--hash=sha256:96e37a3dc86e80bf81758c152fe66dbf60ed5eca3d26305edf01892257049925 \
35+
--hash=sha256:97a68e6ada378df82bc9f16b800ab77cbf4b2fada0081794318520138c088e4a \
36+
--hash=sha256:99a2a507ed3ac881b975a2976d59f38c19386d128e7a9a18b7df6fff1fd4c1d6 \
37+
--hash=sha256:a49907dd8420c5685cfa064a1335b6754b74541bbb3706c259c02ed65b644b3e \
38+
--hash=sha256:b09bf97215625a311f669476f44b8b318b075847b49316d3e28c08e41a7a573f \
39+
--hash=sha256:b7bd98b796e2b6553da7225aeb61f447f80a1ca64f41d83612e6139ca5213aa4 \
40+
--hash=sha256:b87db4360013327109564f0e591bd2a3b318547bcef31b468a92ee504d07ae4f \
41+
--hash=sha256:bcb3ed405ed3222f9904899563d6fc492ff75cce56cba05e32eff40e6acbeaa3 \
42+
--hash=sha256:d4306c36ca495956b6d568d276ac11fdd9c30a36f1b6eb928070dc5360b22e1c \
43+
--hash=sha256:d5ee4f386140395a2c818d149221149c54849dfcfcb9f1debfe07a8b8bd63f9a \
44+
--hash=sha256:dda30ba7e87fbbb7eab1ec9f58678558fd9a6b8b853530e176eabd064da81417 \
45+
--hash=sha256:e04e26803c9c3851c931eac40c695602c6295b8d432cbe78609649ad9bd2da8a \
46+
--hash=sha256:e1c0b87e09fa55a220f058d1d49d3fb8df88fbfab58558f1198e08c1e1de842a \
47+
--hash=sha256:e72591e9ecd94d7feb70c1cbd7be7b3ebea3f548870aa91e2732960fa4d57a37 \
48+
--hash=sha256:e8c843bbcda3a2f1e3c2ab25913c80a3c5376cd00c6e8c4a86a89a28c8dc5452 \
49+
--hash=sha256:efc1913fd2ca4f334418481c7e595c00aad186563bbc1ec76067848c7ca0a933 \
50+
--hash=sha256:f121a1420d4e173a5d96e47e9a0c0dcff965afdf1626d28de1460815f7c4ee7a \
51+
--hash=sha256:fc7b548b17d238737688817ab67deebb30e8073c95749d55538ed473130ec0c7
52+
# via jinja2

requirements_windows.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)