Skip to content

Commit

Permalink
Call pre-commit on generated files from within the generate script.
Browse files Browse the repository at this point in the history
Update pre_commit to rebuild when spec or setup.py has changed.
  • Loading branch information
rtibbles committed Oct 18, 2023
1 parent cb33ad1 commit 1edc3d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
exclude: (\.git/|\.tox/|\.venv/|le_utils\.egg-info)
repos:
- repo: local
hooks:
- id: rebuild
name: Regenerate files
description: Regenerates files when specs or version has changed
entry: make build
language: system
files: (spec/.*\.json|setup\.py)$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.0.0
hooks:
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ test:
build:
pip install -e .
python scripts/generate_from_specs.py
pre-commit run --all-files || true

release: clean build
python setup.py sdist
Expand Down
20 changes: 17 additions & 3 deletions scripts/generate_from_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import re
import string
import subprocess
import sys
from collections import OrderedDict
from glob import glob
Expand Down Expand Up @@ -238,26 +239,34 @@ def write_js_file(output_file, name, ordered_output, schema=None):


def write_labels_src_files(label_outputs):
output_files = []
for label_type, ordered_output in label_outputs.items():
py_output_file = os.path.join(
py_labels_output_dir, "{}.py".format(pascal_to_snake(label_type))
)
write_python_file(py_output_file, label_type, ordered_output)
output_files.append(py_output_file)

js_output_file = os.path.join(js_labels_output_dir, "{}.js".format(label_type))
write_js_file(js_output_file, label_type, ordered_output)
output_files.append(js_output_file)
return output_files


def write_constants_src_files(constants_outputs, schemas):
output_files = []
for constant_type, ordered_output in constants_outputs.items():
py_output_file = os.path.join(
py_output_dir, "{}.py".format(pascal_to_snake(constant_type))
)
schema = schemas.get(constant_type)
write_python_file(py_output_file, constant_type, ordered_output, schema=schema)
output_files.append(py_output_file)

js_output_file = os.path.join(js_output_dir, "{}.js".format(constant_type))
write_js_file(js_output_file, constant_type, ordered_output, schema=schema)
output_files.append(js_output_file)
return output_files


def set_package_json_version():
Expand All @@ -279,6 +288,7 @@ def set_package_json_version():
else:
f.write("\n")
f.write(line.rstrip())
return [package_json]


if __name__ == "__main__":
Expand All @@ -289,8 +299,12 @@ def set_package_json_version():
schemas_to_write, schema_constants_to_write = read_schema_specs()
constants_to_write.update(schema_constants_to_write)

write_labels_src_files(labels_to_write)
output_files = []

write_constants_src_files(constants_to_write, schemas_to_write)
output_files += write_labels_src_files(labels_to_write)

set_package_json_version()
output_files += write_constants_src_files(constants_to_write, schemas_to_write)

output_files += set_package_json_version()

subprocess.call(["pre-commit", "run", "--files"] + output_files)

0 comments on commit 1edc3d0

Please sign in to comment.