-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
98 lines (85 loc) · 2.9 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
#include .env
PROJECTNAME=$(shell basename "$(PWD)")
# Go related variables.
GOBASE=$(shell pwd)
GOPATH=$(GOBASE)/vendor:$(GOBASE)
GOBIN=$(GOBASE)/bin
GOFILES=$(wildcard *.go)
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
# Sed commands
ifeq ($(shell uname), Darwin)
SEDICMD=sed -i ""
else
SEDICMD=sed -i
endif
## compile: Compile binary files (default running logic).
compile: go-compile
## install: Install missing dependencies. Runs `go get` internally. e.g; make install get=github.com/foo/bar
install: go-get
## clean: Clean built & tmp files. Runs `go clean` internally.
clean: go-clean clean-swp
## create: Create new project
create: create-project
## synclib: Sync libraries
synclib: sync-lib
# Go internal commands.
go-compile: go-mkdir go-clean go-get go-build
go-mkdir:
@echo "make> Prepare dirs ..."
@mkdir -p $(GOBASE)/bin
@mkdir -p $(GOBASE)/log
@mkdir -p $(GOBASE)/vendor
go-build:
@echo "make> Building binary files ..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
go-generate:
@echo "make> Generating dependency files ..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(generate)
go-get:
@echo "make> Checking missing dependencies ..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get $(get)
go-install:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install $(GOFILES)
go-clean:
@echo "make> Cleaning build cache ..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean
# Development commands
clean-swp:
@echo "make> Cleaning .swp files ..."
@find . -type f -name '*.swp' | xargs rm -f
create-project: proj_name := $(notdir $(path))
create-project: proj_name_u := `echo $(proj_name) | tr a-z- A-Z_`
create-project:
ifdef path
@echo "make> Create project '$(proj_name)' at $(path) ..."
@mkdir -p $(path); cp ./.gitignore $(path)/.gitignore; cp ./doc/tpl/main.t $(path)/main.go; cp ./doc/tpl/Makefile $(path)/Makefile
@mkdir -p $(path)/etc/; cp -r ./etc/* $(path)/etc/
@mkdir -p $(path)/src/base; cp -r ./src/base/* $(path)/src/base/
@echo "make> Change project base ENV to $(proj_name_u)_ENV ..."
@$(SEDICMD) "s/GO_BASE_/$(proj_name_u)_/g" $(path)/src/base/util/base.go
@$(SEDICMD) "s/GO_BASE_/$(proj_name_u)_/g" $(path)/Makefile
else
@echo "make> Usage : make create path=/path/to/project"
endif
sync-lib: proj_name := $(notdir $(path))
sync-lib: proj_name_u := `echo $(proj_name) | tr a-z- A-Z_`
sync-lib:
ifdef path
@echo "make> Sync all libraries to $(path) ..."
@mkdir -p $(path)/src/base; cp -r ./src/base/* $(path)/src/base/
@echo "make> Change project base ENV to $(proj_name_u)_ENV ..."
@$(SEDICMD) "s/GO_BASE_/$(proj_name_u)_/g" $(path)/src/base/util/base.go
@$(SEDICMD) "s/GO_BASE_/$(proj_name_u)_/g" $(path)/Makefile
else
@echo "make> Usage : make synclib path=/path/to/project"
endif
#Help commands
.PHONY: help
all: help
help: Makefile
@echo
@echo "make> Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo