-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
131 lines (104 loc) · 3.47 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
-include Makefile.local # for optional local options
BUILD_TARGET := bin/worker
# The dbmate command to use
DBMATE ?= dbmate
# The shards command to use
SHARDS ?= shards
# The crystal command to use
CRYSTAL ?= crystal
SRC_SOURCES := $(shell find src -name '*.cr' 2>/dev/null)
LIB_SOURCES := $(shell find lib -name '*.cr' 2>/dev/null)
SPEC_SOURCES := $(shell find spec -name '*.cr' 2>/dev/null)
DATABASE_NAME := $(shell echo $(DATABASE_URL) | grep -o -P '[^/]+$$')
TEST_DATABASE_NAME := $(shell echo $(TEST_DATABASE_URL) | grep -o -P '[^/]+$$')
.PHONY: build
build: ## Build the application binary
build: $(BUILD_TARGET)
$(BUILD_TARGET): $(SRC_SOURCES) $(LIB_SOURCES) lib
mkdir -p $(shell dirname $(@))
$(CRYSTAL) build src/worker.cr -o $(@)
.PHONY: test
test: ## Run the test suite
test: lib test_db
$(CRYSTAL) spec
.PHONY: format
format: ## Apply source code formatting
format: $(SRC_SOURCES) $(SPEC_SOURCES)
$(CRYSTAL) tool format src spec
docs: ## Generate API docs
docs: $(SRC_SOURCES) lib
$(CRYSTAL) docs -o docs
lib: shard.lock
$(SHARDS) install
shard.lock: shard.yml
$(SHARDS) update
.PHONY: DATABASE_URL
DATABASE_URL:
@test "${$@}" || (echo "$@ is undefined" && false)
.PHONY: TEST_DATABASE_URL
TEST_DATABASE_URL:
@test "${$@}" || (echo "$@ is undefined" && false)
.PHONY: test_db
test_db: TEST_DATABASE_URL
@psql $(TEST_DATABASE_NAME) -c "SELECT 1" > /dev/null 2>&1 || \
(createdb $(TEST_DATABASE_NAME) && psql $(TEST_DATABASE_NAME) < db/extension.sql && psql $(TEST_DATABASE_URL) < db/schema.sql)
.PHONY: db
db: DATABASE_URL
@psql $(DATABASE_NAME) -c "SELECT 1" > /dev/null 2>&1 || \
createdb $(DATABASE_NAME)
psql $(DATABASE_NAME) -c "CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public;"
$(DBMATE) up
.PHONY: db/dump
db/dump: DATABASE_URL
pg_dump -d $(DATABASE_NAME) -a -Tschema_migrations --disable-triggers > db/dump/$(shell date +'%Y-%m-%d-%H%M').sql
.PHONY: db/dump_schema
db/dump_schema: DATABASE_URL
pg_dump -s $(DATABASE_NAME) > db/schema.sql
.PHONY: test_db/drop_sync
test_db/drop_sync: test_db/drop
createdb $(TEST_DATABASE_NAME) 2> /dev/null
pg_dump -s $(DATABASE_NAME) | psql $(TEST_DATABASE_NAME) -q
.PHONY: test_db/drop
test_db/drop:
dropdb $(TEST_DATABASE_NAME) || true
.PHONY: test/migration
test/migration:
git add db/schema.sql
make test_db/rollback test_db/migrate
git diff --exit-code db/schema.sql
.PHONY: test_db/migrate
test_db/migrate:
$(DBMATE) -e TEST_DATABASE_URL migrate
.PHONY: test_db/rollback
test_db/rollback:
$(DBMATE) -e TEST_DATABASE_URL rollback
.PHONY: db/migrate
db/migrate:
$(DBMATE) -e DATABASE_URL migrate
.PHONY: db/rollback
db/rollback:
$(DBMATE) -e DATABASE_URL rollback
vendor/bin/dbmate:
mkdir -p vendor/bin
wget -qO "$@" https://github.com/amacneil/dbmate/releases/download/v1.7.0/dbmate-linux-amd64
chmod +x "$@"
.PHONY: clean
clean: ## Remove application binary
clean:
@rm -f $(BUILD_TARGET)
.PHONY: help
help: ## Show this help
@echo
@printf '\033[34mtargets:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34moptional variables:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+ \?=.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = " \\?=.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34mrecipes:\033[0m\n'
@grep -hE '^##.*$$' $(MAKEFILE_LIST) |\
awk 'BEGIN {FS = "## "}; /^## [a-zA-Z_-]/ {printf " \033[36m%s\033[0m\n", $$2}; /^## / {printf " %s\n", $$2}'