From b1156f650380079d87f0062aa9d4e6e481ae0225 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Fri, 4 Aug 2023 21:51:24 +0200 Subject: [PATCH] feat: Set up devbox https://www.jetpack.io/devbox/docs/ --- .github/workflows/publish.yml | 40 +++++------------------- .pre-commit-config.yaml | 10 +++--- .python-version | 1 - CONTRIBUTING.md | 23 +++++++------- Makefile | 57 ----------------------------------- devbox.json | 28 +++++++++++++++++ devbox.lock | 30 ++++++++++++++++++ 7 files changed, 82 insertions(+), 107 deletions(-) delete mode 100644 .python-version delete mode 100644 Makefile create mode 100644 devbox.json create mode 100644 devbox.lock diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 23d54fb..12c466c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -12,51 +12,27 @@ jobs: tests: name: Run tests - strategy: - matrix: - python-version: ["3.10", "3.11"] - os: ["ubuntu-latest"] - - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - # Installing via `pipx` is 'fully supported', so no need for - # https://github.com/snok/install-poetry, which is a bit slower. See also - # https://python-poetry.org/docs/master/#installing-with-pipx - - name: Install Poetry - run: pipx install poetry - - # Counterintuitively, the Python setup step itself is setup *after* installing - # `poetry`, else the `poetry` command isn't found and the setup step fails. See - # also: - # https://github.com/marketplace/actions/setup-python#caching-packages-dependencies - - uses: actions/setup-python@v4 + - name: Install devbox + uses: jetpack-io/devbox-install-action@v0.5.0 with: - python-version: ${{ matrix.python-version }} - cache: poetry - - - name: Set up Python environment - # `poetry env use` instructs all `poetry` runs to go through the correct (NOT - # the default aka system) Python environment, see also: - # https://github.com/actions/setup-python/issues/374#issuecomment-1088938718 - # As long as we then call all actions via `poetry run ...`, we're fine. - run: | - poetry env use ${{ matrix.python-version }} - poetry install + enable-cache: true - name: Run linting - run: make lint + run: devbox run lint - name: Check code formatting - run: make formatcheck + run: devbox run format-check - name: Run type checks - run: make typecheck + run: devbox run typecheck - name: Run tests - run: make test + run: devbox run test env: # Unit tests actually run against the GH API for 'real integration testing', # and providing a token will increase the otherwise too-low rate limit. diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1456c9d..495aed1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: lint name: Run linting language: system - entry: make lint + entry: devbox run lint stages: - "commit" - "push" @@ -20,7 +20,7 @@ repos: - id: check-formatting name: Check formatting language: system - entry: make formatcheck + entry: devbox run format-check stages: - "commit" - "push" @@ -32,7 +32,7 @@ repos: - id: run-typecheck name: Run typecheck language: system - entry: make typecheck + entry: devbox run typecheck stages: - "commit" - "push" @@ -44,7 +44,7 @@ repos: - id: run-tests name: Run tests language: system - entry: make test + entry: devbox run test stages: - "commit" - "push" @@ -56,6 +56,6 @@ repos: - id: build-image name: Build image language: system - entry: make image + entry: devbox run build-image stages: - "push" diff --git a/.python-version b/.python-version deleted file mode 100644 index 8d7f852..0000000 --- a/.python-version +++ /dev/null @@ -1 +0,0 @@ -3.10.4 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 871829c..677de78 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,23 +2,22 @@ ## Environment setup -Sadly, the setup cannot be automated arbitrarily (say, using a `make init` target). -Installing `poetry` requires system packages (`python3`, `pip`, `curl`) that cannot be easily installed on the user's behalf in a distribution-independent way. -Further, inserting `poetry` in one's `$PATH` is best done manually and consciously (environments, user desires etc. differ too much). +[![Built with Devbox](https://jetpack.io/img/devbox/shield_moon.svg)](https://jetpack.io/devbox/docs/contributor-quickstart/) -1. [Install `poetry`](https://python-poetry.org/docs/master/#installing-with-the-official-installer). +Use devbox to set up a development environment. +Refer to [the available `script`s](devbox.json) to see what's possible. +Generally, even when running in a `devbox shell`, `poetry run` is necessary: - If you're having trouble with version mismatch, check which version the `poetry` installation currently uses in the [CI](https://github.com/alexpovel/ancv/actions). -2. Run `poetry install` in the root directly (where the [`pyproject.toml`](./pyproject.toml) is located). +- devbox sets up what used to be system-wide packages (Python, poetry, ...) deterministically and automatically +- within the devbox virtual environment, we still manage and use a Python virtual environment through `poetry run` - `poetry` will throw an error here if your Python version isn't supported by the project. - If that is the case, look into using [`pyenv`](https://github.com/pyenv/pyenv), which will use the version specified in [`.python-version`](.python-version), guaranteeing a version match. -3. Enter into the created environment with `poetry shell`. +That way, we get the normal Python package management for normal Python packages (`ruff`, `black`, `pytest`, ...), and devbox for the overarching rest. - *Alternatively*, prepend all commands pertaining to the repository with `poetry run`, like `poetry run python -m ancv`. -4. Set up `git` hooks provided by [`pre-commit`](https://pre-commit.com/#intro) (already installed through `poetry`): `make hooks`. +Lastly, for bonus points, set up pre-commit hooks: - For this to work, you will have to have `make` installed (Windows alternatives exist). +```bash +devbox run install-hooks +``` ## Creating components diff --git a/Makefile b/Makefile deleted file mode 100644 index b0f33d7..0000000 --- a/Makefile +++ /dev/null @@ -1,57 +0,0 @@ -.PHONY: tests test typecheck formatcheck lint image - -RUN = poetry run -LIBRARY = ancv - -tests: test typecheck formatcheck lint - -# Ensure the Docker image also builds properly -alltests: tests image - -test: - ${RUN} pytest \ - --cov=${LIBRARY} \ - --cov-report=html \ - --cov-report=term \ - --cov-report=xml - -typecheck: - ${RUN} mypy -p ${LIBRARY} - -formatcheck: - ${RUN} black --check --diff ${LIBRARY} - -lint: - ${RUN} ruff . - -image: - @docker build --progress=plain --tag ${LIBRARY}/${LIBRARY} . - -# Hooks need to be added here manually if other 'types' are later added: -hooks: - @pre-commit install --hook-type pre-push --hook-type pre-commit --hook-type commit-msg - -requirements.txt: - poetry export --with=dev --output=requirements.txt - -depgraph.svg: - @command -v dot > /dev/null || (echo "Please install graphviz for its 'dot' command." && exit 1) - @${RUN} pydeps --max-bacon=4 --cluster -T svg -o "$@" ${LIBRARY} - -schema.json: - ${RUN} python -m ${LIBRARY} generate-schema > "$@" - -resume.py: - ${RUN} datamodel-codegen \ - --url "https://raw.githubusercontent.com/jsonresume/resume-schema/master/schema.json" \ - --encoding utf-8 \ - --input-file-type jsonschema \ - --output "$@" - -github.py: - ${RUN} datamodel-codegen \ - --url "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/dereferenced/api.github.com.deref.json" \ - --encoding utf-8 \ - --input-file-type openapi \ - --openapi-scopes paths \ - --output "$@" diff --git a/devbox.json b/devbox.json new file mode 100644 index 0000000..5572733 --- /dev/null +++ b/devbox.json @@ -0,0 +1,28 @@ +{ + "packages": [ + "python@3", + "poetry@1.5", + "graphviz@8", + "pre-commit@3" + ], + "env": { + "LIBRARY": "ancv" + }, + "shell": { + "init_hook": [ + "echo 'Running command in devbox shell...'", + "poetry install" + ], + "scripts": { + "build-image": "docker build --progress=plain --tag \"$LIBRARY\"/\"$LIBRARY\":$(poetry version --short) .", + "format-check": "poetry run black --check --diff \"$LIBRARY\"", + "install-hooks": "pre-commit install --hook-type pre-push --hook-type pre-commit --hook-type commit-msg", + "lint": "poetry run ruff .", + "make-depgraph.svg": "poetry run pydeps --max-bacon=4 --cluster -T svg -o depgraph.svg \"$LIBRARY\"", + "make-github.py": "poetry run datamodel-codegen --url \"https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/dereferenced/api.github.com.deref.json\" --encoding utf-8 --input-file-type openapi --openapi-scopes paths --output github.py", + "make-resume.py": "poetry run datamodel-codegen --url \"https://raw.githubusercontent.com/jsonresume/resume-schema/master/schema.json\" --encoding utf-8 --input-file-type jsonschema --output resume.py", + "test": "poetry run pytest --cov=\"$LIBRARY\" --cov-report=html --cov-report=term --cov-report=xml", + "typecheck": "poetry run mypy -p \"$LIBRARY\"" + } + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 0000000..9b3345e --- /dev/null +++ b/devbox.lock @@ -0,0 +1,30 @@ +{ + "lockfile_version": "1", + "packages": { + "graphviz@8": { + "last_modified": "2023-07-23T03:35:12Z", + "resolved": "github:NixOS/nixpkgs/af8cd5ded7735ca1df1a1174864daab75feeb64a#graphviz", + "source": "devbox-search", + "version": "8.0.5" + }, + "poetry@1.5": { + "last_modified": "2023-07-23T03:35:12Z", + "resolved": "github:NixOS/nixpkgs/af8cd5ded7735ca1df1a1174864daab75feeb64a#poetry", + "source": "devbox-search", + "version": "1.5.1" + }, + "pre-commit@3": { + "last_modified": "2023-06-30T04:44:22Z", + "resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#pre-commit", + "source": "devbox-search", + "version": "3.3.3" + }, + "python@3": { + "last_modified": "2023-07-30T12:29:02Z", + "plugin_version": "0.0.1", + "resolved": "github:NixOS/nixpkgs/3acb5c4264c490e7714d503c7166a3fde0c51324#python312", + "source": "devbox-search", + "version": "3.12.0b4" + } + } +}