Makefile 8.9 KB

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