1#/bin/bash
2
3SOURCE = "./lib"
4TARGET?=70
5
6# By default docker environment is used to run commands. To run without the predefined environment, set RUN_ENV=" " either as parameter or as environment variable
7ifndef RUN_ENV
8	RUN_ENV = docker run --workdir="/elastica" -v $(shell pwd):/elastica ruflin/elastica-dev-base
9	RUN_ENV_LINT = docker run --workdir="/elastica" -v $(shell pwd):/elastica ruflin/elastica-dev-base-linter
10endif
11
12.PHONY: clean
13clean:
14	docker-compose down -v
15	rm -r -f ./build
16	rm -r -f ./vendor
17	rm -r -f ./composer.lock
18
19# Runs commands inside virtual environemnt. Example usage inside docker: make run RUN="make phpunit"
20.PHONY: run
21run:
22	docker run -v $(shell pwd):/elastica ruflin/elastica $(RUN)
23
24
25### Quality checks / development tools ###
26.PHONY: code-browser
27code-browser:
28	${RUN_ENV} phpcb --log ./build/logs --source ${SOURCE} --output ./build/code-browser
29
30# Copy paste detector
31.PHONY: cpd
32cpd:
33	${RUN_ENV} phpcpd --log-pmd ./build/logs/pmd-cpd.xml ${SOURCE}
34
35.PHONY: messdetector
36messdetector:
37	${RUN_ENV} phpmd ${SOURCE} text codesize,unusedcode,naming,design ./build/phpmd.xml
38
39.PHONY: messdetector-ci
40messdetector-ci:
41	${RUN_ENV} phpmd ${SOURCE} xml codesize,unusedcode,naming,design --reportfile ./build/logs/pmd.xml
42
43.PHONY: dependencies
44dependencies:
45	${RUN_ENV} pdepend --jdepend-xml=./build/logs/jdepend.xml \
46		--jdepend-chart=./build/pdepend/dependencies.svg \
47		--overview-pyramid=./build/pdepend/overview-pyramid.svg \
48		${SOURCE}
49
50.PHONY: phpunit
51phpunit:
52	EXIT_STATUS=0 ; \
53	phpunit -c test/ --coverage-clover build/coverage/unit-coverage.xml --group unit || EXIT_STATUS=$$? ; \
54	phpunit -c test/ --coverage-clover build/coverage/functional-coverage.xml --group functional || EXIT_STATUS=$$? ; \
55	phpunit -c test/ --coverage-clover build/coverage/shutdown-coverage.xml --group shutdown || EXIT_STATUS=$$? ; \
56	exit $$EXIT_STATUS
57
58.PHONY: tests
59tests:
60	# Rebuild image to copy changes files to the image
61	make elastica-image
62	make start
63	mkdir -p build
64
65	docker run -e "ES_HOST=elasticsearch" --network=elastica_esnet elastica_elastica make phpunit
66	docker cp elastica:/elastica/build/coverage/ $(shell pwd)/build/coverage
67
68# Makes it easy to run a single test file. Example to run IndexTest.php: make test TEST="IndexTest.php"
69.PHONY: test
70test:
71	make elastica-image
72	make start
73	mkdir -p build
74	docker-compose run -e "ES_HOST=elasticsearch" elastica phpunit -c test/ ${TEST}
75
76.PHONY: doc
77doc:
78	${RUN_ENV} phpdoc run -d lib/ -t build/docs --template=/root/composer/vendor/phpdocumentor/phpdocumentor/data/templates/clean
79
80# Uses the preconfigured standards in .php_cs.dist
81.PHONY: lint
82lint:
83	docker build -t ruflin/elastica-dev-base-linter -f env/elastica/Lint env/elastica/
84	${RUN_ENV_LINT} php-cs-fixer fix --allow-risky=yes
85
86.PHONY: check-style
87check-style:
88	docker build -t ruflin/elastica-dev-base-linter -f env/elastica/Lint env/elastica/
89	${RUN_ENV_LINT} php-cs-fixer fix --allow-risky=yes --dry-run
90
91.PHONY: loc
92loc:
93	${RUN_ENV} cloc --by-file --xml --exclude-dir=build -out=build/cloc.xml .
94
95.PHONY: phploc
96phploc:
97	${RUN_ENV} phploc --log-csv ./build/logs/phploc.csv ${SOURCE}
98
99
100# VIRTUAL ENVIRONMENT
101.PHONY: build
102build:
103	docker-compose build
104
105.PHONY: start
106start:
107	docker-compose up -d --build
108
109.PHONY: stop
110stop:
111	docker-compose stop
112
113.PHONY: destroy
114destroy: clean
115	docker-compose kill
116	docker-compose rm
117
118# Stops and removes all containers and removes all images
119.PHONY: destroy-environment
120destroy-environment:
121	make remove-containers
122	-docker rmi $(shell docker images -q)
123
124.PHONY: remove-containers
125remove-containers:
126	-docker stop $(shell docker ps -a -q)
127	-docker rm -v $(shell docker ps -a -q)
128
129# Starts a shell inside the elastica image
130.PHONY: shell
131shell:
132	docker run -v $(shell pwd):/elastica -ti ruflin/elastica /bin/bash
133
134# Starts a shell inside the elastica image with the full environment running
135.PHONY: env-shell
136env-shell:
137	docker-compose run elastica /bin/bash
138
139# Visualise repo
140.PHONY: gource
141gource:
142	gource --log-format git \
143		--seconds-per-day 0.1 \
144		--title 'Elastica (https://github.com/ruflin/Elastica)' \
145		--user-scale 1 \
146		--max-user-speed 50
147
148## DOCKER IMAGES
149
150.PHONY: elastica-image
151elastica-image:
152	docker build -t ruflin/elastica-dev-base -f env/elastica/Docker${TARGET} env/elastica/
153	docker build -t ruflin/elastica .
154
155# Builds all image locally. This can be used to use local images if changes are made locally to the Dockerfiles
156.PHONY: build-images
157build-images:
158	docker build -t ruflin/elastica-dev-base -f env/elastica/Docker${TARGET} env/elastica/
159	docker build -t ruflin/elasticsearch-elastica env/elasticsearch/
160	docker build -t ruflin/nginx-elastica env/nginx/
161	make elastica-image
162
163# Removes all local images
164.PHONY: clean-images
165clean-images:
166	-docker rmi ruflin/elastica-dev-base
167	-docker rmi ruflin/elasticsearch-elastica
168	-docker rmi ruflin/nginx-elastica
169	-docker rmi ruflin/elastica
170
171# Pushs images as latest to the docker registry. This is normally not needed as they are directly fetched and built from Github
172.PHONY: push-images
173push-images: build-images
174	docker push ruflin/elastica-dev-base
175	docker push ruflin/elasticsearch-elastica
176	docker push ruflin/nginx-elastica
177	docker push ruflin/elastica
178
179
180## OTHER
181
182# google-setup:
183# 	docker-machine create --driver google --google-project elastica-1024 --google-machine-type n1-standard-8 elastica
184# 	eval "$(docker-machine env elastica)"
185