Makefile 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. DOCKER_COMPOSE_FLAGS ?= -f docker-compose.yml --log-level ERROR
  2. BUILD_NUMBER ?= local
  3. BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD)
  4. PROJECT_NAME = web
  5. BUILD_DIR_NAME = $(shell pwd | xargs basename | tr -cd '[a-zA-Z0-9_.\-]')
  6. DOCKER_COMPOSE := BUILD_NUMBER=$(BUILD_NUMBER) \
  7. BRANCH_NAME=$(BRANCH_NAME) \
  8. PROJECT_NAME=$(PROJECT_NAME) \
  9. MOCHA_GREP=${MOCHA_GREP} \
  10. docker-compose ${DOCKER_COMPOSE_FLAGS}
  11. MODULE_DIRS := $(shell find modules -mindepth 1 -maxdepth 1 -type d -not -name '.git' )
  12. MODULE_MAKEFILES := $(MODULE_DIRS:=/Makefile)
  13. MODULE_NAME=$(shell basename $(MODULE))
  14. COFFEE := node_modules/.bin/coffee -m $(COFFEE_OPTIONS)
  15. COFFEE := node_modules/.bin/coffee $(COFFEE_OPTIONS)
  16. BABEL := node_modules/.bin/babel
  17. GRUNT := node_modules/.bin/grunt
  18. LESSC := node_modules/.bin/lessc
  19. CLEANCSS := node_modules/.bin/cleancss
  20. APP_COFFEE_FILES := $(shell find app/coffee -name '*.coffee')
  21. FRONT_END_SRC_FILES := $(shell find public/src -name '*.js')
  22. TEST_COFFEE_FILES := $(shell find test/*/coffee -name '*.coffee')
  23. TEST_SRC_FILES := $(shell find test/*/src -name '*.js')
  24. MODULE_MAIN_SRC_FILES := $(shell find modules -type f -wholename '*main/index.js')
  25. MODULE_IDE_SRC_FILES := $(shell find modules -type f -wholename '*ide/index.js')
  26. COFFEE_FILES := app.coffee $(APP_COFFEE_FILES) $(FRONT_END_COFFEE_FILES) $(TEST_COFFEE_FILES)
  27. SRC_FILES := $(FRONT_END_SRC_FILES) $(TEST_SRC_FILES)
  28. JS_FILES := $(subst coffee,js,$(COFFEE_FILES))
  29. OUTPUT_SRC_FILES := $(subst src,js,$(SRC_FILES))
  30. LESS_FILES := $(shell find public/stylesheets -name '*.less')
  31. LESSC_COMMON_FLAGS := --source-map --autoprefix="last 2 versions, ie >= 10"
  32. CLEANCSS_FLAGS := --s0 --source-map
  33. LESS_SL_FILE := public/stylesheets/sl-style.less
  34. CSS_SL_FILE := public/stylesheets/sl-style.css
  35. LESS_OL_FILE := public/stylesheets/style.less
  36. CSS_OL_FILE := public/stylesheets/style.css
  37. LESS_OL_LIGHT_FILE := public/stylesheets/light-style.less
  38. CSS_OL_LIGHT_FILE := public/stylesheets/light-style.css
  39. LESS_OL_IEEE_FILE := public/stylesheets/ieee-style.less
  40. CSS_OL_IEEE_FILE := public/stylesheets/ieee-style.css
  41. CSS_FILES := $(CSS_SL_FILE) $(CSS_OL_FILE) $(CSS_OL_LIGHT_FILE) $(CSS_OL_IEEE_FILE)
  42. # The automatic variable $(@D) is the target directory name
  43. app.js: app.coffee
  44. $(COFFEE) --compile -o $(@D) $<
  45. app/js/%.js: app/coffee/%.coffee
  46. @mkdir -p $(@D)
  47. $(COFFEE) --compile -o $(@D) $<
  48. public/js/%.js: public/src/%.js
  49. @mkdir -p $(@D)
  50. $(BABEL) $< --out-file $@
  51. test/unit/js/%.js: test/unit/coffee/%.coffee
  52. @mkdir -p $(@D)
  53. $(COFFEE) --compile -o $(@D) $<
  54. test/acceptance/js/%.js: test/acceptance/coffee/%.coffee
  55. @mkdir -p $(@D)
  56. $(COFFEE) --compile -o $(@D) $<
  57. test/unit_frontend/js/%.js: test/unit_frontend/src/%.js
  58. @mkdir -p $(@D)
  59. $(BABEL) $< --out-file $@
  60. test/smoke/js/%.js: test/smoke/coffee/%.coffee
  61. @mkdir -p $(@D)
  62. $(COFFEE) --compile -o $(@D) $<
  63. public/js/ide.js: public/src/ide.js $(MODULE_IDE_SRC_FILES)
  64. @echo Compiling and injecting module includes into public/js/ide.js
  65. @INCLUDES=""; \
  66. for dir in modules/*; \
  67. do \
  68. MODULE=`echo $$dir | cut -d/ -f2`; \
  69. if [ -e $$dir/public/src/ide/index.js ]; then \
  70. INCLUDES="\"ide/$$MODULE/index\",$$INCLUDES"; \
  71. fi \
  72. done; \
  73. INCLUDES=$${INCLUDES%?}; \
  74. $(BABEL) $< | \
  75. sed -e s=\'__IDE_CLIENTSIDE_INCLUDES__\'=$$INCLUDES= \
  76. > $@
  77. public/js/main.js: public/src/main.js $(MODULE_MAIN_SRC_FILES)
  78. @echo Compiling and injecting module includes into public/js/main.js
  79. @INCLUDES=""; \
  80. for dir in modules/*; \
  81. do \
  82. MODULE=`echo $$dir | cut -d/ -f2`; \
  83. if [ -e $$dir/public/src/main/index.js ]; then \
  84. INCLUDES="\"main/$$MODULE/index\",$$INCLUDES"; \
  85. fi \
  86. done; \
  87. INCLUDES=$${INCLUDES%?}; \
  88. $(BABEL) $< | \
  89. sed -e s=\'__MAIN_CLIENTSIDE_INCLUDES__\'=$$INCLUDES= \
  90. > $@
  91. public/stylesheets/%.css: $(LESS_FILES)
  92. $(LESSC) $(LESSC_COMMON_FLAGS) $(@D)/$*.less $(@D)/$*.css
  93. css_full: $(CSS_FILES)
  94. css: $(CSS_OL_FILE)
  95. minify: $(CSS_FILES) $(JS_FILES) $(OUTPUT_SRC_FILES)
  96. $(GRUNT) compile:minify
  97. $(MAKE) minify_css
  98. $(MAKE) minify_es
  99. minify_css: $(CSS_FILES)
  100. $(CLEANCSS) $(CLEANCSS_FLAGS) -o $(CSS_SL_FILE) $(CSS_SL_FILE)
  101. $(CLEANCSS) $(CLEANCSS_FLAGS) -o $(CSS_OL_FILE) $(CSS_OL_FILE)
  102. $(CLEANCSS) $(CLEANCSS_FLAGS) -o $(CSS_OL_LIGHT_FILE) $(CSS_OL_LIGHT_FILE)
  103. $(CLEANCSS) $(CLEANCSS_FLAGS) -o $(CSS_OL_IEEE_FILE) $(CSS_OL_IEEE_FILE)
  104. minify_es:
  105. npm -q run webpack:production
  106. compile: compile_app $(OUTPUT_SRC_FILES) css public/js/main.js public/js/ide.js
  107. compile_app: $(JS_FILES)
  108. @$(MAKE) compile_modules
  109. compile_full:
  110. $(COFFEE) -c -p app.coffee > app.js
  111. $(COFFEE) -o app/js -c app/coffee
  112. $(BABEL) public/src --out-dir public/js
  113. $(COFFEE) -o test/acceptance/js -c test/acceptance/coffee
  114. $(COFFEE) -o test/smoke/js -c test/smoke/coffee
  115. $(COFFEE) -o test/unit/js -c test/unit/coffee
  116. $(BABEL) test/unit_frontend/src --out-dir test/unit_frontend/js
  117. rm -f public/js/ide.js public/js/main.js # We need to generate ide.js, main.js manually later
  118. $(MAKE) css_full
  119. $(MAKE) compile_modules_full
  120. $(MAKE) compile # ide.js, main.js, share.js, and anything missed
  121. compile_css_full:
  122. $(MAKE) css_full
  123. compile_module:
  124. cd modules/$(MODULE_NAME) && $(MAKE) compile
  125. compile_modules: $(MODULE_MAKEFILES)
  126. @set -e; \
  127. for dir in $(MODULE_DIRS); \
  128. do \
  129. if [ -e $$dir/Makefile ]; then \
  130. (cd $$dir && $(MAKE) compile); \
  131. fi; \
  132. if [ ! -e $$dir/Makefile ]; then \
  133. echo "No makefile found in $$dir"; \
  134. fi; \
  135. done
  136. compile_modules_full: $(MODULE_MAKEFILES)
  137. @set -e; \
  138. for dir in $(MODULE_DIRS); \
  139. do \
  140. if [ -e $$dir/Makefile ]; then \
  141. echo "Compiling $$dir in full"; \
  142. (cd $$dir && $(MAKE) compile_full); \
  143. fi; \
  144. if [ ! -e $$dir/Makefile ]; then \
  145. echo "No makefile found in $$dir"; \
  146. fi; \
  147. done
  148. $(MODULE_MAKEFILES): Makefile.module
  149. @set -e; \
  150. for makefile in $(MODULE_MAKEFILES); \
  151. do \
  152. cp Makefile.module $$makefile; \
  153. done
  154. clean: clean_app clean_frontend clean_css clean_tests clean_modules
  155. clean_app:
  156. rm -f app.js app.js.map
  157. rm -rf app/js
  158. clean_frontend:
  159. rm -rf public/js/{analytics,directives,es,filters,ide,main,modules,services,utils}
  160. rm -f public/js/*.{js,map}
  161. clean_tests:
  162. rm -rf test/unit/js
  163. rm -rf test/unit_frontend/js
  164. rm -rf test/acceptance/js
  165. clean_modules:
  166. for dir in modules/*; \
  167. do \
  168. rm -f $$dir/index.js; \
  169. rm -rf $$dir/app/js; \
  170. rm -rf $$dir/test/unit/js; \
  171. rm -rf $$dir/test/acceptance/js; \
  172. done
  173. clean_css:
  174. rm -f public/stylesheets/*.css*
  175. clean_ci:
  176. $(DOCKER_COMPOSE) down -v -t 0
  177. docker container list | grep 'days ago' | cut -d ' ' -f 1 - | xargs -r docker container stop
  178. docker image prune -af --filter "until=48h"
  179. docker network prune -f
  180. test: test_unit test_frontend test_acceptance
  181. test_module: compile_module test_unit_module_run test_acceptance_module_run
  182. test_unit:
  183. @[ ! -d test/unit ] && echo "web has no unit tests" || COMPOSE_PROJECT_NAME=unit_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --name unit_test_$(BUILD_DIR_NAME) --rm test_unit
  184. test_unit_module: compile_module test_unit_module_run
  185. test_unit_module_run:
  186. COMPOSE_PROJECT_NAME=unit_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --name unit_test_$(BUILD_DIR_NAME) --rm test_unit bin/unit_test_module $(MODULE_NAME) --grep=$(MOCHA_GREP)
  187. test_unit_app:
  188. npm -q run test:unit:app -- ${MOCHA_ARGS}
  189. test_frontend: compile build_test_frontend test_frontend_run
  190. test_frontend_run:
  191. COMPOSE_PROJECT_NAME=frontend_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  192. COMPOSE_PROJECT_NAME=frontend_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --rm test_frontend
  193. COMPOSE_PROJECT_NAME=frontend_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  194. test_frontend_build_run: build_test_frontend test_frontend_run
  195. test_acceptance: compile test_acceptance_app_run test_acceptance_modules_run
  196. test_acceptance_app: compile test_acceptance_app_run
  197. test_acceptance_module: compile_module test_acceptance_module_run
  198. test_acceptance_run: test_acceptance_app_run test_acceptance_modules_run
  199. test_acceptance_app_run:
  200. COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  201. COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --rm test_acceptance npm -q run test:acceptance:run_dir test/acceptance/js
  202. COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  203. test_acceptance_modules_run:
  204. @set -e; \
  205. for dir in $(MODULE_DIRS); \
  206. do \
  207. if [ -e $$dir/test/acceptance ]; then \
  208. $(MAKE) test_acceptance_module_run MODULE=$$dir; \
  209. fi; \
  210. done
  211. test_acceptance_module_run: $(MODULE_MAKEFILES)
  212. @if [ -e modules/$(MODULE_NAME)/test/acceptance ]; then \
  213. COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE_NAME) $(DOCKER_COMPOSE) down -v -t 0 \
  214. && cd modules/$(MODULE_NAME) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE_NAME) $(MAKE) test_acceptance \
  215. && cd $(CURDIR) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE_NAME) $(DOCKER_COMPOSE) down -v -t 0; \
  216. fi
  217. ci:
  218. MOCHA_ARGS="--reporter tap" \
  219. $(MAKE) test
  220. format:
  221. npm -q run format
  222. format_fix:
  223. npm -q run format:fix
  224. lint:
  225. npm -q run lint
  226. build:
  227. docker build --pull --tag ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) \
  228. --tag gcr.io/overleaf-ops/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) \
  229. .
  230. build_test_frontend:
  231. COMPOSE_PROJECT_NAME=frontend_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) build test_frontend
  232. publish:
  233. docker push $(DOCKER_REPO)/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER)
  234. tar:
  235. COMPOSE_PROJECT_NAME=tar_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --rm tar
  236. COMPOSE_PROJECT_NAME=tar_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  237. .PHONY:
  238. all add install update test test_unit test_frontend test_acceptance \
  239. test_acceptance_start_service test_acceptance_stop_service \
  240. test_acceptance_run ci ci_clean compile clean css