-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
129 lines (96 loc) · 3.39 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
.DEFAULT_GOAL := all
## parameters
NAME ?= peephole
NAMESPACE ?= github.com/corpix
VERSION ?= development
ENV ?= dev
PARALLEL_JOBS ?= 8
NIX_OPTS ?=
export GOFLAGS ?=
-include .env
## bindings
root := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
nix_dir := nix
pkg_prefix := $(NAMESPACE)/$(NAME)
tmux := tmux -2 -f $(PWD)/.tmux.conf -S $(PWD)/.tmux
tmux_session := $(NAME)
nix := nix $(NIX_OPTS)
shell_volume_nix := nix
### reusable and long opts for commands inside rules
add_shell_opts ?=
shell_opts = -v $(shell_volume_nix):/nix:rw \
-v $(root):/chroot \
-e COLUMNS=$(COLUMNS) \
-e LINES=$(LINES) \
-e TERM=$(TERM) \
-e NIX_BUILD_CORES=$(NIX_BUILD_CORES) \
-e HOME=/chroot \
-w /chroot \
--hostname $(NAMESPACE).localhost \
$(foreach v,$(ports), -p $(v):$(v) ) $(add_shell_opts)
## helpers
, = ,
## macro
define fail
{ echo "error: "$(1) 1>&2; exit 1; }
endef
## targets
.PHONY: all
all: build # test, check and build all cmds
.PHONY: help
help: # print defined targets and their comments
@grep -Po '^[a-zA-Z%_/\-\s]+:+(\s.*$$|$$)' Makefile \
| sort \
| sed 's|:.*#|#|;s|#\s*|#|' \
| column -t -s '#' -o ' | '
### releases
config.yaml: config.nix # generate config from config.nix
nix-instantiate --eval --expr 'builtins.toJSON (import ./$<)' | jq -r . | jq . > $@
### development
.PHONY: build
build: # build application
go build -o main \
--ldflags "-X $(pkg_prefix)/cli.Version=$(VERSION)" \
./main.go
.PHONY: run
run: # run application
go run ./main.go
.PHONY: test
test: # run unit tests
go test -v ./...
#### testing
#### environment management
.PHONY: dev/clean
dev/clean: # clean development environment artifacts
docker volume rm nix
.PHONY: dev/shell
dev/shell: # run development environment shell
@docker run --rm -it \
--log-driver=none \
$(shell_opts) nixos/nix:latest \
nix-shell --command "exec make dev/start-session"
.PHONY: dev/shell/raw
dev/shell/raw: # run development environment shell
@docker run --rm -it \
--log-driver=none \
$(shell_opts) nixos/nix:latest \
nix-shell
.PHONY: dev/session
dev/start-session: # start development environment terminals with database, blockchain, etc... one window per app
@$(tmux) has-session -t $(tmux_session) && $(call fail,tmux session $(tmux_session) already exists$(,) use: '$(tmux) attach-session -t $(tmux_session)' to attach) || true
@$(tmux) new-session -s $(tmux_session) -n console -d
@sleep 1 # sometimes input is messed up (bash+stdin early handling?)
@$(tmux) select-window -t $(tmux_session):0
@if [ -f $(root)/.personal.tmux.conf ]; then \
$(tmux) source-file $(root)/.personal.tmux.conf; \
fi
@$(tmux) attach-session -t $(tmux_session)
.PHONY: dev/attach-session
dev/attach-session: # attach to development session if running
@$(tmux) attach-session -t $(tmux_session)
.PHONY: dev/stop-session
dev/stop-session: # stop development environment terminals
@$(tmux) kill-session -t $(tmux_session)
.PHONY: clean
clean: # clean stored state
rm -rf result*