diff --git a/CHANGES.md b/CHANGES.md index 080361f..3124e36 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,8 @@ - Add `wtr` (Web test runner) domain to `js` topic. +- Add pyupgrade based code formatter, see https://pypi.org/project/pyupgrade/. + ## 1.0a3 (2024-02-06) - Add `typecheck` target and use it for mypy instead of `check` target. diff --git a/src/mxmake/topics/qa/pyupgrade.mk b/src/mxmake/topics/qa/pyupgrade.mk new file mode 100644 index 0000000..b947461 --- /dev/null +++ b/src/mxmake/topics/qa/pyupgrade.mk @@ -0,0 +1,50 @@ +#:[pyupgrade] +#:title = pyupgrade +#:description = Automatically upgrade syntax for newer versions of the language. +#:depends = core.mxenv +#: +#:[target.pyupgrade] +#:description = Run pyupgrade. +#: +#:[setting.PYUPGRADE_SRC] +#:description = Source folder to scan for XML and ZCML files. +#:default = src +#: +#:[setting.PYUPGRADE_PARAMETERS] +#:description = Additional parameters for pyupgrade, see https://github.com/asottile/pyupgrade for details. +#:default = --py38-plus +#: +#:[target.pyupgrade-dirty] +#:description = Marks pyupgrade dirty +#: +#:[target.pyupgrade-clean] +#:description = Uninstall pyupgrade. + +############################################################################## +# pyupgrade +############################################################################## + +PYUPGRADE_PARAMETERS?= +PYUPGRADE_TARGET:=$(SENTINEL_FOLDER)/pyupgrade.sentinel +$(PYUPGRADE_TARGET): $(MXENV_TARGET) + @echo "Install pyupgrade" + @$(MXENV_PYTHON) -m pip install pyupgrade + @touch $(PYUPGRADE_TARGET) + +.PHONY: pyupgrade-format +pyupgrade-format: $(PYUPGRADE_TARGET) + @echo "Run pyupgrade format in: $(PYUPGRADE_SRC)" + @find $(PYUPGRADE_SRC) -name '*.py' -exec $(MXENV_PATH)pyupgrade $(PYUPGRADE_PARAMETERS) {} + + +.PHONY: pyupgrade-dirty +pyupgrade-dirty: + @rm -f $(PYUPGRADE_TARGET) + +.PHONY: pyupgrade-clean +pyupgrade-clean: pyupgrade-dirty + @test -e $(MXENV_PYTHON) && $(MXENV_PYTHON) -m pip uninstall -y pyupgrade || : + +INSTALL_TARGETS+=$(PYUPGRADE_TARGET) +FORMAT_TARGETS+=pyupgrade-format +DIRTY_TARGETS+=pyupgrade-dirty +CLEAN_TARGETS+=pyupgrade-clean