]> arthur.barton.de Git - ansible-collection-boilerplate.git/blob - Makefile.boilerplate
ansible-boilerplate: Only create ".new" files when called with the "init" subcommand
[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 #       Run ansible-playbook syntax check, when a "site play" is found ...
17 ifneq ("$(wildcard playbooks/site.yml)","")
18         "$(VENV_BIN)"/ansible-playbook --syntax-check playbooks/site.yml
19 endif
20 ifneq ("$(wildcard site.yml)","")
21         "$(VENV_BIN)"/ansible-playbook --syntax-check site.yml
22 endif
23 #       Run ansible-lint ...
24         "$(VENV_BIN)"/ansible-lint --offline --project-dir "$(SOURCE_ROOT)" --show-relpath
25
26 distclean-ansible:
27 #       Remove Python "virtual environment" ...
28         rm -fr "$(SOURCE_ROOT)"/.venv
29 #       Remove Ansible Galaxy collections and roles
30         rm -fr "$(SOURCE_ROOT)"/ansible_galaxy/ansible_collections "$(SOURCE_ROOT)"/ansible_galaxy/ansible_roles
31 #       Try to remove the "ansible_galaxy" directory (if empty), but ignore errors.
32         rmdir "$(SOURCE_ROOT)"/ansible_galaxy || true
33 #       Remove cache directory, used by the Ansible "facts cache" for example ...
34         rm -fr "$(SOURCE_ROOT)"/.cache
35 #       Clean up symlinked commands in dependant projects ...
36         for cmd in "$(SOURCE_ROOT)"/bin/a "$(SOURCE_ROOT)"/bin/ap "$(SOURCE_ROOT)"/bin/aps; do \
37           test -h "$$cmd" && rm -f "$$cmd" || true; \
38          done
39
40 $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint venv: $(VENV_BIN)/pip
41 #       Install/upgrade Python package manager
42         "$(VENV_BIN)"/pip install --upgrade pip wheel
43         touch "$(VENV_BIN)/pip" "$(VENV_BIN)/wheel"
44 #       Install/upgrade Python dependencies ...
45         "$(VENV_BIN)"/pip install --upgrade --requirement $(this_dir)/requirements.txt
46         touch "$(VENV_BIN)/ansible" "$(VENV_BIN)/ansible-galaxy" "$(VENV_BIN)/ansible-lint"
47
48 $(VENV_BIN)/pip:
49 #       Create/upgrade Python "virtual environment"
50         "$(PYTHON)" -m venv "$(SOURCE_ROOT)"/.venv
51
52 .PHONY: venv
53
54 ifneq ($(patsubst %..,,$(lastword $(SOURCE_ROOT))),)
55 # SOURCE_ROOT does not end in "..", so looks like this Makefile fragment is
56 # included in the top-level Makefile. So add some proprietary targets to the
57 # "common" toplevel targets:
58
59 all: $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint
60 check: check-ansible
61 distclean: distclean-ansible
62
63 upgrade: $(SOURCE_ROOT)/bin/ansible-boilerplate
64         $(SOURCE_ROOT)/bin/ansible-boilerplate upgrade
65
66 .PHONY: check distclean
67 endif