forked from trustwallet/blockatlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
153 lines (119 loc) · 4.28 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Project variables.
VERSION := $(shell git describe --tags)
BUILD := $(shell git rev-parse --short HEAD)
PROJECT_NAME := $(shell basename "$(PWD)")
API_COMMAND := api
OBSERVER_COMMAND := observer
SYNC_COMMAND := sync-markets
COIN_FILE := coin/coins.yml
COIN_GO_FILE := coin/coins.go
GEN_COIN_FILE := coin/gen.go
# Go related variables.
GOBASE := $(shell pwd)
GOBIN := $(GOBASE)/bin
GOPKG := $(.)
# Environment variables
TEST_CONFIG=$(GOBASE)/config.yml
# Go files
GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)
# Use linker flags to provide version/build settings
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)"
# Redirect error output to a file, so we can show it in development mode.
STDERR := /tmp/.$(PROJECT_NAME)-stderr.txt
# PID file will keep the process id of the server
PID_API := /tmp/.$(PROJECT_NAME).$(API_COMMAND).pid
PID_OBSERVER := /tmp/.$(PROJECT_NAME).$(OBSERVER_COMMAND).pid
PID_SYNC := /tmp/.$(PROJECT_NAME).$(SYNC_COMMAND).pid
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
## install: Install missing dependencies. Runs `go get` internally. e.g; make install get=github.com/foo/bar
install: go-get
## start: Start API, Observer and Sync in development mode.
start:
@bash -c "$(MAKE) clean compile start-api start-observer start-sync"
## stop: Stop development mode.
stop: stop-server
## start-api: Start API in development mode.
start-api: stop-server
@echo " > Starting $(PROJECT_NAME) API"
@-$(GOBIN)/$(PROJECT_NAME) $(API_COMMAND) 2>&1 & echo $$! > $(PID_API)
@cat $(PID_API) | sed "/^/s/^/ \> API PID: /"
@echo " > Error log: $(STDERR)"
## start-observer: Start Observer in development mode.
start-observer: stop-server
@echo " > Starting $(PROJECT_NAME) Observer"
@-$(GOBIN)/$(PROJECT_NAME) $(OBSERVER_COMMAND) 2>&1 & echo $$! > $(PID_OBSERVER)
@cat $(PID_OBSERVER) | sed "/^/s/^/ \> Observer PID: /"
@echo " > Error log: $(STDERR)"
## start-sync-markets: Start Sync markets in development mode.
start-sync-markets: stop-server
@echo " > Starting $(PROJECT_NAME) Sync"
@-$(GOBIN)/$(PROJECT_NAME) $(SYNC_COMMAND) 2>&1 & echo $$! > $(PID_SYNC)
@cat $(PID_SYNC) | sed "/^/s/^/ \> Sync PID: /"
@echo " > Error log: $(STDERR)"
stop-server:
@-touch $(PID_API) $(PID_OBSERVER)
@-kill `cat $(PID_API)` 2> /dev/null || true
@-kill `cat $(PID_OBSERVER)` 2> /dev/null || true
@-rm $(PID_API) $(PID_OBSERVER)
restart-server: stop-server start-server
## compile: Compile the binary.
compile:
@-touch $(STDERR)
@-rm $(STDERR)
@-$(MAKE) -s go-compile 2> $(STDERR)
@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2
## exec: Run given command. e.g; make exec run="go test ./..."
exec:
GOBIN=$(GOBIN) $(run)
## clean: Clean build files. Runs `go clean` internally.
clean:
@-rm $(GOBIN)/$(PROJECT_NAME) 2> /dev/null
@-$(MAKE) go-clean
## test: Run all unit tests.
test: go-test
## integration: Run all integration tests.
integration: go-integration
## fmt: Run `go fmt` for all go files.
fmt: go-fmt
## gen-coins: Generate a new coin file.
gen-coins: remove-coin-file go-gen-coins
## remove-coin-file: Remove auto generated coin file.
remove-coin-file:
@echo " > Removing "$(PROJECT_NAME)""
@-rm $(GOBASE)/$(COIN_GO_FILE)
go-compile: go-get go-build
go-build:
@echo " > Building binary..."
GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(PROJECT_NAME) -v .
go-generate:
@echo " > Generating dependency files..."
GOBIN=$(GOBIN) go generate $(generate)
go-get:
@echo " > Checking if there is any missing dependencies..."
GOBIN=$(GOBIN) go get $(get)
go-install:
GOBIN=$(GOBIN) go install $(GOPKG)
go-clean:
@echo " > Cleaning build cache"
GOBIN=$(GOBIN) go clean
go-test:
@echo " > Runing unit tests"
GOBIN=$(GOBIN) go test -v ./...
go-integration:
@echo " > Runing integration tests"
GOBIN=$(GOBIN) TEST_CONFIG=$(TEST_CONFIG) go test -tags=integration -v ./pkg/integration
go-fmt:
@echo " > Format all go files"
GOBIN=$(GOBIN) gofmt -w ${GOFMT_FILES}
go-gen-coins:
@echo " > Generating coin file"
COIN_FILE=$(COIN_FILE) COIN_GO_FILE=$(COIN_GO_FILE) GOBIN=$(GOBIN) go run -tags=coins $(GEN_COIN_FILE)
.PHONY: help
all: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECT_NAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo