9b2c74519c640e82080637e5b8674975bcbd7af9
[oam.git] / code / network-generator / Makefile
1 .ONESHELL:
2 ENV_PREFIX=$(shell python3 -c "if __import__('pathlib').Path('.venv/bin/pip').exists(): print('.venv/bin/')")
3 USING_POETRY=$(shell grep "tool.poetry" pyproject.toml && echo "yes")
4
5 .PHONY: help
6 help:             ## Show the help.
7         @echo "Usage: make <target>"
8         @echo ""
9         @echo "Targets:"
10         @fgrep "##" Makefile | fgrep -v fgrep
11
12
13 .PHONY: show
14 show:             ## Show the current environment.
15         @echo "Current environment:"
16         @if [ "$(USING_POETRY)" ]; then poetry env info && exit; fi
17         @echo "Running using $(ENV_PREFIX)"
18         @$(ENV_PREFIX)python -V
19         @$(ENV_PREFIX)python -m site
20
21 .PHONY: install
22 install:          ## Install the project in dev mode.
23         @if [ "$(USING_POETRY)" ]; then poetry install && exit; fi
24         @echo "Don't forget to run 'make virtualenv' if you got errors."
25         $(ENV_PREFIX)pip install -e .[test]
26
27 .PHONY: fmt
28 fmt:              ## Format code using black & isort.
29         $(ENV_PREFIX)isort network_generation/
30         $(ENV_PREFIX)black -l 79 network_generation/
31         $(ENV_PREFIX)black -l 79 tests/
32
33 .PHONY: lint
34 lint:             ## Run pep8, black, mypy linters.
35         $(ENV_PREFIX)flake8 network_generation/
36         $(ENV_PREFIX)black -l 79 --check network_generation/
37         $(ENV_PREFIX)black -l 79 --check tests/
38         $(ENV_PREFIX)mypy --ignore-missing-imports network_generation/
39
40 .PHONY: test
41 test: lint        ## Run tests and generate coverage report.
42         $(ENV_PREFIX)pytest -v --cov-config .coveragerc --cov=network_generation -l --tb=short --maxfail=1 tests/
43         $(ENV_PREFIX)coverage xml
44         $(ENV_PREFIX)coverage html
45
46 .PHONY: watch
47 watch:            ## Run tests on every change.
48         ls **/**.py | entr $(ENV_PREFIX)pytest -s -vvv -l --tb=long --maxfail=1 tests/
49
50 .PHONY: clean
51 clean:            ## Clean unused files.
52         @find ./ -name '*.pyc' -exec rm -f {} \;
53         @find ./ -name '__pycache__' -exec rm -rf {} \;
54         @find ./ -name 'Thumbs.db' -exec rm -f {} \;
55         @find ./ -name '*~' -exec rm -f {} \;
56         @rm -rf .cache
57         @rm -rf .pytest_cache
58         @rm -rf .mypy_cache
59         @rm -rf build
60         @rm -rf dist
61         @rm -rf *.egg-info
62         @rm -rf htmlcov
63         @rm -rf .tox/
64         @rm -rf docs/_build
65
66 .PHONY: virtualenv
67 virtualenv:       ## Create a virtual environment.
68         @if [ "$(USING_POETRY)" ]; then poetry install && exit; fi
69         @echo "creating virtualenv ..."
70         @rm -rf .venv
71         @python3 -m venv .venv
72         @./.venv/bin/pip install -U pip
73         @./.venv/bin/pip install -e .[test]
74         @echo
75         @echo "!!! Please run 'source .venv/bin/activate' to enable the environment !!!"
76
77 .PHONY: release
78 release:          ## Create a new tag for release.
79         @echo "WARNING: This operation will create s version tag and push to github"
80         @read -p "Version? (provide the next x.y.z semver) : " TAG
81         @echo "$${TAG}" > network_generation/VERSION
82         @$(ENV_PREFIX)gitchangelog > HISTORY.md
83         @git add network_generation/VERSION HISTORY.md
84         @git commit -m "release: version $${TAG} ðŸš€"
85         @echo "creating git tag : $${TAG}"
86         @git tag $${TAG}
87         @git push -u origin HEAD --tags
88         @echo "Github Actions will detect the new tag and release the new version."
89
90 .PHONY: docs
91 docs:             ## Build the documentation.
92         @echo "building documentation ..."
93         @$(ENV_PREFIX)mkdocs build
94         URL="site/index.html"; xdg-open $$URL || sensible-browser $$URL || x-www-browser $$URL || gnome-open $$URL || open $$URL
95
96 .PHONY: switch-to-poetry
97 switch-to-poetry: ## Switch to poetry package manager.
98         @echo "Switching to poetry ..."
99         @if ! poetry --version > /dev/null; then echo 'poetry is required, install from https://python-poetry.org/'; exit 1; fi
100         @rm -rf .venv
101         @poetry init --no-interaction --name=a_flask_test --author=rochacbruno
102         @echo "" >> pyproject.toml
103         @echo "[tool.poetry.scripts]" >> pyproject.toml
104         @echo "network_generation = 'network_generation.__main__:main'" >> pyproject.toml
105         @cat requirements.txt | while read in; do poetry add --no-interaction "$${in}"; done
106         @cat requirements-test.txt | while read in; do poetry add --no-interaction "$${in}" --dev; done
107         @poetry install --no-interaction
108         @mkdir -p .github/backup
109         @mv requirements* .github/backup
110         @mv setup.py .github/backup
111         @echo "You have switched to https://python-poetry.org/ package manager."
112         @echo "Please run 'poetry shell' or 'poetry run network_generation'"
113
114 .PHONY: init
115 init:             ## Initialize the project based on an application template.
116         @./.github/init.sh
117
118
119 # This project has been generated from rochacbruno/python-project-template
120 # __author__ = 'rochacbruno'
121 # __repo__ = https://github.com/rochacbruno/python-project-template
122 # __sponsor__ = https://github.com/sponsors/rochacbruno/