Skip to content

Commit

Permalink
feat: add python
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed May 10, 2024
1 parent e8a3e18 commit fb203d1
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 21 deletions.
38 changes: 35 additions & 3 deletions scaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ messages:
```sh
cd {{ .ProjectSnake }}
{{- if .Scaffold.fmt }}
aspect run format
{{- end }}
aspect test ...
JavaScript support: {{ .Computed.javascript }}
Python support: {{ .Computed.python }}
TypeScript support: {{ .Computed.typescript }}
```
questions:
- name: fmt
Expand All @@ -20,13 +27,38 @@ questions:
multi: true
message: "Languages to be used in the project"
options:
- JavaScript
- TypeScript
- JavaScript & TypeScript
- Python

features:
- value: "{{ .Scaffold.fmt }}"
globs:
- "**/tools/format/*"
- value: "{{ .Computed.javascript }}"
globs:
- "*/package.json"
- "*/pnpm-workspace.yaml"
- "*/.npmrc"
- "**/packages/**"
- value: "{{ .Computed.python }}"
globs:
- "*/requirements.txt"
- "*/requirements_lock.txt"

computed:
javascript: "{{ has \"JavaScript & TypeScript\" .Scaffold.langs }}"
python: "{{ has \"Python\" .Scaffold.langs }}"

presets:
kitchen-sink:
fmt: true
langs: ['JavaScript', 'TypeScript']
langs: ['JavaScript & TypeScript', 'Python']
js:
fmt: true
langs: ['JavaScript & TypeScript']
py:
fmt: true
langs: ['Python']
minimal:
fmt: false
langs: []
1 change: 1 addition & 0 deletions {{ .ProjectSnake }}/.aspect/bazelrc/javascript.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
# details.
# Docs: https://nodejs.org/en/docs/guides/debugging-getting-started/#command-line-options
run:debug -- --node_options=--inspect-brk
test:debug --test_env=NODE_OPTIONS=--inspect-brk
4 changes: 4 additions & 0 deletions {{ .ProjectSnake }}/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# files or directories Bazel should not read
# NB: this does NOT follow .gitignore semantics, see
# https://github.com/bazelbuild/bazel/issues/7093
node_modules
4 changes: 4 additions & 0 deletions {{ .ProjectSnake }}/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# User-specific settings file for this repo
.aspect/bazelrc/user.bazelrc

# Don't check in lockfile for now. It is unstable.
Expand All @@ -7,3 +8,6 @@
# https://github.com/bazelbuild/bazel/issues/20272
# https://github.com/bazelbuild/bazel/issues/20369
MODULE.bazel.lock

# Symlinks created by Bazel into the output tree
/bazel-*
4 changes: 4 additions & 0 deletions {{ .ProjectSnake }}/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Disabling pnpm [hoisting](https://pnpm.io/npmrc#hoist) by setting `hoist=false` is recommended on
# projects using rules_js so that pnpm outside of Bazel lays out a node_modules tree similar to what
# rules_js lays out under Bazel (without a hidden node_modules/.pnpm/node_modules)
hoist=false
22 changes: 20 additions & 2 deletions {{ .ProjectSnake }}/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
"Targets in the workspace root"
{{- if .Computed.javascript }}
load("@npm//:defs.bzl", "npm_link_all_packages")
{{- end }}
{{- if .Computed.python }}
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
{{- end}}

{{ if .Scaffold.fmt -}}
{{- if .Scaffold.fmt }}
alias(
name = "format",
actual = "//tools/format",
)
{{ end }}
{{- end }}

{{- if .Computed.javascript }}
npm_link_all_packages(name = "node_modules")
{{- end }}

{{- if .Computed.python }}
compile_pip_requirements(
name = "requirements",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)
{{- end }}
39 changes: 25 additions & 14 deletions {{ .ProjectSnake }}/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
bazel_dep(name = "aspect_bazel_lib", version = "2.7.1")
bazel_dep(name = "aspect_rules_lint", version = "0.18.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.3")
bazel_dep(name = "aspect_rules_lint", version = "0.20.0")
bazel_dep(name = "rules_multitool", version = "0.7.1")
{{- range .Scaffold.langs}}
{{- if eq . "JavaScript"}}
bazel_dep(name = "aspect_rules_js", version = "1.32.6")
{{- end}}
{{- if eq . "TypeScript"}}
{{- if .Computed.javascript }}
bazel_dep(name = "aspect_rules_js", version = "1.42.0")
bazel_dep(name = "aspect_rules_ts", version = "2.3.0")
{{- end}}
{{- end}}
{{- if .Computed.python }}
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "aspect_rules_py", version = "0.7.3")
{{- end }}

multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
multitool.hub(lockfile = "//tools:tools.lock.json")
use_repo(multitool, "multitool")

{{- range .Scaffold.langs}}
{{- if eq . "JavaScript"}}
{{- if .Computed.javascript}}
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
# Allows developers to get a local node_modules tree, using
# bazel run -- @pnpm//:pnpm --dir $PWD install
# Allows developers to run the same pnpm version that Bazel manages
use_repo(pnpm, "pnpm")

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm")
npm.npm_translate_lock(
name = "npm",
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)
use_repo(npm, "npm")
{{- end}}

{{- if eq . "TypeScript"}}
rules_ts_ext = use_extension(
"@aspect_rules_ts//ts:extensions.bzl",
"ext",
Expand All @@ -39,4 +36,18 @@ rules_ts_ext = use_extension(
rules_ts_ext.deps()
use_repo(rules_ts_ext, "npm_typescript")
{{- end}}

{{- if .Computed.python }}
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.9",
)

pip.parse(
hub_name = "pip",
requirements_lock = "//:requirements_lock.txt",
python_version = "3.9",
)
use_repo(pip, "pip")
{{- end}}
15 changes: 14 additions & 1 deletion {{ .ProjectSnake }}/README.bazel.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Bazel setup
# Bazel workflows

This repository uses [Aspect Workflows](https://aspect.build) to provide an excellent Bazel developer experience.

Expand All @@ -18,3 +18,16 @@ For developers to be able to run a CLI tool without needing manual installation:
3. Instruct developers to run `./tools/name_of_tool` rather than install that tool on their machine.

See https://blog.aspect.build/run-tools-installed-by-bazel for details.

{{- if .Computed.javascript }}
## Working with npm packages

To install a `node_modules` tree locally for the editor or other tooling outside of Bazel:

```
bazel run -- @pnpm --dir $PWD install
```

Similarly, you can run other `pnpm` commands to install or remove packages.
This ensures you use the same pnpm version as other developers, and the lockfile format will stay constant.
{{- end }}
2 changes: 1 addition & 1 deletion {{ .ProjectSnake }}/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Created by Aspect Starter
# Created by aspect-workflows-template
#
# This file is a marker indicating the root of a Bazel workspace.
# See MODULE.bazel for dependency information.
4 changes: 4 additions & 0 deletions {{ .ProjectSnake }}/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "{{ .Project }}",
"private": true
}
9 changes: 9 additions & 0 deletions {{ .ProjectSnake }}/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions {{ .ProjectSnake }}/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See https://pnpm.io/pnpm-workspace_yaml
packages:
- packages/*
1 change: 1 addition & 0 deletions {{ .ProjectSnake }}/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
26 changes: 26 additions & 0 deletions {{ .ProjectSnake }}/requirements_lock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# bazel run //:requirements.update
#
certifi==2022.12.7 \
--hash=sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \
--hash=sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18
# via requests
charset-normalizer==2.1.1 \
--hash=sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845 \
--hash=sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f
# via requests
idna==3.4 \
--hash=sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4 \
--hash=sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2
# via requests
requests==2.28.1 \
--hash=sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983 \
--hash=sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349
# via -r requirements.txt
urllib3==1.26.13 \
--hash=sha256:47cc05d99aaa09c9e72ed5809b60e7ba354e64b59c9c173ac3018642d8bb41fc \
--hash=sha256:c083dd0dce68dbfbe1129d5271cb90f9447dea7d52097c6e0126120c521ddea8
# via requests

0 comments on commit fb203d1

Please sign in to comment.