.DEFAULT_GOAL := test
NODE_BIN=./node_modules/.bin

.PHONY: accept clean compile_translations dummy_translations extract_translations fake_translations help html_coverage \
	migrate pull_translations push_translations quality requirements production-requirements test \
	update_translations validate stop-devstack static

help:
	@echo "Please use \`make <target>' where <target> is one of"
	@echo "  help                                 display this help message"
	@echo "  make accept                          run acceptance tests"
	@echo "  clean                                delete generated byte code and coverage reports"
	@echo "  compile_translations                 compile translation files, outputting .po files for each supported language"
	@echo "  dummy_translations                   generate dummy translation (.po) files"
	@echo "  extract_translations                 extract strings to be translated, outputting .mo files"
	@echo "  fake_translations                    generate and compile dummy translation files"
	@echo "  html_coverage                        generate and view HTML coverage report"
	@echo "  migrate                              apply database migrations"
	@echo "  pull_translations                    pull translations from Transifex"
	@echo "  push_translations                    push source translation files (.po) from Transifex"
	@echo "  quality                              run PEP8 and Pylint"
	@echo "  production-requirements              install requirements for production"
	@echo "  requirements                         install requirements for local development"
	@echo "  requirements.js                      install JS requirements for local development and production"
	@echo "  test                                 run tests and generate coverage report"
	@echo "  validate                             run tests and quality checks"
	@echo "  static                               gather all static assets for production"
	@echo "  clean_static                         remove all generated static files"
	@echo "  start-devstack                       run a local development copy of the server"
	@echo "  stop-devstack                        shutdown the local development server"
	@echo "  open-devstack                        open a shell on the server started by start-devstack"
	@echo "  detect_changed_source_translations   check if translation files are up-to-date"
	@echo "  validate_translations                install fake translations and check if translation files are up-to-date"
	@echo ""

static:
	$(NODE_BIN)/r.js -o build.js
	python manage.py collectstatic --noinput
	python manage.py compress -v3 --force

clean_static:
	rm -rf course_discovery/assets/ course_discovery/static/build/

clean:
	find . -name '*.pyc' -delete
	coverage erase

requirements.js:
	npm install
	$(NODE_BIN)/bower install

requirements:
	pip install -r requirements/local.txt --exists-action w

production-requirements:
	pip install -r requirements.txt --exists-action w

test: clean
	coverage run ./manage.py test course_discovery --settings=course_discovery.settings.test
	coverage combine
	coverage report

quality:
	pep8 --config=.pep8 acceptance_tests course_discovery *.py
	pylint --rcfile=pylintrc acceptance_tests course_discovery *.py

validate: quality test

migrate:
	python manage.py migrate --noinput
	python manage.py install_es_indexes

html_coverage:
	coverage html && open htmlcov/index.html

extract_translations:
	# NOTE: We need PYTHONPATH defined to avoid ImportError(s) on Travis CI.
	cd course_discovery && PYTHONPATH="..:${PYTHONPATH}" i18n_tool extract --verbose
	cd course_discovery && PYTHONPATH="..:${PYTHONPATH}" i18n_tool generate --verbose

dummy_translations:
	cd course_discovery && i18n_tool dummy

compile_translations:
	python manage.py compilemessages

fake_translations: extract_translations dummy_translations compile_translations

pull_translations:
	tx pull -af

push_translations:
	tx push -s

start-devstack:
	docker-compose up

stop-devstack:
	docker-compose down

open-devstack:
	docker-compose up -d
	docker exec -it course-discovery env TERM=$(TERM) /edx/app/discovery/devstack.sh open

accept:
	nosetests --with-ignore-docstrings -v acceptance_tests

make dev.test:
	coverage run ./manage.py test course_discovery --settings=course_discovery.settings.devstack_test --keepdb
	coverage report

detect_changed_source_translations:
	cd course_discovery && i18n_tool changed

validate_translations: fake_translations detect_changed_source_translations
