-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
97 lines (84 loc) · 3.71 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
.SILENT: database setup migrate build assets clean-assets run-server run-client server-repl client-repl new-migration
.PHONY: database setup migrate build assets clean-assets run-server run-client server-repl client-repl new-migration
GHCI_OPTIONS = --ghci-options -XOverloadedStrings --ghci-options -XScopedTypeVariables
missing_arg = "Missing required argument '$(1)': make $@ $(1)=$(2)"
setup: build migrate
build: assets
# FIXME: dotenv ignores blank environment variables.
stack exec dotenv -- -o -f .envdefaults -f .env 'stack build'
assets: clean-assets
echo 'Compiling assets!'
yarn install
elm make --warn \
--output public/assets/scripts/arksink.elm.js \
`find src/lib/assets/scripts/elm -name '*.elm' -exec echo -n '{} ' ';' | sed 's/ $$//'`
coffee -c --no-header \
-o public/assets/scripts \
-- `find src/lib/assets/scripts -name '*.coffee' -exec echo -n '{} ' ';' | sed 's/ $$//'`
mkdir -p public/assets/styles
sassc -t compressed -m \
-I node_modules \
-- src/lib/assets/styles/arksink.scss public/assets/styles/arksink.css
clean-assets:
echo 'Cleaning assets!'
rm -rf public/assets
run-server: build
stack exec dotenv -- -o -f .envdefaults -f .env 'stack exec arksink-server'
run-client: build
stack exec dotenv -- -o -f .envdefaults -f .env 'stack exec arksink'
client-repl:
stack exec dotenv -- -o -f .envdefaults -f .env 'stack ghci $(GHCI_OPTIONS) --main-is Arksink:exe:arksink-client'
server-repl:
stack exec dotenv -- -o -f .envdefaults -f .env 'stack ghci $(GHCI_OPTIONS) --main-is Arksink:exe:arksink-server'
new-migration:
if [ -n "$(name)" ]; then \
touch db/migrations/`date +'%Y%m%d%H%M%S'`_$(name).sql; \
else \
echo $(call missing_arg,name,migration_name); \
fi
database:
# OPTIMIZE: load environment with dotenv once.
# - Create database if non-existent.
# - Create migrations table if non-existent.
database_exists=$$(stack exec dotenv -- -o -f .envdefaults -f .env \
"psql -t -A -c \
\"SELECT true FROM pg_database WHERE datname = 'arksink'\"") && \
if [ ! "$$database_exists" = "t" ]; then \
stack exec dotenv -- -o -f .envdefaults -f .env \
'createdb $$ARKSINK_DB_NAME' ; \
fi
stack exec dotenv -- -o -f .envdefaults -f .env \
'psql -d $$ARKSINK_DB_NAME -c \
"CREATE TABLE IF NOT EXISTS migrations (id VARCHAR(255) PRIMARY KEY)"' \
2>/dev/null >/dev/null
stack exec dotenv -- -o -f .envdefaults -f .env \
'pg_dump --schema-only --no-owner $$ARKSINK_DB_NAME > db/arksink.sql'
migrate: database
# - Create temporary directory.
# - Store applied migrations in temporary file.
# - Store all applicable migrations in temporary file.
# - Get applicable migrations that aren't applied.
# - Apply unapplied migrations.
# - Dump updated schema.
tmpdir=$$(mktemp -d) && \
stack exec dotenv -- -o -f .envdefaults -f .env \
'psql -d $$ARKSINK_DB_NAME -t -A -c "SELECT id FROM MIGRATIONS"'\
> "$$tmpdir/applied-migrations" && \
find db/migrations -depth 1 \
| sed -e 's:^db/migrations/::' -e 's:\.sql$$::' \
> "$$tmpdir/migrations" && \
unapplied_migrations=$$(diff \
--changed-group-format='%>' --unchanged-group-format='' \
"$$tmpdir/applied-migrations" "$$tmpdir/migrations") || \
for migration in $$unapplied_migrations; do \
echo "=== Applying migration: $${migration}"; \
stack exec dotenv -- -o -f .envdefaults -f .env \
'psql -d $$ARKSINK_DB_NAME -f \
db/migrations/'"$$migration"'.sql' && \
stack exec dotenv -- -o -f .envdefaults -f .env \
'psql -d $$ARKSINK_DB_NAME -c \
"INSERT INTO migrations VALUES ('"'$$migration'"')"' \
>/dev/null ; \
done
stack exec dotenv -- -o -f .envdefaults -f .env \
'pg_dump --schema-only --no-owner $$ARKSINK_DB_NAME > db/arksink.sql'