]> arthur.barton.de Git - ansible-collection-boilerplate.git/blob - Makefile.boilerplate
Makefile.boilerplate: Try to remove empty "ansible_galaxy" folder on "distclean"
[ansible-collection-boilerplate.git] / Makefile.boilerplate
1 #
2 # Ansible Boilerplate Collection: Makefile for inclusion into projects using it.
3 #
4
5 PYTHON ?= python3
6 SOURCE_ROOT ?= $(CURDIR)
7 VENV_D = $(SOURCE_ROOT)/.venv
8 VENV_BIN = $(VENV_D)/bin
9
10 this_makefile_path:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
11 this_dir:=$(shell cd $(dir $(this_makefile_path));pwd)
12
13 check-ansible: $(VENV_BIN)/ansible-lint
14 #       Check Ansible executable ...
15         "$(VENV_BIN)"/ansible --version
16 #       Check YAML files ...
17         "$(VENV_BIN)"/yamllint .
18 #       Run ansible-playbook syntax check, when a "site play" is found ...
19 ifneq ("$(wildcard playbooks/site.yml)","")
20         "$(VENV_BIN)"/ansible-playbook --syntax-check playbooks/site.yml
21 endif
22 ifneq ("$(wildcard site.yml)","")
23         "$(VENV_BIN)"/ansible-playbook --syntax-check site.yml
24 endif
25 #       Run ansible-lint ...
26         "$(VENV_BIN)"/ansible-lint --offline --project-dir "$(SOURCE_ROOT)" --show-relpath
27
28 distclean-ansible:
29 #       Remove Python "virtual environment" ...
30         rm -fr "$(SOURCE_ROOT)"/.venv
31 #       Remove Ansible Galaxy collections and roles
32         rm -fr "$(SOURCE_ROOT)"/ansible_galaxy/ansible_collections "$(SOURCE_ROOT)"/ansible_galaxy/ansible_roles
33 #       Try to remove the "ansible_galaxy" directory (if empty), but ignore errors.
34         rmdir "$(SOURCE_ROOT)"/ansible_galaxy || true
35 #       Remove cache directory, used by the Ansible "facts cache" for example ...
36         rm -fr "$(SOURCE_ROOT)"/.cache
37 #       Clean up symlinked commands in dependant projects ...
38         for cmd in "$(SOURCE_ROOT)"/bin/a "$(SOURCE_ROOT)"/bin/ap "$(SOURCE_ROOT)"/bin/aps; do \
39           test -h "$$cmd" && rm -f "$$cmd" || true; \
40          done
41
42 $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint venv: $(VENV_BIN)/pip
43 #       Install/upgrade Python package manager
44         "$(VENV_BIN)"/pip install --upgrade pip wheel
45         touch "$(VENV_BIN)/pip" "$(VENV_BIN)/wheel"
46 #       Install/upgrade Python dependencies ...
47         "$(VENV_BIN)"/pip install --upgrade --requirement $(this_dir)/requirements.txt
48         touch "$(VENV_BIN)/ansible" "$(VENV_BIN)/ansible-galaxy" "$(VENV_BIN)/ansible-lint"
49
50 $(VENV_BIN)/pip:
51 #       Create/upgrade Python "virtual environment"
52         "$(PYTHON)" -m venv "$(SOURCE_ROOT)"/.venv
53
54 .PHONY: venv
55
56 ifneq ($(patsubst %..,,$(lastword $(SOURCE_ROOT))),)
57 # SOURCE_ROOT does not end in "..", so looks like this Makefile fragment is
58 # included in the top-level Makefile. So add some proprietary targets to the
59 # "common" toplevel targets:
60
61 all: $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint
62 check: check-ansible
63 distclean: distclean-ansible
64
65 upgrade: $(SOURCE_ROOT)/bin/ansible-boilerplate
66         $(SOURCE_ROOT)/bin/ansible-boilerplate upgrade
67
68 .PHONY: check distclean
69 endif